/
dragcontrol.js
400 lines (358 loc) · 13 KB
/
dragcontrol.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
import {preventEvent, addCss, removeCss, pos as getPos, remove} from "../webix/html";
import env from "../webix/env";
import Touch from "../core/touch";
import {zIndex} from "../ui/helpers";
import {_to_array, toNode} from "../webix/helpers";
import {_event, event, eventRemove} from "../webix/htmlevents";
import {attachEvent, callEvent} from "../webix/customevents";
/*
Behavior:DND - low-level dnd handling
@export
getContext
addDrop
addDrag
DND master can define next handlers
onCreateDrag
onDragIng
onDragOut
onDrag
onDrop
all are optional
*/
const DragControl ={
//has of known dnd masters
_drag_masters : _to_array(["dummy"]),
/*
register drop area
@param node html node or ID
@param ctrl options dnd master
@param master_mode true if you have complex drag-area rules
*/
addDrop:function(node,ctrl,master_mode){
node = toNode(node);
node.webix_drop=this._getCtrl(ctrl);
if (master_mode) node.webix_master=true;
},
//return index of master in collection
//it done in such way to prevent dnd master duplication
//probably useless, used only by addDrop and addDrag methods
_getCtrl:function(ctrl){
ctrl = ctrl||DragControl;
var index = this._drag_masters.find(ctrl);
if (index<0){
index = this._drag_masters.length;
this._drag_masters.push(ctrl);
if (ctrl.attachEvent)
ctrl.attachEvent("onDestruct", () => DragControl.unlink(ctrl));
}
return index;
},
unlink(ctrl){
var index = this._drag_masters.find(ctrl);
if (index > -1){
// if active view was destroyed, stop dnd
if (DragControl._active && DragControl._active.webix_drag == index)
DragControl._stopDrag();
// if last target was destroyed, reset it and continue dnd
if (DragControl._last && DragControl._last.webix_drop == index)
DragControl._last = null;
this._drag_masters[index] = null;
}
},
_createTouchDrag: function(e, pointer){
const dragCtrl = DragControl;
const master = this._getActiveDragMaster();
// for data items only
if (master && master.$longTouchLimit){
if (!dragCtrl._html && !dragCtrl.createDrag(e, pointer)) return;
e.longtouch_drag = true;
const pos = getPos(e);
const customPos = dragCtrl.$dragPos(pos, e);
const ctx = dragCtrl._drag_context;
dragCtrl._html.style.top= pos.y+dragCtrl.top+(customPos||!ctx.y_offset?0:ctx.y_offset)+"px";
dragCtrl._html.style.left= pos.x+dragCtrl.left+(customPos||!ctx.x_offset?0:ctx.x_offset)+"px";
}
},
/*
register drag area
@param node html node or ID
@param ctrl options dnd master
*/
addDrag:function(node,ctrl){
node = toNode(node);
node.webix_drag = this._getCtrl(ctrl);
_event(node,"dragstart",preventEvent);
_event(node, env.mouse.down, e => this._preStart(e, node, "mouse"));
if (env.touch)
_event(node, env.touch.down, e => this._preStart(e, node, "touch"));
},
//logic of drag - start, we are not creating drag immediately, instead of that we hears mouse moving
_preStart:function(e, node, pointer){
if (DragControl._active){
//if we have nested drag areas, use the top one and ignore the inner one
if (DragControl._saved_event == e) return;
DragControl._preStartFalse(e);
DragControl.destroyDrag(e);
}
DragControl._active = node;
const evobj = env[pointer].context(e);
DragControl._start_pos = evobj;
DragControl._saved_event = e;
const passive = (pointer === "touch") ? { passive:false } : null;
DragControl._webix_drag_mm = event(document.body, env[pointer].move, e => DragControl._startDrag(e, pointer), passive);
DragControl._webix_drag_mu = event(document, env[pointer].up, DragControl._preStartFalse);
//need to run here, or will not work in IE
addCss(document.body,"webix_noselect", 1);
},
//if mouse was released before moving - this is not a dnd, remove event handlers
_preStartFalse:function(e){
DragControl._clean_dom_after_drag();
DragControl._touch_animation = !e.cancelable;
},
//mouse was moved without button released - dnd started, update event handlers
_startDrag:function(e, pointer){
const touch = (pointer === "touch");
// mouse: allow dnd only on left click
if(!touch && DragControl._saved_event.button)
return;
// check touch scroll animation
DragControl._touch_animation = !e.cancelable;
if (touch && DragControl._touch_animation){
DragControl._clean_dom_after_drag();
return DragControl.destroyDrag(e);
}
//prevent unwanted dnd
var pos = env[pointer].context(e);
var master = DragControl._getActiveDragMaster();
// only long-touched elements can be dragged
var longTouchLimit = (touch && master && master.$longTouchLimit && !Touch._long_touched);
if (longTouchLimit || Math.abs(pos.x-DragControl._start_pos.x)<5 && Math.abs(pos.y-DragControl._start_pos.y)<5)
return;
if (!DragControl._html && !DragControl.createDrag(DragControl._saved_event, pointer))
return DragControl._clean_dom_after_drag();
DragControl._clean_dom_after_drag(true);
DragControl.sendSignal("start"); //useless for now
if (touch) {
// important: for touch events use e.target as EventTarget
DragControl._webix_drag_mm = event(e.target, env[pointer].move, e => DragControl._moveDrag(e, pointer), { passive:false });
DragControl._webix_drag_mu = event(e.target, env[pointer].up, DragControl._stopDrag);
} else {
DragControl._webix_drag_mm = event(document.body, env[pointer].move, e => DragControl._moveDrag(e, pointer));
DragControl._webix_drag_mu = event(document, env[pointer].up, DragControl._stopDrag);
}
DragControl._moveDrag(e, pointer, true);
},
//mouse was released while dnd is active - process target
_stopDrag:function(e){
DragControl._clean_dom_after_drag();
DragControl._saved_event = null;
if (DragControl._last && e){ //if some drop target was confirmed
DragControl.$drop(DragControl._active, DragControl._last, e);
DragControl.$dragOut(DragControl._active, DragControl._last, null, e);
}
DragControl.destroyDrag(e);
DragControl.sendSignal("stop"); //useless for now
},
_clean_dom_after_drag:function(still_drag){
this._webix_drag_mm = eventRemove(this._webix_drag_mm);
this._webix_drag_mu = eventRemove(this._webix_drag_mu);
if (!still_drag)
removeCss(document.body,"webix_noselect");
},
//dnd is active and mouse position was changed
_moveDrag:function(e, pointer, first){
var dragCtrl = DragControl;
var pos = getPos(e);
//give possibility to customize drag position
var customPos = dragCtrl.$dragPos(pos, e);
//adjust drag marker position
var ctx = dragCtrl._drag_context;
dragCtrl._html.style.top=pos.y+dragCtrl.top+(customPos||!ctx.y_offset?0:ctx.y_offset) +"px";
dragCtrl._html.style.left=pos.x+dragCtrl.left+(customPos||!ctx.x_offset?0:ctx.x_offset)+"px";
// check landing at least once
if (first) dragCtrl._skip = false;
if (dragCtrl._skip)
dragCtrl._skip = false;
else {
let evobj = e;
if (pointer === "touch"){
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
const context = env[pointer].context(e);
const target = document.elementFromPoint(context.x - scrollLeft, context.y - scrollTop);
evobj = new Proxy(e, {
get: function(obj, prop){
if (prop === "target"){
return target;
}
const res = obj[prop];
if (typeof res === "function"){
return res.bind(e);
}
return res;
}
});
}
dragCtrl._checkLand((evobj.target), evobj);
}
return preventEvent(e);
},
//check if item under mouse can be used as drop landing
_checkLand:function(node,e){
while (node && node.tagName!="BODY"){
if (node.webix_drop){ //if drop area registered
if (this._last && (this._last!=node || node.webix_master)) //if this area with complex dnd master
this.$dragOut(this._active,this._last,node,e); //inform master about possible mouse-out
if (!this._last || this._last!=node || node.webix_master){ //if this is new are or area with complex dnd master
this._last=null; //inform master about possible mouse-in
this._landing=this.$dragIn(DragControl._active,node,e);
if (this._landing) //landing was rejected
this._last=node;
return;
}
return;
}
node=node.parentNode;
}
if (this._last) //mouse was moved out of previous landing, and without finding new one
this._last = this._landing = this.$dragOut(this._active,this._last,null,e);
},
//mostly useless for now, can be used to add cross-frame dnd
sendSignal:function(signal){
DragControl.active=(signal=="start");
callEvent("onDragMode", [signal]);
},
//return master for html area
getMaster:function(t){
return this._drag_masters[t.webix_drag||t.webix_drop];
},
//return dhd-context object
getContext:function(){
return this._drag_context;
},
getNode:function(){
return this._html;
},
//called when dnd is initiated, must create drag representation
createDrag:function(e, pointer){
var a = DragControl._active;
DragControl._drag_context = {};
var master = this._drag_masters[a.webix_drag];
var drag_container;
//if custom method is defined - use it
if (master.$dragCreate){
drag_container = master.$dragCreate(a,e,pointer);
if (!drag_container) return false;
this._setDragOffset(e);
drag_container.style.position = "absolute";
} else {
//overvise use default one
var text = DragControl.$drag(a,e,pointer);
DragControl._setDragOffset(e);
if (!text) return false;
drag_container = document.createElement("DIV");
drag_container.innerHTML=text;
drag_container.className="webix_drag_zone";
document.body.appendChild(drag_container);
var context = DragControl._drag_context;
if (context.html){
context.x_offset = -Math.round(drag_container.offsetWidth * 0.5);
context.y_offset = -Math.round(drag_container.offsetHeight * 0.75);
}
}
/*
dragged item must have topmost z-index
in some cases item already have z-index
so we will preserve it if possible
*/
drag_container.style.zIndex = Math.max(drag_container.style.zIndex,zIndex());
DragControl._skipDropH = event(drag_container, env[pointer].move, DragControl._skip_mark);
if (!DragControl._drag_context.from)
DragControl._drag_context = {source:a, from:a};
DragControl._html = drag_container;
return true;
},
//helper, prevents unwanted mouse-out events
_skip_mark:function(){
DragControl._skip = true;
},
//after dnd end, remove all traces and used html elements
destroyDrag:function(e){
var a = DragControl._active;
var master = this._drag_masters[a.webix_drag];
if (DragControl._skipDropH)
DragControl._skipDropH = eventRemove(DragControl._skipDropH);
if (master && master.$dragDestroy){
if(DragControl._html)
master.$dragDestroy(a,DragControl._html,e);
} else
remove(DragControl._html);
if (master && master._auto_scroll_delay)
master._auto_scroll_delay = window.clearTimeout(master._auto_scroll_delay);
if (DragControl._dropHTML)
remove(DragControl._dropHTML);
DragControl._landing=DragControl._active=DragControl._last=DragControl._html=DragControl._dropHTML=null;
DragControl._drag_context = null;
},
_getActiveDragMaster: function(){
return DragControl._drag_masters[DragControl._active.webix_drag];
},
top:0, //relative position of drag marker to mouse cursor
left:0,
_setDragOffset:function(e){
const pos = DragControl._start_pos;
const ctx = DragControl._drag_context;
if(typeof ctx.x_offset != "undefined" && typeof ctx.y_offset != "undefined")
return null;
ctx.x_offset = ctx.y_offset = 0;
const m = DragControl._getActiveDragMaster();
if (m._getDragItemPos && m !== this){
const itemPos = m._getDragItemPos(pos,e);
if (itemPos){
ctx.x_offset = itemPos.x - pos.x;
ctx.y_offset = itemPos.y - pos.y;
}
}
},
$dragPos:function(pos, e){
var m=this._drag_masters[DragControl._active.webix_drag];
if (m.$dragPos && m!=this){
m.$dragPos(pos, e, DragControl._html);
return true;
}
},
//called when mouse was moved in drop area
$dragIn:function(s,t,e){
var m=this._drag_masters[t.webix_drop];
if (m.$dragIn && m!=this) return m.$dragIn(s,t,e);
t.className=t.className+" webix_drop_zone";
return t;
},
//called when mouse was moved out drop area
$dragOut:function(s,t,n,e){
var m=this._drag_masters[t.webix_drop];
if (m.$dragOut && m!=this) return m.$dragOut(s,t,n,e);
t.className=t.className.replace("webix_drop_zone","");
return null;
},
//called when mouse was released over drop area
$drop:function(s,t,e){
var m=this._drag_masters[t.webix_drop];
DragControl._drag_context.from = DragControl.getMaster(s);
if (m.$drop && m!=this) return m.$drop(s,t,e);
t.appendChild(s);
},
//called when dnd just started
$drag:function(s,e,p){
var m=this._drag_masters[s.webix_drag];
if (m.$drag && m!=this) return m.$drag(s,e,p);
return "<div style='"+s.style.cssText+"'>"+s.innerHTML+"</div>";
}
};
//global touch-drag handler
attachEvent("onLongTouch", function(ev){
const active = DragControl._active;
if(!DragControl._touch_animation && active && active.contains(ev.target))
DragControl._createTouchDrag(ev, "touch");
});
export default DragControl;