[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.grid.PivotGridView |
---|
| 9 | * @extends Ext.grid.GridView |
---|
| 10 | * Specialised GridView for rendering Pivot Grid components. Config can be passed to the PivotGridView via the PivotGrid constructor's |
---|
| 11 | * viewConfig option: |
---|
| 12 | <pre><code> |
---|
| 13 | new Ext.grid.PivotGrid({ |
---|
| 14 | viewConfig: { |
---|
| 15 | title: 'My Pivot Grid', |
---|
| 16 | getCellCls: function(value) { |
---|
| 17 | return value > 10 'red' : 'green'; |
---|
| 18 | } |
---|
| 19 | } |
---|
| 20 | }); |
---|
| 21 | </code></pre> |
---|
| 22 | * <p>Currently {@link #title} and {@link #getCellCls} are the only configuration options accepted by PivotGridView. All other |
---|
| 23 | * interaction is performed via the {@link Ext.grid.PivotGrid PivotGrid} class.</p> |
---|
| 24 | */ |
---|
| 25 | Ext.grid.PivotGridView = Ext.extend(Ext.grid.GridView, { |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * The CSS class added to all group header cells. Defaults to 'grid-hd-group-cell' |
---|
| 29 | * @property colHeaderCellCls |
---|
| 30 | * @type String |
---|
| 31 | */ |
---|
| 32 | colHeaderCellCls: 'grid-hd-group-cell', |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * @cfg {String} title Optional title to be placed in the top left corner of the PivotGrid. Defaults to an empty string. |
---|
| 36 | */ |
---|
| 37 | title: '', |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * @cfg {Function} getCellCls Optional function which should return a CSS class name for each cell value. This is useful when |
---|
| 41 | * color coding cells based on their value. Defaults to undefined. |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Returns the headers to be rendered at the top of the grid. Should be a 2-dimensional array, where each item specifies the number |
---|
| 46 | * of columns it groups (column in this case refers to normal grid columns). In the example below we have 5 city groups, which are |
---|
| 47 | * each part of a continent supergroup. The colspan for each city group refers to the number of normal grid columns that group spans, |
---|
| 48 | * so in this case the grid would be expected to have a total of 12 columns: |
---|
| 49 | <pre><code> |
---|
| 50 | [ |
---|
| 51 | { |
---|
| 52 | items: [ |
---|
| 53 | {header: 'England', colspan: 5}, |
---|
| 54 | {header: 'USA', colspan: 3} |
---|
| 55 | ] |
---|
| 56 | }, |
---|
| 57 | { |
---|
| 58 | items: [ |
---|
| 59 | {header: 'London', colspan: 2}, |
---|
| 60 | {header: 'Cambridge', colspan: 3}, |
---|
| 61 | {header: 'Palo Alto', colspan: 3} |
---|
| 62 | ] |
---|
| 63 | } |
---|
| 64 | ] |
---|
| 65 | </code></pre> |
---|
| 66 | * In the example above we have cities nested under countries. The nesting could be deeper if desired - e.g. Continent -> Country -> |
---|
| 67 | * State -> City, or any other structure. The only constaint is that the same depth must be used throughout the structure. |
---|
| 68 | * @return {Array} A tree structure containing the headers to be rendered. Must include the colspan property at each level, which should |
---|
| 69 | * be the sum of all child nodes beneath this node. |
---|
| 70 | */ |
---|
| 71 | getColumnHeaders: function() { |
---|
| 72 | return this.grid.topAxis.buildHeaders();; |
---|
| 73 | }, |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Returns the headers to be rendered on the left of the grid. Should be a 2-dimensional array, where each item specifies the number |
---|
| 77 | * of rows it groups. In the example below we have 5 city groups, which are each part of a continent supergroup. The rowspan for each |
---|
| 78 | * city group refers to the number of normal grid columns that group spans, so in this case the grid would be expected to have a |
---|
| 79 | * total of 12 rows: |
---|
| 80 | <pre><code> |
---|
| 81 | [ |
---|
| 82 | { |
---|
| 83 | width: 90, |
---|
| 84 | items: [ |
---|
| 85 | {header: 'England', rowspan: 5}, |
---|
| 86 | {header: 'USA', rowspan: 3} |
---|
| 87 | ] |
---|
| 88 | }, |
---|
| 89 | { |
---|
| 90 | width: 50, |
---|
| 91 | items: [ |
---|
| 92 | {header: 'London', rowspan: 2}, |
---|
| 93 | {header: 'Cambridge', rowspan: 3}, |
---|
| 94 | {header: 'Palo Alto', rowspan: 3} |
---|
| 95 | ] |
---|
| 96 | } |
---|
| 97 | ] |
---|
| 98 | </code></pre> |
---|
| 99 | * In the example above we have cities nested under countries. The nesting could be deeper if desired - e.g. Continent -> Country -> |
---|
| 100 | * State -> City, or any other structure. The only constaint is that the same depth must be used throughout the structure. |
---|
| 101 | * @return {Array} A tree structure containing the headers to be rendered. Must include the colspan property at each level, which should |
---|
| 102 | * be the sum of all child nodes beneath this node. |
---|
| 103 | * Each group may specify the width it should be rendered with. |
---|
| 104 | * @return {Array} The row groups |
---|
| 105 | */ |
---|
| 106 | getRowHeaders: function() { |
---|
| 107 | return this.grid.leftAxis.buildHeaders(); |
---|
| 108 | }, |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * @private |
---|
| 112 | * Renders rows between start and end indexes |
---|
| 113 | * @param {Number} startRow Index of the first row to render |
---|
| 114 | * @param {Number} endRow Index of the last row to render |
---|
| 115 | */ |
---|
| 116 | renderRows : function(startRow, endRow) { |
---|
| 117 | var grid = this.grid, |
---|
| 118 | rows = grid.extractData(), |
---|
| 119 | rowCount = rows.length, |
---|
| 120 | templates = this.templates, |
---|
| 121 | renderer = grid.renderer, |
---|
| 122 | hasRenderer = typeof renderer == 'function', |
---|
| 123 | getCellCls = this.getCellCls, |
---|
| 124 | hasGetCellCls = typeof getCellCls == 'function', |
---|
| 125 | cellTemplate = templates.cell, |
---|
| 126 | rowTemplate = templates.row, |
---|
| 127 | rowBuffer = [], |
---|
| 128 | meta = {}, |
---|
| 129 | tstyle = 'width:' + this.getGridInnerWidth() + 'px;', |
---|
| 130 | colBuffer, colCount, column, i, row; |
---|
| 131 | |
---|
| 132 | startRow = startRow || 0; |
---|
| 133 | endRow = Ext.isDefined(endRow) ? endRow : rowCount - 1; |
---|
| 134 | |
---|
| 135 | for (i = 0; i < rowCount; i++) { |
---|
| 136 | row = rows[i]; |
---|
| 137 | colCount = row.length; |
---|
| 138 | colBuffer = []; |
---|
| 139 | |
---|
| 140 | //build up each column's HTML |
---|
| 141 | for (var j = 0; j < colCount; j++) { |
---|
| 142 | |
---|
| 143 | meta.id = i + '-' + j; |
---|
| 144 | meta.css = j === 0 ? 'x-grid3-cell-first ' : (j == (colCount - 1) ? 'x-grid3-cell-last ' : ''); |
---|
| 145 | meta.attr = meta.cellAttr = ''; |
---|
| 146 | meta.value = row[j]; |
---|
| 147 | |
---|
| 148 | if (Ext.isEmpty(meta.value)) { |
---|
| 149 | meta.value = ' '; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | if (hasRenderer) { |
---|
| 153 | meta.value = renderer(meta.value); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if (hasGetCellCls) { |
---|
| 157 | meta.css += getCellCls(meta.value) + ' '; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | colBuffer[colBuffer.length] = cellTemplate.apply(meta); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | rowBuffer[rowBuffer.length] = rowTemplate.apply({ |
---|
| 164 | tstyle: tstyle, |
---|
| 165 | cols : colCount, |
---|
| 166 | cells : colBuffer.join(""), |
---|
| 167 | alt : '' |
---|
| 168 | }); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | return rowBuffer.join(""); |
---|
| 172 | }, |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * The master template to use when rendering the GridView. Has a default template |
---|
| 176 | * @property Ext.Template |
---|
| 177 | * @type masterTpl |
---|
| 178 | */ |
---|
| 179 | masterTpl: new Ext.Template( |
---|
| 180 | '<div class="x-grid3 x-pivotgrid" hidefocus="true">', |
---|
| 181 | '<div class="x-grid3-viewport">', |
---|
| 182 | '<div class="x-grid3-header">', |
---|
| 183 | '<div class="x-grid3-header-title"><span>{title}</span></div>', |
---|
| 184 | '<div class="x-grid3-header-inner">', |
---|
| 185 | '<div class="x-grid3-header-offset" style="{ostyle}"></div>', |
---|
| 186 | '</div>', |
---|
| 187 | '<div class="x-clear"></div>', |
---|
| 188 | '</div>', |
---|
| 189 | '<div class="x-grid3-scroller">', |
---|
| 190 | '<div class="x-grid3-row-headers"></div>', |
---|
| 191 | '<div class="x-grid3-body" style="{bstyle}">{body}</div>', |
---|
| 192 | '<a href="#" class="x-grid3-focus" tabIndex="-1"></a>', |
---|
| 193 | '</div>', |
---|
| 194 | '</div>', |
---|
| 195 | '<div class="x-grid3-resize-marker"> </div>', |
---|
| 196 | '<div class="x-grid3-resize-proxy"> </div>', |
---|
| 197 | '</div>' |
---|
| 198 | ), |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * @private |
---|
| 202 | * Adds a gcell template to the internal templates object. This is used to render the headers in a multi-level column header. |
---|
| 203 | */ |
---|
| 204 | initTemplates: function() { |
---|
| 205 | Ext.grid.PivotGridView.superclass.initTemplates.apply(this, arguments); |
---|
| 206 | |
---|
| 207 | var templates = this.templates || {}; |
---|
| 208 | if (!templates.gcell) { |
---|
| 209 | templates.gcell = new Ext.XTemplate( |
---|
| 210 | '<td class="x-grid3-hd x-grid3-gcell x-grid3-td-{id} ux-grid-hd-group-row-{row} ' + this.colHeaderCellCls + '" style="{style}">', |
---|
| 211 | '<div {tooltip} class="x-grid3-hd-inner x-grid3-hd-{id}" unselectable="on" style="{istyle}">', |
---|
| 212 | this.grid.enableHdMenu ? '<a class="x-grid3-hd-btn" href="#"></a>' : '', '{value}', |
---|
| 213 | '</div>', |
---|
| 214 | '</td>' |
---|
| 215 | ); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | this.templates = templates; |
---|
| 219 | this.hrowRe = new RegExp("ux-grid-hd-group-row-(\\d+)", ""); |
---|
| 220 | }, |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * @private |
---|
| 224 | * Sets up the reference to the row headers element |
---|
| 225 | */ |
---|
| 226 | initElements: function() { |
---|
| 227 | Ext.grid.PivotGridView.superclass.initElements.apply(this, arguments); |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * @property rowHeadersEl |
---|
| 231 | * @type Ext.Element |
---|
| 232 | * The element containing all row headers |
---|
| 233 | */ |
---|
| 234 | this.rowHeadersEl = new Ext.Element(this.scroller.child('div.x-grid3-row-headers')); |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * @property headerTitleEl |
---|
| 238 | * @type Ext.Element |
---|
| 239 | * The element that contains the optional title (top left section of the pivot grid) |
---|
| 240 | */ |
---|
| 241 | this.headerTitleEl = new Ext.Element(this.mainHd.child('div.x-grid3-header-title')); |
---|
| 242 | }, |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * @private |
---|
| 246 | * Takes row headers into account when calculating total available width |
---|
| 247 | */ |
---|
| 248 | getGridInnerWidth: function() { |
---|
| 249 | var previousWidth = Ext.grid.PivotGridView.superclass.getGridInnerWidth.apply(this, arguments); |
---|
| 250 | |
---|
| 251 | return previousWidth - this.getTotalRowHeaderWidth(); |
---|
| 252 | }, |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * Returns the total width of all row headers as specified by {@link #getRowHeaders} |
---|
| 256 | * @return {Number} The total width |
---|
| 257 | */ |
---|
| 258 | getTotalRowHeaderWidth: function() { |
---|
| 259 | var headers = this.getRowHeaders(), |
---|
| 260 | length = headers.length, |
---|
| 261 | total = 0, |
---|
| 262 | i; |
---|
| 263 | |
---|
| 264 | for (i = 0; i< length; i++) { |
---|
| 265 | total += headers[i].width; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | return total; |
---|
| 269 | }, |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * @private |
---|
| 273 | * Returns the total height of all column headers |
---|
| 274 | * @return {Number} The total height |
---|
| 275 | */ |
---|
| 276 | getTotalColumnHeaderHeight: function() { |
---|
| 277 | return this.getColumnHeaders().length * 21; |
---|
| 278 | }, |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Inherit docs |
---|
| 282 | * @private |
---|
| 283 | * @param {HTMLElement} el |
---|
| 284 | */ |
---|
| 285 | getCellIndex : function(el) { |
---|
| 286 | if (el) { |
---|
| 287 | var match = el.className.match(this.colRe), |
---|
| 288 | data; |
---|
| 289 | |
---|
| 290 | if (match && (data = match[1])) { |
---|
| 291 | return parseInt(data.split('-')[1], 10); |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | return false; |
---|
| 295 | }, |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | /** |
---|
| 299 | * @private |
---|
| 300 | * Slight specialisation of the GridView renderUI - just adds the row headers |
---|
| 301 | */ |
---|
| 302 | renderUI : function() { |
---|
| 303 | var templates = this.templates, |
---|
| 304 | innerWidth = this.getGridInnerWidth(); |
---|
| 305 | |
---|
| 306 | return templates.master.apply({ |
---|
| 307 | body : templates.body.apply({rows:' '}), |
---|
| 308 | ostyle: 'width:' + innerWidth + 'px', |
---|
| 309 | bstyle: 'width:' + innerWidth + 'px' |
---|
| 310 | }); |
---|
| 311 | }, |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * @private |
---|
| 315 | * Make sure that the headers and rows are all sized correctly during layout |
---|
| 316 | */ |
---|
| 317 | onLayout: function(width, height) { |
---|
| 318 | Ext.grid.PivotGridView.superclass.onLayout.apply(this, arguments); |
---|
| 319 | |
---|
| 320 | var width = this.getGridInnerWidth(); |
---|
| 321 | |
---|
| 322 | this.resizeColumnHeaders(width); |
---|
| 323 | this.resizeAllRows(width); |
---|
| 324 | }, |
---|
| 325 | |
---|
| 326 | /** |
---|
| 327 | * Refreshs the grid UI |
---|
| 328 | * @param {Boolean} headersToo (optional) True to also refresh the headers |
---|
| 329 | */ |
---|
| 330 | refresh : function(headersToo) { |
---|
| 331 | this.fireEvent('beforerefresh', this); |
---|
| 332 | this.grid.stopEditing(true); |
---|
| 333 | |
---|
| 334 | var result = this.renderBody(); |
---|
| 335 | this.mainBody.update(result).setWidth(this.getGridInnerWidth()); |
---|
| 336 | if (headersToo === true) { |
---|
| 337 | this.updateHeaders(); |
---|
| 338 | this.updateHeaderSortState(); |
---|
| 339 | } |
---|
| 340 | this.processRows(0, true); |
---|
| 341 | this.layout(); |
---|
| 342 | this.applyEmptyText(); |
---|
| 343 | this.fireEvent('refresh', this); |
---|
| 344 | }, |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | * @private |
---|
| 348 | * Bypasses GridView's renderHeaders as they are taken care of separately by the PivotAxis instances |
---|
| 349 | */ |
---|
| 350 | renderHeaders: Ext.emptyFn, |
---|
| 351 | |
---|
| 352 | /** |
---|
| 353 | * @private |
---|
| 354 | * Taken care of by PivotAxis |
---|
| 355 | */ |
---|
| 356 | fitColumns: Ext.emptyFn, |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | * @private |
---|
| 360 | * Called on layout, ensures that the width of each column header is correct. Omitting this can lead to faulty |
---|
| 361 | * layouts when nested in a container. |
---|
| 362 | * @param {Number} width The new width |
---|
| 363 | */ |
---|
| 364 | resizeColumnHeaders: function(width) { |
---|
| 365 | var topAxis = this.grid.topAxis; |
---|
| 366 | |
---|
| 367 | if (topAxis.rendered) { |
---|
| 368 | topAxis.el.setWidth(width); |
---|
| 369 | } |
---|
| 370 | }, |
---|
| 371 | |
---|
| 372 | /** |
---|
| 373 | * @private |
---|
| 374 | * Sets the row header div to the correct width. Should be called after rendering and reconfiguration of headers |
---|
| 375 | */ |
---|
| 376 | resizeRowHeaders: function() { |
---|
| 377 | var rowHeaderWidth = this.getTotalRowHeaderWidth(), |
---|
| 378 | marginStyle = String.format("margin-left: {0}px;", rowHeaderWidth); |
---|
| 379 | |
---|
| 380 | this.rowHeadersEl.setWidth(rowHeaderWidth); |
---|
| 381 | this.mainBody.applyStyles(marginStyle); |
---|
| 382 | Ext.fly(this.innerHd).applyStyles(marginStyle); |
---|
| 383 | |
---|
| 384 | this.headerTitleEl.setWidth(rowHeaderWidth); |
---|
| 385 | this.headerTitleEl.setHeight(this.getTotalColumnHeaderHeight()); |
---|
| 386 | }, |
---|
| 387 | |
---|
| 388 | /** |
---|
| 389 | * @private |
---|
| 390 | * Resizes all rendered rows to the given width. Usually called by onLayout |
---|
| 391 | * @param {Number} width The new width |
---|
| 392 | */ |
---|
| 393 | resizeAllRows: function(width) { |
---|
| 394 | var rows = this.getRows(), |
---|
| 395 | length = rows.length, |
---|
| 396 | i; |
---|
| 397 | |
---|
| 398 | for (i = 0; i < length; i++) { |
---|
| 399 | Ext.fly(rows[i]).setWidth(width); |
---|
| 400 | Ext.fly(rows[i]).child('table').setWidth(width); |
---|
| 401 | } |
---|
| 402 | }, |
---|
| 403 | |
---|
| 404 | /** |
---|
| 405 | * @private |
---|
| 406 | * Updates the Row Headers, deferring the updating of Column Headers to GridView |
---|
| 407 | */ |
---|
| 408 | updateHeaders: function() { |
---|
| 409 | this.renderGroupRowHeaders(); |
---|
| 410 | this.renderGroupColumnHeaders(); |
---|
| 411 | }, |
---|
| 412 | |
---|
| 413 | /** |
---|
| 414 | * @private |
---|
| 415 | * Renders all row header groups at all levels based on the structure fetched from {@link #getGroupRowHeaders} |
---|
| 416 | */ |
---|
| 417 | renderGroupRowHeaders: function() { |
---|
| 418 | var leftAxis = this.grid.leftAxis; |
---|
| 419 | |
---|
| 420 | this.resizeRowHeaders(); |
---|
| 421 | leftAxis.rendered = false; |
---|
| 422 | leftAxis.render(this.rowHeadersEl); |
---|
| 423 | |
---|
| 424 | this.setTitle(this.title); |
---|
| 425 | }, |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * Sets the title text in the top left segment of the PivotGridView |
---|
| 429 | * @param {String} title The title |
---|
| 430 | */ |
---|
| 431 | setTitle: function(title) { |
---|
| 432 | this.headerTitleEl.child('span').dom.innerHTML = title; |
---|
| 433 | }, |
---|
| 434 | |
---|
| 435 | /** |
---|
| 436 | * @private |
---|
| 437 | * Renders all column header groups at all levels based on the structure fetched from {@link #getColumnHeaders} |
---|
| 438 | */ |
---|
| 439 | renderGroupColumnHeaders: function() { |
---|
| 440 | var topAxis = this.grid.topAxis; |
---|
| 441 | |
---|
| 442 | topAxis.rendered = false; |
---|
| 443 | topAxis.render(this.innerHd.firstChild); |
---|
| 444 | }, |
---|
| 445 | |
---|
| 446 | /** |
---|
| 447 | * @private |
---|
| 448 | * Overridden to test whether the user is hovering over a group cell, in which case we don't show the menu |
---|
| 449 | */ |
---|
| 450 | isMenuDisabled: function(cellIndex, el) { |
---|
| 451 | return true; |
---|
| 452 | } |
---|
| 453 | }); |
---|