~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to src/maasserver/static/js/yui/3.4.1/datatable-sort/datatable-sort.js

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-15 18:14:08 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120315181408-zgl94hzo0x4n99an
Tags: 0.1+bzr295+dfsg-0ubuntu2
* debian/patches:
  - 01-fix-database-settings.patch: Update to set PSERV_URL.
  - 02-pserv-config.patch: Set port to 8001.
* debian/maas.postinst: Run maas-import-isos on install.
* debian/control: Depends on rabbitmq-server.

Show diffs side-by-side

added added

removed removed

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