~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/datatable-sort-deprecated/datatable-sort-deprecated-debug.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
 
YUI.add('datatable-sort-deprecated', function(Y) {
3
 
 
4
 
// API Doc comments disabled to avoid deprecated class leakage into
5
 
// non-deprecated class API docs.  See the 3.4.1 datatable API doc files in the
6
 
// download at http://yui.zenfs.com/releases/yui3/yui_3.4.1.zip for reference.
7
 
/**
8
 
Plugs DataTable with sorting functionality.
9
 
 
10
 
DEPRECATED. As of YUI 3.5.0, DataTable has been rebuilt.  This module
11
 
is designed to work with `datatable-base-deprecated` (effectively the 3.4.1
12
 
version of DataTable) and will be removed from the library in a future version.
13
 
 
14
 
See http://yuilibrary.com/yui/docs/migration.html for help upgrading to the
15
 
latest version.
16
 
 
17
 
For complete API docs for the classes in this and other deprecated
18
 
DataTable-related modules, refer to the static API doc files in the 3.4.1
19
 
download at http://yui.zenfs.com/releases/yui3/yui_3.4.1.zip
20
 
 
21
 
@module datatable-deprecated
22
 
@submodule datatable-sort-deprecated
23
 
@deprecated
24
 
**/
25
 
 
26
 
/*
27
 
 * Adds column sorting to DataTable.
28
 
 * @class DataTableSort
29
 
 * @extends Plugin.Base
30
 
 */
31
 
var YgetClassName = Y.ClassNameManager.getClassName,
32
 
 
33
 
    DATATABLE = "datatable",
34
 
    COLUMN = "column",
35
 
    ASC = "asc",
36
 
    DESC = "desc",
37
 
 
38
 
    //TODO: Don't use hrefs - use tab/arrow/enter
39
 
    TEMPLATE = '<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>';
40
 
 
41
 
 
42
 
function DataTableSort() {
43
 
    DataTableSort.superclass.constructor.apply(this, arguments);
44
 
}
45
 
 
46
 
/////////////////////////////////////////////////////////////////////////////
47
 
//
48
 
// STATIC PROPERTIES
49
 
//
50
 
/////////////////////////////////////////////////////////////////////////////
51
 
Y.mix(DataTableSort, {
52
 
    /*
53
 
     * The namespace for the plugin. This will be the property on the host which
54
 
     * references the plugin instance.
55
 
     *
56
 
     * @property NS
57
 
     * @type String
58
 
     * @static
59
 
     * @final
60
 
     * @value "sort"
61
 
     */
62
 
    NS: "sort",
63
 
 
64
 
    /*
65
 
     * Class name.
66
 
     *
67
 
     * @property NAME
68
 
     * @type String
69
 
     * @static
70
 
     * @final
71
 
     * @value "dataTableSort"
72
 
     */
73
 
    NAME: "dataTableSort",
74
 
 
75
 
/////////////////////////////////////////////////////////////////////////////
76
 
//
77
 
// ATTRIBUTES
78
 
//
79
 
/////////////////////////////////////////////////////////////////////////////
80
 
    ATTRS: {
81
 
        /*
82
 
        * @attribute trigger
83
 
        * @description Defines the trigger that causes a column to be sorted:
84
 
        * {event, selector}, where "event" is an event type and "selector" is
85
 
        * is a node query selector.
86
 
        * @type Object
87
 
        * @default {event:"click", selector:"th"}
88
 
        * @writeOnce "initOnly"
89
 
        */
90
 
        trigger: {
91
 
            value: {event:"click", selector:"th"},
92
 
            writeOnce: "initOnly"
93
 
        },
94
 
        
95
 
        /*
96
 
        * @attribute lastSortedBy
97
 
        * @description Describes last known sort state: {key,dir}, where
98
 
        * "key" is column key and "dir" is either "asc" or "desc".
99
 
        * @type Object
100
 
        */
101
 
        lastSortedBy: {
102
 
            setter: "_setLastSortedBy",
103
 
            lazyAdd: false
104
 
        },
105
 
        
106
 
        /*
107
 
        * @attribute template
108
 
        * @description Tokenized markup template for TH sort element.
109
 
        * @type String
110
 
        * @default '<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>'
111
 
        */
112
 
        template: {
113
 
            value: TEMPLATE
114
 
        },
115
 
 
116
 
        /*
117
 
         * Strings used in the UI elements.
118
 
         *
119
 
         * The strings used are defaulted from the datatable-sort language pack
120
 
         * for the language identified in the YUI "lang" configuration (which
121
 
         * defaults to "en").
122
 
         *
123
 
         * Configurable strings are "sortBy" and "reverseSortBy", which are
124
 
         * assigned to the sort link's title attribute.
125
 
         *
126
 
         * @attribute strings
127
 
         * @type {Object}
128
 
         */
129
 
        strings: {
130
 
            valueFn: function () { return Y.Intl.get('datatable-sort-deprecated'); }
131
 
        }
132
 
    }
133
 
});
134
 
 
135
 
/////////////////////////////////////////////////////////////////////////////
136
 
//
137
 
// PROTOTYPE
138
 
//
139
 
/////////////////////////////////////////////////////////////////////////////
140
 
Y.extend(DataTableSort, Y.Plugin.Base, {
141
 
 
142
 
    /////////////////////////////////////////////////////////////////////////////
143
 
    //
144
 
    // METHODS
145
 
    //
146
 
    /////////////////////////////////////////////////////////////////////////////
147
 
    /*
148
 
    * Initializer.
149
 
    *
150
 
    * @method initializer
151
 
    * @param config {Object} Config object.
152
 
    * @private
153
 
    */
154
 
    initializer: function(config) {
155
 
        var dt = this.get("host"),
156
 
            trigger = this.get("trigger");
157
 
            
158
 
        dt.get("recordset").plug(Y.Plugin.RecordsetSort, {dt: dt});
159
 
        dt.get("recordset").sort.addTarget(dt);
160
 
        
161
 
        // Wrap link around TH value
162
 
        this.doBefore("_createTheadThNode", this._beforeCreateTheadThNode);
163
 
        
164
 
        // Add class
165
 
        this.doBefore("_attachTheadThNode", this._beforeAttachTheadThNode);
166
 
        this.doBefore("_attachTbodyTdNode", this._beforeAttachTbodyTdNode);
167
 
 
168
 
        // Attach trigger handlers
169
 
        dt.delegate(trigger.event, Y.bind(this._onEventSortColumn,this), trigger.selector);
170
 
 
171
 
        // Attach UI hooks
172
 
        dt.after("recordsetSort:sort", function() {
173
 
            this._uiSetRecordset(this.get("recordset"));
174
 
        });
175
 
        this.on("lastSortedByChange", function(e) {
176
 
            this._uiSetLastSortedBy(e.prevVal, e.newVal, dt);
177
 
        });
178
 
 
179
 
        //TODO
180
 
        //dt.after("recordset:mutation", function() {//reset lastSortedBy});
181
 
        
182
 
        //TODO
183
 
        //add Column sortFn ATTR
184
 
        
185
 
        // Update UI after the fact (render-then-plug case)
186
 
        if(dt.get("rendered")) {
187
 
            dt._uiSetColumnset(dt.get("columnset"));
188
 
            this._uiSetLastSortedBy(null, this.get("lastSortedBy"), dt);
189
 
        }
190
 
    },
191
 
 
192
 
    /*
193
 
    * @method _setLastSortedBy
194
 
    * @description Normalizes lastSortedBy
195
 
    * @param val {String | Object} {key, dir} or "key"
196
 
    * @return {key, dir, notdir}
197
 
    * @private
198
 
    */
199
 
    _setLastSortedBy: function(val) {
200
 
        if (Y.Lang.isString(val)) {
201
 
            val = { key: val, dir: "desc" };
202
 
        }
203
 
 
204
 
        if (val) {
205
 
            return (val.dir === "desc") ?
206
 
                { key: val.key, dir: "desc", notdir: "asc" } :
207
 
                { key: val.key, dir: "asc",  notdir:"desc" };
208
 
        } else {
209
 
            return null;
210
 
        }
211
 
    },
212
 
 
213
 
    /*
214
 
     * Updates sort UI.
215
 
     *
216
 
     * @method _uiSetLastSortedBy
217
 
     * @param val {Object} New lastSortedBy object {key,dir}.
218
 
     * @param dt {Y.DataTable.Base} Host.
219
 
     * @protected
220
 
     */
221
 
    _uiSetLastSortedBy: function(prevVal, newVal, dt) {
222
 
        var strings    = this.get('strings'),
223
 
            columnset  = dt.get("columnset"),
224
 
            prevKey    = prevVal && prevVal.key,
225
 
            newKey     = newVal && newVal.key,
226
 
            prevClass  = prevVal && dt.getClassName(prevVal.dir),
227
 
            newClass   = newVal && dt.getClassName(newVal.dir),
228
 
            prevColumn = columnset.keyHash[prevKey],
229
 
            newColumn  = columnset.keyHash[newKey],
230
 
            tbodyNode  = dt._tbodyNode,
231
 
            fromTemplate = Y.Lang.sub,
232
 
            th, sortArrow, sortLabel;
233
 
 
234
 
        // Clear previous UI
235
 
        if (prevColumn && prevClass) {
236
 
            th = prevColumn.thNode;
237
 
            sortArrow = th.one('a');
238
 
 
239
 
            if (sortArrow) {
240
 
                sortArrow.set('title', fromTemplate(strings.sortBy, {
241
 
                    column: prevColumn.get('label')
242
 
                }));
243
 
            }
244
 
 
245
 
            th.removeClass(prevClass);
246
 
            tbodyNode.all("." + YgetClassName(COLUMN, prevColumn.get("id")))
247
 
                .removeClass(prevClass);
248
 
        }
249
 
 
250
 
        // Add new sort UI
251
 
        if (newColumn && newClass) {
252
 
            th = newColumn.thNode;
253
 
            sortArrow = th.one('a');
254
 
 
255
 
            if (sortArrow) {
256
 
                sortLabel = (newVal.dir === ASC) ? "reverseSortBy" : "sortBy";
257
 
 
258
 
                sortArrow.set('title', fromTemplate(strings[sortLabel], {
259
 
                    column: newColumn.get('label')
260
 
                }));
261
 
            }
262
 
 
263
 
            th.addClass(newClass);
264
 
 
265
 
            tbodyNode.all("." + YgetClassName(COLUMN, newColumn.get("id")))
266
 
                .addClass(newClass);
267
 
        }
268
 
    },
269
 
 
270
 
    /*
271
 
    * Before header cell element is created, inserts link markup around {value}.
272
 
    *
273
 
    * @method _beforeCreateTheadThNode
274
 
    * @param o {Object} {value, column, tr}.
275
 
    * @protected
276
 
    */
277
 
    _beforeCreateTheadThNode: function(o) {
278
 
        var sortedBy, sortLabel;
279
 
 
280
 
        if (o.column.get("sortable")) {
281
 
            sortedBy = this.get('lastSortedBy');
282
 
 
283
 
            sortLabel = (sortedBy && sortedBy.dir === ASC &&
284
 
                         sortedBy.key === o.column.get('key')) ?
285
 
                            "reverseSortBy" : "sortBy";
286
 
 
287
 
            o.value = Y.Lang.sub(this.get("template"), {
288
 
                link_class: o.link_class || "",
289
 
                link_title: Y.Lang.sub(this.get('strings.' + sortLabel), {
290
 
                                column: o.column.get('label')
291
 
                            }),
292
 
                link_href: "#",
293
 
                value: o.value
294
 
            });
295
 
        }
296
 
    },
297
 
 
298
 
    /*
299
 
    * Before header cell element is attached, sets applicable class names.
300
 
    *
301
 
    * @method _beforeAttachTheadThNode
302
 
    * @param o {Object} {value, column, tr}.
303
 
    * @protected
304
 
    */
305
 
    _beforeAttachTheadThNode: function(o) {
306
 
        var lastSortedBy = this.get("lastSortedBy"),
307
 
            key = lastSortedBy && lastSortedBy.key,
308
 
            dir = lastSortedBy && lastSortedBy.dir,
309
 
            notdir = lastSortedBy && lastSortedBy.notdir;
310
 
 
311
 
        // This Column is sortable
312
 
        if(o.column.get("sortable")) {
313
 
            o.th.addClass(YgetClassName(DATATABLE, "sortable"));
314
 
        }
315
 
        // This Column is currently sorted
316
 
        if(key && (key === o.column.get("key"))) {
317
 
            o.th.replaceClass(YgetClassName(DATATABLE, notdir), YgetClassName(DATATABLE, dir));
318
 
        }
319
 
    },
320
 
 
321
 
    /*
322
 
    * Before header cell element is attached, sets applicable class names.
323
 
    *
324
 
    * @method _beforeAttachTbodyTdNode
325
 
    * @param o {Object} {record, column, tr, headers, classnames, value}.
326
 
    * @protected
327
 
    */
328
 
    _beforeAttachTbodyTdNode: function(o) {
329
 
        var lastSortedBy = this.get("lastSortedBy"),
330
 
            key = lastSortedBy && lastSortedBy.key,
331
 
            dir = lastSortedBy && lastSortedBy.dir,
332
 
            notdir = lastSortedBy && lastSortedBy.notdir;
333
 
 
334
 
        // This Column is sortable
335
 
        if(o.column.get("sortable")) {
336
 
            o.td.addClass(YgetClassName(DATATABLE, "sortable"));
337
 
        }
338
 
        // This Column is currently sorted
339
 
        if(key && (key === o.column.get("key"))) {
340
 
            o.td.replaceClass(YgetClassName(DATATABLE, notdir), YgetClassName(DATATABLE, dir));
341
 
        }
342
 
    },
343
 
    /*
344
 
    * In response to the "trigger" event, sorts the underlying Recordset and
345
 
    * updates the lastSortedBy attribute.
346
 
    *
347
 
    * @method _onEventSortColumn
348
 
    * @param o {Object} {value, column, tr}.
349
 
    * @protected
350
 
    */
351
 
    _onEventSortColumn: function(e) {
352
 
        e.halt();
353
 
        //TODO: normalize e.currentTarget to TH
354
 
        var table  = this.get("host"),
355
 
            column = table.get("columnset").idHash[e.currentTarget.get("id")],
356
 
            key, field, lastSort, desc, sorter;
357
 
 
358
 
        if (column.get("sortable")) {
359
 
            key       = column.get("key");
360
 
            field     = column.get("field");
361
 
            lastSort  = this.get("lastSortedBy") || {};
362
 
            desc      = (lastSort.key === key && lastSort.dir === ASC);
363
 
            sorter    = column.get("sortFn");
364
 
 
365
 
            table.get("recordset").sort.sort(field, desc, sorter);
366
 
 
367
 
            this.set("lastSortedBy", {
368
 
                key: key,
369
 
                dir: (desc) ? DESC : ASC
370
 
            });
371
 
        }
372
 
    }
373
 
});
374
 
 
375
 
Y.namespace("Plugin").DataTableSort = DataTableSort;
376
 
 
377
 
 
378
 
 
379
 
 
380
 
 
381
 
}, '3.9.1' ,{requires:['datatable-base-deprecated','plugin','recordset-sort'], lang:['en']});