]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/src/util/Grouper.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / util / Grouper.js
CommitLineData
6527f429
DM
1/**\r
2 * Represents a grouping of items. The grouper works in a similar fashion as the\r
3 * `Ext.util.Sorter` except that groups must be able to extract a value by which all items\r
4 * in the group can be collected. By default this is derived from the `property` config\r
5 * but can be customized using the `groupFn` if necessary.\r
6 *\r
7 * All items with the same group value compare as equal. If the group values do not compare\r
8 * equally, the sort can be controlled further by setting `sortProperty` or `sorterFn`.\r
9 */\r
10Ext.define('Ext.util.Grouper', {\r
11 extend: 'Ext.util.Sorter',\r
12\r
13 isGrouper: true,\r
14\r
15 config: {\r
16 /**\r
17 * @cfg {Function} groupFn This function is called for each item in the collection\r
18 * to determine the group to which it belongs. By default the `property` value is\r
19 * used to group items.\r
20 * @cfg {Object} groupFn.item The current item from the collection.\r
21 * @cfg {String} groupFn.return The group identifier for the item.\r
22 */\r
23 groupFn: null,\r
24\r
25 /**\r
26 * @cfg {String} property The field by which records are grouped. Groups are \r
27 * sorted alphabetically by group value as the default. To sort groups by a different \r
28 * property, use the {@link #sortProperty} configuration.\r
29 */\r
30\r
31 /**\r
32 * @cfg {String} sortProperty You can set this configuration if you want the groups\r
33 * to be sorted on something other then the group string returned by the `groupFn`.\r
34 * This serves the same role as `property` on a normal `Ext.util.Sorter`.\r
35 */\r
36 sortProperty: null\r
37 },\r
38\r
39 constructor: function(config) {\r
40 //<debug>\r
41 if (config) {\r
42 if (config.getGroupString) {\r
43 Ext.raise("Cannot set getGroupString - use groupFn instead");\r
44 }\r
45 }\r
46 //</debug>\r
47\r
48 this.callParent(arguments);\r
49 },\r
50\r
51 /**\r
52 * Returns the value for grouping to be used.\r
53 * @param {Ext.data.Model} item The Model instance\r
54 * @return {String}\r
55 */\r
56 getGroupString: function (item) {\r
57 var group = this._groupFn(item);\r
58 return (group != null) ? String(group) : '';\r
59 },\r
60\r
61 sortFn: function (item1, item2) {\r
62 var me = this,\r
63 lhs = me._groupFn(item1),\r
64 rhs = me._groupFn(item2),\r
65 property = me._sortProperty, // Sorter's sortFn uses "_property"\r
66 root = me._root,\r
67 sorterFn = me._sorterFn,\r
68 transform = me._transform;\r
69\r
70 // Items with the same groupFn result must be equal... otherwise we sort them\r
71 // by sorterFn or sortProperty.\r
72 if (lhs === rhs) {\r
73 return 0;\r
74 }\r
75\r
76 if (property || sorterFn) {\r
77 if (sorterFn) {\r
78 return sorterFn.call(this, item1, item2);\r
79 }\r
80\r
81 if (root) {\r
82 item1 = item1[root];\r
83 item2 = item2[root];\r
84 }\r
85\r
86 lhs = item1[property];\r
87 rhs = item2[property];\r
88\r
89 if (transform) {\r
90 lhs = transform(lhs);\r
91 rhs = transform(rhs);\r
92 }\r
93 }\r
94\r
95 return (lhs > rhs) ? 1 : (lhs < rhs ? -1 : 0);\r
96 },\r
97\r
98 standardGroupFn: function (item) {\r
99 var root = this._root;\r
100 return (root ? item[root] : item)[this._property];\r
101 },\r
102\r
103 updateSorterFn: function () {\r
104 // don't callParent here - we don't want to smash sortFn w/sorterFn\r
105 },\r
106\r
107 updateProperty: function () {\r
108 // we don't callParent since that is related to sorterFn smashing sortFn\r
109 if (!this.getGroupFn()) {\r
110 this.setGroupFn(this.standardGroupFn);\r
111 }\r
112 }\r
113});\r