/ext-4.0.7/src/grid/feature/AbstractSummary.js

https://bitbucket.org/srogerf/javascript · JavaScript · 143 lines · 68 code · 18 blank · 57 comment · 6 complexity · 3221552e5a55144326699402fc784f38 MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 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 published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.grid.feature.AbstractSummary
  11. * @extends Ext.grid.feature.Feature
  12. * A small abstract class that contains the shared behaviour for any summary
  13. * calculations to be used in the grid.
  14. */
  15. Ext.define('Ext.grid.feature.AbstractSummary', {
  16. /* Begin Definitions */
  17. extend: 'Ext.grid.feature.Feature',
  18. alias: 'feature.abstractsummary',
  19. /* End Definitions */
  20. /**
  21. * @cfg {Boolean} showSummaryRow True to show the summary row. Defaults to <tt>true</tt>.
  22. */
  23. showSummaryRow: true,
  24. // @private
  25. nestedIdRe: /\{\{id\}([\w\-]*)\}/g,
  26. /**
  27. * Toggle whether or not to show the summary row.
  28. * @param {Boolean} visible True to show the summary row
  29. */
  30. toggleSummaryRow: function(visible){
  31. this.showSummaryRow = !!visible;
  32. },
  33. /**
  34. * Gets any fragments to be used in the tpl
  35. * @private
  36. * @return {Object} The fragments
  37. */
  38. getSummaryFragments: function(){
  39. var fragments = {};
  40. if (this.showSummaryRow) {
  41. Ext.apply(fragments, {
  42. printSummaryRow: Ext.bind(this.printSummaryRow, this)
  43. });
  44. }
  45. return fragments;
  46. },
  47. /**
  48. * Prints a summary row
  49. * @private
  50. * @param {Object} index The index in the template
  51. * @return {String} The value of the summary row
  52. */
  53. printSummaryRow: function(index){
  54. var inner = this.view.getTableChunker().metaRowTpl.join(''),
  55. prefix = Ext.baseCSSPrefix;
  56. inner = inner.replace(prefix + 'grid-row', prefix + 'grid-row-summary');
  57. inner = inner.replace('{{id}}', '{gridSummaryValue}');
  58. inner = inner.replace(this.nestedIdRe, '{id$1}');
  59. inner = inner.replace('{[this.embedRowCls()]}', '{rowCls}');
  60. inner = inner.replace('{[this.embedRowAttr()]}', '{rowAttr}');
  61. inner = Ext.create('Ext.XTemplate', inner, {
  62. firstOrLastCls: Ext.view.TableChunker.firstOrLastCls
  63. });
  64. return inner.applyTemplate({
  65. columns: this.getPrintData(index)
  66. });
  67. },
  68. /**
  69. * Gets the value for the column from the attached data.
  70. * @param {Ext.grid.column.Column} column The header
  71. * @param {Object} data The current data
  72. * @return {String} The value to be rendered
  73. */
  74. getColumnValue: function(column, summaryData){
  75. var comp = Ext.getCmp(column.id),
  76. value = summaryData[column.id],
  77. renderer = comp.summaryRenderer;
  78. if (renderer) {
  79. value = renderer.call(
  80. comp.scope || this,
  81. value,
  82. summaryData,
  83. column.dataIndex
  84. );
  85. }
  86. return value;
  87. },
  88. /**
  89. * Get the summary data for a field.
  90. * @private
  91. * @param {Ext.data.Store} store The store to get the data from
  92. * @param {String/Function} type The type of aggregation. If a function is specified it will
  93. * be passed to the stores aggregate function.
  94. * @param {String} field The field to aggregate on
  95. * @param {Boolean} group True to aggregate in grouped mode
  96. * @return {Number/String/Object} See the return type for the store functions.
  97. */
  98. getSummary: function(store, type, field, group){
  99. if (type) {
  100. if (Ext.isFunction(type)) {
  101. return store.aggregate(type, null, group);
  102. }
  103. switch (type) {
  104. case 'count':
  105. return store.count(group);
  106. case 'min':
  107. return store.min(field, group);
  108. case 'max':
  109. return store.max(field, group);
  110. case 'sum':
  111. return store.sum(field, group);
  112. case 'average':
  113. return store.average(field, group);
  114. default:
  115. return group ? {} : '';
  116. }
  117. }
  118. }
  119. });