-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.js
169 lines (135 loc) · 4.03 KB
/
sort.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Sort init
*
*/
function init (module, config) {
// get the module instance
var self = module;
(function (mod) {
// set sort cache
mod.sortCache = { "sort": [] };
mod.sortCount = 1;
mod.sortClass = "sort"
mod.sortSelector = "th span." + mod.sortClass;
// click handlers on headers
$(mod.dom).on("click", "th", function (e) {
// no sort
if ($(this).attr('data-nosort')) { return; }
// clicked header
var $current = $(this);
$current = $current.find("." + mod.sortClass);
// th element
var $th = $current.parent()
, $current = $th.find("." + mod.sortClass + ":visible")
, $next = $current.next()
;
// change sort icon
if (!$next.length) { $next = $th.find("." + mod.sortClass).first(); }
// get sort data
var sort = $next.data(mod.sortClass);
if (!sort) { return; }
// get sort cache
var options = mod.sortCache
, infScroll = mod.config.options.infiniteScroll
;
if (infScroll) {
options.limit = infScroll.skip + infScroll.count;
options.skip = 0;
mod.clearTable = true;
}
setSort.call(mod, sort, !e.ctrlKey, true);
updateUI.call(mod);
});
})(self);
}
/**
* Sets the sort and emits "setOptions" sending the sortCache
*
*/
function setSort (sort, reset, callFind) {
// get self
var self = this;
// if reset
if (reset) {
// clear sort
clear.call(this);
// and call again this function with reset false
setSort.call(this, sort, false, callFind);
} else {
// return if no sort
if (!sort.length) { return; }
// remove existing sorts
for (var i = 0; i < self.sortCache.sort.length; ++i) {
if (sort[0] === self.sortCache.sort[i][0]) {
self.sortCache.sort.splice(i, 1);
break;
}
}
// pushSort is set to false when there is no sort
var pushSort = true;
// no sort
if (sort[1] === 0) {
// so, pushSort must be false
pushSort = false;
}
// remove first sort when sort length is higher than sortCount
if (self.sortCache.sort.length >= self.sortCount) {
self.sortCache.sort.splice(0, 1);
}
// push the new sort array in the sortCache.sort array
if (pushSort) {
// only if pushSort is true
self.sortCache.sort.push(sort);
}
// create a sort cache clone
var sCacheClone = JSON.parse(JSON.stringify(self.sortCache));
// if no-sort, don't send any sort (empty) array
if (!pushSort) {
// set undefined for sort clone
sCacheClone.sort = undefined;
}
// and emit setOptions:
// sort, reset: false, callFind: true/false
self.emit("setOptions", sCacheClone, false, callFind);
}
}
/**
* Clears the sort cache
*
*/
function clear () {
var self = this;
self.sortCache = self.sortCache || {};
self.sortCache.sort = [];
}
/**
* Sets the sort count value
*
*/
function setSortCount (sortCountToSet) {
var self = this;
self.sortCount = sortCountToSet;
}
/**
* Updates the UI elements using sortCache
*
*/
function updateUI () {
var self = this;
// hide all sort spans
$(self.sortSelector, self.dom).hide();
// show non-sorted
$(self.sortSelector + ".sort0", self.dom).show();
for (var i = 0; i < self.sortCache.sort.length; ++i) {
var cSort = self.sortCache.sort[i];
$("[data-key-sort='" + cSort[0] + "']").hide();
$("[data-key-sort='" + cSort[0] + "']." + self.sortClass + cSort[1]).show();
}
}
// exports the sort methods
module.exports = {
init: init,
clear: clear,
setSortCount: setSortCount,
setSort: setSort
};