1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('datatable-sort-deprecated', function(Y) {
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.
8
Plugs DataTable with sorting functionality.
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.
14
See http://yuilibrary.com/yui/docs/migration.html for help upgrading to the
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
21
@module datatable-deprecated
22
@submodule datatable-sort-deprecated
27
* Adds column sorting to DataTable.
28
* @class DataTableSort
29
* @extends Plugin.Base
31
var YgetClassName = Y.ClassNameManager.getClassName,
33
DATATABLE = "datatable",
38
//TODO: Don't use hrefs - use tab/arrow/enter
39
TEMPLATE = '<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>';
42
function DataTableSort() {
43
DataTableSort.superclass.constructor.apply(this, arguments);
46
/////////////////////////////////////////////////////////////////////////////
50
/////////////////////////////////////////////////////////////////////////////
51
Y.mix(DataTableSort, {
53
* The namespace for the plugin. This will be the property on the host which
54
* references the plugin instance.
71
* @value "dataTableSort"
73
NAME: "dataTableSort",
75
/////////////////////////////////////////////////////////////////////////////
79
/////////////////////////////////////////////////////////////////////////////
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.
87
* @default {event:"click", selector:"th"}
88
* @writeOnce "initOnly"
91
value: {event:"click", selector:"th"},
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".
102
setter: "_setLastSortedBy",
107
* @attribute template
108
* @description Tokenized markup template for TH sort element.
110
* @default '<a class="{link_class}" title="{link_title}" href="{link_href}">{value}</a>'
117
* Strings used in the UI elements.
119
* The strings used are defaulted from the datatable-sort language pack
120
* for the language identified in the YUI "lang" configuration (which
123
* Configurable strings are "sortBy" and "reverseSortBy", which are
124
* assigned to the sort link's title attribute.
130
valueFn: function () { return Y.Intl.get('datatable-sort-deprecated'); }
135
/////////////////////////////////////////////////////////////////////////////
139
/////////////////////////////////////////////////////////////////////////////
140
Y.extend(DataTableSort, Y.Plugin.Base, {
142
/////////////////////////////////////////////////////////////////////////////
146
/////////////////////////////////////////////////////////////////////////////
150
* @method initializer
151
* @param config {Object} Config object.
154
initializer: function(config) {
155
var dt = this.get("host"),
156
trigger = this.get("trigger");
158
dt.get("recordset").plug(Y.Plugin.RecordsetSort, {dt: dt});
159
dt.get("recordset").sort.addTarget(dt);
161
// Wrap link around TH value
162
this.doBefore("_createTheadThNode", this._beforeCreateTheadThNode);
165
this.doBefore("_attachTheadThNode", this._beforeAttachTheadThNode);
166
this.doBefore("_attachTbodyTdNode", this._beforeAttachTbodyTdNode);
168
// Attach trigger handlers
169
dt.delegate(trigger.event, Y.bind(this._onEventSortColumn,this), trigger.selector);
172
dt.after("recordsetSort:sort", function() {
173
this._uiSetRecordset(this.get("recordset"));
175
this.on("lastSortedByChange", function(e) {
176
this._uiSetLastSortedBy(e.prevVal, e.newVal, dt);
180
//dt.after("recordset:mutation", function() {//reset lastSortedBy});
183
//add Column sortFn ATTR
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);
193
* @method _setLastSortedBy
194
* @description Normalizes lastSortedBy
195
* @param val {String | Object} {key, dir} or "key"
196
* @return {key, dir, notdir}
199
_setLastSortedBy: function(val) {
200
if (Y.Lang.isString(val)) {
201
val = { key: val, dir: "desc" };
205
return (val.dir === "desc") ?
206
{ key: val.key, dir: "desc", notdir: "asc" } :
207
{ key: val.key, dir: "asc", notdir:"desc" };
216
* @method _uiSetLastSortedBy
217
* @param val {Object} New lastSortedBy object {key,dir}.
218
* @param dt {Y.DataTable.Base} Host.
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;
235
if (prevColumn && prevClass) {
236
th = prevColumn.thNode;
237
sortArrow = th.one('a');
240
sortArrow.set('title', fromTemplate(strings.sortBy, {
241
column: prevColumn.get('label')
245
th.removeClass(prevClass);
246
tbodyNode.all("." + YgetClassName(COLUMN, prevColumn.get("id")))
247
.removeClass(prevClass);
251
if (newColumn && newClass) {
252
th = newColumn.thNode;
253
sortArrow = th.one('a');
256
sortLabel = (newVal.dir === ASC) ? "reverseSortBy" : "sortBy";
258
sortArrow.set('title', fromTemplate(strings[sortLabel], {
259
column: newColumn.get('label')
263
th.addClass(newClass);
265
tbodyNode.all("." + YgetClassName(COLUMN, newColumn.get("id")))
271
* Before header cell element is created, inserts link markup around {value}.
273
* @method _beforeCreateTheadThNode
274
* @param o {Object} {value, column, tr}.
277
_beforeCreateTheadThNode: function(o) {
278
var sortedBy, sortLabel;
280
if (o.column.get("sortable")) {
281
sortedBy = this.get('lastSortedBy');
283
sortLabel = (sortedBy && sortedBy.dir === ASC &&
284
sortedBy.key === o.column.get('key')) ?
285
"reverseSortBy" : "sortBy";
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')
299
* Before header cell element is attached, sets applicable class names.
301
* @method _beforeAttachTheadThNode
302
* @param o {Object} {value, column, tr}.
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;
311
// This Column is sortable
312
if(o.column.get("sortable")) {
313
o.th.addClass(YgetClassName(DATATABLE, "sortable"));
315
// This Column is currently sorted
316
if(key && (key === o.column.get("key"))) {
317
o.th.replaceClass(YgetClassName(DATATABLE, notdir), YgetClassName(DATATABLE, dir));
322
* Before header cell element is attached, sets applicable class names.
324
* @method _beforeAttachTbodyTdNode
325
* @param o {Object} {record, column, tr, headers, classnames, value}.
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;
334
// This Column is sortable
335
if(o.column.get("sortable")) {
336
o.td.addClass(YgetClassName(DATATABLE, "sortable"));
338
// This Column is currently sorted
339
if(key && (key === o.column.get("key"))) {
340
o.td.replaceClass(YgetClassName(DATATABLE, notdir), YgetClassName(DATATABLE, dir));
344
* In response to the "trigger" event, sorts the underlying Recordset and
345
* updates the lastSortedBy attribute.
347
* @method _onEventSortColumn
348
* @param o {Object} {value, column, tr}.
351
_onEventSortColumn: function(e) {
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;
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");
365
table.get("recordset").sort.sort(field, desc, sorter);
367
this.set("lastSortedBy", {
369
dir: (desc) ? DESC : ASC
375
Y.namespace("Plugin").DataTableSort = DataTableSort;
381
}, '3.9.1' ,{requires:['datatable-base-deprecated','plugin','recordset-sort'], lang:['en']});