3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('recordset-sort', function(Y) {
10
* Adds default and custom sorting functionality to the Recordset utility
12
* @submodule recordset-sort
15
var Compare = Y.ArraySort.compare,
16
isValue = Y.Lang.isValue;
19
* Plugin that adds default and custom sorting functionality to the Recordset utility
20
* @class RecordsetSort
23
function RecordsetSort(field, desc, sorter) {
24
RecordsetSort.superclass.constructor.apply(this, arguments);
27
Y.mix(RecordsetSort, {
30
NAME: "recordsetSort",
35
* @description The last properties used to sort. Consists of an object literal with the keys "field", "desc", and "sorter"
37
* @attribute lastSortProperties
47
validator: function(v) {
48
return (isValue(v.field) && isValue(v.desc) && isValue(v.sorter));
53
* @description Default sort function to use if none is specified by the user.
54
* Takes two records, the key to sort by, and whether sorting direction is descending or not (boolean).
55
* If two records have the same value for a given key, the ID is used as the tie-breaker.
57
* @attribute defaultSorter
62
value: function(recA, recB, field, desc) {
63
var sorted = Compare(recA.getValue(field), recB.getValue(field), desc);
65
return Compare(recA.get("id"), recB.get("id"), desc);
74
* @description A boolean telling if the recordset is in a sorted state.
76
* @attribute defaultSorter
86
Y.extend(RecordsetSort, Y.Plugin.Base, {
89
* @description Sets up the default function to use when the "sort" event is fired.
94
initializer: function(config) {
97
host = this.get('host');
100
this.publish("sort", {
101
defaultFn: Y.bind("_defSortFn", this)
104
//Toggle the isSorted ATTR based on events.
105
//Remove events dont affect isSorted, as they are just popped/sliced out
108
self.set('isSorted', true);
111
this.onHostEvent('add',
113
self.set('isSorted', false);
116
this.onHostEvent('update',
118
self.set('isSorted', false);
124
destructor: function(config) {
128
* @description Method that all sort calls go through.
129
* Sets up the lastSortProperties object with the details of the sort, and passes in parameters
130
* to the "defaultSorter" or a custom specified sort function.
135
_defSortFn: function(e) {
136
//have to work directly with _items here - changing the recordset.
137
this.get("host")._items.sort(function(a, b) {
138
return (e.sorter)(a, b, e.field, e.desc);
141
this.set('lastSortProperties', e);
145
* @description Sorts the recordset.
148
* @param field {string} A key to sort by.
149
* @param desc {boolean} True if you want sort order to be descending, false if you want sort order to be ascending
152
sort: function(field, desc, sorter) {
156
sorter: sorter || this.get("defaultSorter")
161
* @description Resorts the recordset based on the last-used sort parameters (stored in 'lastSortProperties' ATTR)
167
var p = this.get('lastSortProperties');
171
sorter: p.sorter || this.get("defaultSorter")
176
* @description Reverses the recordset calling the standard array.reverse() method.
181
reverse: function() {
182
this.get('host')._items.reverse();
186
* @description Sorts the recordset based on the last-used sort parameters, but flips the order. (ie: Descending becomes ascending, and vice versa).
192
var p = this.get('lastSortProperties');
194
//If a predefined field is not provided by which to sort by, throw an error
195
if (isValue(p.field)) {
199
sorter: p.sorter || this.get("defaultSorter")
203
Y.log('You called flip before setting a field by which to sort by. Maybe you meant to call reverse().');
208
Y.namespace("Plugin").RecordsetSort = RecordsetSort;
212
}, '3.4.1' ,{requires:['arraysort','recordset-base','plugin']});