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.

334 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-grid-PivotAxis'>/**
  19. </span> * @class Ext.grid.PivotAxis
  20. * @extends Ext.Component
  21. * &lt;p&gt;PivotAxis is a class that supports a {@link Ext.grid.PivotGrid}. Each PivotGrid contains two PivotAxis instances - the left
  22. * axis and the top axis. Each PivotAxis defines an ordered set of dimensions, each of which should correspond to a field in a
  23. * Store's Record (see {@link Ext.grid.PivotGrid} documentation for further explanation).&lt;/p&gt;
  24. * &lt;p&gt;Developers should have little interaction with the PivotAxis instances directly as most of their management is performed by
  25. * the PivotGrid. An exception is the dynamic reconfiguration of axes at run time - to achieve this we use PivotAxis's
  26. * {@link #setDimensions} function and refresh the grid:&lt;/p&gt;
  27. &lt;pre&gt;&lt;code&gt;
  28. var pivotGrid = new Ext.grid.PivotGrid({
  29. //some PivotGrid config here
  30. });
  31. //change the left axis dimensions
  32. pivotGrid.leftAxis.setDimensions([
  33. {
  34. dataIndex: 'person',
  35. direction: 'DESC',
  36. width : 100
  37. },
  38. {
  39. dataIndex: 'product',
  40. direction: 'ASC',
  41. width : 80
  42. }
  43. ]);
  44. pivotGrid.view.refresh(true);
  45. &lt;/code&gt;&lt;/pre&gt;
  46. * This clears the previous dimensions on the axis and redraws the grid with the new dimensions.
  47. */
  48. Ext.grid.PivotAxis = Ext.extend(Ext.Component, {
  49. <span id='Ext-grid-PivotAxis-cfg-orientation'> /**
  50. </span> * @cfg {String} orientation One of 'vertical' or 'horizontal'. Defaults to horizontal
  51. */
  52. orientation: 'horizontal',
  53. <span id='Ext-grid-PivotAxis-cfg-defaultHeaderWidth'> /**
  54. </span> * @cfg {Number} defaultHeaderWidth The width to render each row header that does not have a width specified via
  55. {@link #getRowGroupHeaders}. Defaults to 80.
  56. */
  57. defaultHeaderWidth: 80,
  58. <span id='Ext-grid-PivotAxis-cfg-paddingWidth'> /**
  59. </span> * @private
  60. * @cfg {Number} paddingWidth The amount of padding used by each cell.
  61. * TODO: From 4.x onwards this can be removed as it won't be needed. For now it is used to account for the differences between
  62. * the content box and border box measurement models
  63. */
  64. paddingWidth: 7,
  65. <span id='Ext-grid-PivotAxis-method-setDimensions'> /**
  66. </span> * Updates the dimensions used by this axis
  67. * @param {Array} dimensions The new dimensions
  68. */
  69. setDimensions: function(dimensions) {
  70. this.dimensions = dimensions;
  71. },
  72. <span id='Ext-grid-PivotAxis-method-onRender'> /**
  73. </span> * @private
  74. * Builds the html table that contains the dimensions for this axis. This branches internally between vertical
  75. * and horizontal orientations because the table structure is slightly different in each case
  76. */
  77. onRender: function(ct, position) {
  78. var rows = this.orientation == 'horizontal'
  79. ? this.renderHorizontalRows()
  80. : this.renderVerticalRows();
  81. this.el = Ext.DomHelper.overwrite(ct.dom, {tag: 'table', cn: rows}, true);
  82. },
  83. <span id='Ext-grid-PivotAxis-method-renderHorizontalRows'> /**
  84. </span> * @private
  85. * Specialised renderer for horizontal oriented axes
  86. * @return {Object} The HTML Domspec for a horizontal oriented axis
  87. */
  88. renderHorizontalRows: function() {
  89. var headers = this.buildHeaders(),
  90. rowCount = headers.length,
  91. rows = [],
  92. cells, cols, colCount, i, j;
  93. for (i = 0; i &lt; rowCount; i++) {
  94. cells = [];
  95. cols = headers[i].items;
  96. colCount = cols.length;
  97. for (j = 0; j &lt; colCount; j++) {
  98. cells.push({
  99. tag: 'td',
  100. html: cols[j].header,
  101. colspan: cols[j].span
  102. });
  103. }
  104. rows[i] = {
  105. tag: 'tr',
  106. cn: cells
  107. };
  108. }
  109. return rows;
  110. },
  111. <span id='Ext-grid-PivotAxis-method-renderVerticalRows'> /**
  112. </span> * @private
  113. * Specialised renderer for vertical oriented axes
  114. * @return {Object} The HTML Domspec for a vertical oriented axis
  115. */
  116. renderVerticalRows: function() {
  117. var headers = this.buildHeaders(),
  118. colCount = headers.length,
  119. rowCells = [],
  120. rows = [],
  121. rowCount, col, row, colWidth, i, j;
  122. for (i = 0; i &lt; colCount; i++) {
  123. col = headers[i];
  124. colWidth = col.width || 80;
  125. rowCount = col.items.length;
  126. for (j = 0; j &lt; rowCount; j++) {
  127. row = col.items[j];
  128. rowCells[row.start] = rowCells[row.start] || [];
  129. rowCells[row.start].push({
  130. tag : 'td',
  131. html : row.header,
  132. rowspan: row.span,
  133. width : Ext.isBorderBox ? colWidth : colWidth - this.paddingWidth
  134. });
  135. }
  136. }
  137. rowCount = rowCells.length;
  138. for (i = 0; i &lt; rowCount; i++) {
  139. rows[i] = {
  140. tag: 'tr',
  141. cn : rowCells[i]
  142. };
  143. }
  144. return rows;
  145. },
  146. <span id='Ext-grid-PivotAxis-method-getTuples'> /**
  147. </span> * @private
  148. * Returns the set of all unique tuples based on the bound store and dimension definitions.
  149. * Internally we construct a new, temporary store to make use of the multi-sort capabilities of Store. In
  150. * 4.x this functionality should have been moved to MixedCollection so this step should not be needed.
  151. * @return {Array} All unique tuples
  152. */
  153. getTuples: function() {
  154. var newStore = new Ext.data.Store({});
  155. newStore.data = this.store.data.clone();
  156. newStore.fields = this.store.fields;
  157. var sorters = [],
  158. dimensions = this.dimensions,
  159. length = dimensions.length,
  160. i;
  161. for (i = 0; i &lt; length; i++) {
  162. sorters.push({
  163. field : dimensions[i].dataIndex,
  164. direction: dimensions[i].direction || 'ASC'
  165. });
  166. }
  167. newStore.sort(sorters);
  168. var records = newStore.data.items,
  169. hashes = [],
  170. tuples = [],
  171. recData, hash, info, data, key;
  172. length = records.length;
  173. for (i = 0; i &lt; length; i++) {
  174. info = this.getRecordInfo(records[i]);
  175. data = info.data;
  176. hash = &quot;&quot;;
  177. for (key in data) {
  178. hash += data[key] + '---';
  179. }
  180. if (hashes.indexOf(hash) == -1) {
  181. hashes.push(hash);
  182. tuples.push(info);
  183. }
  184. }
  185. newStore.destroy();
  186. return tuples;
  187. },
  188. <span id='Ext-grid-PivotAxis-method-getRecordInfo'> /**
  189. </span> * @private
  190. */
  191. getRecordInfo: function(record) {
  192. var dimensions = this.dimensions,
  193. length = dimensions.length,
  194. data = {},
  195. dimension, dataIndex, i;
  196. //get an object containing just the data we are interested in based on the configured dimensions
  197. for (i = 0; i &lt; length; i++) {
  198. dimension = dimensions[i];
  199. dataIndex = dimension.dataIndex;
  200. data[dataIndex] = record.get(dataIndex);
  201. }
  202. //creates a specialised matcher function for a given tuple. The returned function will return
  203. //true if the record passed to it matches the dataIndex values of each dimension in this axis
  204. var createMatcherFunction = function(data) {
  205. return function(record) {
  206. for (var dataIndex in data) {
  207. if (record.get(dataIndex) != data[dataIndex]) {
  208. return false;
  209. }
  210. }
  211. return true;
  212. };
  213. };
  214. return {
  215. data: data,
  216. matcher: createMatcherFunction(data)
  217. };
  218. },
  219. <span id='Ext-grid-PivotAxis-method-buildHeaders'> /**
  220. </span> * @private
  221. * Uses the calculated set of tuples to build an array of headers that can be rendered into a table using rowspan or
  222. * colspan. Basically this takes the set of tuples and spans any cells that run into one another, so if we had dimensions
  223. * of Person and Product and several tuples containing different Products for the same Person, those Products would be
  224. * spanned.
  225. * @return {Array} The headers
  226. */
  227. buildHeaders: function() {
  228. var tuples = this.getTuples(),
  229. rowCount = tuples.length,
  230. dimensions = this.dimensions,
  231. dimension,
  232. colCount = dimensions.length,
  233. headers = [],
  234. tuple, rows, currentHeader, previousHeader, span, start, isLast, changed, i, j;
  235. for (i = 0; i &lt; colCount; i++) {
  236. dimension = dimensions[i];
  237. rows = [];
  238. span = 0;
  239. start = 0;
  240. for (j = 0; j &lt; rowCount; j++) {
  241. tuple = tuples[j];
  242. isLast = j == (rowCount - 1);
  243. currentHeader = tuple.data[dimension.dataIndex];
  244. /*
  245. * 'changed' indicates that we need to create a new cell. This should be true whenever the cell
  246. * above (previousHeader) is different from this cell, or when the cell on the previous dimension
  247. * changed (e.g. if the current dimension is Product and the previous was Person, we need to start
  248. * a new cell if Product is the same but Person changed, so we check the previous dimension and tuple)
  249. */
  250. changed = previousHeader != undefined &amp;&amp; previousHeader != currentHeader;
  251. if (i &gt; 0 &amp;&amp; j &gt; 0) {
  252. changed = changed || tuple.data[dimensions[i-1].dataIndex] != tuples[j-1].data[dimensions[i-1].dataIndex];
  253. }
  254. if (changed) {
  255. rows.push({
  256. header: previousHeader,
  257. span : span,
  258. start : start
  259. });
  260. start += span;
  261. span = 0;
  262. }
  263. if (isLast) {
  264. rows.push({
  265. header: currentHeader,
  266. span : span + 1,
  267. start : start
  268. });
  269. start += span;
  270. span = 0;
  271. }
  272. previousHeader = currentHeader;
  273. span++;
  274. }
  275. headers.push({
  276. items: rows,
  277. width: dimension.width || this.defaultHeaderWidth
  278. });
  279. previousHeader = undefined;
  280. }
  281. return headers;
  282. }
  283. });
  284. </pre>
  285. </body>
  286. </html>