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.

263 lines
9.4 KiB

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