3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('datatable-message', function(Y) {
10
Adds support for a message container to appear in the table. This can be used
11
to indicate loading progress, lack of records, or any other communication
15
@submodule datatable-message
21
_API docs for this extension are included in the DataTable class._
23
Adds support for a message container to appear in the table. This can be used
24
to indicate loading progress, lack of records, or any other communication
27
Features added to `Y.DataTable`, and made available for custom classes at
28
`Y.DataTable.Message`.
30
@class DataTable.Message
34
Y.namespace('DataTable').Message = Message = function () {};
38
Enables the display of messages in the table. Setting this to false will
39
prevent the message Node from being created and `showMessage` from doing
42
@attribute showMessages
49
validator: Y.Lang.isBoolean
53
Y.mix(Message.prototype, {
55
Template used to generate the node that will be used to report messages.
57
@property MESSAGE_TEMPLATE
59
@default <tbody class="{className}"><td class="{contentClass}" colspan="{colspan}"></td></tbody>
62
MESSAGE_TEMPLATE: '<tbody class="{className}"><tr><td class="{contentClass}" colspan="{colspan}"></td></tr></tbody>',
65
Hides the message node.
72
hideMessage: function () {
73
this.get('boundingBox').removeClass(
74
this.getClassName('message', 'visible'));
80
Display the message node and set its content to `message`. If there is a
81
localized `strings` entry for the value of `message`, that string will be
85
@param {String} message The message name or message itself to display
90
showMessage: function (message) {
91
var content = this.getString(message) || message;
93
if (!this._messageNode) {
94
this._initMessageNode();
97
if (this.get('showMessages')) {
99
this._messageNode.one(
100
'.' + this.getClassName('message', 'content'))
101
.setContent(content);
103
this.get('boundingBox').addClass(
104
this.getClassName('message','visible'));
106
// TODO: is this right?
107
// If no message provided, remove the message node.
115
//--------------------------------------------------------------------------
117
//--------------------------------------------------------------------------
119
Updates the colspan of the `<td>` used to display the messages.
121
@method _afterMessageColumnsChange
122
@param {EventFacade} e The columnsChange event
126
_afterMessageColumnsChange: function (e) {
129
if (this._messageNode) {
130
contentNode = this._messageNode.one(
131
'.' + this.getClassName('message', 'content'));
134
contentNode.set('colSpan', this._displayColumns.length);
140
Relays to `_uiSetMessage` to hide or show the message node.
142
@method _afterMessageDataChange
143
@param {EventFacade} e The dataChange event
147
_afterMessageDataChange: function (e) {
148
this._uiSetMessage();
152
Removes the message node if `showMessages` is `false`, or relays to
153
`_uiSetMessage` if `true`.
155
@method _afterShowMessagesChange
156
@param {EventFacade} e The showMessagesChange event
160
_afterShowMessagesChange: function (e) {
162
this._uiSetMessage(e);
163
} else if (this._messageNode) {
164
this.get('boundingBox').removeClass(
165
this.getClassName('message', 'visible'));
167
this._messageNode.remove().destroy(true);
168
this._messageNode = null;
173
Binds the events necessary to keep the message node in sync with the current
174
table and configuration state.
176
@method _bindMessageUI
180
_bindMessageUI: function () {
181
this.after(['dataChange', '*:add', '*:remove', '*:reset'],
182
Y.bind('_afterMessageDataChange', this));
184
this.after('columnsChange', Y.bind('_afterMessageColumnsChange', this));
186
this.after('showMessagesChange',
187
Y.bind('_afterShowMessagesChange', this));
191
Merges in the message related strings and hooks into the rendering cycle to
192
also render and bind the message node.
198
initializer: function () {
199
this._initMessageStrings();
201
if (this.get('showMessages')) {
202
this.after('renderBody', Y.bind('_initMessageNode', this));
205
this.after(Y.bind('_bindMessageUI', this), this, 'bindUI');
206
this.after(Y.bind('_syncMessageUI', this), this, 'syncUI');
210
Creates the `_messageNode` property from the configured `MESSAGE_TEMPLATE`
211
and inserts it before the `<table>`'s `<tbody>` node.
213
@method _initMessageNode
217
_initMessageNode: function () {
218
if (!this._messageNode) {
219
this._messageNode = Y.Node.create(
220
Y.Lang.sub(this.MESSAGE_TEMPLATE, {
221
className: this.getClassName('message'),
222
contentClass: this.getClassName('message', 'content'),
223
colspan: this._displayColumns.length || 1
226
this._tableNode.insertBefore(this._messageNode, this._tbodyNode);
231
Add the messaging related strings to the `strings` map.
233
@method _initMessageStrings
237
_initMessageStrings: function () {
238
// Not a valueFn because other class extensions will want to add to it
239
this.set('strings', Y.mix((this.get('strings') || {}),
240
Y.Intl.get('datatable-message')));
244
Node used to display messages from `showMessage`.
246
@property _messageNode
248
@value `undefined` (not initially set)
251
//_messageNode: null,
254
Synchronizes the message UI with the table state.
256
@method _syncMessageUI
260
_syncMessageUI: function () {
261
this._uiSetMessage();
265
Calls `hideMessage` or `showMessage` as appropriate based on the presence of
266
records in the `data` ModelList.
268
This is called when `data` is reset or records are added or removed. Also,
269
if the `showMessages` attribute is updated. In either case, if the
270
triggering event has a `message` property on the EventFacade, it will be
271
passed to `showMessage` (if appropriate). If no such property is on the
272
facade, the `emptyMessage` will be used (see the strings).
274
@method _uiSetMessage
275
@param {EventFacade} e The columnsChange event
279
_uiSetMessage: function (e) {
280
if (!this.data.size()) {
281
this.showMessage((e && e.message) || 'emptyMessage');
289
if (Y.Lang.isFunction(Y.DataTable)) {
290
Y.Base.mix(Y.DataTable, [ Message ]);
294
}, '3.5.0' ,{requires:['datatable-base'], skinnable:true, lang:['en']});