4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-Sortable'>/**
19 </span> * @class Ext.util.Sortable
21 A mixin which allows a data component to be sorted. This is used by e.g. {@link Ext.data.Store} and {@link Ext.data.TreeStore}.
23 **NOTE**: This mixin is mainly for internal library use and most users should not need to use it directly. It
24 is more likely you will want to use one of the component classes that import this mixin, such as
25 {@link Ext.data.Store} or {@link Ext.data.TreeStore}.
27 * @docauthor Tommy Maintz <tommy@sencha.com>
29 Ext.define("Ext.util.Sortable", {
30 <span id='Ext-util-Sortable-property-isSortable'> /**
31 </span> * @property isSortable
33 * Flag denoting that this object is sortable. Always true.
37 <span id='Ext-util-Sortable-property-defaultSortDirection'> /**
38 </span> * The default sort direction to use if one is not specified (defaults to "ASC")
39 * @property defaultSortDirection
42 defaultSortDirection: "ASC",
48 <span id='Ext-util-Sortable-property-sortRoot'> /**
49 </span> * The property in each item that contains the data to sort. (defaults to null)
54 <span id='Ext-util-Sortable-method-initSortable'> /**
55 </span> * Performs initialization of this mixin. Component classes using this mixin should call this method
56 * during their own initialization.
58 initSortable: function() {
62 <span id='Ext-util-Sortable-property-sorters'> /**
63 </span> * The collection of {@link Ext.util.Sorter Sorters} currently applied to this Store
65 * @type Ext.util.MixedCollection
67 me.sorters = Ext.create('Ext.util.AbstractMixedCollection', false, function(item) {
68 return item.id || item.property;
72 me.sorters.addAll(me.decodeSorters(sorters));
76 <span id='Ext-util-Sortable-method-sort'> /**
77 </span> * <p>Sorts the data in the Store by one or more of its properties. Example usage:</p>
78 <pre><code>
79 //sort by a single field
80 myStore.sort('myField', 'DESC');
82 //sorting by multiple fields
93 </code></pre>
94 * <p>Internally, Store converts the passed arguments into an array of {@link Ext.util.Sorter} instances, and delegates the actual
95 * sorting to its internal {@link Ext.util.MixedCollection}.</p>
96 * <p>When passing a single string argument to sort, Store maintains a ASC/DESC toggler per field, so this code:</p>
97 <pre><code>
98 store.sort('myField');
99 store.sort('myField');
100 </code></pre>
101 * <p>Is equivalent to this code, because Store handles the toggling automatically:</p>
102 <pre><code>
103 store.sort('myField', 'ASC');
104 store.sort('myField', 'DESC');
105 </code></pre>
106 * @param {String|Array} sorters Either a string name of one of the fields in this Store's configured {@link Ext.data.Model Model},
107 * or an Array of sorter configurations.
108 * @param {String} direction The overall direction to sort the data by. Defaults to "ASC".
110 sort: function(sorters, direction, where, doSort) {
115 if (Ext.isArray(sorters)) {
118 newSorters = sorters;
120 else if (Ext.isObject(sorters)) {
123 newSorters = [sorters];
125 else if (Ext.isString(sorters)) {
126 sorter = me.sorters.get(sorters);
133 newSorters = [sorter];
135 else if (direction === undefined) {
139 sorter.setDirection(direction);
143 if (newSorters && newSorters.length) {
144 newSorters = me.decodeSorters(newSorters);
145 if (Ext.isString(where)) {
146 if (where === 'prepend') {
147 sorters = me.sorters.clone().items;
150 me.sorters.addAll(newSorters);
151 me.sorters.addAll(sorters);
154 me.sorters.addAll(newSorters);
159 me.sorters.addAll(newSorters);
162 if (doSort !== false) {
163 me.onBeforeSort(newSorters);
167 if (doSort !== false) {
168 sorters = me.sorters.items;
169 if (sorters.length) {
170 //construct an amalgamated sorter function which combines all of the Sorters passed
171 sorterFn = function(r1, r2) {
172 var result = sorters[0].sort(r1, r2),
173 length = sorters.length,
176 //if we have more than one sorter, OR any additional sorter functions together
177 for (i = 1; i < length; i++) {
178 result = result || sorters[i].sort.call(this, r1, r2);
191 onBeforeSort: Ext.emptyFn,
193 <span id='Ext-util-Sortable-method-decodeSorters'> /**
195 * Normalizes an array of sorter objects, ensuring that they are all Ext.util.Sorter instances
196 * @param {Array} sorters The sorters array
197 * @return {Array} Array of Ext.util.Sorter objects
199 decodeSorters: function(sorters) {
200 if (!Ext.isArray(sorters)) {
201 if (sorters === undefined) {
208 var length = sorters.length,
209 Sorter = Ext.util.Sorter,
210 fields = this.model ? this.model.prototype.fields : null,
214 for (i = 0; i < length; i++) {
217 if (!(config instanceof Sorter)) {
218 if (Ext.isString(config)) {
224 Ext.applyIf(config, {
225 root : this.sortRoot,
226 direction: "ASC"
229 //support for 3.x style sorters where a function can be defined as 'fn'
231 config.sorterFn = config.fn;
234 //support a function to be passed as a sorter definition
235 if (typeof config == 'function') {
241 // ensure sortType gets pushed on if necessary
242 if (fields && !config.transform) {
243 field = fields.get(config.property);
244 config.transform = field ? field.sortType : undefined;
246 sorters[i] = Ext.create('Ext.util.Sorter', config);
253 getSorters: function() {
254 return this.sorters.items;