TreeGridColumnResizer.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:4k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*!
  2.  * Ext JS Library 3.1.0
  3.  * Copyright(c) 2006-2009 Ext JS, LLC
  4.  * licensing@extjs.com
  5.  * http://www.extjs.com/license
  6.  */
  7. /**
  8.  * @class Ext.tree.ColumnResizer
  9.  * @extends Ext.util.Observable
  10.  */
  11. Ext.tree.ColumnResizer = Ext.extend(Ext.util.Observable, {
  12.     /**
  13.      * @cfg {Number} minWidth The minimum width the column can be dragged to.
  14.      * Defaults to <tt>14</tt>.
  15.      */
  16.     minWidth: 14,
  17.     constructor: function(config){
  18.         Ext.apply(this, config);
  19.         Ext.tree.ColumnResizer.superclass.constructor.call(this);
  20.     },
  21.     init : function(tree){
  22.         this.tree = tree;
  23.         tree.on('render', this.initEvents, this);
  24.     },
  25.     initEvents : function(tree){
  26.         tree.mon(tree.innerHd, 'mousemove', this.handleHdMove, this);
  27.         this.tracker = new Ext.dd.DragTracker({
  28.             onBeforeStart: this.onBeforeStart.createDelegate(this),
  29.             onStart: this.onStart.createDelegate(this),
  30.             onDrag: this.onDrag.createDelegate(this),
  31.             onEnd: this.onEnd.createDelegate(this),
  32.             tolerance: 3,
  33.             autoStart: 300
  34.         });
  35.         this.tracker.initEl(tree.innerHd);
  36.         tree.on('beforedestroy', this.tracker.destroy, this.tracker);
  37.     },
  38.     handleHdMove : function(e, t){
  39.         var hw = 5,
  40.             x = e.getPageX(),
  41.             hd = e.getTarget('.x-treegrid-hd', 3, true);
  42.         
  43.         if(hd){                                 
  44.             var r = hd.getRegion(),
  45.                 ss = hd.dom.style,
  46.                 pn = hd.dom.parentNode;
  47.             
  48.             if(x - r.left <= hw && hd.dom !== pn.firstChild) {
  49.                 var ps = hd.dom.previousSibling;
  50.                 while(ps && Ext.fly(ps).hasClass('x-treegrid-hd-hidden')) {
  51.                     ps = ps.previousSibling;
  52.                 }
  53.                 if(ps) {                    
  54.                     this.activeHd = Ext.get(ps);
  55.      ss.cursor = Ext.isWebKit ? 'e-resize' : 'col-resize';
  56.                 }
  57.             } else if(r.right - x <= hw) {
  58.                 var ns = hd.dom;
  59.                 while(ns && Ext.fly(ns).hasClass('x-treegrid-hd-hidden')) {
  60.                     ns = ns.previousSibling;
  61.                 }
  62.                 if(ns) {
  63.                     this.activeHd = Ext.get(ns);
  64.      ss.cursor = Ext.isWebKit ? 'w-resize' : 'col-resize';                    
  65.                 }
  66.             } else{
  67.                 delete this.activeHd;
  68.                 ss.cursor = '';
  69.             }
  70.         }
  71.     },
  72.     onBeforeStart : function(e){
  73.         this.dragHd = this.activeHd;
  74.         return !!this.dragHd;
  75.     },
  76.     onStart : function(e){
  77.         this.tree.headersDisabled = true;
  78.         this.proxy = this.tree.body.createChild({cls:'x-treegrid-resizer'});
  79.         this.proxy.setHeight(this.tree.body.getHeight());
  80.         var x = this.tracker.getXY()[0];
  81.         this.hdX = this.dragHd.getX();
  82.         this.hdIndex = this.tree.findHeaderIndex(this.dragHd);
  83.         this.proxy.setX(this.hdX);
  84.         this.proxy.setWidth(x-this.hdX);
  85.         this.maxWidth = this.tree.outerCt.getWidth() - this.tree.innerBody.translatePoints(this.hdX).left;
  86.     },
  87.     onDrag : function(e){
  88.         var cursorX = this.tracker.getXY()[0];
  89.         this.proxy.setWidth((cursorX-this.hdX).constrain(this.minWidth, this.maxWidth));
  90.     },
  91.     onEnd : function(e){
  92.         var nw = this.proxy.getWidth(),
  93.             tree = this.tree;
  94.         
  95.         this.proxy.remove();
  96.         delete this.dragHd;
  97.         
  98.         tree.columns[this.hdIndex].width = nw;
  99.         tree.updateColumnWidths();
  100.         
  101.         setTimeout(function(){
  102.             tree.headersDisabled = false;
  103.         }, 100);
  104.     }
  105. });