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         if (this.scroll) {
62              // The client height
63             var clientH = Roo.lib.Dom.getViewWidth();
64
65             // The client width
66             var clientW = Roo.lib.Dom.getViewHeight();
67
68             // The amt scrolled down
69             var st = this.DDM.getScrollTop();
70
71             // The amt scrolled right
72             var sl = this.DDM.getScrollLeft();
73
74             // Location of the bottom of the element
75             var bot = h + y;
76
77             // Location of the right of the element
78             var right = w + x;
79
80             // The distance from the cursor to the bottom of the visible area,
81             // adjusted so that we don't scroll if the cursor is beyond the
82             // element drag constraints
83             var toBot = (clientH + st - y - this.deltaY);
84
85             // The distance from the cursor to the right of the visible area
86             var toRight = (clientW + sl - x - this.deltaX);
87
88
89             // How close to the edge the cursor must be before we scroll
90             // var thresh = (document.all) ? 100 : 40;
91             var thresh = 40;
92
93             // How many pixels to scroll per autoscroll op.  This helps to reduce
94             // clunky scrolling. IE is more sensitive about this ... it needs this
95             // value to be higher.
96             var scrAmt = (document.all) ? 80 : 30;
97
98             // Scroll down if we are near the bottom of the visible page and the
99             // obj extends below the crease
100             if ( bot > clientH && toBot < thresh ) {
101                 this.el.scrollTo(sl, st + scrAmt);
102             }
103
104             // Scroll up if the window is scrolled down and the top of the object
105             // goes above the top border
106             if ( y < st && st > 0 && y - st < thresh ) {
107                 this.el.scrollTo(sl, st - scrAmt);
108             }
109
110             // Scroll right if the obj is beyond the right border and the cursor is
111             // near the border.
112             if ( right > clientW && toRight < thresh ) {
113                 this.el.scrollTo(sl + scrAmt, st);
114             }
115
116             // Scroll left if the window has been scrolled to the right and the obj
117             // extends past the left border
118             if ( x < sl && sl > 0 && x - sl < thresh ) {
119                 this.el.scrollTo(sl - scrAmt, st);
120             }
121         }
122     }
123 });
124 }