-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathindex.ts
1643 lines (1325 loc) · 63.8 KB
/
index.ts
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// debug mode, which will highlight drop target, immediate user selection and events fired as you interact.
const DEBUG = false;
//<editor-fold desc="feature detection">
interface DetectedFeatures {
draggable:boolean;
dragEvents:boolean;
touchEvents:boolean;
userAgentSupportingNativeDnD:boolean;
}
function detectFeatures():DetectedFeatures {
let features:DetectedFeatures = {
dragEvents: ("ondragstart" in document.documentElement),
draggable: ("draggable" in document.documentElement),
touchEvents: ("ontouchstart" in document.documentElement),
userAgentSupportingNativeDnD: undefined
};
const isBlinkEngine = !!((<any>window).chrome) || /chrome/i.test(navigator.userAgent);
features.userAgentSupportingNativeDnD = !(
// if is mobile safari or android browser -> no native dnd
(/iPad|iPhone|iPod|Android/.test(navigator.userAgent))
|| // OR
//if is blink(chrome/opera) with touch events enabled -> no native dnd
(isBlinkEngine && features.touchEvents)
);
if (DEBUG) {
Object.keys(features).forEach(function (key) {
console.log("dnd-poly: detected feature '" + key + " = " + features[key] + "'");
});
}
return features;
}
let supportsPassive:boolean;
function supportsPassiveEventListener():boolean {
let supportsPassiveEventListeners = false;
// reference https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
try {
let opts = Object.defineProperty({}, "passive", {
get: function () {
supportsPassiveEventListeners = true;
}
});
window.addEventListener("test", null, opts);
}
// tslint:disable-next-line:no-empty
catch (e) {
}
return supportsPassiveEventListeners;
}
//</editor-fold>
//<editor-fold desc="public api">
// function signature for the dragImageTranslateOverride hook
export type DragImageTranslateOverrideFn = (// corresponding touchmove event
event:TouchEvent,
// the processed touch event viewport coordinates
hoverCoordinates:Point,
// the element under the calculated touch coordinates
hoveredElement:HTMLElement,
// callback for updating the drag image offset
translateDragImageFn:(offsetX:number, offsetY:number) => void) => void;
export interface Config {
// flag to force the polyfill being applied and not rely on internal feature detection
forceApply?:boolean;
// useful for when you want the default drag image but still want to apply
// some static offset from touch coordinates to drag image coordinates
// defaults to (0,0)
dragImageOffset?:Point;
// if the dragImage shall be centered on the touch coordinates
// defaults to false
dragImageCenterOnTouch?:boolean;
// the drag and drop operation involves some processing. here you can specify in what interval this processing takes place.
// defaults to 150ms
iterationInterval?:number;
// hook for custom logic that decides if a drag operation should start
dragStartConditionOverride?:(event:TouchEvent) => boolean;
// hook for custom logic that can manipulate the drag image translate offset
dragImageTranslateOverride?:DragImageTranslateOverrideFn;
// hook for custom logic that can override the default action based on the original touch event when the drag never started
// be sure to call event.preventDefault() if handling the default action in the override to prevent the browser default.
defaultActionOverride?:(event:TouchEvent) => void;
}
// default config
const config:Config = {
iterationInterval: 150,
};
export function polyfill(override?:Config):boolean {
if (override) {
// overwrite default config with user config
Object.keys(override).forEach(function (key) {
config[key] = override[key];
});
}
// only do feature detection when config does not force apply the polyfill
if (!config.forceApply) {
// feature/browser detection
const detectedFeatures = detectFeatures();
// check if native drag and drop support is there
if (detectedFeatures.userAgentSupportingNativeDnD
&& detectedFeatures.draggable
&& detectedFeatures.dragEvents) {
// no polyfilling required
return false;
}
}
console.log("dnd-poly: Applying mobile drag and drop polyfill.");
supportsPassive = supportsPassiveEventListener();
// add listeners suitable for detecting a potential drag operation
addDocumentListener("touchstart", onTouchstart, false);
return true;
}
//</editor-fold>
//<editor-fold desc="drag operation start/end">
// reference the currently active drag operation
let activeDragOperation:DragOperationController;
/**
* event handler listening for initial events that possibly start a drag and drop operation.
*/
function onTouchstart(e:TouchEvent) {
console.log("dnd-poly: global touchstart");
// From the moment that the user agent is to initiate the drag-and-drop operation,
// until the end of the drag-and-drop operation, device input events (e.g. mouse and keyboard events) must be suppressed.
// only allow one drag operation at a time
if (activeDragOperation) {
console.log("dnd-poly: drag operation already active");
return;
}
let dragTarget = tryFindDraggableTarget(e);
// If there is no such element, then nothing is being dragged; abort these
// steps, the drag-and-drop operation is never started.
if (!dragTarget) {
return;
}
try {
activeDragOperation = new DragOperationController(e, config, <HTMLElement>dragTarget, dragOperationEnded);
}
catch (err) {
dragOperationEnded(config, e, DragOperationState.CANCELLED);
// rethrow exception after cleanup
throw err;
}
}
/**
* Search for a possible draggable item upon an event that can initialize a drag operation.
*/
function tryFindDraggableTarget(event:TouchEvent):Element {
//1. Determine what is being dragged, as follows:
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// If the drag operation was invoked on a selection, then it is the selection that is being dragged.
//if( (<Element>event.target).nodeType === 3 ) {
//
// config.log( "drag on text" );
// return <Element>event.target;
//}
//Otherwise, if the drag operation was invoked on a Document, it is the first element, going up the ancestor chain, starting at the node that the
// user tried to drag, that has the IDL attribute draggable set to true.
//else {
let el = <HTMLElement>event.target;
do {
if (el.draggable === false) {
continue;
}
if (el.getAttribute && el.getAttribute("draggable") === "true") {
return el;
}
} while ((el = <HTMLElement>el.parentNode) && el !== document.body);
}
/**
* Implements callback invoked when a drag operation has ended or crashed.
*/
function dragOperationEnded(_config:Config, event:TouchEvent, state:DragOperationState) {
// we need to make the default action happen only when no drag operation took place
if (state === DragOperationState.POTENTIAL) {
console.log("dnd-poly: Drag never started. Last event was " + event.type);
// when lifecycle hook is present
if (_config.defaultActionOverride) {
try {
_config.defaultActionOverride(event);
if (event.defaultPrevented) {
console.log("dnd-poly: defaultActionOverride has taken care of triggering the default action. preventing default on original event");
}
}
catch (e) {
console.log("dnd-poly: error in defaultActionOverride: " + e);
}
}
}
// reset drag operation container
activeDragOperation = null;
}
//</editor-fold>
//<editor-fold desc="drag operation">
/**
* For tracking the different states of a drag operation.
*/
const enum DragOperationState {
// initial state of a controller, if no movement is detected the operation ends with this state
POTENTIAL,
// after movement is detected the drag operation starts and keeps this state until it ends
STARTED,
// when the drag operation ended normally
ENDED,
// when the drag operation ended with a cancelled input event
CANCELLED
}
// contains all possible values of the effectAllowed property
const enum EFFECT_ALLOWED {
NONE = 0,
COPY = 1,
COPY_LINK = 2,
COPY_MOVE = 3,
LINK = 4,
LINK_MOVE = 5,
MOVE = 6,
ALL = 7
}
const ALLOWED_EFFECTS = ["none", "copy", "copyLink", "copyMove", "link", "linkMove", "move", "all"];
// contains all possible values of the dropEffect property
const enum DROP_EFFECT {
NONE = 0,
COPY = 1,
MOVE = 2,
LINK = 3,
}
const DROP_EFFECTS = ["none", "copy", "move", "link"];
// cross-browser css transform property prefixes
const TRANSFORM_CSS_VENDOR_PREFIXES = ["", "-webkit-"];
// css classes
const CLASS_PREFIX = "dnd-poly-";
const CLASS_DRAG_IMAGE = CLASS_PREFIX + "drag-image";
const CLASS_DRAG_IMAGE_SNAPBACK = CLASS_PREFIX + "snapback";
const CLASS_DRAG_OPERATION_ICON = CLASS_PREFIX + "icon";
/**
* Aims to implement the HTML5 d'n'd spec (https://html.spec.whatwg.org/multipage/interaction.html#dnd) as close as it can get.
* Note that all props that are private should start with an underscore to enable better minification.
*
* TODO remove lengthy spec comments in favor of short references to the spec
*/
class DragOperationController {
private _dragOperationState:DragOperationState = DragOperationState.POTENTIAL;
private _dragImage:HTMLElement;
private _dragImageTransforms:string[];
private _dragImagePageCoordinates:Point; // the current page coordinates of the dragImage
private _dragImageOffset:Point; // offset of the drag image relative to the coordinates
private _currentHotspotCoordinates:Point; // the point relative to viewport for determining the immediate user selection
private _immediateUserSelection:HTMLElement = null; // the element the user currently hovers while dragging
private _currentDropTarget:HTMLElement = null; // the element that was selected as a valid drop target by the d'n'd operation
private _dragDataStore:DragDataStore;
private _dataTransfer:DataTransfer;
private _currentDragOperation:string; // the current drag operation set according to the d'n'd processing model
private _initialTouch:Touch; // the identifier for the touch that initiated the drag operation
private _touchMoveHandler:EventListener;
private _touchEndOrCancelHandler:EventListener;
private _lastTouchEvent:TouchEvent;
private _iterationLock:boolean;
private _iterationIntervalId:number;
constructor(private _initialEvent:TouchEvent,
private _config:Config,
private _sourceNode:HTMLElement,
private _dragOperationEndedCb:(config:Config, event:TouchEvent, state:DragOperationState) => void) {
console.log("dnd-poly: setting up potential drag operation..");
this._lastTouchEvent = _initialEvent;
this._initialTouch = _initialEvent.changedTouches[0];
// create bound event listeners
this._touchMoveHandler = this._onTouchMove.bind(this);
this._touchEndOrCancelHandler = this._onTouchEndOrCancel.bind(this);
addDocumentListener("touchmove", this._touchMoveHandler, false);
addDocumentListener("touchend", this._touchEndOrCancelHandler, false);
addDocumentListener("touchcancel", this._touchEndOrCancelHandler, false);
// the only thing we do is setup the touch listeners. if drag will really start is decided in touch move handler.
//<spec>
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// 3. Establish which DOM node is the source node, as follows:
// If it is a selection that is being dragged, then the source node is the text node that the user started the drag on (typically the text node
// that the user originally clicked). If the user did not specify a particular node, for example if the user just told the user agent to begin
// a drag of "the selection", then the source node is the first text node containing a part of the selection. Otherwise, if it is an element
// that is being dragged, then the source node is the element that is being dragged. Otherwise, the source node is part of another document or
// application. When this specification requires that an event be dispatched at the source node in this case, the user agent must instead
// follow the platform-specific conventions relevant to that situation.
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// 4. Determine the list of dragged nodes, as follows:
// If it is a selection that is being dragged, then the list of dragged nodes contains, in tree order, every node that is partially or
// completely included in the selection (including all their ancestors).
// Otherwise, the list of dragged nodes contains only the source node, if any.
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// 5. If it is a selection that is being dragged, then add an item to the drag data store item list, with its properties set as follows:
//The drag data item type string
//"text/plain"
//The drag data item kind
//Plain Unicode string
//The actual data
//The text of the selection
//Otherwise, if any files are being dragged, then add one item per file to the drag data store item list, with their properties set as follows:
//
//The drag data item type string
//The MIME type of the file, if known, or "application/octet-stream" otherwise.
// The drag data item kind
//File
//The actual data
//The file's contents and name.
//Dragging files can currently only happen from outside a browsing context, for example from a file system manager application.
//
// If the drag initiated outside of the application, the user agent must add items to the drag data store item list as appropriate for the data
// being dragged, honoring platform conventions where appropriate; however, if the platform conventions do not use MIME types to label dragged
// data, the user agent must make a best-effort attempt to map the types to MIME types, and, in any case, all the drag data item type strings must
// be converted to ASCII lowercase. Perform drag-and-drop initialization steps defined in any other applicable specifications.
//</spec>
}
//<editor-fold desc="setup/teardown">
/**
* Setup dragImage, input listeners and the drag
* and drop process model iteration interval.
*/
private _setup():boolean {
console.log("dnd-poly: starting drag and drop operation");
this._dragOperationState = DragOperationState.STARTED;
this._currentDragOperation = DROP_EFFECTS[DROP_EFFECT.NONE];
this._dragDataStore = {
_data: {},
_effectAllowed: undefined,
_mode: DragDataStoreMode.PROTECTED,
_types: [],
};
this._currentHotspotCoordinates = {
x: null,
y: null
};
this._dragImagePageCoordinates = {
x: null,
y: null
};
let dragImageSrc:HTMLElement = this._sourceNode;
this._dataTransfer = new DataTransfer(this._dragDataStore, (element:HTMLElement, x:number, y:number) => {
dragImageSrc = element;
if (typeof x === "number" || typeof y === "number") {
this._dragImageOffset = {
x: x || 0,
y: y || 0
};
}
});
// 9. Fire a DND event named dragstart at the source node.
this._dragDataStore._mode = DragDataStoreMode.READWRITE;
this._dataTransfer.dropEffect = DROP_EFFECTS[DROP_EFFECT.NONE];
if (dispatchDragEvent("dragstart", this._sourceNode, this._lastTouchEvent, this._dragDataStore, this._dataTransfer)) {
console.log("dnd-poly: dragstart cancelled");
// dragstart has been prevented -> cancel d'n'd
this._dragOperationState = DragOperationState.CANCELLED;
this._cleanup();
return false;
}
updateCentroidCoordinatesOfTouchesIn("page", this._lastTouchEvent, this._dragImagePageCoordinates);
this._dragImage = createDragImage(dragImageSrc);
this._dragImageTransforms = extractTransformStyles(this._dragImage);
if (!this._dragImageOffset) {
// apply specific offset
if (this._config.dragImageOffset) {
this._dragImageOffset = {
x: this._config.dragImageOffset.x,
y: this._config.dragImageOffset.y
};
}
// center drag image on touch coordinates
else if (this._config.dragImageCenterOnTouch) {
const cs = getComputedStyle(dragImageSrc);
this._dragImageOffset = {
x: 0 - parseInt(cs.marginLeft, 10),
y: 0 - parseInt(cs.marginTop, 10)
};
}
// by default initialize drag image offset the same as desktop
else {
const targetRect = dragImageSrc.getBoundingClientRect();
const cs = getComputedStyle(dragImageSrc);
this._dragImageOffset = {
x: targetRect.left - this._initialTouch.clientX - parseInt(cs.marginLeft, 10) + targetRect.width / 2,
y: targetRect.top - this._initialTouch.clientY - parseInt(cs.marginTop, 10) + targetRect.height / 2
};
}
}
translateDragImage(this._dragImage, this._dragImagePageCoordinates, this._dragImageTransforms, this._dragImageOffset, this._config.dragImageCenterOnTouch);
document.body.appendChild(this._dragImage);
// 10. Initiate the drag-and-drop operation in a manner consistent with platform conventions, and as described below.
this._iterationIntervalId = setInterval(() => {
// If the user agent is still performing the previous iteration of the sequence (if any) when the next iteration becomes due,
// abort these steps for this iteration (effectively "skipping missed frames" of the drag-and-drop operation).
if (this._iterationLock) {
console.log("dnd-poly: iteration skipped because previous iteration hast not yet finished.");
return;
}
this._iterationLock = true;
this._dragAndDropProcessModelIteration();
this._iterationLock = false;
}, this._config.iterationInterval);
return true;
}
private _cleanup() {
console.log("dnd-poly: cleanup");
if (this._iterationIntervalId) {
clearInterval(this._iterationIntervalId);
this._iterationIntervalId = null;
}
removeDocumentListener("touchmove", this._touchMoveHandler);
removeDocumentListener("touchend", this._touchEndOrCancelHandler);
removeDocumentListener("touchcancel", this._touchEndOrCancelHandler);
if (this._dragImage) {
this._dragImage.parentNode.removeChild(this._dragImage);
this._dragImage = null;
}
this._dragOperationEndedCb(this._config, this._lastTouchEvent, this._dragOperationState);
}
//</editor-fold>
//<editor-fold desc="touch handlers">
private _onTouchMove(event:TouchEvent) {
// filter unrelated touches
if (isTouchIdentifierContainedInTouchEvent(event, this._initialTouch.identifier) === false) {
return;
}
// update the reference to the last received touch event
this._lastTouchEvent = event;
// drag operation did not start yet but on movement it should start
if (this._dragOperationState === DragOperationState.POTENTIAL) {
let startDrag:boolean;
// is a lifecycle hook present?
if (this._config.dragStartConditionOverride) {
try {
startDrag = this._config.dragStartConditionOverride(event);
}
catch (e) {
console.error("dnd-poly: error in dragStartConditionOverride hook: " + e);
startDrag = false;
}
}
else {
// by default only allow a single moving finger to initiate a drag operation
startDrag = (event.touches.length === 1);
}
if (!startDrag) {
this._cleanup();
return;
}
// setup will return true when drag operation starts
if (this._setup() === true) {
// prevent scrolling when drag operation starts
this._initialEvent.preventDefault();
event.preventDefault();
}
return;
}
console.log("dnd-poly: moving draggable..");
// we emulate d'n'd so we dont want any defaults to apply
event.preventDefault();
// populate shared coordinates from touch event
updateCentroidCoordinatesOfTouchesIn("client", event, this._currentHotspotCoordinates);
updateCentroidCoordinatesOfTouchesIn("page", event, this._dragImagePageCoordinates);
if (this._config.dragImageTranslateOverride) {
try {
let handledDragImageTranslate = false;
this._config.dragImageTranslateOverride(
event,
{
x: this._currentHotspotCoordinates.x,
y: this._currentHotspotCoordinates.y
},
this._immediateUserSelection,
(offsetX:number, offsetY:number) => {
// preventing translation of drag image when there was a drag operation cleanup meanwhile
if (!this._dragImage) {
return;
}
handledDragImageTranslate = true;
this._currentHotspotCoordinates.x += offsetX;
this._currentHotspotCoordinates.y += offsetY;
this._dragImagePageCoordinates.x += offsetX;
this._dragImagePageCoordinates.y += offsetY;
translateDragImage(
this._dragImage,
this._dragImagePageCoordinates,
this._dragImageTransforms,
this._dragImageOffset,
this._config.dragImageCenterOnTouch
);
}
);
if (handledDragImageTranslate) {
return;
}
}
catch (e) {
console.log("dnd-poly: error in dragImageTranslateOverride hook: " + e);
}
}
translateDragImage(this._dragImage, this._dragImagePageCoordinates, this._dragImageTransforms, this._dragImageOffset, this._config.dragImageCenterOnTouch);
}
private _onTouchEndOrCancel(event:TouchEvent) {
// filter unrelated touches
if (isTouchIdentifierContainedInTouchEvent(event, this._initialTouch.identifier) === false) {
return;
}
// let the dragImageTranslateOverride know that its over
if (this._config.dragImageTranslateOverride) {
try {
/* tslint:disable */
this._config.dragImageTranslateOverride(undefined, undefined, undefined, function () {
});
}
catch (e) {
console.log("dnd-poly: error in dragImageTranslateOverride hook: " + e);
}
}
// drag operation did not even start
if (this._dragOperationState === DragOperationState.POTENTIAL) {
this._cleanup();
return;
}
// we emulate d'n'd so we dont want any defaults to apply
event.preventDefault();
this._dragOperationState = (event.type === "touchcancel") ? DragOperationState.CANCELLED : DragOperationState.ENDED;
}
//</editor-fold>
//<editor-fold desc="dnd spec logic">
/**
* according to https://html.spec.whatwg.org/multipage/interaction.html#drag-and-drop-processing-model
*/
private _dragAndDropProcessModelIteration():void {
if (DEBUG) {
var debug_class = CLASS_PREFIX + "debug",
debug_class_user_selection = CLASS_PREFIX + "immediate-user-selection",
debug_class_drop_target = CLASS_PREFIX + "current-drop-target";
}
const previousDragOperation = this._currentDragOperation;
// Fire a DND event named drag event at the source node.
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = DROP_EFFECTS[DROP_EFFECT.NONE];
const dragCancelled = dispatchDragEvent("drag", this._sourceNode, this._lastTouchEvent, this._dragDataStore, this._dataTransfer);
if (dragCancelled) {
console.log("dnd-poly: drag event cancelled.");
// If this event is canceled, the user agent must set the current drag operation to "none" (no drag operation).
this._currentDragOperation = DROP_EFFECTS[DROP_EFFECT.NONE];
}
// Otherwise, if the user ended the drag-and-drop operation (e.g. by releasing the mouse button in a mouse-driven drag-and-drop interface),
// or if the drag event was canceled, then this will be the last iteration.
if (dragCancelled || this._dragOperationState === DragOperationState.ENDED || this._dragOperationState === DragOperationState.CANCELLED) {
const dragFailed = this._dragOperationEnded(this._dragOperationState);
// if drag failed transition snap back
if (dragFailed) {
applyDragImageSnapback(this._sourceNode, this._dragImage, this._dragImageTransforms, () => {
this._finishDragOperation();
});
return;
}
// Otherwise immediately
// Fire a DND event named dragend at the source node.
this._finishDragOperation();
return;
}
// If the drag event was not canceled and the user has not ended the drag-and-drop operation,
// check the state of the drag-and-drop operation, as follows:
const newUserSelection:HTMLElement = <HTMLElement>document.elementFromPoint(this._currentHotspotCoordinates.x, this._currentHotspotCoordinates.y);
console.log("dnd-poly: new immediate user selection is: " + newUserSelection);
const previousTargetElement = this._currentDropTarget;
// If the user is indicating a different immediate user selection than during the last iteration (or if this is the first iteration),
// and if this immediate user selection is not the same as the current target element,
// then fire a DND event named dragexit at the current target element,
// and then update the current target element as follows:
if (newUserSelection !== this._immediateUserSelection && newUserSelection !== this._currentDropTarget) {
if (DEBUG) {
if (this._immediateUserSelection) {
this._immediateUserSelection.classList.remove(debug_class_user_selection);
}
if (newUserSelection) {
newUserSelection.classList.add(debug_class);
newUserSelection.classList.add(debug_class_user_selection);
}
}
this._immediateUserSelection = newUserSelection;
if (this._currentDropTarget !== null) {
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = DROP_EFFECTS[DROP_EFFECT.NONE];
dispatchDragEvent("dragexit", this._currentDropTarget, this._lastTouchEvent, this._dragDataStore, this._dataTransfer, false);
}
// If the new immediate user selection is null
if (this._immediateUserSelection === null) {
//Set the current target element to null also.
this._currentDropTarget = this._immediateUserSelection;
console.log("dnd-poly: current drop target changed to null");
}
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// If the new immediate user selection is in a non-DOM document or application
// else if() {
// Set the current target element to the immediate user selection.
// this.currentDropTarget = this.immediateUserSelection;
// return;
// }
// Otherwise
else {
// Fire a DND event named dragenter at the immediate user selection.
//the polyfill cannot determine if a handler even exists as browsers do to silently
// allow drop when no listener existed, so this event MUST be handled by the client
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = determineDropEffect(this._dragDataStore._effectAllowed, this._sourceNode);
if (dispatchDragEvent("dragenter", this._immediateUserSelection, this._lastTouchEvent, this._dragDataStore, this._dataTransfer)) {
console.log("dnd-poly: dragenter default prevented");
// If the event is canceled, then set the current target element to the immediate user selection.
this._currentDropTarget = this._immediateUserSelection;
this._currentDragOperation = determineDragOperation(this._dataTransfer.effectAllowed, this._dataTransfer.dropEffect);
}
// Otherwise, run the appropriate step from the following list:
else {
// NO DROPZONE SUPPORT SINCE NATIVE IMPLEMENTATIONS IN BROWSERS ALSO DO NOT
//console.log( "dnd-poly: dragenter not prevented, searching for dropzone.." );
//var newTarget = DragOperationController.FindDropzoneElement( this.immediateUserSelection );
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// If the current target element is a text field (e.g. textarea, or an input element whose type attribute is in the Text state) or an
// editable element, and the drag data store item list has an item with the drag data item type string "text/plain" and the drag data
// item kind Plain Unicode string
//if( ElementIsTextDropzone( this.immediateUserSelection, this.dragDataStore ) ) {
//Set the current target element to the immediate user selection anyway.
//this.currentDropTarget = this.immediateUserSelection;
//}
//else
// If the current target element is an element with a dropzone attribute that matches the drag data store
//if( newTarget === this.immediateUserSelection &&
// DragOperationController.GetOperationForMatchingDropzone( this.immediateUserSelection, this.dragDataStore ) !== "none" ) {
// Set the current target element to the immediate user selection anyway.
// this.currentDropTarget = this.immediateUserSelection;
//}
// If the immediate user selection is an element that itself has an ancestor element
// with a dropzone attribute that matches the drag data store
// NO DROPZONE SUPPORT SINCE NATIVE IMPLEMENTATIONS IN BROWSERS ALSO DO NOT
//else if( newTarget !== null && DragOperationController.GetOperationForMatchingDropzone( newTarget, this.dragDataStore ) ) {
// If the immediate user selection is new target, then leave the current target element unchanged.
// Otherwise, fire a DND event named dragenter at new target, with the current target element
// as the specific related target. Then, set the current target element to new target,
// regardless of whether that event was canceled or not.
//this.dragenter( newTarget, this.currentDropTarget );
//this.currentDropTarget = newTarget;
//}
// If the current target element is not the body element
//else
if (this._immediateUserSelection !== document.body) {
// Fire a DND event named dragenter at the body element, and set the current target element to the body element, regardless of
// whether that event was canceled or not.
// Note: If the body element is null, then the event will be fired at the Document object (as
// required by the definition of the body element), but the current target element would be set to null, not the Document object.
// We do not listen to what the spec says here because this results in doubled events on the body/document because if the first one
// was not cancelled it will have bubbled up to the body already ;)
// this.dragenter( window.document.body );
this._currentDropTarget = document.body;
}
// Otherwise
//else {
// leave the current drop target unchanged
//}
}
}
}
// If the previous step caused the current target element to change,
// and if the previous target element was not null or a part of a non-DOM document,
// then fire a DND event named dragleave at the previous target element.
if (previousTargetElement !== this._currentDropTarget && (isDOMElement(previousTargetElement) )) {
if (DEBUG) {
previousTargetElement.classList.remove(debug_class_drop_target);
}
console.log("dnd-poly: current drop target changed.");
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = DROP_EFFECTS[DROP_EFFECT.NONE];
dispatchDragEvent("dragleave", previousTargetElement, this._lastTouchEvent, this._dragDataStore, this._dataTransfer, false, this._currentDropTarget);
}
// If the current target element is a DOM element, then fire a DND event named dragover at this current target element.
if (isDOMElement(this._currentDropTarget)) {
if (DEBUG) {
this._currentDropTarget.classList.add(debug_class);
this._currentDropTarget.classList.add(debug_class_drop_target);
}
// If the dragover event is not canceled, run the appropriate step from the following list:
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = determineDropEffect(this._dragDataStore._effectAllowed, this._sourceNode);
if (dispatchDragEvent("dragover", this._currentDropTarget, this._lastTouchEvent, this._dragDataStore, this._dataTransfer) === false) {
console.log("dnd-poly: dragover not prevented on possible drop-target.");
// NO DROPZONE SUPPORT SINCE NATIVE IMPLEMENTATIONS IN BROWSERS ALSO DO NOT
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// If the current target element is a text field (e.g. textarea, or an input element whose type attribute is in the Text state) or
// an editable element, and the drag data store item list has an item with the drag data item type string "text/plain" and the drag
// data item kind Plain Unicode string
//if( ElementIsTextDropzone( this.currentDropTarget, this.dragDataStore ) ) {
// Set the current drag operation to either "copy" or "move", as appropriate given the platform conventions.
//this.currentDragOperation = "copy"; //or move. spec says its platform specific behaviour.
//}
//else {
// If the current target element is an element with a dropzone attribute that matches the drag data store
//this.currentDragOperation = DragOperationController.GetOperationForMatchingDropzone( this.currentDropTarget, this.dragDataStore );
//}
// when dragover is not prevented and no dropzones are there, no drag operation
this._currentDragOperation = DROP_EFFECTS[DROP_EFFECT.NONE];
}
// Otherwise (if the dragover event is canceled), set the current drag operation based on the values of the effectAllowed and
// dropEffect attributes of the DragEvent object's dataTransfer object as they stood after the event dispatch finished
else {
console.log("dnd-poly: dragover prevented.");
this._currentDragOperation = determineDragOperation(this._dataTransfer.effectAllowed, this._dataTransfer.dropEffect);
}
}
console.log("dnd-poly: d'n'd iteration ended. current drag operation: " + this._currentDragOperation);
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// Otherwise, if the current target element is not a DOM element, use platform-specific mechanisms to determine what drag operation is
// being performed (none, copy, link, or move), and set the current drag operation accordingly.
//Update the drag feedback (e.g. the mouse cursor) to match the current drag operation, as follows:
// ---------------------------------------------------------------------------------------------------------
// Drag operation | Feedback
// "copy" | Data will be copied if dropped here.
// "link" | Data will be linked if dropped here.
// "move" | Data will be moved if dropped here.
// "none" | No operation allowed, dropping here will cancel the drag-and-drop operation.
// ---------------------------------------------------------------------------------------------------------
if (previousDragOperation !== this._currentDragOperation) {
this._dragImage.classList.remove(CLASS_PREFIX + previousDragOperation);
}
const currentDragOperationClass = CLASS_PREFIX + this._currentDragOperation;
if (this._dragImage.classList.contains(currentDragOperationClass) === false) {
this._dragImage.classList.add(currentDragOperationClass);
}
}
/**
* according to https://html.spec.whatwg.org/multipage/interaction.html#drag-and-drop-processing-model
*/
private _dragOperationEnded(state:DragOperationState):boolean {
console.log("dnd-poly: drag operation end detected with " + this._currentDragOperation);
if (DEBUG) {
var debug_class_user_selection = CLASS_PREFIX + "immediate-user-selection",
debug_class_drop_target = CLASS_PREFIX + "current-drop-target";
if (this._currentDropTarget) {
this._currentDropTarget.classList.remove(debug_class_drop_target);
}
if (this._immediateUserSelection) {
this._immediateUserSelection.classList.remove(debug_class_user_selection);
}
}
//var dropped:boolean = undefined;
// Run the following steps, then stop the drag-and-drop operation:
// If the current drag operation is "none" (no drag operation), or,
// if the user ended the drag-and-drop operation by canceling it (e.g. by hitting the Escape key), or
// if the current target element is null, then the drag operation failed.
const dragFailed = (this._currentDragOperation === DROP_EFFECTS[DROP_EFFECT.NONE]
|| this._currentDropTarget === null
|| state === DragOperationState.CANCELLED);
if (dragFailed) {
// Run these substeps:
// Let dropped be false.
//dropped = false;
// If the current target element is a DOM element, fire a DND event named dragleave at it;
if (isDOMElement(this._currentDropTarget)) {
this._dragDataStore._mode = DragDataStoreMode.PROTECTED;
this._dataTransfer.dropEffect = DROP_EFFECTS[DROP_EFFECT.NONE];
dispatchDragEvent("dragleave", this._currentDropTarget, this._lastTouchEvent, this._dragDataStore, this._dataTransfer, false);
}
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// otherwise, if it is not null, use platform-specific conventions for drag cancellation.
//else if( this.currentDropTarget !== null ) {
//}
}
// Otherwise, the drag operation was as success; run these substeps:
else {
// Let dropped be true.
//dropped = true;
// If the current target element is a DOM element, fire a DND event named drop at it;
if (isDOMElement(this._currentDropTarget)) {
// If the event is canceled, set the current drag operation to the value of the dropEffect attribute of the
// DragEvent object's dataTransfer object as it stood after the event dispatch finished.
this._dragDataStore._mode = DragDataStoreMode.READONLY;
this._dataTransfer.dropEffect = this._currentDragOperation;
if (dispatchDragEvent("drop", this._currentDropTarget, this._lastTouchEvent, this._dragDataStore, this._dataTransfer) ===
true) {
this._currentDragOperation = this._dataTransfer.dropEffect;
}
// Otherwise, the event is not canceled; perform the event's default action, which depends on the exact target as follows:
else {
// THIS IS SKIPPED SINCE SUPPORT IS ONLY AVAILABLE FOR DOM ELEMENTS
// If the current target element is a text field (e.g. textarea, or an input element whose type attribute is in the Text state)
// or an editable element,
// and the drag data store item list has an item with the drag data item type string "text/plain"
// and the drag data item kind Plain Unicode string
//if( ElementIsTextDropzone( this.currentDropTarget, this.dragDataStore ) ) {
// Insert the actual data of the first item in the drag data store item list to have a drag data item type string of
// "text/plain" and a drag data item kind that is Plain Unicode string into the text field or editable element in a manner
// consistent with platform-specific conventions (e.g. inserting it at the current mouse cursor position, or inserting it at
// the end of the field).
//}
// Otherwise
//else {
// Reset the current drag operation to "none".
this._currentDragOperation = DROP_EFFECTS[DROP_EFFECT.NONE];