You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.9 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.data.GroupingStore
  17. * @extends Ext.data.Store
  18. * A specialized store implementation that provides for grouping records by one of the available fields. This
  19. * is usually used in conjunction with an {@link Ext.grid.GroupingView} to provide the data model for
  20. * a grouped GridPanel.
  21. *
  22. * Internally, GroupingStore is simply a normal Store with multi sorting enabled from the start. The grouping field
  23. * and direction are always injected as the first sorter pair. GroupingView picks up on the configured groupField and
  24. * builds grid rows appropriately.
  25. *
  26. * @constructor
  27. * Creates a new GroupingStore.
  28. * @param {Object} config A config object containing the objects needed for the Store to access data,
  29. * and read the data into Records.
  30. * @xtype groupingstore
  31. */
  32. Ext.data.GroupingStore = Ext.extend(Ext.data.Store, {
  33. //inherit docs
  34. constructor: function(config) {
  35. config = config || {};
  36. //We do some preprocessing here to massage the grouping + sorting options into a single
  37. //multi sort array. If grouping and sorting options are both presented to the constructor,
  38. //the sorters array consists of the grouping sorter object followed by the sorting sorter object
  39. //see Ext.data.Store's sorting functions for details about how multi sorting works
  40. this.hasMultiSort = true;
  41. this.multiSortInfo = this.multiSortInfo || {sorters: []};
  42. var sorters = this.multiSortInfo.sorters,
  43. groupField = config.groupField || this.groupField,
  44. sortInfo = config.sortInfo || this.sortInfo,
  45. groupDir = config.groupDir || this.groupDir;
  46. //add the grouping sorter object first
  47. if(groupField){
  48. sorters.push({
  49. field : groupField,
  50. direction: groupDir
  51. });
  52. }
  53. //add the sorting sorter object if it is present
  54. if (sortInfo) {
  55. sorters.push(sortInfo);
  56. }
  57. Ext.data.GroupingStore.superclass.constructor.call(this, config);
  58. this.addEvents(
  59. /**
  60. * @event groupchange
  61. * Fired whenever a call to store.groupBy successfully changes the grouping on the store
  62. * @param {Ext.data.GroupingStore} store The grouping store
  63. * @param {String} groupField The field that the store is now grouped by
  64. */
  65. 'groupchange'
  66. );
  67. this.applyGroupField();
  68. },
  69. /**
  70. * @cfg {String} groupField
  71. * The field name by which to sort the store's data (defaults to '').
  72. */
  73. /**
  74. * @cfg {Boolean} remoteGroup
  75. * True if the grouping should apply on the server side, false if it is local only (defaults to false). If the
  76. * grouping is local, it can be applied immediately to the data. If it is remote, then it will simply act as a
  77. * helper, automatically sending the grouping field name as the 'groupBy' param with each XHR call.
  78. */
  79. remoteGroup : false,
  80. /**
  81. * @cfg {Boolean} groupOnSort
  82. * True to sort the data on the grouping field when a grouping operation occurs, false to sort based on the
  83. * existing sort info (defaults to false).
  84. */
  85. groupOnSort:false,
  86. /**
  87. * @cfg {String} groupDir
  88. * The direction to sort the groups. Defaults to <tt>'ASC'</tt>.
  89. */
  90. groupDir : 'ASC',
  91. /**
  92. * Clears any existing grouping and refreshes the data using the default sort.
  93. */
  94. clearGrouping : function(){
  95. this.groupField = false;
  96. if(this.remoteGroup){
  97. if(this.baseParams){
  98. delete this.baseParams.groupBy;
  99. delete this.baseParams.groupDir;
  100. }
  101. var lo = this.lastOptions;
  102. if(lo && lo.params){
  103. delete lo.params.groupBy;
  104. delete lo.params.groupDir;
  105. }
  106. this.reload();
  107. }else{
  108. this.sort();
  109. this.fireEvent('datachanged', this);
  110. }
  111. },
  112. /**
  113. * Groups the data by the specified field.
  114. * @param {String} field The field name by which to sort the store's data
  115. * @param {Boolean} forceRegroup (optional) True to force the group to be refreshed even if the field passed
  116. * in is the same as the current grouping field, false to skip grouping on the same field (defaults to false)
  117. */
  118. groupBy : function(field, forceRegroup, direction) {
  119. direction = direction ? (String(direction).toUpperCase() == 'DESC' ? 'DESC' : 'ASC') : this.groupDir;
  120. if (this.groupField == field && this.groupDir == direction && !forceRegroup) {
  121. return; // already grouped by this field
  122. }
  123. //check the contents of the first sorter. If the field matches the CURRENT groupField (before it is set to the new one),
  124. //remove the sorter as it is actually the grouper. The new grouper is added back in by this.sort
  125. var sorters = this.multiSortInfo.sorters;
  126. if (sorters.length > 0 && sorters[0].field == this.groupField) {
  127. sorters.shift();
  128. }
  129. this.groupField = field;
  130. this.groupDir = direction;
  131. this.applyGroupField();
  132. var fireGroupEvent = function() {
  133. this.fireEvent('groupchange', this, this.getGroupState());
  134. };
  135. if (this.groupOnSort) {
  136. this.sort(field, direction);
  137. fireGroupEvent.call(this);
  138. return;
  139. }
  140. if (this.remoteGroup) {
  141. this.on('load', fireGroupEvent, this, {single: true});
  142. this.reload();
  143. } else {
  144. this.sort(sorters);
  145. fireGroupEvent.call(this);
  146. }
  147. },
  148. //GroupingStore always uses multisorting so we intercept calls to sort here to make sure that our grouping sorter object
  149. //is always injected first.
  150. sort : function(fieldName, dir) {
  151. if (this.remoteSort) {
  152. return Ext.data.GroupingStore.superclass.sort.call(this, fieldName, dir);
  153. }
  154. var sorters = [];
  155. //cater for any existing valid arguments to this.sort, massage them into an array of sorter objects
  156. if (Ext.isArray(arguments[0])) {
  157. sorters = arguments[0];
  158. } else if (fieldName == undefined) {
  159. //we preserve the existing sortInfo here because this.sort is called after
  160. //clearGrouping and there may be existing sorting
  161. sorters = this.sortInfo ? [this.sortInfo] : [];
  162. } else {
  163. //TODO: this is lifted straight from Ext.data.Store's singleSort function. It should instead be
  164. //refactored into a common method if possible
  165. var field = this.fields.get(fieldName);
  166. if (!field) return false;
  167. var name = field.name,
  168. sortInfo = this.sortInfo || null,
  169. sortToggle = this.sortToggle ? this.sortToggle[name] : null;
  170. if (!dir) {
  171. if (sortInfo && sortInfo.field == name) { // toggle sort dir
  172. dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC');
  173. } else {
  174. dir = field.sortDir;
  175. }
  176. }
  177. this.sortToggle[name] = dir;
  178. this.sortInfo = {field: name, direction: dir};
  179. sorters = [this.sortInfo];
  180. }
  181. //add the grouping sorter object as the first multisort sorter
  182. if (this.groupField) {
  183. sorters.unshift({direction: this.groupDir, field: this.groupField});
  184. }
  185. return this.multiSort.call(this, sorters, dir);
  186. },
  187. /**
  188. * @private
  189. * Saves the current grouping field and direction to this.baseParams and this.lastOptions.params
  190. * if we're using remote grouping. Does not actually perform any grouping - just stores values
  191. */
  192. applyGroupField: function(){
  193. if (this.remoteGroup) {
  194. if(!this.baseParams){
  195. this.baseParams = {};
  196. }
  197. Ext.apply(this.baseParams, {
  198. groupBy : this.groupField,
  199. groupDir: this.groupDir
  200. });
  201. var lo = this.lastOptions;
  202. if (lo && lo.params) {
  203. lo.params.groupDir = this.groupDir;
  204. //this is deleted because of a bug reported at http://www.extjs.com/forum/showthread.php?t=82907
  205. delete lo.params.groupBy;
  206. }
  207. }
  208. },
  209. /**
  210. * @private
  211. * TODO: This function is apparently never invoked anywhere in the framework. It has no documentation
  212. * and should be considered for deletion
  213. */
  214. applyGrouping : function(alwaysFireChange){
  215. if(this.groupField !== false){
  216. this.groupBy(this.groupField, true, this.groupDir);
  217. return true;
  218. }else{
  219. if(alwaysFireChange === true){
  220. this.fireEvent('datachanged', this);
  221. }
  222. return false;
  223. }
  224. },
  225. /**
  226. * @private
  227. * Returns the grouping field that should be used. If groupOnSort is used this will be sortInfo's field,
  228. * otherwise it will be this.groupField
  229. * @return {String} The group field
  230. */
  231. getGroupState : function(){
  232. return this.groupOnSort && this.groupField !== false ?
  233. (this.sortInfo ? this.sortInfo.field : undefined) : this.groupField;
  234. }
  235. });
  236. Ext.reg('groupingstore', Ext.data.GroupingStore);