initial import
[roojs1] / Roo / grid / HeaderDropZone.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 // private
12 // This is a support class used internally by the Grid components
13 Roo.grid.HeaderDropZone = function(grid, hd, hd2){
14     this.grid = grid;
15     this.view = grid.getView();
16     // split the proxies so they don't interfere with mouse events
17     this.proxyTop = Roo.DomHelper.append(document.body, {
18         cls:"col-move-top", html:"&#160;"
19     }, true);
20     this.proxyBottom = Roo.DomHelper.append(document.body, {
21         cls:"col-move-bottom", html:"&#160;"
22     }, true);
23     this.proxyTop.hide = this.proxyBottom.hide = function(){
24         this.setLeftTop(-100,-100);
25         this.setStyle("visibility", "hidden");
26     };
27     this.ddGroup = "gridHeader" + this.grid.getGridEl().id;
28     // temporarily disabled
29     //Roo.dd.ScrollManager.register(this.view.scroller.dom);
30     Roo.grid.HeaderDropZone.superclass.constructor.call(this, grid.getGridEl().dom);
31 };
32 Roo.extend(Roo.grid.HeaderDropZone, Roo.dd.DropZone, {
33     proxyOffsets : [-4, -9],
34     fly: Roo.Element.fly,
35
36     getTargetFromEvent : function(e){
37         var t = Roo.lib.Event.getTarget(e);
38         var cindex = this.view.findCellIndex(t);
39         if(cindex !== false){
40             return this.view.getHeaderCell(cindex);
41         }
42     },
43
44     nextVisible : function(h){
45         var v = this.view, cm = this.grid.colModel;
46         h = h.nextSibling;
47         while(h){
48             if(!cm.isHidden(v.getCellIndex(h))){
49                 return h;
50             }
51             h = h.nextSibling;
52         }
53         return null;
54     },
55
56     prevVisible : function(h){
57         var v = this.view, cm = this.grid.colModel;
58         h = h.prevSibling;
59         while(h){
60             if(!cm.isHidden(v.getCellIndex(h))){
61                 return h;
62             }
63             h = h.prevSibling;
64         }
65         return null;
66     },
67
68     positionIndicator : function(h, n, e){
69         var x = Roo.lib.Event.getPageX(e);
70         var r = Roo.lib.Dom.getRegion(n.firstChild);
71         var px, pt, py = r.top + this.proxyOffsets[1];
72         if((r.right - x) <= (r.right-r.left)/2){
73             px = r.right+this.view.borderWidth;
74             pt = "after";
75         }else{
76             px = r.left;
77             pt = "before";
78         }
79         var oldIndex = this.view.getCellIndex(h);
80         var newIndex = this.view.getCellIndex(n);
81
82         if(this.grid.colModel.isFixed(newIndex)){
83             return false;
84         }
85
86         var locked = this.grid.colModel.isLocked(newIndex);
87
88         if(pt == "after"){
89             newIndex++;
90         }
91         if(oldIndex < newIndex){
92             newIndex--;
93         }
94         if(oldIndex == newIndex && (locked == this.grid.colModel.isLocked(oldIndex))){
95             return false;
96         }
97         px +=  this.proxyOffsets[0];
98         this.proxyTop.setLeftTop(px, py);
99         this.proxyTop.show();
100         if(!this.bottomOffset){
101             this.bottomOffset = this.view.mainHd.getHeight();
102         }
103         this.proxyBottom.setLeftTop(px, py+this.proxyTop.dom.offsetHeight+this.bottomOffset);
104         this.proxyBottom.show();
105         return pt;
106     },
107
108     onNodeEnter : function(n, dd, e, data){
109         if(data.header != n){
110             this.positionIndicator(data.header, n, e);
111         }
112     },
113
114     onNodeOver : function(n, dd, e, data){
115         var result = false;
116         if(data.header != n){
117             result = this.positionIndicator(data.header, n, e);
118         }
119         if(!result){
120             this.proxyTop.hide();
121             this.proxyBottom.hide();
122         }
123         return result ? this.dropAllowed : this.dropNotAllowed;
124     },
125
126     onNodeOut : function(n, dd, e, data){
127         this.proxyTop.hide();
128         this.proxyBottom.hide();
129     },
130
131     onNodeDrop : function(n, dd, e, data){
132         var h = data.header;
133         if(h != n){
134             var cm = this.grid.colModel;
135             var x = Roo.lib.Event.getPageX(e);
136             var r = Roo.lib.Dom.getRegion(n.firstChild);
137             var pt = (r.right - x) <= ((r.right-r.left)/2) ? "after" : "before";
138             var oldIndex = this.view.getCellIndex(h);
139             var newIndex = this.view.getCellIndex(n);
140             var locked = cm.isLocked(newIndex);
141             if(pt == "after"){
142                 newIndex++;
143             }
144             if(oldIndex < newIndex){
145                 newIndex--;
146             }
147             if(oldIndex == newIndex && (locked == cm.isLocked(oldIndex))){
148                 return false;
149             }
150             cm.setLocked(oldIndex, locked, true);
151             cm.moveColumn(oldIndex, newIndex);
152             this.grid.fireEvent("columnmove", oldIndex, newIndex);
153             return true;
154         }
155         return false;
156     }
157 });