Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojo.dnd.Manager"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojo.dnd.Manager"] = true;
3
dojo.provide("dojo.dnd.Manager");
4
 
5
dojo.require("dojo.dnd.common");
6
dojo.require("dojo.dnd.autoscroll");
7
dojo.require("dojo.dnd.Avatar");
8
 
9
dojo.dnd.Manager = function(){
10
	// summary: the manager of DnD operations (usually a singleton)
11
	this.avatar  = null;
12
	this.source = null;
13
	this.nodes = [];
14
	this.copy  = true;
15
	this.target = null;
16
	this.canDropFlag = false;
17
	this.events = [];
18
};
19
 
20
dojo.extend(dojo.dnd.Manager, {
21
	// avatar's offset from the mouse
22
	OFFSET_X: 16,
23
	OFFSET_Y: 16,
24
	// methods
25
	overSource: function(source){
26
		// summary: called when a source detected a mouse-over conditiion
27
		// source: Object: the reporter
28
		if(this.avatar){
29
			this.target = (source && source.targetState != "Disabled") ? source : null;
30
			this.avatar.update();
31
		}
32
		dojo.publish("/dnd/source/over", [source]);
33
	},
34
	outSource: function(source){
35
		// summary: called when a source detected a mouse-out conditiion
36
		// source: Object: the reporter
37
		if(this.avatar){
38
			if(this.target == source){
39
				this.target = null;
40
				this.canDropFlag = false;
41
				this.avatar.update();
42
				dojo.publish("/dnd/source/over", [null]);
43
			}
44
		}else{
45
			dojo.publish("/dnd/source/over", [null]);
46
		}
47
	},
48
	startDrag: function(source, nodes, copy){
49
		// summary: called to initiate the DnD operation
50
		// source: Object: the source which provides items
51
		// nodes: Array: the list of transferred items
52
		// copy: Boolean: copy items, if true, move items otherwise
53
		this.source = source;
54
		this.nodes  = nodes;
55
		this.copy   = Boolean(copy); // normalizing to true boolean
56
		this.avatar = this.makeAvatar();
57
		dojo.body().appendChild(this.avatar.node);
58
		dojo.publish("/dnd/start", [source, nodes, this.copy]);
59
		this.events = [
60
			dojo.connect(dojo.doc, "onmousemove", this, "onMouseMove"),
61
			dojo.connect(dojo.doc, "onmouseup",   this, "onMouseUp"),
62
			dojo.connect(dojo.doc, "onkeydown",   this, "onKeyDown"),
63
			dojo.connect(dojo.doc, "onkeyup",     this, "onKeyUp")
64
		];
65
		var c = "dojoDnd" + (copy ? "Copy" : "Move");
66
		dojo.addClass(dojo.body(), c);
67
	},
68
	canDrop: function(flag){
69
		// summary: called to notify if the current target can accept items
70
		var canDropFlag = this.target && flag;
71
		if(this.canDropFlag != canDropFlag){
72
			this.canDropFlag = canDropFlag;
73
			this.avatar.update();
74
		}
75
	},
76
	stopDrag: function(){
77
		// summary: stop the DnD in progress
78
		dojo.removeClass(dojo.body(), "dojoDndCopy");
79
		dojo.removeClass(dojo.body(), "dojoDndMove");
80
		dojo.forEach(this.events, dojo.disconnect);
81
		this.events = [];
82
		this.avatar.destroy();
83
		this.avatar = null;
84
		this.source = null;
85
		this.nodes = [];
86
	},
87
	makeAvatar: function(){
88
		// summary: makes the avatar, it is separate to be overwritten dynamically, if needed
89
		return new dojo.dnd.Avatar(this);
90
	},
91
	updateAvatar: function(){
92
		// summary: updates the avatar, it is separate to be overwritten dynamically, if needed
93
		this.avatar.update();
94
	},
95
	// mouse event processors
96
	onMouseMove: function(e){
97
		// summary: event processor for onmousemove
98
		// e: Event: mouse event
99
		var a = this.avatar;
100
		if(a){
101
			//dojo.dnd.autoScrollNodes(e);
102
			dojo.dnd.autoScroll(e);
103
			dojo.marginBox(a.node, {l: e.pageX + this.OFFSET_X, t: e.pageY + this.OFFSET_Y});
104
			var copy = Boolean(this.source.copyState(dojo.dnd.getCopyKeyState(e)));
105
			if(this.copy != copy){
106
				this._setCopyStatus(copy);
107
			}
108
		}
109
	},
110
	onMouseUp: function(e){
111
		// summary: event processor for onmouseup
112
		// e: Event: mouse event
113
		if(this.avatar && (!("mouseButton" in this.source) || this.source.mouseButton == e.button)){
114
			if(this.target && this.canDropFlag){
115
				var params = [this.source, this.nodes, Boolean(this.source.copyState(dojo.dnd.getCopyKeyState(e))), this.target];
116
				dojo.publish("/dnd/drop/before", params);
117
				dojo.publish("/dnd/drop", params);
118
			}else{
119
				dojo.publish("/dnd/cancel");
120
			}
121
			this.stopDrag();
122
		}
123
	},
124
	// keyboard event processors
125
	onKeyDown: function(e){
126
		// summary: event processor for onkeydown:
127
		//	watching for CTRL for copy/move status, watching for ESCAPE to cancel the drag
128
		// e: Event: keyboard event
129
		if(this.avatar){
130
			switch(e.keyCode){
131
				case dojo.keys.CTRL:
132
					var copy = Boolean(this.source.copyState(true));
133
					if(this.copy != copy){
134
						this._setCopyStatus(copy);
135
					}
136
					break;
137
				case dojo.keys.ESCAPE:
138
					dojo.publish("/dnd/cancel");
139
					this.stopDrag();
140
					break;
141
			}
142
		}
143
	},
144
	onKeyUp: function(e){
145
		// summary: event processor for onkeyup, watching for CTRL for copy/move status
146
		// e: Event: keyboard event
147
		if(this.avatar && e.keyCode == dojo.keys.CTRL){
148
			var copy = Boolean(this.source.copyState(false));
149
			if(this.copy != copy){
150
				this._setCopyStatus(copy);
151
			}
152
		}
153
	},
154
	// utilities
155
	_setCopyStatus: function(copy){
156
		// summary: changes the copy status
157
		// copy: Boolean: the copy status
158
		this.copy = copy;
159
		this.source._markDndStatus(this.copy);
160
		this.updateAvatar();
161
		dojo.removeClass(dojo.body(), "dojoDnd" + (this.copy ? "Move" : "Copy"));
162
		dojo.addClass(dojo.body(), "dojoDnd" + (this.copy ? "Copy" : "Move"));
163
	}
164
});
165
 
166
// summary: the manager singleton variable, can be overwritten, if needed
167
dojo.dnd._manager = null;
168
 
169
dojo.dnd.manager = function(){
170
	// summary: returns the current DnD manager, creates one if it is not created yet
171
	if(!dojo.dnd._manager){
172
		dojo.dnd._manager = new dojo.dnd.Manager();
173
	}
174
	return dojo.dnd._manager;	// Object
175
};
176
 
177
}