]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/dnd/Mover.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / dnd / Mover.js
1 if(!dojo._hasResource["dojo.dnd.Mover"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.dnd.Mover"] = true;
3 dojo.provide("dojo.dnd.Mover");
4
5 dojo.require("dojo.dnd.common");
6 dojo.require("dojo.dnd.autoscroll");
7
8 dojo.declare("dojo.dnd.Mover", null, {
9         constructor: function(node, e, host){
10                 // summary: an object, which makes a node follow the mouse, 
11                 //      used as a default mover, and as a base class for custom movers
12                 // node: Node: a node (or node's id) to be moved
13                 // e: Event: a mouse event, which started the move;
14                 //      only pageX and pageY properties are used
15                 // host: Object?: object which implements the functionality of the move,
16                 //       and defines proper events (onMoveStart and onMoveStop)
17                 this.node = dojo.byId(node);
18                 this.marginBox = {l: e.pageX, t: e.pageY};
19                 this.mouseButton = e.button;
20                 var h = this.host = host, d = node.ownerDocument, 
21                         firstEvent = dojo.connect(d, "onmousemove", this, "onFirstMove");
22                 this.events = [
23                         dojo.connect(d, "onmousemove", this, "onMouseMove"),
24                         dojo.connect(d, "onmouseup",   this, "onMouseUp"),
25                         // cancel text selection and text dragging
26                         dojo.connect(d, "ondragstart",   dojo, "stopEvent"),
27                         dojo.connect(d, "onselectstart", dojo, "stopEvent"),
28                         firstEvent
29                 ];
30                 // notify that the move has started
31                 if(h && h.onMoveStart){
32                         h.onMoveStart(this);
33                 }
34         },
35         // mouse event processors
36         onMouseMove: function(e){
37                 // summary: event processor for onmousemove
38                 // e: Event: mouse event
39                 dojo.dnd.autoScroll(e);
40                 var m = this.marginBox;
41                 this.host.onMove(this, {l: m.l + e.pageX, t: m.t + e.pageY});
42         },
43         onMouseUp: function(e){
44                 if(this.mouseButton == e.button){
45                         this.destroy();
46                 }
47         },
48         // utilities
49         onFirstMove: function(){
50                 // summary: makes the node absolute; it is meant to be called only once
51                 var s = this.node.style, l, t;
52                 switch(s.position){
53                         case "relative":
54                         case "absolute":
55                                 // assume that left and top values are in pixels already
56                                 l = Math.round(parseFloat(s.left));
57                                 t = Math.round(parseFloat(s.top));
58                                 break;
59                         default:
60                                 s.position = "absolute";        // enforcing the absolute mode
61                                 var m = dojo.marginBox(this.node);
62                                 l = m.l;
63                                 t = m.t;
64                                 break;
65                 }
66                 this.marginBox.l = l - this.marginBox.l;
67                 this.marginBox.t = t - this.marginBox.t;
68                 this.host.onFirstMove(this);
69                 dojo.disconnect(this.events.pop());
70         },
71         destroy: function(){
72                 // summary: stops the move, deletes all references, so the object can be garbage-collected
73                 dojo.forEach(this.events, dojo.disconnect);
74                 // undo global settings
75                 var h = this.host;
76                 if(h && h.onMoveStop){
77                         h.onMoveStop(this);
78                 }
79                 // destroy objects
80                 this.events = this.node = null;
81         }
82 });
83
84 }