-
Notifications
You must be signed in to change notification settings - Fork 0
/
Container.js
455 lines (423 loc) · 12.2 KB
/
Container.js
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
define([
"../_base/array",
"../_base/declare",
"../_base/kernel",
"../_base/lang",
"../_base/window",
"../dom",
"../dom-class",
"../dom-construct",
"../Evented",
"../has",
"../on",
"../query",
"../touch",
"./common"
], function(
array, declare, kernel, lang, win,
dom, domClass, domConstruct, Evented, has, on, query, touch, dnd){
// module:
// dojo/dnd/Container
/*
Container states:
"" - normal state
"Over" - mouse over a container
Container item states:
"" - normal state
"Over" - mouse over a container item
*/
var Container = declare("dojo.dnd.Container", Evented, {
// summary:
// a Container object, which knows when mouse hovers over it,
// and over which element it hovers
// object attributes (for markup)
skipForm: false,
// allowNested: Boolean
// Indicates whether to allow dnd item nodes to be nested within other elements.
// By default this is false, indicating that only direct children of the container can
// be draggable dnd item nodes
allowNested: false,
/*=====
// current: DomNode
// The DOM node the mouse is currently hovered over
current: null,
// map: Hash<String, Container.Item>
// Map from an item's id (which is also the DOMNode's id) to
// the dojo/dnd/Container.Item itself.
map: {},
=====*/
constructor: function(node, params){
// summary:
// a constructor of the Container
// node: Node
// node or node's id to build the container on
// params: Container.__ContainerArgs
// a dictionary of parameters
this.node = dom.byId(node);
if(!params){ params = {}; }
this.creator = params.creator || null;
this.skipForm = params.skipForm;
this.parent = params.dropParent && dom.byId(params.dropParent);
// class-specific variables
this.map = {};
this.current = null;
// states
this.containerState = "";
domClass.add(this.node, "dojoDndContainer");
// mark up children
if(!(params && params._skipStartup)){
this.startup();
}
// set up events
this.events = [
on(this.node, touch.over, lang.hitch(this, "onMouseOver")),
on(this.node, touch.out, lang.hitch(this, "onMouseOut")),
// cancel text selection and text dragging
on(this.node, "dragstart", lang.hitch(this, "onSelectStart")),
on(this.node, "selectstart", lang.hitch(this, "onSelectStart"))
];
},
// object attributes (for markup)
creator: function(){
// summary:
// creator function, dummy at the moment
},
// abstract access to the map
getItem: function(/*String*/ key){
// summary:
// returns a data item by its key (id)
return this.map[key]; // Container.Item
},
setItem: function(/*String*/ key, /*Container.Item*/ data){
// summary:
// associates a data item with its key (id)
this.map[key] = data;
},
delItem: function(/*String*/ key){
// summary:
// removes a data item from the map by its key (id)
delete this.map[key];
},
forInItems: function(/*Function*/ f, /*Object?*/ o){
// summary:
// iterates over a data map skipping members that
// are present in the empty object (IE and/or 3rd-party libraries).
o = o || kernel.global;
var m = this.map, e = dnd._empty;
for(var i in m){
if(i in e){ continue; }
f.call(o, m[i], i, this);
}
return o; // Object
},
clearItems: function(){
// summary:
// removes all data items from the map
this.map = {};
},
// methods
getAllNodes: function(){
// summary:
// returns a list (an array) of all valid child nodes
return query((this.allowNested ? "" : "> ") + ".dojoDndItem", this.parent); // NodeList
},
sync: function(){
// summary:
// sync up the node list with the data map
var map = {};
this.getAllNodes().forEach(function(node){
if(node.id){
var item = this.getItem(node.id);
if(item){
map[node.id] = item;
return;
}
}else{
node.id = dnd.getUniqueId();
}
var type = node.getAttribute("dndType"),
data = node.getAttribute("dndData");
map[node.id] = {
data: data || node.innerHTML,
type: type ? type.split(/\s*,\s*/) : ["text"]
};
}, this);
this.map = map;
return this; // self
},
insertNodes: function(data, before, anchor){
// summary:
// inserts an array of new nodes before/after an anchor node
// data: Array
// a list of data items, which should be processed by the creator function
// before: Boolean
// insert before the anchor, if true, and after the anchor otherwise
// anchor: Node
// the anchor node to be used as a point of insertion
if(!this.parent.firstChild){
anchor = null;
}else if(before){
if(!anchor){
anchor = this.parent.firstChild;
}
}else{
if(anchor){
anchor = anchor.nextSibling;
}
}
var i, t;
if(anchor){
for(i = 0; i < data.length; ++i){
t = this._normalizedCreator(data[i]);
this.setItem(t.node.id, {data: t.data, type: t.type});
anchor.parentNode.insertBefore(t.node, anchor);
}
}else{
for(i = 0; i < data.length; ++i){
t = this._normalizedCreator(data[i]);
this.setItem(t.node.id, {data: t.data, type: t.type});
this.parent.appendChild(t.node);
}
}
return this; // self
},
destroy: function(){
// summary:
// prepares this object to be garbage-collected
array.forEach(this.events, function(handle){ handle.remove(); });
this.clearItems();
this.node = this.parent = this.current = null;
},
// markup methods
markupFactory: function(params, node, Ctor){
params._skipStartup = true;
return new Ctor(node, params);
},
startup: function(){
// summary:
// collects valid child items and populate the map
// set up the real parent node
if(!this.parent){
// use the standard algorithm, if not assigned
this.parent = this.node;
if(this.parent.tagName.toLowerCase() == "table"){
var c = this.parent.getElementsByTagName("tbody");
if(c && c.length){ this.parent = c[0]; }
}
}
this.defaultCreator = dnd._defaultCreator(this.parent);
// process specially marked children
this.sync();
},
// mouse events
onMouseOver: function(e){
// summary:
// event processor for onmouseover or touch, to mark that element as the current element
// e: Event
// mouse event
var n = e.relatedTarget;
while(n){
if(n == this.node){ break; }
try{
n = n.parentNode;
}catch(x){
n = null;
}
}
if(!n){
this._changeState("Container", "Over");
this.onOverEvent();
}
n = this._getChildByEvent(e);
if(this.current == n){ return; }
if(this.current){ this._removeItemClass(this.current, "Over"); }
if(n){ this._addItemClass(n, "Over"); }
this.current = n;
},
onMouseOut: function(e){
// summary:
// event processor for onmouseout
// e: Event
// mouse event
for(var n = e.relatedTarget; n;){
if(n == this.node){ return; }
try{
n = n.parentNode;
}catch(x){
n = null;
}
}
if(this.current){
this._removeItemClass(this.current, "Over");
this.current = null;
}
this._changeState("Container", "");
this.onOutEvent();
},
onSelectStart: function(e){
// summary:
// event processor for onselectevent and ondragevent
// e: Event
// mouse event
if(!this.skipForm || !dnd.isFormElement(e)){
e.stopPropagation();
e.preventDefault();
}
},
// utilities
onOverEvent: function(){
// summary:
// this function is called once, when mouse is over our container
},
onOutEvent: function(){
// summary:
// this function is called once, when mouse is out of our container
},
_changeState: function(type, newState){
// summary:
// changes a named state to new state value
// type: String
// a name of the state to change
// newState: String
// new state
var prefix = "dojoDnd" + type;
var state = type.toLowerCase() + "State";
//domClass.replace(this.node, prefix + newState, prefix + this[state]);
domClass.replace(this.node, prefix + newState, prefix + this[state]);
this[state] = newState;
},
_addItemClass: function(node, type){
// summary:
// adds a class with prefix "dojoDndItem"
// node: Node
// a node
// type: String
// a variable suffix for a class name
domClass.add(node, "dojoDndItem" + type);
},
_removeItemClass: function(node, type){
// summary:
// removes a class with prefix "dojoDndItem"
// node: Node
// a node
// type: String
// a variable suffix for a class name
domClass.remove(node, "dojoDndItem" + type);
},
_getChildByEvent: function(e){
// summary:
// gets a child, which is under the mouse at the moment, or null
// e: Event
// a mouse event
var node = e.target;
if(node){
for(var parent = node.parentNode; parent; node = parent, parent = node.parentNode){
if((parent == this.parent || this.allowNested) && domClass.contains(node, "dojoDndItem")){ return node; }
}
}
return null;
},
_normalizedCreator: function(/*Container.Item*/ item, /*String*/ hint){
// summary:
// adds all necessary data to the output of the user-supplied creator function
var t = (this.creator || this.defaultCreator).call(this, item, hint);
if(!lang.isArray(t.type)){ t.type = ["text"]; }
if(!t.node.id){ t.node.id = dnd.getUniqueId(); }
domClass.add(t.node, "dojoDndItem");
return t;
}
});
dnd._createNode = function(tag){
// summary:
// returns a function, which creates an element of given tag
// (SPAN by default) and sets its innerHTML to given text
// tag: String
// a tag name or empty for SPAN
if(!tag){ return dnd._createSpan; }
return function(text){ // Function
return domConstruct.create(tag, {innerHTML: text}); // Node
};
};
dnd._createTrTd = function(text){
// summary:
// creates a TR/TD structure with given text as an innerHTML of TD
// text: String
// a text for TD
var tr = domConstruct.create("tr");
domConstruct.create("td", {innerHTML: text}, tr);
return tr; // Node
};
dnd._createSpan = function(text){
// summary:
// creates a SPAN element with given text as its innerHTML
// text: String
// a text for SPAN
return domConstruct.create("span", {innerHTML: text}); // Node
};
// dnd._defaultCreatorNodes: Object
// a dictionary that maps container tag names to child tag names
dnd._defaultCreatorNodes = {ul: "li", ol: "li", div: "div", p: "div"};
dnd._defaultCreator = function(node){
// summary:
// takes a parent node, and returns an appropriate creator function
// node: Node
// a container node
var tag = node.tagName.toLowerCase();
var c = tag == "tbody" || tag == "thead" ? dnd._createTrTd :
dnd._createNode(dnd._defaultCreatorNodes[tag]);
return function(item, hint){ // Function
var isObj = item && lang.isObject(item), data, type, n;
if(isObj && item.tagName && item.nodeType && item.getAttribute){
// process a DOM node
data = item.getAttribute("dndData") || item.innerHTML;
type = item.getAttribute("dndType");
type = type ? type.split(/\s*,\s*/) : ["text"];
n = item; // this node is going to be moved rather than copied
}else{
// process a DnD item object or a string
data = (isObj && item.data) ? item.data : item;
type = (isObj && item.type) ? item.type : ["text"];
n = (hint == "avatar" ? dnd._createSpan : c)(String(data));
}
if(!n.id){
n.id = dnd.getUniqueId();
}
return {node: n, data: data, type: type};
};
};
/*=====
Container.__ContainerArgs = declare([], {
creator: function(){
// summary:
// a creator function, which takes a data item, and returns an object like that:
// {node: newNode, data: usedData, type: arrayOfStrings}
},
// skipForm: Boolean
// don't start the drag operation, if clicked on form elements
skipForm: false,
// dropParent: Node||String
// node or node's id to use as the parent node for dropped items
// (must be underneath the 'node' parameter in the DOM)
dropParent: null,
// _skipStartup: Boolean
// skip startup(), which collects children, for deferred initialization
// (this is used in the markup mode)
_skipStartup: false
});
Container.Item = function(){
// summary:
// Represents (one of) the source node(s) being dragged.
// Contains (at least) the "type" and "data" attributes.
// type: String[]
// Type(s) of this item, by default this is ["text"]
// data: Object
// Logical representation of the object being dragged.
// If the drag object's type is "text" then data is a String,
// if it's another type then data could be a different Object,
// perhaps a name/value hash.
this.type = type;
this.data = data;
};
=====*/
return Container;
});