Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / grid / ColumnDD.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 // private\r
8 // This is a support class used internally by the Grid components\r
9 Ext.grid.HeaderDragZone = function(grid, hd, hd2){\r
10     this.grid = grid;\r
11     this.view = grid.getView();\r
12     this.ddGroup = "gridHeader" + this.grid.getGridEl().id;\r
13     Ext.grid.HeaderDragZone.superclass.constructor.call(this, hd);\r
14     if(hd2){\r
15         this.setHandleElId(Ext.id(hd));\r
16         this.setOuterHandleElId(Ext.id(hd2));\r
17     }\r
18     this.scroll = false;\r
19 };\r
20 Ext.extend(Ext.grid.HeaderDragZone, Ext.dd.DragZone, {\r
21     maxDragWidth: 120,\r
22     getDragData : function(e){\r
23         var t = Ext.lib.Event.getTarget(e);\r
24         var h = this.view.findHeaderCell(t);\r
25         if(h){\r
26             return {ddel: h.firstChild, header:h};\r
27         }\r
28         return false;\r
29     },\r
30 \r
31     onInitDrag : function(e){\r
32         this.view.headersDisabled = true;\r
33         var clone = this.dragData.ddel.cloneNode(true);\r
34         clone.id = Ext.id();\r
35         clone.style.width = Math.min(this.dragData.header.offsetWidth,this.maxDragWidth) + "px";\r
36         this.proxy.update(clone);\r
37         return true;\r
38     },\r
39 \r
40     afterValidDrop : function(){\r
41         var v = this.view;\r
42         setTimeout(function(){\r
43             v.headersDisabled = false;\r
44         }, 50);\r
45     },\r
46 \r
47     afterInvalidDrop : function(){\r
48         var v = this.view;\r
49         setTimeout(function(){\r
50             v.headersDisabled = false;\r
51         }, 50);\r
52     }\r
53 });\r
54 \r
55 // private\r
56 // This is a support class used internally by the Grid components\r
57 Ext.grid.HeaderDropZone = function(grid, hd, hd2){\r
58     this.grid = grid;\r
59     this.view = grid.getView();\r
60     // split the proxies so they don't interfere with mouse events\r
61     this.proxyTop = Ext.DomHelper.append(document.body, {\r
62         cls:"col-move-top", html:" "\r
63     }, true);\r
64     this.proxyBottom = Ext.DomHelper.append(document.body, {\r
65         cls:"col-move-bottom", html:" "\r
66     }, true);\r
67     this.proxyTop.hide = this.proxyBottom.hide = function(){\r
68         this.setLeftTop(-100,-100);\r
69         this.setStyle("visibility", "hidden");\r
70     };\r
71     this.ddGroup = "gridHeader" + this.grid.getGridEl().id;\r
72     // temporarily disabled\r
73     //Ext.dd.ScrollManager.register(this.view.scroller.dom);\r
74     Ext.grid.HeaderDropZone.superclass.constructor.call(this, grid.getGridEl().dom);\r
75 };\r
76 Ext.extend(Ext.grid.HeaderDropZone, Ext.dd.DropZone, {\r
77     proxyOffsets : [-4, -9],\r
78     fly: Ext.Element.fly,\r
79 \r
80     getTargetFromEvent : function(e){\r
81         var t = Ext.lib.Event.getTarget(e);\r
82         var cindex = this.view.findCellIndex(t);\r
83         if(cindex !== false){\r
84             return this.view.getHeaderCell(cindex);\r
85         }\r
86     },\r
87 \r
88     nextVisible : function(h){\r
89         var v = this.view, cm = this.grid.colModel;\r
90         h = h.nextSibling;\r
91         while(h){\r
92             if(!cm.isHidden(v.getCellIndex(h))){\r
93                 return h;\r
94             }\r
95             h = h.nextSibling;\r
96         }\r
97         return null;\r
98     },\r
99 \r
100     prevVisible : function(h){\r
101         var v = this.view, cm = this.grid.colModel;\r
102         h = h.prevSibling;\r
103         while(h){\r
104             if(!cm.isHidden(v.getCellIndex(h))){\r
105                 return h;\r
106             }\r
107             h = h.prevSibling;\r
108         }\r
109         return null;\r
110     },\r
111 \r
112     positionIndicator : function(h, n, e){\r
113         var x = Ext.lib.Event.getPageX(e);\r
114         var r = Ext.lib.Dom.getRegion(n.firstChild);\r
115         var px, pt, py = r.top + this.proxyOffsets[1];\r
116         if((r.right - x) <= (r.right-r.left)/2){\r
117             px = r.right+this.view.borderWidth;\r
118             pt = "after";\r
119         }else{\r
120             px = r.left;\r
121             pt = "before";\r
122         }\r
123 \r
124         if(this.grid.colModel.isFixed(this.view.getCellIndex(n))){\r
125             return false;\r
126         }\r
127 \r
128         px +=  this.proxyOffsets[0];\r
129         this.proxyTop.setLeftTop(px, py);\r
130         this.proxyTop.show();\r
131         if(!this.bottomOffset){\r
132             this.bottomOffset = this.view.mainHd.getHeight();\r
133         }\r
134         this.proxyBottom.setLeftTop(px, py+this.proxyTop.dom.offsetHeight+this.bottomOffset);\r
135         this.proxyBottom.show();\r
136         return pt;\r
137     },\r
138 \r
139     onNodeEnter : function(n, dd, e, data){\r
140         if(data.header != n){\r
141             this.positionIndicator(data.header, n, e);\r
142         }\r
143     },\r
144 \r
145     onNodeOver : function(n, dd, e, data){\r
146         var result = false;\r
147         if(data.header != n){\r
148             result = this.positionIndicator(data.header, n, e);\r
149         }\r
150         if(!result){\r
151             this.proxyTop.hide();\r
152             this.proxyBottom.hide();\r
153         }\r
154         return result ? this.dropAllowed : this.dropNotAllowed;\r
155     },\r
156 \r
157     onNodeOut : function(n, dd, e, data){\r
158         this.proxyTop.hide();\r
159         this.proxyBottom.hide();\r
160     },\r
161 \r
162     onNodeDrop : function(n, dd, e, data){\r
163         var h = data.header;\r
164         if(h != n){\r
165             var cm = this.grid.colModel;\r
166             var x = Ext.lib.Event.getPageX(e);\r
167             var r = Ext.lib.Dom.getRegion(n.firstChild);\r
168             var pt = (r.right - x) <= ((r.right-r.left)/2) ? "after" : "before";\r
169             var oldIndex = this.view.getCellIndex(h);\r
170             var newIndex = this.view.getCellIndex(n);\r
171             if(pt == "after"){\r
172                 newIndex++;\r
173             }\r
174             if(oldIndex < newIndex){\r
175                 newIndex--;\r
176             }\r
177             cm.moveColumn(oldIndex, newIndex);\r
178             return true;\r
179         }\r
180         return false;\r
181     }\r
182 });\r
183 \r
184 \r
185 Ext.grid.GridView.ColumnDragZone = function(grid, hd){\r
186     Ext.grid.GridView.ColumnDragZone.superclass.constructor.call(this, grid, hd, null);\r
187     this.proxy.el.addClass('x-grid3-col-dd');\r
188 };\r
189 \r
190 Ext.extend(Ext.grid.GridView.ColumnDragZone, Ext.grid.HeaderDragZone, {\r
191     handleMouseDown : function(e){\r
192 \r
193     },\r
194 \r
195     callHandleMouseDown : function(e){\r
196         Ext.grid.GridView.ColumnDragZone.superclass.handleMouseDown.call(this, e);\r
197     }\r
198 });