Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('recordset-sort', function (Y, NAME) {
2
 
3
/**
4
 * Adds default and custom sorting functionality to the Recordset utility
5
 * @module recordset
6
 * @submodule recordset-sort
7
 */
8
 
9
var Compare = Y.ArraySort.compare,
10
isValue = Y.Lang.isValue;
11
 
12
/**
13
 * Plugin that adds default and custom sorting functionality to the Recordset utility
14
 * @class RecordsetSort
15
 */
16
 
17
function RecordsetSort(field, desc, sorter) {
18
    RecordsetSort.superclass.constructor.apply(this, arguments);
19
}
20
 
21
Y.mix(RecordsetSort, {
22
    NS: "sort",
23
 
24
    NAME: "recordsetSort",
25
 
26
    ATTRS: {
27
 
28
        /**
29
        * @description The last properties used to sort. Consists of an object literal with the keys "field", "desc", and "sorter"
30
        *
31
        * @attribute lastSortProperties
32
        * @public
33
        * @type object
34
        */
35
        lastSortProperties: {
36
            value: {
37
                field: undefined,
38
                desc: true,
39
                sorter: undefined
40
            },
41
            validator: function(v) {
42
                return (isValue(v.field) && isValue(v.desc) && isValue(v.sorter));
43
            }
44
        },
45
 
46
        /**
47
        * @description Default sort function to use if none is specified by the user.
48
        * Takes two records, the key to sort by, and whether sorting direction is descending or not (boolean).
49
        * If two records have the same value for a given key, the ID is used as the tie-breaker.
50
        *
51
        * @attribute defaultSorter
52
        * @public
53
        * @type function
54
        */
55
        defaultSorter: {
56
            value: function(recA, recB, field, desc) {
57
                var sorted = Compare(recA.getValue(field), recB.getValue(field), desc);
58
                if (sorted === 0) {
59
                    return Compare(recA.get("id"), recB.get("id"), desc);
60
                }
61
                else {
62
                    return sorted;
63
                }
64
            }
65
        },
66
 
67
        /**
68
        * @description A boolean telling if the recordset is in a sorted state.
69
        *
70
        * @attribute defaultSorter
71
        * @public
72
        * @type function
73
        */
74
        isSorted: {
75
            value: false
76
        }
77
    }
78
});
79
 
80
Y.extend(RecordsetSort, Y.Plugin.Base, {
81
 
82
    /**
83
     * @description Sets up the default function to use when the "sort" event is fired.
84
     *
85
     * @method initializer
86
     * @protected
87
     */
88
    initializer: function(config) {
89
 
90
        var self = this,
91
        host = this.get('host');
92
 
93
 
94
        this.publish("sort", {
95
            defaultFn: Y.bind("_defSortFn", this)
96
        });
97
 
98
        //Toggle the isSorted ATTR based on events.
99
        //Remove events dont affect isSorted, as they are just popped/sliced out
100
        this.on("sort",
101
        function() {
102
            self.set('isSorted', true);
103
        });
104
 
105
        this.onHostEvent('add',
106
        function() {
107
            self.set('isSorted', false);
108
        },
109
        host);
110
        this.onHostEvent('update',
111
        function() {
112
            self.set('isSorted', false);
113
        },
114
        host);
115
 
116
    },
117
 
118
    destructor: function(config) {
119
        },
120
 
121
    /**
122
     * @description Method that all sort calls go through.
123
     * Sets up the lastSortProperties object with the details of the sort, and passes in parameters
124
     * to the "defaultSorter" or a custom specified sort function.
125
     *
126
     * @method _defSortFn
127
     * @private
128
     */
129
    _defSortFn: function(e) {
130
        //have to work directly with _items here - changing the recordset.
131
        this.get("host")._items.sort(function(a, b) {
132
            return (e.sorter)(a, b, e.field, e.desc);
133
        });
134
 
135
        this.set('lastSortProperties', e);
136
    },
137
 
138
    /**
139
     * @description Sorts the recordset.
140
     *
141
     * @method sort
142
     * @param field {string} A key to sort by.
143
     * @param desc {boolean} True if you want sort order to be descending, false if you want sort order to be ascending
144
     * @public
145
     */
146
    sort: function(field, desc, sorter) {
147
        this.fire("sort", {
148
            field: field,
149
            desc: desc,
150
            sorter: sorter || this.get("defaultSorter")
151
        });
152
    },
153
 
154
    /**
155
     * @description Resorts the recordset based on the last-used sort parameters (stored in 'lastSortProperties' ATTR)
156
     *
157
     * @method resort
158
     * @public
159
     */
160
    resort: function() {
161
        var p = this.get('lastSortProperties');
162
        this.fire("sort", {
163
            field: p.field,
164
            desc: p.desc,
165
            sorter: p.sorter || this.get("defaultSorter")
166
        });
167
    },
168
 
169
    /**
170
     * @description Reverses the recordset calling the standard array.reverse() method.
171
     *
172
     * @method reverse
173
     * @public
174
     */
175
    reverse: function() {
176
        this.get('host')._items.reverse();
177
    },
178
 
179
    /**
180
     * @description Sorts the recordset based on the last-used sort parameters, but flips the order. (ie: Descending becomes ascending, and vice versa).
181
     *
182
     * @method flip
183
     * @public
184
     */
185
    flip: function() {
186
        var p = this.get('lastSortProperties');
187
 
188
        //If a predefined field is not provided by which to sort by, throw an error
189
        if (isValue(p.field)) {
190
            this.fire("sort", {
191
                field: p.field,
192
                desc: !p.desc,
193
                sorter: p.sorter || this.get("defaultSorter")
194
            });
195
        }
196
        else {
197
        }
198
    }
199
});
200
 
201
Y.namespace("Plugin").RecordsetSort = RecordsetSort;
202
 
203
 
204
 
205
}, '3.18.1', {"requires": ["arraysort", "recordset-base", "plugin"]});