Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
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.

576 lines
21 KiB

  1. /*!
  2. * Ext JS Library 3.2.0
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.grid.GroupingView
  9. * @extends Ext.grid.GridView
  10. * Adds the ability for single level grouping to the grid. A {@link Ext.data.GroupingStore GroupingStore}
  11. * must be used to enable grouping. Some grouping characteristics may also be configured at the
  12. * {@link Ext.grid.Column Column level}<div class="mdetail-params"><ul>
  13. * <li><code>{@link Ext.grid.Column#emptyGroupText emptyGroupText}</code></li>
  14. * <li><code>{@link Ext.grid.Column#groupable groupable}</code></li>
  15. * <li><code>{@link Ext.grid.Column#groupName groupName}</code></li>
  16. * <li><code>{@link Ext.grid.Column#groupRender groupRender}</code></li>
  17. * </ul></div>
  18. * <p>Sample usage:</p>
  19. * <pre><code>
  20. var grid = new Ext.grid.GridPanel({
  21. // A groupingStore is required for a GroupingView
  22. store: new {@link Ext.data.GroupingStore}({
  23. autoDestroy: true,
  24. reader: reader,
  25. data: xg.dummyData,
  26. sortInfo: {field: 'company', direction: 'ASC'},
  27. {@link Ext.data.GroupingStore#groupOnSort groupOnSort}: true,
  28. {@link Ext.data.GroupingStore#remoteGroup remoteGroup}: true,
  29. {@link Ext.data.GroupingStore#groupField groupField}: 'industry'
  30. }),
  31. colModel: new {@link Ext.grid.ColumnModel}({
  32. columns:[
  33. {id:'company',header: 'Company', width: 60, dataIndex: 'company'},
  34. // {@link Ext.grid.Column#groupable groupable}, {@link Ext.grid.Column#groupName groupName}, {@link Ext.grid.Column#groupRender groupRender} are also configurable at column level
  35. {header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price', {@link Ext.grid.Column#groupable groupable}: false},
  36. {header: 'Change', dataIndex: 'change', renderer: Ext.util.Format.usMoney},
  37. {header: 'Industry', dataIndex: 'industry'},
  38. {header: 'Last Updated', renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
  39. ],
  40. defaults: {
  41. sortable: true,
  42. menuDisabled: false,
  43. width: 20
  44. }
  45. }),
  46. view: new Ext.grid.GroupingView({
  47. {@link Ext.grid.GridView#forceFit forceFit}: true,
  48. // custom grouping text template to display the number of items per group
  49. {@link #groupTextTpl}: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
  50. }),
  51. frame:true,
  52. width: 700,
  53. height: 450,
  54. collapsible: true,
  55. animCollapse: false,
  56. title: 'Grouping Example',
  57. iconCls: 'icon-grid',
  58. renderTo: document.body
  59. });
  60. * </code></pre>
  61. * @constructor
  62. * @param {Object} config
  63. */
  64. Ext.grid.GroupingView = Ext.extend(Ext.grid.GridView, {
  65. /**
  66. * @cfg {String} groupByText Text displayed in the grid header menu for grouping by a column
  67. * (defaults to 'Group By This Field').
  68. */
  69. groupByText : 'Group By This Field',
  70. /**
  71. * @cfg {String} showGroupsText Text displayed in the grid header for enabling/disabling grouping
  72. * (defaults to 'Show in Groups').
  73. */
  74. showGroupsText : 'Show in Groups',
  75. /**
  76. * @cfg {Boolean} hideGroupedColumn <tt>true</tt> to hide the column that is currently grouped (defaults to <tt>false</tt>)
  77. */
  78. hideGroupedColumn : false,
  79. /**
  80. * @cfg {Boolean} showGroupName If <tt>true</tt> will display a prefix plus a ': ' before the group field value
  81. * in the group header line. The prefix will consist of the <tt><b>{@link Ext.grid.Column#groupName groupName}</b></tt>
  82. * (or the configured <tt><b>{@link Ext.grid.Column#header header}</b></tt> if not provided) configured in the
  83. * {@link Ext.grid.Column} for each set of grouped rows (defaults to <tt>true</tt>).
  84. */
  85. showGroupName : true,
  86. /**
  87. * @cfg {Boolean} startCollapsed <tt>true</tt> to start all groups collapsed (defaults to <tt>false</tt>)
  88. */
  89. startCollapsed : false,
  90. /**
  91. * @cfg {Boolean} enableGrouping <tt>false</tt> to disable grouping functionality (defaults to <tt>true</tt>)
  92. */
  93. enableGrouping : true,
  94. /**
  95. * @cfg {Boolean} enableGroupingMenu <tt>true</tt> to enable the grouping control in the column menu (defaults to <tt>true</tt>)
  96. */
  97. enableGroupingMenu : true,
  98. /**
  99. * @cfg {Boolean} enableNoGroups <tt>true</tt> to allow the user to turn off grouping (defaults to <tt>true</tt>)
  100. */
  101. enableNoGroups : true,
  102. /**
  103. * @cfg {String} emptyGroupText The text to display when there is an empty group value (defaults to <tt>'(None)'</tt>).
  104. * May also be specified per column, see {@link Ext.grid.Column}.{@link Ext.grid.Column#emptyGroupText emptyGroupText}.
  105. */
  106. emptyGroupText : '(None)',
  107. /**
  108. * @cfg {Boolean} ignoreAdd <tt>true</tt> to skip refreshing the view when new rows are added (defaults to <tt>false</tt>)
  109. */
  110. ignoreAdd : false,
  111. /**
  112. * @cfg {String} groupTextTpl The template used to render the group header (defaults to <tt>'{text}'</tt>).
  113. * This is used to format an object which contains the following properties:
  114. * <div class="mdetail-params"><ul>
  115. * <li><b>group</b> : String<p class="sub-desc">The <i>rendered</i> value of the group field.
  116. * By default this is the unchanged value of the group field. If a <tt><b>{@link Ext.grid.Column#groupRenderer groupRenderer}</b></tt>
  117. * is specified, it is the result of a call to that function.</p></li>
  118. * <li><b>gvalue</b> : Object<p class="sub-desc">The <i>raw</i> value of the group field.</p></li>
  119. * <li><b>text</b> : String<p class="sub-desc">The configured header (as described in <tt>{@link #showGroupName})</tt>
  120. * if <tt>{@link #showGroupName}</tt> is <tt>true</tt>) plus the <i>rendered</i> group field value.</p></li>
  121. * <li><b>groupId</b> : String<p class="sub-desc">A unique, generated ID which is applied to the
  122. * View Element which contains the group.</p></li>
  123. * <li><b>startRow</b> : Number<p class="sub-desc">The row index of the Record which caused group change.</p></li>
  124. * <li><b>rs</b> : Array<p class="sub-desc">Contains a single element: The Record providing the data
  125. * for the row which caused group change.</p></li>
  126. * <li><b>cls</b> : String<p class="sub-desc">The generated class name string to apply to the group header Element.</p></li>
  127. * <li><b>style</b> : String<p class="sub-desc">The inline style rules to apply to the group header Element.</p></li>
  128. * </ul></div></p>
  129. * See {@link Ext.XTemplate} for information on how to format data using a template. Possible usage:<pre><code>
  130. var grid = new Ext.grid.GridPanel({
  131. ...
  132. view: new Ext.grid.GroupingView({
  133. groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
  134. }),
  135. });
  136. * </code></pre>
  137. */
  138. groupTextTpl : '{text}',
  139. /**
  140. * @cfg {String} groupMode Indicates how to construct the group identifier. <tt>'value'</tt> constructs the id using
  141. * raw value, <tt>'display'</tt> constructs the id using the rendered value. Defaults to <tt>'value'</tt>.
  142. */
  143. groupMode: 'value',
  144. /**
  145. * @cfg {Function} groupRenderer This property must be configured in the {@link Ext.grid.Column} for
  146. * each column.
  147. */
  148. // private
  149. initTemplates : function(){
  150. Ext.grid.GroupingView.superclass.initTemplates.call(this);
  151. this.state = {};
  152. var sm = this.grid.getSelectionModel();
  153. sm.on(sm.selectRow ? 'beforerowselect' : 'beforecellselect',
  154. this.onBeforeRowSelect, this);
  155. if(!this.startGroup){
  156. this.startGroup = new Ext.XTemplate(
  157. '<div id="{groupId}" class="x-grid-group {cls}">',
  158. '<div id="{groupId}-hd" class="x-grid-group-hd" style="{style}"><div class="x-grid-group-title">', this.groupTextTpl ,'</div></div>',
  159. '<div id="{groupId}-bd" class="x-grid-group-body">'
  160. );
  161. }
  162. this.startGroup.compile();
  163. if (!this.endGroup) {
  164. this.endGroup = '</div></div>';
  165. }
  166. },
  167. // private
  168. findGroup : function(el){
  169. return Ext.fly(el).up('.x-grid-group', this.mainBody.dom);
  170. },
  171. // private
  172. getGroups : function(){
  173. return this.hasRows() ? this.mainBody.dom.childNodes : [];
  174. },
  175. // private
  176. onAdd : function(ds, records, index) {
  177. if (this.canGroup() && !this.ignoreAdd) {
  178. var ss = this.getScrollState();
  179. this.fireEvent('beforerowsinserted', ds, index, index + (records.length-1));
  180. this.refresh();
  181. this.restoreScroll(ss);
  182. this.fireEvent('rowsinserted', ds, index, index + (records.length-1));
  183. } else if (!this.canGroup()) {
  184. Ext.grid.GroupingView.superclass.onAdd.apply(this, arguments);
  185. }
  186. },
  187. // private
  188. onRemove : function(ds, record, index, isUpdate){
  189. Ext.grid.GroupingView.superclass.onRemove.apply(this, arguments);
  190. var g = document.getElementById(record._groupId);
  191. if(g && g.childNodes[1].childNodes.length < 1){
  192. Ext.removeNode(g);
  193. }
  194. this.applyEmptyText();
  195. },
  196. // private
  197. refreshRow : function(record){
  198. if(this.ds.getCount()==1){
  199. this.refresh();
  200. }else{
  201. this.isUpdating = true;
  202. Ext.grid.GroupingView.superclass.refreshRow.apply(this, arguments);
  203. this.isUpdating = false;
  204. }
  205. },
  206. // private
  207. beforeMenuShow : function(){
  208. var item, items = this.hmenu.items, disabled = this.cm.config[this.hdCtxIndex].groupable === false;
  209. if((item = items.get('groupBy'))){
  210. item.setDisabled(disabled);
  211. }
  212. if((item = items.get('showGroups'))){
  213. item.setDisabled(disabled);
  214. item.setChecked(this.enableGrouping, true);
  215. }
  216. },
  217. // private
  218. renderUI : function(){
  219. Ext.grid.GroupingView.superclass.renderUI.call(this);
  220. this.mainBody.on('mousedown', this.interceptMouse, this);
  221. if(this.enableGroupingMenu && this.hmenu){
  222. this.hmenu.add('-',{
  223. itemId:'groupBy',
  224. text: this.groupByText,
  225. handler: this.onGroupByClick,
  226. scope: this,
  227. iconCls:'x-group-by-icon'
  228. });
  229. if(this.enableNoGroups){
  230. this.hmenu.add({
  231. itemId:'showGroups',
  232. text: this.showGroupsText,
  233. checked: true,
  234. checkHandler: this.onShowGroupsClick,
  235. scope: this
  236. });
  237. }
  238. this.hmenu.on('beforeshow', this.beforeMenuShow, this);
  239. }
  240. },
  241. processEvent: function(name, e){
  242. Ext.grid.GroupingView.superclass.processEvent.call(this, name, e);
  243. var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
  244. if(hd){
  245. // group value is at the end of the string
  246. var field = this.getGroupField(),
  247. prefix = this.getPrefix(field),
  248. groupValue = hd.id.substring(prefix.length);
  249. // remove trailing '-hd'
  250. groupValue = groupValue.substr(0, groupValue.length - 3);
  251. if(groupValue){
  252. this.grid.fireEvent('group' + name, this.grid, field, groupValue, e);
  253. }
  254. }
  255. },
  256. // private
  257. onGroupByClick : function(){
  258. this.enableGrouping = true;
  259. this.grid.store.groupBy(this.cm.getDataIndex(this.hdCtxIndex));
  260. this.grid.fireEvent('groupchange', this, this.grid.store.getGroupState());
  261. this.beforeMenuShow(); // Make sure the checkboxes get properly set when changing groups
  262. this.refresh();
  263. },
  264. // private
  265. onShowGroupsClick : function(mi, checked){
  266. this.enableGrouping = checked;
  267. if(checked){
  268. this.onGroupByClick();
  269. }else{
  270. this.grid.store.clearGrouping();
  271. this.grid.fireEvent('groupchange', this, null);
  272. }
  273. },
  274. /**
  275. * Toggle the group that contains the specific row.
  276. * @param {Number} rowIndex The row inside the group
  277. * @param {Boolean} expanded (optional)
  278. */
  279. toggleRowIndex : function(rowIndex, expanded){
  280. if(!this.canGroup()){
  281. return;
  282. }
  283. var row = this.getRow(rowIndex);
  284. if(row){
  285. this.toggleGroup(this.findGroup(row), expanded);
  286. }
  287. },
  288. /**
  289. * Toggles the specified group if no value is passed, otherwise sets the expanded state of the group to the value passed.
  290. * @param {String} groupId The groupId assigned to the group (see getGroupId)
  291. * @param {Boolean} expanded (optional)
  292. */
  293. toggleGroup : function(group, expanded){
  294. var gel = Ext.get(group);
  295. expanded = Ext.isDefined(expanded) ? expanded : gel.hasClass('x-grid-group-collapsed');
  296. if(this.state[gel.id] !== expanded){
  297. this.grid.stopEditing(true);
  298. this.state[gel.id] = expanded;
  299. gel[expanded ? 'removeClass' : 'addClass']('x-grid-group-collapsed');
  300. }
  301. },
  302. /**
  303. * Toggles all groups if no value is passed, otherwise sets the expanded state of all groups to the value passed.
  304. * @param {Boolean} expanded (optional)
  305. */
  306. toggleAllGroups : function(expanded){
  307. var groups = this.getGroups();
  308. for(var i = 0, len = groups.length; i < len; i++){
  309. this.toggleGroup(groups[i], expanded);
  310. }
  311. },
  312. /**
  313. * Expands all grouped rows.
  314. */
  315. expandAllGroups : function(){
  316. this.toggleAllGroups(true);
  317. },
  318. /**
  319. * Collapses all grouped rows.
  320. */
  321. collapseAllGroups : function(){
  322. this.toggleAllGroups(false);
  323. },
  324. // private
  325. interceptMouse : function(e){
  326. var hd = e.getTarget('.x-grid-group-hd', this.mainBody);
  327. if(hd){
  328. e.stopEvent();
  329. this.toggleGroup(hd.parentNode);
  330. }
  331. },
  332. // private
  333. getGroup : function(v, r, groupRenderer, rowIndex, colIndex, ds){
  334. var g = groupRenderer ? groupRenderer(v, {}, r, rowIndex, colIndex, ds) : String(v);
  335. if(g === '' || g === '&#160;'){
  336. g = this.cm.config[colIndex].emptyGroupText || this.emptyGroupText;
  337. }
  338. return g;
  339. },
  340. // private
  341. getGroupField : function(){
  342. return this.grid.store.getGroupState();
  343. },
  344. // private
  345. afterRender : function(){
  346. if(!this.ds || !this.cm){
  347. return;
  348. }
  349. Ext.grid.GroupingView.superclass.afterRender.call(this);
  350. if(this.grid.deferRowRender){
  351. this.updateGroupWidths();
  352. }
  353. },
  354. // private
  355. renderRows : function(){
  356. var groupField = this.getGroupField();
  357. var eg = !!groupField;
  358. // if they turned off grouping and the last grouped field is hidden
  359. if(this.hideGroupedColumn) {
  360. var colIndex = this.cm.findColumnIndex(groupField),
  361. hasLastGroupField = Ext.isDefined(this.lastGroupField);
  362. if(!eg && hasLastGroupField){
  363. this.mainBody.update('');
  364. this.cm.setHidden(this.cm.findColumnIndex(this.lastGroupField), false);
  365. delete this.lastGroupField;
  366. }else if (eg && !hasLastGroupField){
  367. this.lastGroupField = groupField;
  368. this.cm.setHidden(colIndex, true);
  369. }else if (eg && hasLastGroupField && groupField !== this.lastGroupField) {
  370. this.mainBody.update('');
  371. var oldIndex = this.cm.findColumnIndex(this.lastGroupField);
  372. this.cm.setHidden(oldIndex, false);
  373. this.lastGroupField = groupField;
  374. this.cm.setHidden(colIndex, true);
  375. }
  376. }
  377. return Ext.grid.GroupingView.superclass.renderRows.apply(
  378. this, arguments);
  379. },
  380. // private
  381. doRender : function(cs, rs, ds, startRow, colCount, stripe){
  382. if(rs.length < 1){
  383. return '';
  384. }
  385. if(!this.canGroup() || this.isUpdating){
  386. return Ext.grid.GroupingView.superclass.doRender.apply(this, arguments);
  387. }
  388. var groupField = this.getGroupField(),
  389. colIndex = this.cm.findColumnIndex(groupField),
  390. g,
  391. gstyle = 'width:' + this.getTotalWidth() + ';',
  392. cfg = this.cm.config[colIndex],
  393. groupRenderer = cfg.groupRenderer || cfg.renderer,
  394. prefix = this.showGroupName ? (cfg.groupName || cfg.header)+': ' : '',
  395. groups = [],
  396. curGroup, i, len, gid;
  397. for(i = 0, len = rs.length; i < len; i++){
  398. var rowIndex = startRow + i,
  399. r = rs[i],
  400. gvalue = r.data[groupField];
  401. g = this.getGroup(gvalue, r, groupRenderer, rowIndex, colIndex, ds);
  402. if(!curGroup || curGroup.group != g){
  403. gid = this.constructId(gvalue, groupField, colIndex);
  404. // if state is defined use it, however state is in terms of expanded
  405. // so negate it, otherwise use the default.
  406. this.state[gid] = !(Ext.isDefined(this.state[gid]) ? !this.state[gid] : this.startCollapsed);
  407. curGroup = {
  408. group: g,
  409. gvalue: gvalue,
  410. text: prefix + g,
  411. groupId: gid,
  412. startRow: rowIndex,
  413. rs: [r],
  414. cls: this.state[gid] ? '' : 'x-grid-group-collapsed',
  415. style: gstyle
  416. };
  417. groups.push(curGroup);
  418. }else{
  419. curGroup.rs.push(r);
  420. }
  421. r._groupId = gid;
  422. }
  423. var buf = [];
  424. for(i = 0, len = groups.length; i < len; i++){
  425. g = groups[i];
  426. this.doGroupStart(buf, g, cs, ds, colCount);
  427. buf[buf.length] = Ext.grid.GroupingView.superclass.doRender.call(
  428. this, cs, g.rs, ds, g.startRow, colCount, stripe);
  429. this.doGroupEnd(buf, g, cs, ds, colCount);
  430. }
  431. return buf.join('');
  432. },
  433. /**
  434. * Dynamically tries to determine the groupId of a specific value
  435. * @param {String} value
  436. * @return {String} The group id
  437. */
  438. getGroupId : function(value){
  439. var field = this.getGroupField();
  440. return this.constructId(value, field, this.cm.findColumnIndex(field));
  441. },
  442. // private
  443. constructId : function(value, field, idx){
  444. var cfg = this.cm.config[idx],
  445. groupRenderer = cfg.groupRenderer || cfg.renderer,
  446. val = (this.groupMode == 'value') ? value : this.getGroup(value, {data:{}}, groupRenderer, 0, idx, this.ds);
  447. return this.getPrefix(field) + Ext.util.Format.htmlEncode(val);
  448. },
  449. // private
  450. canGroup : function(){
  451. return this.enableGrouping && !!this.getGroupField();
  452. },
  453. // private
  454. getPrefix: function(field){
  455. return this.grid.getGridEl().id + '-gp-' + field + '-';
  456. },
  457. // private
  458. doGroupStart : function(buf, g, cs, ds, colCount){
  459. buf[buf.length] = this.startGroup.apply(g);
  460. },
  461. // private
  462. doGroupEnd : function(buf, g, cs, ds, colCount){
  463. buf[buf.length] = this.endGroup;
  464. },
  465. // private
  466. getRows : function(){
  467. if(!this.canGroup()){
  468. return Ext.grid.GroupingView.superclass.getRows.call(this);
  469. }
  470. var r = [],
  471. gs = this.getGroups(),
  472. g,
  473. i = 0,
  474. len = gs.length,
  475. j,
  476. jlen;
  477. for(; i < len; ++i){
  478. g = gs[i].childNodes[1];
  479. if(g){
  480. g = g.childNodes;
  481. for(j = 0, jlen = g.length; j < jlen; ++j){
  482. r[r.length] = g[j];
  483. }
  484. }
  485. }
  486. return r;
  487. },
  488. // private
  489. updateGroupWidths : function(){
  490. if(!this.canGroup() || !this.hasRows()){
  491. return;
  492. }
  493. var tw = Math.max(this.cm.getTotalWidth(), this.el.dom.offsetWidth-this.getScrollOffset()) +'px';
  494. var gs = this.getGroups();
  495. for(var i = 0, len = gs.length; i < len; i++){
  496. gs[i].firstChild.style.width = tw;
  497. }
  498. },
  499. // private
  500. onColumnWidthUpdated : function(col, w, tw){
  501. Ext.grid.GroupingView.superclass.onColumnWidthUpdated.call(this, col, w, tw);
  502. this.updateGroupWidths();
  503. },
  504. // private
  505. onAllColumnWidthsUpdated : function(ws, tw){
  506. Ext.grid.GroupingView.superclass.onAllColumnWidthsUpdated.call(this, ws, tw);
  507. this.updateGroupWidths();
  508. },
  509. // private
  510. onColumnHiddenUpdated : function(col, hidden, tw){
  511. Ext.grid.GroupingView.superclass.onColumnHiddenUpdated.call(this, col, hidden, tw);
  512. this.updateGroupWidths();
  513. },
  514. // private
  515. onLayout : function(){
  516. this.updateGroupWidths();
  517. },
  518. // private
  519. onBeforeRowSelect : function(sm, rowIndex){
  520. this.toggleRowIndex(rowIndex, true);
  521. }
  522. });
  523. // private
  524. Ext.grid.GroupingView.GROUP_ID = 1000;