API Docs for: 3.8.0
Show:

File: datatable/js/message.js

  1. /**
  2. Adds support for a message container to appear in the table.  This can be used
  3. to indicate loading progress, lack of records, or any other communication
  4. needed.

  5. @module datatable
  6. @submodule datatable-message
  7. @since 3.5.0
  8. **/
  9. var Message;

  10. /**
  11. _API docs for this extension are included in the DataTable class._

  12. Adds support for a message container to appear in the table.  This can be used
  13. to indicate loading progress, lack of records, or any other communication
  14. needed.

  15. Features added to `Y.DataTable`, and made available for custom classes at
  16. `Y.DataTable.Message`.

  17. @class DataTable.Message
  18. @for DataTable
  19. @since 3.5.0
  20. **/
  21. Y.namespace('DataTable').Message = Message = function () {};

  22. Message.ATTRS = {
  23.     /**
  24.     Enables the display of messages in the table.  Setting this to false will
  25.     prevent the message Node from being created and `showMessage` from doing
  26.     anything.

  27.     @attribute showMessages
  28.     @type {Boolean}
  29.     @default true
  30.     @since 3.5.0
  31.     **/
  32.     showMessages: {
  33.         value: true,
  34.         validator: Y.Lang.isBoolean
  35.     }
  36. };

  37. Y.mix(Message.prototype, {
  38.     /**
  39.     Template used to generate the node that will be used to report messages.

  40.     @property MESSAGE_TEMPLATE
  41.     @type {HTML}
  42.     @default <tbody class="{className}"><td class="{contentClass}" colspan="{colspan}"></td></tbody>
  43.     @since 3.5.0
  44.     **/
  45.     MESSAGE_TEMPLATE: '<tbody class="{className}"><tr><td class="{contentClass}" colspan="{colspan}"></td></tr></tbody>',

  46.     /**
  47.     Hides the message node.

  48.     @method hideMessage
  49.     @return {DataTable}
  50.     @chainable
  51.     @since 3.5.0
  52.     **/
  53.     hideMessage: function () {
  54.         this.get('boundingBox').removeClass(
  55.             this.getClassName('message', 'visible'));

  56.         return this;
  57.     },

  58.     /**
  59.     Display the message node and set its content to `message`.  If there is a
  60.     localized `strings` entry for the value of `message`, that string will be
  61.     used.

  62.     @method showMessage
  63.     @param {String} message The message name or message itself to display
  64.     @return {DataTable}
  65.     @chainable
  66.     @since 3.5.0
  67.     **/
  68.     showMessage: function (message) {
  69.         var content = this.getString(message) || message;

  70.         if (!this._messageNode) {
  71.             this._initMessageNode();
  72.         }

  73.         if (this.get('showMessages')) {
  74.             if (content) {
  75.                 this._messageNode.one(
  76.                     '.' + this.getClassName('message', 'content'))
  77.                     .setHTML(content);

  78.                 this.get('boundingBox').addClass(
  79.                     this.getClassName('message','visible'));
  80.             } else {
  81.                 // TODO: is this right?
  82.                 // If no message provided, remove the message node.
  83.                 this.hideMessage();
  84.             }
  85.         }

  86.         return this;
  87.     },

  88.     //--------------------------------------------------------------------------
  89.     // Protected methods
  90.     //--------------------------------------------------------------------------
  91.     /**
  92.     Updates the colspan of the `<td>` used to display the messages.

  93.     @method _afterMessageColumnsChange
  94.     @param {EventFacade} e The columnsChange event
  95.     @protected
  96.     @since 3.5.0
  97.     **/
  98.     _afterMessageColumnsChange: function (e) {
  99.         var contentNode;

  100.         if (this._messageNode) {
  101.             contentNode = this._messageNode.one(
  102.                 '.' + this.getClassName('message', 'content'));

  103.             if (contentNode) {
  104.                 // FIXME: This needs to become a class extension plus a view or
  105.                 // plugin for the table view.
  106.                 contentNode.set('colSpan', this._displayColumns.length);
  107.             }
  108.         }
  109.     },

  110.     /**
  111.     Relays to `_uiSetMessage` to hide or show the message node.

  112.     @method _afterMessageDataChange
  113.     @param {EventFacade} e The dataChange event
  114.     @protected
  115.     @since 3.5.0
  116.     **/
  117.     _afterMessageDataChange: function (e) {
  118.         this._uiSetMessage();
  119.     },

  120.     /**
  121.     Removes the message node if `showMessages` is `false`, or relays to
  122.     `_uiSetMessage` if `true`.

  123.     @method _afterShowMessagesChange
  124.     @param {EventFacade} e The showMessagesChange event
  125.     @protected
  126.     @since 3.5.0
  127.     **/
  128.     _afterShowMessagesChange: function (e) {
  129.         if (e.newVal) {
  130.             this._uiSetMessage(e);
  131.         } else if (this._messageNode) {
  132.             this.get('boundingBox').removeClass(
  133.                 this.getClassName('message', 'visible'));

  134.             this._messageNode.remove().destroy(true);
  135.             this._messageNode = null;
  136.         }
  137.     },

  138.     /**
  139.     Binds the events necessary to keep the message node in sync with the current
  140.     table and configuration state.

  141.     @method _bindMessageUI
  142.     @protected
  143.     @since 3.5.0
  144.     **/
  145.     _bindMessageUI: function () {
  146.         this.after(['dataChange', '*:add', '*:remove', '*:reset'],
  147.             Y.bind('_afterMessageDataChange', this));

  148.         this.after('columnsChange', Y.bind('_afterMessageColumnsChange', this));

  149.         this.after('showMessagesChange',
  150.             Y.bind('_afterShowMessagesChange', this));
  151.     },

  152.     /**
  153.     Merges in the message related strings and hooks into the rendering cycle to
  154.     also render and bind the message node.

  155.     @method initializer
  156.     @protected
  157.     @since 3.5.0
  158.     **/
  159.     initializer: function () {
  160.         this._initMessageStrings();

  161.         if (this.get('showMessages')) {
  162.             this.after('renderBody', Y.bind('_initMessageNode', this));
  163.         }

  164.         this.after(Y.bind('_bindMessageUI', this), this, 'bindUI');
  165.         this.after(Y.bind('_syncMessageUI', this), this, 'syncUI');
  166.     },

  167.     /**
  168.     Creates the `_messageNode` property from the configured `MESSAGE_TEMPLATE`
  169.     and inserts it before the `<table>`'s `<tbody>` node.

  170.     @method _initMessageNode
  171.     @protected
  172.     @since 3.5.0
  173.     **/
  174.     _initMessageNode: function () {
  175.         if (!this._messageNode) {
  176.             this._messageNode = Y.Node.create(
  177.                 Y.Lang.sub(this.MESSAGE_TEMPLATE, {
  178.                     className: this.getClassName('message'),
  179.                     contentClass: this.getClassName('message', 'content'),
  180.                     colspan: this._displayColumns.length || 1
  181.                 }));

  182.             this._tableNode.insertBefore(this._messageNode, this._tbodyNode);
  183.         }
  184.     },

  185.     /**
  186.     Add the messaging related strings to the `strings` map.
  187.    
  188.     @method _initMessageStrings
  189.     @protected
  190.     @since 3.5.0
  191.     **/
  192.     _initMessageStrings: function () {
  193.         // Not a valueFn because other class extensions will want to add to it
  194.         this.set('strings', Y.mix((this.get('strings') || {}),
  195.             Y.Intl.get('datatable-message')));
  196.     },

  197.     /**
  198.     Node used to display messages from `showMessage`.

  199.     @property _messageNode
  200.     @type {Node}
  201.     @value `undefined` (not initially set)
  202.     @since 3.5.0
  203.     **/
  204.     //_messageNode: null,

  205.     /**
  206.     Synchronizes the message UI with the table state.

  207.     @method _syncMessageUI
  208.     @protected
  209.     @since 3.5.0
  210.     **/
  211.     _syncMessageUI: function () {
  212.         this._uiSetMessage();
  213.     },

  214.     /**
  215.     Calls `hideMessage` or `showMessage` as appropriate based on the presence of
  216.     records in the `data` ModelList.

  217.     This is called when `data` is reset or records are added or removed.  Also,
  218.     if the `showMessages` attribute is updated.  In either case, if the
  219.     triggering event has a `message` property on the EventFacade, it will be
  220.     passed to `showMessage` (if appropriate).  If no such property is on the
  221.     facade, the `emptyMessage` will be used (see the strings).

  222.     @method _uiSetMessage
  223.     @param {EventFacade} e The columnsChange event
  224.     @protected
  225.     @since 3.5.0
  226.     **/
  227.     _uiSetMessage: function (e) {
  228.         if (!this.data.size()) {
  229.             this.showMessage((e && e.message) || 'emptyMessage');
  230.         } else {
  231.             this.hideMessage();
  232.         }
  233.     }
  234. });


  235. if (Y.Lang.isFunction(Y.DataTable)) {
  236.     Y.Base.mix(Y.DataTable, [ Message ]);
  237. }

  238.