kendo.columnsorter.js
8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Kendo UI v2016.1.112 (http://www.telerik.com/kendo-ui)
* Copyright 2016 Telerik AD. All rights reserved.
*
* Kendo UI commercial licenses may be obtained at
* http://www.telerik.com/purchase/license-agreement/kendo-ui-complete
* If you do not own a commercial license, this file shall be governed by the trial license terms.
*/
(function (f, define) {
define('kendo.columnsorter', ['kendo.core'], f);
}(function () {
var __meta__ = {
id: 'columnsorter',
name: 'Column Sorter',
category: 'framework',
depends: ['core'],
advanced: true
};
(function ($, undefined) {
var kendo = window.kendo;
var ui = kendo.ui;
var Widget = ui.Widget;
var DIR = 'dir';
var ASC = 'asc';
var SINGLE = 'single';
var FIELD = 'field';
var DESC = 'desc';
var sorterNS = '.kendoColumnSorter';
var TLINK = '.k-link';
var ARIASORT = 'aria-sort';
var proxy = $.proxy;
var ColumnSorter = Widget.extend({
init: function (element, options) {
var that = this, link;
Widget.fn.init.call(that, element, options);
that._refreshHandler = proxy(that.refresh, that);
that.dataSource = that.options.dataSource.bind('change', that._refreshHandler);
link = that.element.find(TLINK);
if (!link[0]) {
link = that.element.wrapInner('<a class="k-link" href="#"/>').find(TLINK);
}
that.link = link;
that.element.on('click' + sorterNS, proxy(that._click, that));
},
options: {
name: 'ColumnSorter',
mode: SINGLE,
allowUnsort: true,
compare: null,
filter: ''
},
destroy: function () {
var that = this;
Widget.fn.destroy.call(that);
that.element.off(sorterNS);
that.dataSource.unbind('change', that._refreshHandler);
that._refreshHandler = that.element = that.link = that.dataSource = null;
},
refresh: function () {
var that = this, sort = that.dataSource.sort() || [], idx, length, descriptor, dir, element = that.element, field = element.attr(kendo.attr(FIELD));
element.removeAttr(kendo.attr(DIR));
element.removeAttr(ARIASORT);
for (idx = 0, length = sort.length; idx < length; idx++) {
descriptor = sort[idx];
if (field == descriptor.field) {
element.attr(kendo.attr(DIR), descriptor.dir);
}
}
dir = element.attr(kendo.attr(DIR));
element.find('.k-i-arrow-n,.k-i-arrow-s').remove();
if (dir === ASC) {
$('<span class="k-icon k-i-arrow-n" />').appendTo(that.link);
element.attr(ARIASORT, 'ascending');
} else if (dir === DESC) {
$('<span class="k-icon k-i-arrow-s" />').appendTo(that.link);
element.attr(ARIASORT, 'descending');
}
},
_click: function (e) {
var that = this, element = that.element, field = element.attr(kendo.attr(FIELD)), dir = element.attr(kendo.attr(DIR)), options = that.options, compare = that.options.compare === null ? undefined : that.options.compare, sort = that.dataSource.sort() || [], idx, length;
e.preventDefault();
if (options.filter && !element.is(options.filter)) {
return;
}
if (dir === ASC) {
dir = DESC;
} else if (dir === DESC && options.allowUnsort) {
dir = undefined;
} else {
dir = ASC;
}
if (options.mode === SINGLE) {
sort = [{
field: field,
dir: dir,
compare: compare
}];
} else if (options.mode === 'multiple') {
for (idx = 0, length = sort.length; idx < length; idx++) {
if (sort[idx].field === field) {
sort.splice(idx, 1);
break;
}
}
sort.push({
field: field,
dir: dir,
compare: compare
});
}
this.dataSource.sort(sort);
}
});
ui.plugin(ColumnSorter);
}(window.kendo.jQuery));
return window.kendo;
}, typeof define == 'function' && define.amd ? define : function (a1, a2, a3) {
(a3 || a2)();
}));