source: Dev/branches/rest-dojo-ui/client/dojox/mdnd/Moveable.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 7.0 KB
Line 
1define([
2        "dojo/_base/kernel",
3        "dojo/_base/array",
4        "dojo/_base/connect",
5        "dojo/_base/declare",
6        "dojo/_base/event",
7        "dojo/_base/html",
8        "dojo/_base/sniff",
9        "dojo/_base/window"
10],function(dojo){
11        return dojo.declare(
12                "dojox.mdnd.Moveable",
13                null,
14        {
15                // summary:
16                //              Allow end-users to track a DOM node into the web page
17       
18                // handle: DOMNode
19                //              The node on which the user clicks to drag the main node.
20                handle: null,
21               
22                // skip: Boolean
23                //              A flag to control a drag action if a form element has been focused.
24                //              If true, the drag action is not executed.
25                skip: true,
26       
27                // dragDistance: Integer
28                //              The user clicks on the handle, but the drag action will really begin
29                //              if he tracks the main node to more than 3 pixels.
30                dragDistance: 3,
31               
32                constructor: function(/*Object*/params, /*DOMNode*/node){
33                        // summary:
34                        //              Configure parameters and listen to mousedown events from handle
35                        //              node.
36                        // params:
37                        //              Hash of parameters
38                        // node:
39                        //              The draggable node
40       
41                        //console.log("dojox.mdnd.Moveable ::: constructor");
42                        this.node = dojo.byId(node);
43                       
44                        this.d = this.node.ownerDocument;
45                       
46                        if(!params){ params = {}; }
47                        this.handle = params.handle ? dojo.byId(params.handle) : null;
48                        if(!this.handle){ this.handle = this.node; }
49                        this.skip = params.skip;
50                        this.events = [
51                                dojo.connect(this.handle, "onmousedown", this, "onMouseDown")
52                        ];
53                        if(dojox.mdnd.autoScroll){
54                                this.autoScroll = dojox.mdnd.autoScroll;
55                        }
56                       
57                },
58               
59                isFormElement: function(/*DOMEvent*/ e){
60                        // summary:
61                        //              identify the type of target node associated with a DOM event.
62                        // e:
63                        //              a DOM event
64                        // returns:
65                        //              if true, the target is one of those specific nodes.
66       
67                        //console.log("dojox.mdnd.Moveable ::: isFormElement");
68                        var t = e.target;
69                        if(t.nodeType == 3 /*TEXT_NODE*/){
70                                t = t.parentNode;
71                        }
72                        return " a button textarea input select option ".indexOf(" " + t.tagName.toLowerCase() + " ") >= 0;     // Boolean
73                },
74               
75                onMouseDown: function(/*DOMEvent*/e){
76                        // summary:
77                        //              Occurs when the user clicks on the handle node.
78                        //              Skip the drag action if a specific node is targeted.
79                        //              Listens to mouseup and mousemove events on to the HTML document.
80                        // e:
81                        //              a DOM event
82                        // tags:
83                        //              callback
84       
85                        //console.log("dojox.mdnd.Moveable ::: onMouseDown");
86                        if(this._isDragging){ return;}
87                        var isLeftButton = (e.which || e.button) == 1;
88                        if(!isLeftButton){
89                                return;
90                        }
91                        if(this.skip && this.isFormElement(e)){ return; }
92                        if(this.autoScroll){
93                                this.autoScroll.setAutoScrollNode(this.node);
94                                this.autoScroll.setAutoScrollMaxPage();
95                        }
96                        this.events.push(dojo.connect(this.d, "onmouseup", this, "onMouseUp"));
97                        this.events.push(dojo.connect(this.d, "onmousemove", this, "onFirstMove"));
98                        this._selectStart = dojo.connect(dojo.body(), "onselectstart", dojo.stopEvent);
99                        this._firstX = e.clientX;
100                        this._firstY = e.clientY;
101                        dojo.stopEvent(e);
102                },
103               
104                onFirstMove: function(/*DOMEvent*/e){
105                        // summary:
106                        //              Occurs when the user moves the mouse after clicking on the
107                        //              handle.
108                        //              Determinate when the drag action will have to begin (see
109                        //              dragDistance).
110                        // e:
111                        //              A DOM event
112                        // tags:
113                        //              callback
114       
115                        //console.log("dojox.mdnd.Moveable ::: onFirstMove");
116                        dojo.stopEvent(e);
117                        var d = (this._firstX - e.clientX) * (this._firstX - e.clientX)
118                                        + (this._firstY - e.clientY) * (this._firstY - e.clientY);
119                        if(d > this.dragDistance * this.dragDistance){
120                                this._isDragging = true;
121                                dojo.disconnect(this.events.pop());
122                                dojo.style(this.node, "width", dojo.contentBox(this.node).w + "px");
123                                this.initOffsetDrag(e);
124                                this.events.push(dojo.connect(this.d, "onmousemove", this, "onMove"));
125                        }
126                },
127               
128                initOffsetDrag: function(/*DOMEvent*/e){
129                        // summary:
130                        //              Initialize the gap between main node coordinates and the clicked point.
131                        //              Call the onDragStart method.
132                        // e:
133                        //              A DOM event
134       
135                        //console.log("dojox.mdnd.Moveable ::: initOffsetDrag");
136                        this.offsetDrag = { 'l': e.pageX, 't': e.pageY };
137                        var s = this.node.style;
138                        var position = dojo.position(this.node, true);
139                        /*if(s.position == "relative" || s.position == ""){
140                                s.position = "absolute"; // enforcing the absolute mode
141                        }*/
142                        this.offsetDrag.l = position.x - this.offsetDrag.l;
143                        this.offsetDrag.t = position.y - this.offsetDrag.t;
144                        var coords = {
145                                'x': position.x,
146                                'y': position.y
147                        };
148                        this.size = {
149                                'w': position.w,
150                                'h': position.h
151                        };
152                        // method to catch
153                        this.onDragStart(this.node, coords, this.size);
154                },
155               
156                onMove: function(/*DOMEvent*/e){
157                        // summary:
158                        //              Occurs when the user moves the mouse.
159                        //              Calls the onDrag method.
160                        // e:
161                        //              a DOM event
162                        // tags:
163                        //              callback
164       
165                        //console.log("dojox.mdnd.Moveable ::: onMove");
166                        dojo.stopEvent(e);
167                        // hack to avoid too many calls to onMove in IE8 causing sometimes slowness
168                        if(dojo.isIE == 8 && new Date() - this.date < 20){
169                                return;
170                        }
171                        if(this.autoScroll){
172                                this.autoScroll.checkAutoScroll(e);
173                        }
174                        var coords = {
175                                'x': this.offsetDrag.l + e.pageX,
176                                'y': this.offsetDrag.t + e.pageY
177                        };
178                        var s = this.node.style;
179                        s.left = coords.x + "px";
180                        s.top = coords.y + "px";
181                       
182                        // method to catch
183                        this.onDrag(this.node, coords, this.size, {'x':e.pageX, 'y':e.pageY});
184                        if(dojo.isIE == 8){
185                                this.date = new Date();
186                        }
187                },
188               
189                onMouseUp: function(/*DOMEvent*/e){
190                        // summary:
191                        //              Occurs when the user releases the mouse
192                        //              Calls the onDragEnd method.
193                        // e:
194                        //              a DOM event
195       
196                        if (this._isDragging){
197                                dojo.stopEvent(e);
198                                this._isDragging = false;
199                                if(this.autoScroll){
200                                        this.autoScroll.stopAutoScroll();
201                                }
202                                delete this.onMove;
203                                this.onDragEnd(this.node);
204                                this.node.focus();
205                        }
206                        dojo.disconnect(this.events.pop());
207                        dojo.disconnect(this.events.pop());
208                },
209               
210                onDragStart: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size){
211                        // summary:
212                        //              Stub function.
213                        //              Notes : border box model
214                        // node:
215                        //              a DOM node
216                        //      coords:
217                        //              absolute position of the main node
218                        // size:
219                        //              an object encapsulating width an height values
220                        // tags:
221                        //              callback
222       
223                },
224               
225                onDragEnd: function(/*DOMNode*/node){
226                        // summary:
227                        //              Stub function
228                        //              Notes : Coordinates don't contain margins
229                        // node:
230                        //              a DOM node
231                        // tags:
232                        //              callback
233       
234                },
235               
236                onDrag: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size, /*Object*/mousePosition){
237                        // summary:
238                        //              Stub function.
239                        //              Notes : border box model for size value, margin box model for coordinates
240                        // node:
241                        //              a DOM node
242                        // coords:
243                        //              position of the main node (equals to css left/top properties)
244                        // size:
245                        //              an object encapsulating width and height values
246                        // mousePosition:
247                        //              coordiantes of mouse
248                        // tags:
249                        //              callback
250       
251                },
252       
253                destroy: function(){
254                        // summary:
255                        //              Delecte associated events
256       
257                        // console.log("dojox.mdnd.Moveable ::: destroy");
258                        dojo.forEach(this.events, dojo.disconnect);
259                        this.events = this.node = null;
260                }
261        });
262});
Note: See TracBrowser for help on using the repository browser.