source: Dev/trunk/src/client/dojox/mdnd/Moveable.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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