/
recordset-sort-debug.js
206 lines (176 loc) · 5.49 KB
/
recordset-sort-debug.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
YUI.add('recordset-sort', function(Y) {
/**
* Adds default and custom sorting functionality to the Recordset utility
* @module recordset
* @submodule recordset-sort
*/
var Compare = Y.ArraySort.compare,
isValue = Y.Lang.isValue;
/**
* Plugin that adds default and custom sorting functionality to the Recordset utility
* @class RecordsetSort
*/
function RecordsetSort(field, desc, sorter) {
RecordsetSort.superclass.constructor.apply(this, arguments);
}
Y.mix(RecordsetSort, {
NS: "sort",
NAME: "recordsetSort",
ATTRS: {
/**
* @description The last properties used to sort. Consists of an object literal with the keys "field", "desc", and "sorter"
*
* @attribute lastSortProperties
* @public
* @type object
*/
lastSortProperties: {
value: {
field: undefined,
desc: true,
sorter: undefined
},
validator: function(v) {
return (isValue(v.field) && isValue(v.desc) && isValue(v.sorter));
}
},
/**
* @description Default sort function to use if none is specified by the user.
* Takes two records, the key to sort by, and whether sorting direction is descending or not (boolean).
* If two records have the same value for a given key, the ID is used as the tie-breaker.
*
* @attribute defaultSorter
* @public
* @type function
*/
defaultSorter: {
value: function(recA, recB, field, desc) {
var sorted = Compare(recA.getValue(field), recB.getValue(field), desc);
if (sorted === 0) {
return Compare(recA.get("id"), recB.get("id"), desc);
}
else {
return sorted;
}
}
},
/**
* @description A boolean telling if the recordset is in a sorted state.
*
* @attribute defaultSorter
* @public
* @type function
*/
isSorted: {
value: false
}
}
});
Y.extend(RecordsetSort, Y.Plugin.Base, {
/**
* @description Sets up the default function to use when the "sort" event is fired.
*
* @method initializer
* @protected
*/
initializer: function(config) {
var self = this,
host = this.get('host');
this.publish("sort", {
defaultFn: Y.bind("_defSortFn", this)
});
//Toggle the isSorted ATTR based on events.
//Remove events dont affect isSorted, as they are just popped/sliced out
this.on("sort",
function() {
self.set('isSorted', true);
});
this.onHostEvent('add',
function() {
self.set('isSorted', false);
},
host);
this.onHostEvent('update',
function() {
self.set('isSorted', false);
},
host);
},
destructor: function(config) {
},
/**
* @description Method that all sort calls go through.
* Sets up the lastSortProperties object with the details of the sort, and passes in parameters
* to the "defaultSorter" or a custom specified sort function.
*
* @method _defSortFn
* @private
*/
_defSortFn: function(e) {
//have to work directly with _items here - changing the recordset.
this.get("host")._items.sort(function(a, b) {
return (e.sorter)(a, b, e.field, e.desc);
});
this.set('lastSortProperties', e);
},
/**
* @description Sorts the recordset.
*
* @method sort
* @param field {string} A key to sort by.
* @param desc {boolean} True if you want sort order to be descending, false if you want sort order to be ascending
* @public
*/
sort: function(field, desc, sorter) {
this.fire("sort", {
field: field,
desc: desc,
sorter: sorter || this.get("defaultSorter")
});
},
/**
* @description Resorts the recordset based on the last-used sort parameters (stored in 'lastSortProperties' ATTR)
*
* @method resort
* @public
*/
resort: function() {
var p = this.get('lastSortProperties');
this.fire("sort", {
field: p.field,
desc: p.desc,
sorter: p.sorter || this.get("defaultSorter")
});
},
/**
* @description Reverses the recordset calling the standard array.reverse() method.
*
* @method reverse
* @public
*/
reverse: function() {
this.get('host')._items.reverse();
},
/**
* @description Sorts the recordset based on the last-used sort parameters, but flips the order. (ie: Descending becomes ascending, and vice versa).
*
* @method flip
* @public
*/
flip: function() {
var p = this.get('lastSortProperties');
//If a predefined field is not provided by which to sort by, throw an error
if (isValue(p.field)) {
this.fire("sort", {
field: p.field,
desc: !p.desc,
sorter: p.sorter || this.get("defaultSorter")
});
}
else {
Y.log('You called flip before setting a field by which to sort by. Maybe you meant to call reverse().');
}
}
});
Y.namespace("Plugin").RecordsetSort = RecordsetSort;
}, '@VERSION@' ,{requires:['arraysort','recordset-base','plugin']});