4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-grid-header-Container'>/**
19 </span> * @class Ext.grid.header.Container
20 * @extends Ext.container.Container
22 * Container which holds headers and is docked at the top or bottom of a TablePanel.
23 * The HeaderContainer drives resizing/moving/hiding of columns within the TableView.
24 * As headers are hidden, moved or resized the headercontainer is responsible for
25 * triggering changes within the view.
27 Ext.define('Ext.grid.header.Container', {
28 extend: 'Ext.container.Container',
30 'Ext.grid.ColumnLayout',
31 'Ext.grid.column.Column',
35 'Ext.grid.plugin.HeaderResizer',
36 'Ext.grid.plugin.HeaderReorderer'
40 alias: 'widget.headercontainer',
42 baseCls: Ext.baseCSSPrefix + 'grid-header-ct',
45 <span id='Ext-grid-header-Container-cfg-weight'> /**
46 </span> * @cfg {Number} weight
47 * HeaderContainer overrides the default weight of 0 for all docked items to 100.
48 * This is so that it has more priority over things like toolbars.
51 defaultType: 'gridcolumn',
52 <span id='Ext-grid-header-Container-cfg-defaultWidth'> /**
53 </span> * @cfg {Number} defaultWidth
54 * Width of the header if no width or flex is specified. Defaults to 100.
59 sortAscText: 'Sort Ascending',
60 sortDescText: 'Sort Descending',
61 sortClearText: 'Clear Sort',
62 columnsText: 'Columns',
64 lastHeaderCls: Ext.baseCSSPrefix + 'column-header-last',
65 firstHeaderCls: Ext.baseCSSPrefix + 'column-header-first',
66 headerOpenCls: Ext.baseCSSPrefix + 'column-header-open',
68 // private; will probably be removed by 4.0
75 <span id='Ext-grid-header-Container-property-isGroupHeader'> /**
76 </span> * <code>true</code> if this HeaderContainer is in fact a group header which contains sub headers.
78 * @property isGroupHeader
81 <span id='Ext-grid-header-Container-cfg-sortable'> /**
82 </span> * @cfg {Boolean} sortable
83 * Provides the default sortable state for all Headers within this HeaderContainer.
84 * Also turns on or off the menus in the HeaderContainer. Note that the menu is
85 * shared across every header and therefore turning it off will remove the menu
86 * items for every header.
90 initComponent: function() {
94 me.plugins = me.plugins || [];
96 // TODO: Pass in configurations to turn on/off dynamic
97 // resizing and disable resizing all together
99 // Only set up a Resizer and Reorderer for the topmost HeaderContainer.
100 // Nested Group Headers are themselves HeaderContainers
102 me.resizer = Ext.create('Ext.grid.plugin.HeaderResizer');
103 me.reorderer = Ext.create('Ext.grid.plugin.HeaderReorderer');
104 if (!me.enableColumnResize) {
105 me.resizer.disable();
107 if (!me.enableColumnMove) {
108 me.reorderer.disable();
110 me.plugins.push(me.reorderer, me.resizer);
113 // Base headers do not need a box layout
114 if (me.isHeader && !me.items) {
117 // HeaderContainer and Group header needs a gridcolumn layout.
121 availableSpaceOffset: me.availableSpaceOffset,
126 me.defaults = me.defaults || {};
127 Ext.applyIf(me.defaults, {
128 width: me.defaultWidth,
129 triStateSort: me.triStateSort,
130 sortable: me.sortable
134 <span id='Ext-grid-header-Container-event-columnresize'> /**
135 </span> * @event columnresize
136 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
137 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
138 * @param {Number} width
142 <span id='Ext-grid-header-Container-event-headerclick'> /**
143 </span> * @event headerclick
144 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
145 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
146 * @param {Ext.EventObject} e
147 * @param {HTMLElement} t
151 <span id='Ext-grid-header-Container-event-headertriggerclick'> /**
152 </span> * @event headertriggerclick
153 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
154 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
155 * @param {Ext.EventObject} e
156 * @param {HTMLElement} t
158 'headertriggerclick',
160 <span id='Ext-grid-header-Container-event-columnmove'> /**
161 </span> * @event columnmove
162 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
163 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
164 * @param {Number} fromIdx
165 * @param {Number} toIdx
168 <span id='Ext-grid-header-Container-event-columnhide'> /**
169 </span> * @event columnhide
170 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
171 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
174 <span id='Ext-grid-header-Container-event-columnshow'> /**
175 </span> * @event columnshow
176 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
177 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
180 <span id='Ext-grid-header-Container-event-sortchange'> /**
181 </span> * @event sortchange
182 * @param {Ext.grid.header.Container} ct The grid's header Container which encapsulates all column headers.
183 * @param {Ext.grid.column.Column} column The Column header Component which provides the column definition
184 * @param {String} direction
187 <span id='Ext-grid-header-Container-event-menucreate'> /**
188 </span> * @event menucreate
189 * Fired immediately after the column header menu is created.
190 * @param {Ext.grid.header.Container} ct This instance
191 * @param {Ext.menu.Menu} menu The Menu that was created
197 onDestroy: function() {
198 Ext.destroy(this.resizer, this.reorderer);
202 // Invalidate column cache on add
203 // We cannot refresh the View on every add because this method is called
204 // when the HeaderDropZone moves Headers around, that will also refresh the view
208 c.headerId = 'h' + (++me.headerCounter);
210 me.callParent(arguments);
214 // Invalidate column cache on remove
215 // We cannot refresh the View on every remove because this method is called
216 // when the HeaderDropZone moves Headers around, that will also refresh the view
217 onRemove: function(c) {
219 me.callParent(arguments);
223 afterRender: function() {
225 var store = this.up('[store]').store,
226 sorters = store.sorters,
227 first = sorters.first(),
231 hd = this.down('gridcolumn[dataIndex=' + first.property +']');
233 hd.setSortState(first.direction, false, true);
238 afterLayout: function() {
239 if (!this.isHeader) {
241 topHeaders = me.query('>gridcolumn:not([hidden])'),
246 me.callParent(arguments);
248 if (topHeaders.length) {
249 firstHeaderEl = topHeaders[0].el;
250 if (firstHeaderEl !== me.pastFirstHeaderEl) {
251 if (me.pastFirstHeaderEl) {
252 me.pastFirstHeaderEl.removeCls(me.firstHeaderCls);
254 firstHeaderEl.addCls(me.firstHeaderCls);
255 me.pastFirstHeaderEl = firstHeaderEl;
258 lastHeaderEl = topHeaders[topHeaders.length - 1].el;
259 if (lastHeaderEl !== me.pastLastHeaderEl) {
260 if (me.pastLastHeaderEl) {
261 me.pastLastHeaderEl.removeCls(me.lastHeaderCls);
263 lastHeaderEl.addCls(me.lastHeaderCls);
264 me.pastLastHeaderEl = lastHeaderEl
271 onHeaderShow: function(header) {
272 // Pass up to the GridSection
274 gridSection = me.ownerCt,
276 topItems, topItemsVisible,
283 colCheckItem = menu.down('menucheckitem[headerId=' + header.id + ']');
285 colCheckItem.setChecked(true, true);
288 // There's more than one header visible, and we've disabled some checked items... re-enable them
289 topItems = menu.query('#columnItem>menucheckitem[checked]');
290 topItemsVisible = topItems.length;
291 if ((me.getVisibleGridColumns().length > 1) && me.disabledMenuItems && me.disabledMenuItems.length) {
292 if (topItemsVisible == 1) {
293 Ext.Array.remove(me.disabledMenuItems, topItems[0]);
295 for (i = 0, len = me.disabledMenuItems.length; i < len; i++) {
296 itemToEnable = me.disabledMenuItems[i];
297 if (!itemToEnable.isDestroyed) {
298 itemToEnable[itemToEnable.menu ? 'enableCheckChange' : 'enable']();
301 if (topItemsVisible == 1) {
302 me.disabledMenuItems = topItems;
304 me.disabledMenuItems = [];
309 // Only update the grid UI when we are notified about base level Header shows;
310 // Group header shows just cause a layout of the HeaderContainer
311 if (!header.isGroupHeader) {
313 me.view.onHeaderShow(me, header, true);
316 gridSection.onHeaderShow(me, header);
319 me.fireEvent('columnshow', me, header);
321 // The header's own hide suppresses cascading layouts, so lay the headers out now
325 onHeaderHide: function(header, suppressLayout) {
326 // Pass up to the GridSection
328 gridSection = me.ownerCt,
334 // If the header was hidden programmatically, sync the Menu state
335 colCheckItem = menu.down('menucheckitem[headerId=' + header.id + ']');
337 colCheckItem.setChecked(false, true);
339 me.setDisabledItems();
342 // Only update the UI when we are notified about base level Header hides;
343 if (!header.isGroupHeader) {
345 me.view.onHeaderHide(me, header, true);
348 gridSection.onHeaderHide(me, header);
351 // The header's own hide suppresses cascading layouts, so lay the headers out now
352 if (!suppressLayout) {
356 me.fireEvent('columnhide', me, header);
359 setDisabledItems: function(){
367 // Find what to disable. If only one top level item remaining checked, we have to disable stuff.
368 itemsToDisable = menu.query('#columnItem>menucheckitem[checked]');
369 if ((itemsToDisable.length === 1)) {
370 if (!me.disabledMenuItems) {
371 me.disabledMenuItems = [];
374 // If down to only one column visible, also disable any descendant checkitems
375 if ((me.getVisibleGridColumns().length === 1) && itemsToDisable[0].menu) {
376 itemsToDisable = itemsToDisable.concat(itemsToDisable[0].menu.query('menucheckitem[checked]'));
379 len = itemsToDisable.length;
380 // Disable any further unchecking at any level.
381 for (i = 0; i < len; i++) {
382 itemToDisable = itemsToDisable[i];
383 if (!Ext.Array.contains(me.disabledMenuItems, itemToDisable)) {
385 // If we only want to disable check change: it might be a disabled item, so enable it prior to
386 // setting its correct disablement level.
387 itemToDisable.disabled = false;
388 itemToDisable[itemToDisable.menu ? 'disableCheckChange' : 'disable']();
389 me.disabledMenuItems.push(itemToDisable);
395 <span id='Ext-grid-header-Container-method-tempLock'> /**
396 </span> * Temporarily lock the headerCt. This makes it so that clicking on headers
397 * don't trigger actions like sorting or opening of the header menu. This is
398 * done because extraneous events may be fired on the headers after interacting
399 * with a drag drop operation.
402 tempLock: function() {
404 Ext.Function.defer(function() {
409 onHeaderResize: function(header, w, suppressFocus) {
411 if (this.view && this.view.rendered) {
412 this.view.onHeaderResize(header, w, suppressFocus);
414 this.fireEvent('columnresize', this, header, w);
417 onHeaderClick: function(header, e, t) {
418 this.fireEvent("headerclick", this, header, e, t);
421 onHeaderTriggerClick: function(header, e, t) {
422 // generate and cache menu, provide ability to cancel/etc
423 if (this.fireEvent("headertriggerclick", this, header, e, t) !== false) {
424 this.showMenuBy(t, header);
428 showMenuBy: function(t, header) {
429 var menu = this.getMenu(),
430 ascItem = menu.down('#ascItem'),
431 descItem = menu.down('#descItem'),
434 menu.activeHeader = menu.ownerCt = header;
435 menu.setFloatParent(header);
436 // TODO: remove coupling to Header's titleContainer el
437 header.titleContainer.addCls(this.headerOpenCls);
439 // enable or disable asc & desc menu items based on header being sortable
440 sortableMth = header.sortable ? 'enable' : 'disable';
442 ascItem[sortableMth]();
445 descItem[sortableMth]();
450 // remove the trigger open class when the menu is hidden
451 onMenuDeactivate: function() {
452 var menu = this.getMenu();
453 // TODO: remove coupling to Header's titleContainer el
454 menu.activeHeader.titleContainer.removeCls(this.headerOpenCls);
457 moveHeader: function(fromIdx, toIdx) {
459 // An automatically expiring lock
461 this.onHeaderMoved(this.move(fromIdx, toIdx), fromIdx, toIdx);
464 purgeCache: function() {
466 // Delete column cache - column order has changed.
467 delete me.gridDataColumns;
469 // Menu changes when columns are moved. It will be recreated.
476 onHeaderMoved: function(header, fromIdx, toIdx) {
478 gridSection = me.ownerCt;
481 gridSection.onHeaderMove(me, header, fromIdx, toIdx);
483 me.fireEvent("columnmove", me, header, fromIdx, toIdx);
486 <span id='Ext-grid-header-Container-method-getMenu'> /**
487 </span> * Gets the menu (and will create it if it doesn't already exist)
490 getMenu: function() {
494 me.menu = Ext.create('Ext.menu.Menu', {
495 hideOnParentHide: false, // Persists when owning ColumnHeader is hidden
496 items: me.getMenuItems(),
498 deactivate: me.onMenuDeactivate,
502 me.setDisabledItems();
503 me.fireEvent('menucreate', me, me.menu);
508 <span id='Ext-grid-header-Container-method-getMenuItems'> /**
509 </span> * Returns an array of menu items to be placed into the shared menu
510 * across all headers in this header container.
511 * @returns {Array} menuItems
513 getMenuItems: function() {
516 hideableColumns = me.enableColumnHide ? me.getColumnMenu(me) : null;
521 text: me.sortAscText,
522 cls: 'xg-hmenu-sort-asc',
523 handler: me.onSortAscClick,
527 text: me.sortDescText,
528 cls: 'xg-hmenu-sort-desc',
529 handler: me.onSortDescClick,
533 if (hideableColumns && hideableColumns.length) {
534 menuItems.push('-', {
535 itemId: 'columnItem',
536 text: me.columnsText,
537 cls: Ext.baseCSSPrefix + 'cols-icon',
538 menu: hideableColumns
544 // sort asc when clicking on item in menu
545 onSortAscClick: function() {
546 var menu = this.getMenu(),
547 activeHeader = menu.activeHeader;
549 activeHeader.setSortState('ASC');
552 // sort desc when clicking on item in menu
553 onSortDescClick: function() {
554 var menu = this.getMenu(),
555 activeHeader = menu.activeHeader;
557 activeHeader.setSortState('DESC');
560 <span id='Ext-grid-header-Container-method-getColumnMenu'> /**
561 </span> * Returns an array of menu CheckItems corresponding to all immediate children of the passed Container which have been configured as hideable.
563 getColumnMenu: function(headerContainer) {
567 items = headerContainer.query('>gridcolumn[hideable]'),
568 itemsLn = items.length,
571 for (; i < itemsLn; i++) {
573 menuItem = Ext.create('Ext.menu.CheckItem', {
575 checked: !item.hidden,
578 menu: item.isGroupHeader ? this.getColumnMenu(item) : undefined,
579 checkHandler: this.onColumnCheckChange,
583 menuItem.disabled = true;
585 menuItems.push(menuItem);
587 // If the header is ever destroyed - for instance by dragging out the last remaining sub header,
588 // then the associated menu item must also be destroyed.
590 destroy: Ext.Function.bind(menuItem.destroy, menuItem)
596 onColumnCheckChange: function(checkItem, checked) {
597 var header = Ext.getCmp(checkItem.headerId);
598 header[checked ? 'show' : 'hide']();
601 <span id='Ext-grid-header-Container-method-getColumnsForTpl'> /**
602 </span> * Get the columns used for generating a template via TableChunker.
603 * Returns an array of all columns and their
608 * - columnId - used to create an identifying CSS class
609 * - cls The tdCls configuration from the Column object
612 getColumnsForTpl: function(flushCache) {
614 headers = this.getGridColumns(flushCache),
615 headersLn = headers.length,
620 for (; i < headersLn; i++) {
626 width = header.getDesiredWidth();
628 // Setting the width of the first TD does not work - ends up with a 1 pixel discrepancy.
629 // We need to increment the passed with in this case.
630 if ((i == 0) && (Ext.isIE6 || Ext.isIE7)) {
635 dataIndex: header.dataIndex,
640 columnId: header.getItemId()
646 <span id='Ext-grid-header-Container-method-getColumnCount'> /**
647 </span> * Returns the number of <b>grid columns</b> descended from this HeaderContainer.
648 * Group Columns are HeaderContainers. All grid columns are returned, including hidden ones.
650 getColumnCount: function() {
651 return this.getGridColumns().length;
654 <span id='Ext-grid-header-Container-method-getFullWidth'> /**
655 </span> * Gets the full width of all columns that are visible.
657 getFullWidth: function(flushCache) {
659 headers = this.getVisibleGridColumns(flushCache),
660 headersLn = headers.length,
663 for (; i < headersLn; i++) {
664 if (!isNaN(headers[i].width)) {
665 // use headers getDesiredWidth if its there
666 if (headers[i].getDesiredWidth) {
667 fullWidth += headers[i].getDesiredWidth();
668 // if injected a diff cmp use getWidth
670 fullWidth += headers[i].getWidth();
677 // invoked internally by a header when not using triStateSorting
678 clearOtherSortStates: function(activeHeader) {
679 var headers = this.getGridColumns(),
680 headersLn = headers.length,
684 for (; i < headersLn; i++) {
685 if (headers[i] !== activeHeader) {
686 oldSortState = headers[i].sortState;
687 // unset the sortstate and dont recurse
688 headers[i].setSortState(null, true);
689 //if (!silent && oldSortState !== null) {
690 // this.fireEvent('sortchange', this, headers[i], null);
696 <span id='Ext-grid-header-Container-method-getVisibleGridColumns'> /**
697 </span> * Returns an array of the <b>visible</b> columns in the grid. This goes down to the lowest column header
698 * level, and does not return <i>grouped</i> headers which contain sub headers.
699 * @param {Boolean} refreshCache If omitted, the cached set of columns will be returned. Pass true to refresh the cache.
702 getVisibleGridColumns: function(refreshCache) {
703 return Ext.ComponentQuery.query(':not([hidden])', this.getGridColumns(refreshCache));
706 <span id='Ext-grid-header-Container-method-getGridColumns'> /**
707 </span> * Returns an array of all columns which map to Store fields. This goes down to the lowest column header
708 * level, and does not return <i>grouped</i> headers which contain sub headers.
709 * @param {Boolean} refreshCache If omitted, the cached set of columns will be returned. Pass true to refresh the cache.
712 getGridColumns: function(refreshCache) {
714 result = refreshCache ? null : me.gridDataColumns;
716 // Not already got the column cache, so collect the base columns
718 me.gridDataColumns = result = [];
719 me.cascade(function(c) {
720 if ((c !== me) && !c.isGroupHeader) {
729 <span id='Ext-grid-header-Container-method-getHeaderIndex'> /**
730 </span> * Get the index of a leaf level header regardless of what the nesting
733 getHeaderIndex: function(header) {
734 var columns = this.getGridColumns();
735 return Ext.Array.indexOf(columns, header);
738 <span id='Ext-grid-header-Container-method-getHeaderAtIndex'> /**
739 </span> * Get a leaf level header by index regardless of what the nesting
742 getHeaderAtIndex: function(index) {
743 var columns = this.getGridColumns();
744 return columns[index];
747 <span id='Ext-grid-header-Container-method-prepareData'> /**
748 </span> * Maps the record data to base it on the header id's.
749 * This correlates to the markup/template generated by
752 prepareData: function(data, rowIdx, record, view, panel) {
754 headers = this.gridDataColumns || this.getGridColumns(),
755 headersLn = headers.length,
764 for (; colIdx < headersLn; colIdx++) {
769 header = headers[colIdx];
770 headerId = header.id;
771 renderer = header.renderer;
772 value = data[header.dataIndex];
774 // When specifying a renderer as a string, it always resolves
775 // to Ext.util.Format
776 if (typeof renderer === "string") {
777 header.renderer = renderer = Ext.util.Format[renderer];
780 if (typeof renderer === "function") {
781 value = renderer.call(
782 header.scope || this.ownerCt,
784 // metadata per cell passing an obj by reference so that
785 // it can be manipulated inside the renderer
797 // This warning attribute is used by the compat layer
798 obj.cssWarning = true;
799 metaData.tdCls = metaData.css;
804 obj[headerId+'-modified'] = record.isModified(header.dataIndex) ? Ext.baseCSSPrefix + 'grid-dirty-cell' : '';
805 obj[headerId+'-tdCls'] = metaData.tdCls;
806 obj[headerId+'-tdAttr'] = metaData.tdAttr;
807 obj[headerId+'-style'] = metaData.style;
808 if (value === undefined || value === null || value === '') {
809 value = '&#160;';
811 obj[headerId] = value;
816 expandToFit: function(header) {
818 this.view.expandToFit(header);