summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/mdnd/adapter/DndToDojo.xd.js
blob: 8e0e139808decf139750c7c604460cb53f76b4a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.mdnd.adapter.DndToDojo"],
["require", "dojox.mdnd.PureSource"],
["require", "dojox.mdnd.LazyManager"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.mdnd.adapter.DndToDojo"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.mdnd.adapter.DndToDojo"] = true;
dojo.provide("dojox.mdnd.adapter.DndToDojo");

dojo.require("dojox.mdnd.PureSource");
dojo.require("dojox.mdnd.LazyManager");

dojo.declare(
	"dojox.mdnd.adapter.DndToDojo",
	null,
{
	// summary:
	//		Allow communication between an item of dojox D&D area to a target dojo.

	// _dojoList: Array
	//		Array containing object references the dojo Target list
	_dojoList: null,

	// _currentDojoArea: DOMNode
	//		Representing the current dojo area
	_currentDojoArea: null,

	// _dojoxManager: dojox.mdnd.AreaManager
	//		The reference to the dojox AreaManager
	_dojoxManager: null,

	// _dragStartHandler: Object
	//		Handle to keep start subscribe
	_dragStartHandler: null,

	// _dropHandler: Object
	//		Handle to keep drop subscribe
	_dropHandler: null,

	// _moveHandler: Object
	//		Handle to keep move subscribe
	_moveHandler: null,

	// _moveUpHandler: Object
	//		Handle to kee move up subscribe
	_moveUpHandler: null,

	// _draggedNode: DOMNode
	// 		The current dragged node
	_draggedNode: null,

	constructor: function(){
		this._dojoList = [];
		this._currentDojoArea = null;
		this._dojoxManager = dojox.mdnd.areaManager();
		this._dragStartHandler = dojo.subscribe("/dojox/mdnd/drag/start", this, function(node, sourceArea, sourceDropIndex){
			this._draggedNode = node;
			this._moveHandler = dojo.connect(dojo.doc, "onmousemove", this, "onMouseMove");
		});
		this._dropHandler = dojo.subscribe("/dojox/mdnd/drop", this, function(node, targetArea, indexChild){
			if(this._currentDojoArea){
				dojo.publish("/dojox/mdnd/adapter/dndToDojo/cancel", [this._currentDojoArea.node, this._currentDojoArea.type, this._draggedNode, this.accept]);
			}
			this._draggedNode = null;
			this._currentDojoArea = null;
			dojo.disconnect(this._moveHandler);
		});
	},

	_getIndexDojoArea: function(/*node*/area){
		// summary:
		//		Check if a dojo area is registered.
		// area: DOMNode
		//		A node corresponding to the target dojo.
		// returns:
		//		The index of area if it's registered else -1.
		// tags:
		//		protected

		//console.log('dojox.mdnd.adapter.DndToDojo ::: _getIndexDojoArea');
		if(area){
			for(var i = 0, l = this._dojoList.length; i < l; i++){
				if(this._dojoList[i].node === area){
					return i;
				}
			}
		}
		return -1;
	},

	_initCoordinates: function(/*DOMNode*/area){
		// summary:
		//		Initialize the coordinates of the target dojo.
		// area:
		//		A registered DOM node.
		// returns:
		//		An object which contains coordinates : *{x:0,y:,x1:0,y1:0}*
		// tags:
		//		protected

 		//console.log('dojox.mdnd.adapter.DndToDojo ::: _initCoordinates');
		if(area){
			var position = dojo.position(area, true),
				coords = {};
			coords.x = position.x
			coords.y = position.y
			coords.x1 = position.x + position.w;
			coords.y1 = position.y + position.h;
			return coords;	// 	Object
		}
		return null;
	},

	register: function(/*DOMNode*/area, /*String*/ type,/*Boolean*/ dojoTarget){
		// summary:
		//		Register a target dojo.
		//		The target is represented by an object containing :
		// 			- the dojo area node
		// 			- the type reference to identify a group node
		// 			- the coords of the area to enable refresh position
		// area:
		//		The DOM node which has to be registered.
		// type:
		//		A String to identify the node.
		// dojoTarger:
		//		True if the dojo D&D have to be enable when mouse is hover the registered target dojo.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: registerDojoArea", area, type, dojoTarget);
		if(this._getIndexDojoArea(area) == -1){
			var coords = this._initCoordinates(area),
				object = {
					'node': area,
					'type': type,
					'dojo': (dojoTarget)?dojoTarget:false,
					'coords': coords
				};
			this._dojoList.push(object);
			// initialization of the _fakeSource to allow Dnd switching
			if(dojoTarget && !this._lazyManager){
				this._lazyManager = new dojox.mdnd.LazyManager();
			}
		}
	},

	unregisterByNode: function(/*DOMNode*/area){
		// summary:
		//		Unregister a target dojo.
		// area:
		//		The DOM node of target dojo.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: unregisterByNode", area);
		var index = this._getIndexDojoArea(area);
		// if area is registered
		if(index != -1){
			this._dojoList.splice(index, 1);
		}
	},

	unregisterByType: function(/*String*/type){
		// summary:
		//		Unregister several targets dojo having the same type passing in parameter.
		// type:
		//		A String to identify dojo targets.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: unregisterByType", type);
		if(type){
			var tempList = [];
			dojo.forEach(this._dojoList, function(item, i){
				if(item.type != type){
					tempList.push(item);
				}
			});
			this._dojoList = tempList;
		}
	},

	unregister: function(){
		// summary:
		//		Unregister all targets dojo.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: unregister");
		this._dojoList = [];
	},

	refresh: function(){
		// summary:
		//		Refresh the coordinates of all registered dojo target.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: refresh");
		var dojoList = this._dojoList;
		this.unregister();
		dojo.forEach(dojoList, function(dojo){
			dojo.coords = this._initCoordinates(dojo.node);
		}, this);
		this._dojoList = dojoList;
	},

	refreshByType: function(/*String*/ type){
		// summary:
		//		Refresh the coordinates of registered dojo target with a specific type.
		// type:
		//		A String to identify dojo targets.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: refresh");
		var dojoList = this._dojoList;
		this.unregister();
		dojo.forEach(dojoList, function(dojo){
			if(dojo.type == type){
				dojo.coords = this._initCoordinates(dojo.node);
			}
		}, this);
		this._dojoList = dojoList;
	},

	_getHoverDojoArea: function(/*Object*/coords){
		// summary:
		//		Check if the coordinates of the mouse is in a dojo target.
		// coords:
		//		Coordinates of the mouse.
		// tags:
		//		protected

		//console.log("dojox.mdnd.adapter.DndToDojo ::: _getHoverDojoArea");
		this._oldDojoArea = this._currentDojoArea;
		this._currentDojoArea = null;
		var x = coords.x;
		var y = coords.y;
		var length = this._dojoList.length;
		for(var i = 0; i < length; i++){
			var dojoArea = this._dojoList[i];
			var coordinates = dojoArea.coords;
			if(coordinates.x <= x && x <= coordinates.x1 && coordinates.y <= y && y <= coordinates.y1){
				this._currentDojoArea = dojoArea;
				break;
			}
		}
	},

	onMouseMove: function(/*DOMEvent*/e){
		// summary:
		//		Call when the mouse moving after an onStartDrag of AreaManger.
		//		Check if the coordinates of the mouse is in a dojo target.
		// e:
		//		Event object.
		// tags:
		//		callback

		//console.log("dojox.mdnd.adapter.DndToDojo ::: onMouseMove");
		var coords = {
			'x': e.pageX,
			'y': e.pageY
		};
		this._getHoverDojoArea(coords);
		if(this._currentDojoArea != this._oldDojoArea){
			if(this._currentDojoArea == null){
				this.onDragExit(e);
			}
			else if(this._oldDojoArea == null){
				this.onDragEnter(e);
			}
			else{
				this.onDragExit(e);
				this.onDragEnter(e);
			}
		}
	},

	isAccepted: function(/*DOMNode*/draggedNode, /*Object*/ target){
		// summary:
		//		Return true if the dragged node is accepted.
		//		This method has to be overwritten according to registered target.

		//console.log("dojox.mdnd.adapter.DndToDojo ::: isAccepted");
		return true;
	},


	onDragEnter: function(/*DOMEvent*/e){
		// summary:
		//		Call when the mouse enters in a registered dojo target.
		// e:
		//		The current Javascript Event.
		// tags:
		//		callback

		//console.log("dojox.mdnd.adapter.DndToDojo ::: onDragEnter");
		// specific for drag and drop switch
		if(this._currentDojoArea.dojo){
			// disconnect
			dojo.disconnect(this._dojoxManager._dragItem.handlers.pop());
			dojo.disconnect(this._dojoxManager._dragItem.handlers.pop());
			//disconnect onmousemove of moveable item
			//console.info("before",this._dojoxManager._dragItem.item.events.pop());
			dojo.disconnect(this._dojoxManager._dragItem.item.events.pop());
			dojo.body().removeChild(this._dojoxManager._cover);
			dojo.body().removeChild(this._dojoxManager._cover2);
			var node = this._dojoxManager._dragItem.item.node;
			// hide dragNode :
			// disconnect the dojoDndAdapter if it's initialize
			if(dojox.mdnd.adapter._dndFromDojo){
				dojox.mdnd.adapter._dndFromDojo.unsubscribeDnd();
			}
			dojo.style(node, {
				'position': "relative",
				'top': '0',
				'left': '0'
			});
			// launch the drag and drop Dojo.
			this._lazyManager.startDrag(e, node);
			var handle = dojo.connect(this._lazyManager.manager, "overSource", this, function(){
				dojo.disconnect(handle);
				if(this._lazyManager.manager.canDropFlag){
					// remove dropIndicator
					this._dojoxManager._dropIndicator.node.style.display = "none";
				}
			});

			this.cancelHandler = dojo.subscribe("/dnd/cancel", this, function(){
				var moveableItem = this._dojoxManager._dragItem.item;
				// connect onmousemove of moveable item
				// need to reconnect the onmousedown of movable class.
				moveableItem.events = [
					dojo.connect(moveableItem.handle, "onmousedown", moveableItem, "onMouseDown")
				];
				// replace the cover and the dragNode in the cover.
				dojo.body().appendChild(this._dojoxManager._cover);
				dojo.body().appendChild(this._dojoxManager._cover2);
				this._dojoxManager._cover.appendChild(moveableItem.node);

				var objectArea = this._dojoxManager._areaList[this._dojoxManager._sourceIndexArea];
				var dropIndex = this._dojoxManager._sourceDropIndex;
				var nodeRef = null;
				if(dropIndex != objectArea.items.length
						&& dropIndex != -1){
					nodeRef = objectArea.items[this._dojoxManager._sourceDropIndex].item.node;
				}
				if(this._dojoxManager._dropIndicator.node.style.display == "none"){
					this._dojoxManager._dropIndicator.node.style.display == "";
				}
				this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDrag", this._dojoxManager, "onDrag"));
				this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDragEnd", this._dojoxManager, "onDrop"));
				this._draggedNode.style.display = "";
				this._dojoxManager.onDrop(this._draggedNode);
				dojo.unsubscribe(this.cancelHandler);
				dojo.unsubscribe(this.dropHandler);
				if(dojox.mdnd.adapter._dndFromDojo){
					dojox.mdnd.adapter._dndFromDojo.subscribeDnd();
				}
			});
			this.dropHandler = dojo.subscribe("/dnd/drop/before", this, function(params){
				dojo.unsubscribe(this.cancelHandler);
				dojo.unsubscribe(this.dropHandler);
				this.onDrop();
			});
		}
		else{
			this.accept = this.isAccepted(this._dojoxManager._dragItem.item.node, this._currentDojoArea);
			if(this.accept){
				// disconnect
				dojo.disconnect(this._dojoxManager._dragItem.handlers.pop());
				dojo.disconnect(this._dojoxManager._dragItem.handlers.pop());
				// remove dropIndicator
				this._dojoxManager._dropIndicator.node.style.display = "none";
				if(!this._moveUpHandler){
					this._moveUpHandler = dojo.connect(dojo.doc, "onmouseup", this, "onDrop");
				}
			}
		}
		// publish a topic
		dojo.publish("/dojox/mdnd/adapter/dndToDojo/over",[this._currentDojoArea.node, this._currentDojoArea.type, this._draggedNode, this.accept]);
	},

	onDragExit: function(/*DOMEvent*/e){
		// summary:
		//		Call when the mouse exit of a registered dojo target.
		// e:
		//		current javscript event

		//console.log("dojox.mdnd.adapter.DndToDojo ::: onDragExit",e, this._dojoxManager._dragItem.item);
		// set the old height of dropIndicator.
		if(this._oldDojoArea.dojo){
			// unsubscribe the topic /dnd/cancel and /dnd/drop/before
			dojo.unsubscribe(this.cancelHandler);
			dojo.unsubscribe(this.dropHandler);
			// launch Drag and Drop
			var moveableItem = this._dojoxManager._dragItem.item;
			// connect onmousemove of moveable item
			this._dojoxManager._dragItem.item.events.push(dojo.connect(
				moveableItem.node.ownerDocument,
				"onmousemove",
				moveableItem,
				"onMove"
			));
			// replace the cover and the dragNode in the cover.
			dojo.body().appendChild(this._dojoxManager._cover);
			dojo.body().appendChild(this._dojoxManager._cover2);
			this._dojoxManager._cover.appendChild(moveableItem.node);
			// fix style :
			var style = moveableItem.node.style;
			style.position = "absolute";
			style.left = (moveableItem.offsetDrag.l + e.pageX)+"px";
			style.top = (moveableItem.offsetDrag.t + e.pageX)+"px";
			style.display = "";
			// stop dojoDrag
			this._lazyManager.cancelDrag();
			// reconnect the dndFromDojo
			if(dojox.mdnd.adapter._dndFromDojo){
				dojox.mdnd.adapter._dndFromDojo.subscribeDnd();
			}
			if(this._dojoxManager._dropIndicator.node.style.display == "none"){
				this._dojoxManager._dropIndicator.node.style.display = "";
			}
			// reconnect the areaManager.
			this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDrag", this._dojoxManager, "onDrag"));
			this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDragEnd", this._dojoxManager, "onDrop"));
			this._dojoxManager._dragItem.item.onMove(e);
		}
		else{
			if(this.accept){
				// disconnect the mouseUp event.
				if(this._moveUpHandler){
					dojo.disconnect(this._moveUpHandler);
					this._moveUpHandler = null;
				}
				// redisplay dropIndicator
				if(this._dojoxManager._dropIndicator.node.style.display == "none"){
					this._dojoxManager._dropIndicator.node.style.display = "";
				}
				// reconnect the areaManager.
				this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDrag", this._dojoxManager, "onDrag"));
				this._dojoxManager._dragItem.handlers.push(dojo.connect(this._dojoxManager._dragItem.item, "onDragEnd", this._dojoxManager, "onDrop"));
				this._dojoxManager._dragItem.item.onMove(e);
			}
		}
		// publish a topic
		dojo.publish("/dojox/mdnd/adapter/dndToDojo/out",[this._oldDojoArea.node, this._oldDojoArea.type, this._draggedNode, this.accept]);
	},

	onDrop: function(/*DOMEvent*/e){
		// summary:
		//		Called when an onmouseup event is loaded on a registered target dojo.
		// e:
		//		Event object.

//		console.log("dojox.mdnd.adapter.DndToDojo ::: onDrop", this._currentDojoArea);
		if(this._currentDojoArea.dojo){
			// reconnect the dojoDndAdapter
			if(dojox.mdnd.adapter._dndFromDojo){
				dojox.mdnd.adapter._dndFromDojo.subscribeDnd();
			}
		}
		if(this._dojoxManager._dropIndicator.node.style.display == "none"){
			this._dojoxManager._dropIndicator.node.style.display = "";
		}
		// remove the cover
		if(this._dojoxManager._cover.parentNode && this._dojoxManager._cover.parentNode.nodeType == 1){
			dojo.body().removeChild(this._dojoxManager._cover);
			dojo.body().removeChild(this._dojoxManager._cover2);
		}
		// remove draggedNode of target :
		if(this._draggedNode.parentNode == this._dojoxManager._cover){
			this._dojoxManager._cover.removeChild(this._draggedNode);
		}
		dojo.disconnect(this._moveHandler);
		dojo.disconnect(this._moveUpHandler);
		this._moveHandler = this._moveUpHandler = null;
		dojo.publish("/dojox/mdnd/adapter/dndToDojo/drop", [this._draggedNode, this._currentDojoArea.node, this._currentDojoArea.type]);
		dojo.removeClass(this._draggedNode, "dragNode");
		var style = this._draggedNode.style;
		style.position = "relative";
		style.left = "0";
		style.top = "0";
		style.width = "auto";
		dojo.forEach(this._dojoxManager._dragItem.handlers, dojo.disconnect);
		this._dojoxManager._deleteMoveableItem(this._dojoxManager._dragItem);
		this._draggedNode = null;
		this._currentDojoArea = null;
		// reset of area manager.
		this._dojoxManager._resetAfterDrop();
	}
});

dojox.mdnd.adapter._dndToDojo = null;
dojox.mdnd.adapter.dndToDojo = function(){
	// summary:
	// 		returns the current areaManager, creates one if it is not created yet
	if(!dojox.mdnd.adapter._dndToDojo){
		dojox.mdnd.adapter._dndToDojo = new dojox.mdnd.adapter.DndToDojo();
	}
	return dojox.mdnd.adapter._dndToDojo;	// Object
};

}

}};});