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.

423 lines
14 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 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
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.ux.tree.TreeGrid
  17. * @extends Ext.tree.TreePanel
  18. *
  19. * @xtype treegrid
  20. */
  21. Ext.ux.tree.TreeGrid = Ext.extend(Ext.tree.TreePanel, {
  22. rootVisible : false,
  23. useArrows : true,
  24. lines : false,
  25. borderWidth : Ext.isBorderBox ? 0 : 2, // the combined left/right border for each cell
  26. cls : 'x-treegrid',
  27. columnResize : true,
  28. enableSort : true,
  29. reserveScrollOffset : true,
  30. enableHdMenu : true,
  31. columnsText : 'Columns',
  32. initComponent : function() {
  33. if(!this.root) {
  34. this.root = new Ext.tree.AsyncTreeNode({text: 'Root'});
  35. }
  36. // initialize the loader
  37. var l = this.loader;
  38. if(!l){
  39. l = new Ext.ux.tree.TreeGridLoader({
  40. dataUrl: this.dataUrl,
  41. requestMethod: this.requestMethod,
  42. store: this.store
  43. });
  44. }else if(Ext.isObject(l) && !l.load){
  45. l = new Ext.ux.tree.TreeGridLoader(l);
  46. }
  47. this.loader = l;
  48. Ext.ux.tree.TreeGrid.superclass.initComponent.call(this);
  49. this.initColumns();
  50. if(this.enableSort) {
  51. this.treeGridSorter = new Ext.ux.tree.TreeGridSorter(this, this.enableSort);
  52. }
  53. if(this.columnResize){
  54. this.colResizer = new Ext.tree.ColumnResizer(this.columnResize);
  55. this.colResizer.init(this);
  56. }
  57. var c = this.columns;
  58. if(!this.internalTpl){
  59. this.internalTpl = new Ext.XTemplate(
  60. '<div class="x-grid3-header">',
  61. '<div class="x-treegrid-header-inner">',
  62. '<div class="x-grid3-header-offset">',
  63. '<table style="table-layout: fixed;" cellspacing="0" cellpadding="0" border="0"><colgroup><tpl for="columns"><col /></tpl></colgroup>',
  64. '<thead><tr class="x-grid3-hd-row">',
  65. '<tpl for="columns">',
  66. '<td class="x-grid3-hd x-grid3-cell x-treegrid-hd" style="text-align: {align};" id="', this.id, '-xlhd-{#}">',
  67. '<div class="x-grid3-hd-inner x-treegrid-hd-inner" unselectable="on">',
  68. this.enableHdMenu ? '<a class="x-grid3-hd-btn" href="#"></a>' : '',
  69. '{header}<img class="x-grid3-sort-icon" src="', Ext.BLANK_IMAGE_URL, '" />',
  70. '</div>',
  71. '</td></tpl>',
  72. '</tr></thead>',
  73. '</table>',
  74. '</div></div>',
  75. '</div>',
  76. '<div class="x-treegrid-root-node">',
  77. '<table class="x-treegrid-root-table" cellpadding="0" cellspacing="0" style="table-layout: fixed;"></table>',
  78. '</div>'
  79. );
  80. }
  81. if(!this.colgroupTpl) {
  82. this.colgroupTpl = new Ext.XTemplate(
  83. '<colgroup><tpl for="columns"><col style="width: {width}px"/></tpl></colgroup>'
  84. );
  85. }
  86. },
  87. initColumns : function() {
  88. var cs = this.columns,
  89. len = cs.length,
  90. columns = [],
  91. i, c;
  92. for(i = 0; i < len; i++){
  93. c = cs[i];
  94. if(!c.isColumn) {
  95. c.xtype = c.xtype ? (/^tg/.test(c.xtype) ? c.xtype : 'tg' + c.xtype) : 'tgcolumn';
  96. c = Ext.create(c);
  97. }
  98. c.init(this);
  99. columns.push(c);
  100. if(this.enableSort !== false && c.sortable !== false) {
  101. c.sortable = true;
  102. this.enableSort = true;
  103. }
  104. }
  105. this.columns = columns;
  106. },
  107. onRender : function(){
  108. Ext.tree.TreePanel.superclass.onRender.apply(this, arguments);
  109. this.el.addClass('x-treegrid');
  110. this.outerCt = this.body.createChild({
  111. cls:'x-tree-root-ct x-treegrid-ct ' + (this.useArrows ? 'x-tree-arrows' : this.lines ? 'x-tree-lines' : 'x-tree-no-lines')
  112. });
  113. this.internalTpl.overwrite(this.outerCt, {columns: this.columns});
  114. this.mainHd = Ext.get(this.outerCt.dom.firstChild);
  115. this.innerHd = Ext.get(this.mainHd.dom.firstChild);
  116. this.innerBody = Ext.get(this.outerCt.dom.lastChild);
  117. this.innerCt = Ext.get(this.innerBody.dom.firstChild);
  118. this.colgroupTpl.insertFirst(this.innerCt, {columns: this.columns});
  119. if(this.hideHeaders){
  120. this.el.child('.x-grid3-header').setDisplayed('none');
  121. }
  122. else if(this.enableHdMenu !== false){
  123. this.hmenu = new Ext.menu.Menu({id: this.id + '-hctx'});
  124. if(this.enableColumnHide !== false){
  125. this.colMenu = new Ext.menu.Menu({id: this.id + '-hcols-menu'});
  126. this.colMenu.on({
  127. scope: this,
  128. beforeshow: this.beforeColMenuShow,
  129. itemclick: this.handleHdMenuClick
  130. });
  131. this.hmenu.add({
  132. itemId:'columns',
  133. hideOnClick: false,
  134. text: this.columnsText,
  135. menu: this.colMenu,
  136. iconCls: 'x-cols-icon'
  137. });
  138. }
  139. this.hmenu.on('itemclick', this.handleHdMenuClick, this);
  140. }
  141. },
  142. setRootNode : function(node){
  143. node.attributes.uiProvider = Ext.ux.tree.TreeGridRootNodeUI;
  144. node = Ext.ux.tree.TreeGrid.superclass.setRootNode.call(this, node);
  145. if(this.innerCt) {
  146. this.colgroupTpl.insertFirst(this.innerCt, {columns: this.columns});
  147. }
  148. return node;
  149. },
  150. clearInnerCt : function(){
  151. if(Ext.isIE){
  152. var dom = this.innerCt.dom;
  153. while(dom.firstChild){
  154. dom.removeChild(dom.firstChild);
  155. }
  156. }else{
  157. Ext.ux.tree.TreeGrid.superclass.clearInnerCt.call(this);
  158. }
  159. },
  160. initEvents : function() {
  161. Ext.ux.tree.TreeGrid.superclass.initEvents.apply(this, arguments);
  162. this.mon(this.innerBody, 'scroll', this.syncScroll, this);
  163. this.mon(this.innerHd, 'click', this.handleHdDown, this);
  164. this.mon(this.mainHd, {
  165. scope: this,
  166. mouseover: this.handleHdOver,
  167. mouseout: this.handleHdOut
  168. });
  169. },
  170. onResize : function(w, h) {
  171. Ext.ux.tree.TreeGrid.superclass.onResize.apply(this, arguments);
  172. var bd = this.innerBody.dom;
  173. var hd = this.innerHd.dom;
  174. if(!bd){
  175. return;
  176. }
  177. if(Ext.isNumber(h)){
  178. bd.style.height = this.body.getHeight(true) - hd.offsetHeight + 'px';
  179. }
  180. if(Ext.isNumber(w)){
  181. var sw = Ext.num(this.scrollOffset, Ext.getScrollBarWidth());
  182. if(this.reserveScrollOffset || ((bd.offsetWidth - bd.clientWidth) > 10)){
  183. this.setScrollOffset(sw);
  184. }else{
  185. var me = this;
  186. setTimeout(function(){
  187. me.setScrollOffset(bd.offsetWidth - bd.clientWidth > 10 ? sw : 0);
  188. }, 10);
  189. }
  190. }
  191. },
  192. updateColumnWidths : function() {
  193. var cols = this.columns,
  194. colCount = cols.length,
  195. groups = this.outerCt.query('colgroup'),
  196. groupCount = groups.length,
  197. c, g, i, j;
  198. for(i = 0; i<colCount; i++) {
  199. c = cols[i];
  200. for(j = 0; j<groupCount; j++) {
  201. g = groups[j];
  202. g.childNodes[i].style.width = (c.hidden ? 0 : c.width) + 'px';
  203. }
  204. }
  205. for(i = 0, groups = this.innerHd.query('td'), len = groups.length; i<len; i++) {
  206. c = Ext.fly(groups[i]);
  207. if(cols[i] && cols[i].hidden) {
  208. c.addClass('x-treegrid-hd-hidden');
  209. }
  210. else {
  211. c.removeClass('x-treegrid-hd-hidden');
  212. }
  213. }
  214. var tcw = this.getTotalColumnWidth();
  215. Ext.fly(this.innerHd.dom.firstChild).setWidth(tcw + (this.scrollOffset || 0));
  216. this.outerCt.select('table').setWidth(tcw);
  217. this.syncHeaderScroll();
  218. },
  219. getVisibleColumns : function() {
  220. var columns = [],
  221. cs = this.columns,
  222. len = cs.length,
  223. i;
  224. for(i = 0; i<len; i++) {
  225. if(!cs[i].hidden) {
  226. columns.push(cs[i]);
  227. }
  228. }
  229. return columns;
  230. },
  231. getTotalColumnWidth : function() {
  232. var total = 0;
  233. for(var i = 0, cs = this.getVisibleColumns(), len = cs.length; i<len; i++) {
  234. total += cs[i].width;
  235. }
  236. return total;
  237. },
  238. setScrollOffset : function(scrollOffset) {
  239. this.scrollOffset = scrollOffset;
  240. this.updateColumnWidths();
  241. },
  242. // private
  243. handleHdDown : function(e, t){
  244. var hd = e.getTarget('.x-treegrid-hd');
  245. if(hd && Ext.fly(t).hasClass('x-grid3-hd-btn')){
  246. var ms = this.hmenu.items,
  247. cs = this.columns,
  248. index = this.findHeaderIndex(hd),
  249. c = cs[index],
  250. sort = c.sortable;
  251. e.stopEvent();
  252. Ext.fly(hd).addClass('x-grid3-hd-menu-open');
  253. this.hdCtxIndex = index;
  254. this.fireEvent('headerbuttonclick', ms, c, hd, index);
  255. this.hmenu.on('hide', function(){
  256. Ext.fly(hd).removeClass('x-grid3-hd-menu-open');
  257. }, this, {single:true});
  258. this.hmenu.show(t, 'tl-bl?');
  259. }
  260. else if(hd) {
  261. var index = this.findHeaderIndex(hd);
  262. this.fireEvent('headerclick', this.columns[index], hd, index);
  263. }
  264. },
  265. // private
  266. handleHdOver : function(e, t){
  267. var hd = e.getTarget('.x-treegrid-hd');
  268. if(hd && !this.headersDisabled){
  269. index = this.findHeaderIndex(hd);
  270. this.activeHdRef = t;
  271. this.activeHdIndex = index;
  272. var el = Ext.get(hd);
  273. this.activeHdRegion = el.getRegion();
  274. el.addClass('x-grid3-hd-over');
  275. this.activeHdBtn = el.child('.x-grid3-hd-btn');
  276. if(this.activeHdBtn){
  277. this.activeHdBtn.dom.style.height = (hd.firstChild.offsetHeight-1)+'px';
  278. }
  279. }
  280. },
  281. // private
  282. handleHdOut : function(e, t){
  283. var hd = e.getTarget('.x-treegrid-hd');
  284. if(hd && (!Ext.isIE || !e.within(hd, true))){
  285. this.activeHdRef = null;
  286. Ext.fly(hd).removeClass('x-grid3-hd-over');
  287. hd.style.cursor = '';
  288. }
  289. },
  290. findHeaderIndex : function(hd){
  291. hd = hd.dom || hd;
  292. var cs = hd.parentNode.childNodes;
  293. for(var i = 0, c; c = cs[i]; i++){
  294. if(c == hd){
  295. return i;
  296. }
  297. }
  298. return -1;
  299. },
  300. // private
  301. beforeColMenuShow : function(){
  302. var cols = this.columns,
  303. colCount = cols.length,
  304. i, c;
  305. this.colMenu.removeAll();
  306. for(i = 1; i < colCount; i++){
  307. c = cols[i];
  308. if(c.hideable !== false){
  309. this.colMenu.add(new Ext.menu.CheckItem({
  310. itemId: 'col-' + i,
  311. text: c.header,
  312. checked: !c.hidden,
  313. hideOnClick:false,
  314. disabled: c.hideable === false
  315. }));
  316. }
  317. }
  318. },
  319. // private
  320. handleHdMenuClick : function(item){
  321. var index = this.hdCtxIndex,
  322. id = item.getItemId();
  323. if(this.fireEvent('headermenuclick', this.columns[index], id, index) !== false) {
  324. index = id.substr(4);
  325. if(index > 0 && this.columns[index]) {
  326. this.setColumnVisible(index, !item.checked);
  327. }
  328. }
  329. return true;
  330. },
  331. setColumnVisible : function(index, visible) {
  332. this.columns[index].hidden = !visible;
  333. this.updateColumnWidths();
  334. },
  335. /**
  336. * Scrolls the grid to the top
  337. */
  338. scrollToTop : function(){
  339. this.innerBody.dom.scrollTop = 0;
  340. this.innerBody.dom.scrollLeft = 0;
  341. },
  342. // private
  343. syncScroll : function(){
  344. this.syncHeaderScroll();
  345. var mb = this.innerBody.dom;
  346. this.fireEvent('bodyscroll', mb.scrollLeft, mb.scrollTop);
  347. },
  348. // private
  349. syncHeaderScroll : function(){
  350. var mb = this.innerBody.dom;
  351. this.innerHd.dom.scrollLeft = mb.scrollLeft;
  352. this.innerHd.dom.scrollLeft = mb.scrollLeft; // second time for IE (1/2 time first fails, other browsers ignore)
  353. },
  354. registerNode : function(n) {
  355. Ext.ux.tree.TreeGrid.superclass.registerNode.call(this, n);
  356. if(!n.uiProvider && !n.isRoot && !n.ui.isTreeGridNodeUI) {
  357. n.ui = new Ext.ux.tree.TreeGridNodeUI(n);
  358. }
  359. }
  360. });
  361. Ext.reg('treegrid', Ext.ux.tree.TreeGrid);