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    
22     onBeforeDrag : function(data, e){
23         var n = data.node;
24         return n && n.draggable && !n.disabled;
25     },
26      
27     
28     onInitDrag : function(e){
29         var data = this.dragData;
30         this.tree.getSelectionModel().select(data.node);
31         this.proxy.update("");
32         data.node.ui.appendDDGhost(this.proxy.ghost.dom);
33         this.tree.fireEvent("startdrag", this.tree, data.node, e);
34     },
35     
36     getRepairXY : function(e, data){
37         return data.node.ui.getDDRepairXY();
38     },
39     
40     onEndDrag : function(data, e){
41         this.tree.fireEvent("enddrag", this.tree, data.node, e);
42         
43         
44     },
45     
46     onValidDrop : function(dd, e, id){
47         this.tree.fireEvent("dragdrop", this.tree, this.dragData.node, dd, e);
48         this.hideProxy();
49     },
50     
51     beforeInvalidDrop : function(e, id){
52         // this scrolls the original position back into view
53         var sm = this.tree.getSelectionModel();
54         sm.clearSelections();
55         sm.select(this.dragData.node);
56     },
57     autoScroll: function(x, y, h, w) {
58         Roo.log("drop zone - autoscroll called");
59         
60         Roo.log( [ x, y, h , w, this.scroll ] );
61         Roo.log(this.el.getBox());
62         
63         if (this.scroll) {
64              // The client height
65             var clientH = Roo.lib.Dom.getViewWidth();
66
67             // The client width
68             var clientW = Roo.lib.Dom.getViewHeight();
69
70             // The amt scrolled down
71             var st = this.DDM.getScrollTop();
72
73             // The amt scrolled right
74             var sl = this.DDM.getScrollLeft();
75
76             // Location of the bottom of the element
77             var bot = h + y;
78
79             // Location of the right of the element
80             var right = w + x;
81
82             // The distance from the cursor to the bottom of the visible area,
83             // adjusted so that we don't scroll if the cursor is beyond the
84             // element drag constraints
85             var toBot = (clientH + st - y - this.deltaY);
86
87             // The distance from the cursor to the right of the visible area
88             var toRight = (clientW + sl - x - this.deltaX);
89
90
91             // How close to the edge the cursor must be before we scroll
92             // var thresh = (document.all) ? 100 : 40;
93             var thresh = 40;
94
95             // How many pixels to scroll per autoscroll op.  This helps to reduce
96             // clunky scrolling. IE is more sensitive about this ... it needs this
97             // value to be higher.
98             var scrAmt = (document.all) ? 80 : 30;
99
100             // Scroll down if we are near the bottom of the visible page and the
101             // obj extends below the crease
102             if ( bot > clientH && toBot < thresh ) {
103                 this.el.scrollTo(sl, st + scrAmt);
104             }
105
106             // Scroll up if the window is scrolled down and the top of the object
107             // goes above the top border
108             if ( y < st && st > 0 && y - st < thresh ) {
109                 this.el.scrollTo(sl, st - scrAmt);
110             }
111
112             // Scroll right if the obj is beyond the right border and the cursor is
113             // near the border.
114             if ( right > clientW && toRight < thresh ) {
115                 this.el.scrollTo(sl + scrAmt, st);
116             }
117
118             // Scroll left if the window has been scrolled to the right and the obj
119             // extends past the left border
120             if ( x < sl && sl > 0 && x - sl < thresh ) {
121                 this.el.scrollTo(sl - scrAmt, st);
122             }
123         }
124     }
125 });
126 }