Roo/tree/TreeDragZone.js
[roojs1] / Roo / tree / TreeDragZone.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  
12
13 if(Roo.dd.DragZone){
14 Roo.tree.TreeDragZone = function(tree, config){
15     Roo.tree.TreeDragZone.superclass.constructor.call(this, tree.getTreeEl(), config);
16     this.tree = tree;
17 };
18
19 Roo.extend(Roo.tree.TreeDragZone, Roo.dd.DragZone, {
20     ddGroup : "TreeDD",
21     scroller : false, // 't' or 'b'
22     scroll: true,
23     
24     onBeforeDrag : function(data, e){
25         var n = data.node;
26         return n && n.draggable && !n.disabled;
27     },
28     
29     onDragOut : function(e, id) 
30     {
31         
32           Roo.log('ondragout');
33         var ret = Roo.tree.TreeDragZone.superclass.constructor.prototype.onDragOut.call(this, e, id);
34         
35         // if it's gone off top and bottom, start the scroller
36        
37         this.scrollDir = 't';
38         
39         if (this.scroller === false) {
40             Roo.log('start interval');
41             this.scroller = window.setInterval(
42                 this.scrollAct.createDelegate(this),
43                 500
44             );
45         }
46         
47         //Roo.log(this.el.dom);
48         
49         return ret;
50     },
51     onDragEnter : function(e, id) 
52     {
53         var ret = Roo.tree.TreeDragZone.superclass.constructor.prototype.onDragEnter.call(this, e, id);
54         Roo.log('ondrageneter');
55         if (this.scroller !== false) {
56             Roo.log('clear scroller');
57             window.clearInterval(this.scroller);
58             this.scroller =false;
59             
60         }
61         
62         //
63         
64         
65         //Roo.log(this);
66         
67         return ret;
68     },
69     
70     scrollAct: function()
71     {
72         Roo.log('scrollAct');
73         if (this.scrollDir === false) {
74             return;
75         }
76         Roo.log('scroll!');
77         this.el.scroll(this.scrollDir, 20, true);
78         
79         
80     },
81     
82     onInitDrag : function(e){
83         var data = this.dragData;
84         this.tree.getSelectionModel().select(data.node);
85         this.proxy.update("");
86         data.node.ui.appendDDGhost(this.proxy.ghost.dom);
87         this.tree.fireEvent("startdrag", this.tree, data.node, e);
88     },
89     
90     getRepairXY : function(e, data){
91         return data.node.ui.getDDRepairXY();
92     },
93     
94     onEndDrag : function(data, e){
95         this.tree.fireEvent("enddrag", this.tree, data.node, e);
96         if (this.scroller !== false) {
97             Roo.log('clear scroller');
98             window.clearInterval(this.scroller);
99             this.scroller =false;
100             
101         }
102         
103     },
104     
105     onValidDrop : function(dd, e, id){
106         this.tree.fireEvent("dragdrop", this.tree, this.dragData.node, dd, e);
107         this.hideProxy();
108     },
109     
110     beforeInvalidDrop : function(e, id){
111         // this scrolls the original position back into view
112         var sm = this.tree.getSelectionModel();
113         sm.clearSelections();
114         sm.select(this.dragData.node);
115     },
116     autoScroll: function(x, y, h, w) {
117         Roo.log("drop zone - autoscroll called");
118         
119         Roo.log(this.scroll ? "scroll=y": "scroll=m" );
120         if (this.scroll) {
121             // The client height
122             var clientH = Roo.lib.Dom.getViewWidth();
123
124             // The client width
125             var clientW = Roo.lib.Dom.getViewHeight();
126
127             // The amt scrolled down
128             var st = this.DDM.getScrollTop();
129
130             // The amt scrolled right
131             var sl = this.DDM.getScrollLeft();
132
133             // Location of the bottom of the element
134             var bot = h + y;
135
136             // Location of the right of the element
137             var right = w + x;
138
139             // The distance from the cursor to the bottom of the visible area,
140             // adjusted so that we don't scroll if the cursor is beyond the
141             // element drag constraints
142             var toBot = (clientH + st - y - this.deltaY);
143
144             // The distance from the cursor to the right of the visible area
145             var toRight = (clientW + sl - x - this.deltaX);
146
147
148             // How close to the edge the cursor must be before we scroll
149             // var thresh = (document.all) ? 100 : 40;
150             var thresh = 40;
151
152             // How many pixels to scroll per autoscroll op.  This helps to reduce
153             // clunky scrolling. IE is more sensitive about this ... it needs this
154             // value to be higher.
155             var scrAmt = (document.all) ? 80 : 30;
156
157             // Scroll down if we are near the bottom of the visible page and the
158             // obj extends below the crease
159             if ( bot > clientH && toBot < thresh ) {
160                 window.scrollTo(sl, st + scrAmt);
161             }
162
163             // Scroll up if the window is scrolled down and the top of the object
164             // goes above the top border
165             if ( y < st && st > 0 && y - st < thresh ) {
166                 window.scrollTo(sl, st - scrAmt);
167             }
168
169             // Scroll right if the obj is beyond the right border and the cursor is
170             // near the border.
171             if ( right > clientW && toRight < thresh ) {
172                 window.scrollTo(sl + scrAmt, st);
173             }
174
175             // Scroll left if the window has been scrolled to the right and the obj
176             // extends past the left border
177             if ( x < sl && sl > 0 && x - sl < thresh ) {
178                 window.scrollTo(sl - scrAmt, st);
179             }
180         }
181     }
182 });
183 }