cuprite 0.19 → 0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,116 +5,111 @@
5
5
  // Unlike the original, a drag ends the way it does in a browser: with `drop` if
6
6
  // the target accepted it, otherwise with `dragleave`, never both [#325].
7
7
 
8
- function rectCenter(rect){
9
- return new DOMPoint(
10
- (rect.left + rect.right)/2,
11
- (rect.top + rect.bottom)/2
12
- );
13
- }
14
-
15
- function pointOnRect(pt, rect) {
16
- var rectPt = rectCenter(rect);
17
- var slope = (rectPt.y - pt.y) / (rectPt.x - pt.x);
18
-
19
- if (pt.x <= rectPt.x) { // left side
20
- var minXy = slope * (rect.left - pt.x) + pt.y;
21
- if (rect.top <= minXy && minXy <= rect.bottom)
22
- return new DOMPoint(rect.left, minXy);
8
+ (function(source, target, step_delay, drop_modifier_keys, drop_offset, callback) {
9
+ function rectCenter(rect){
10
+ return new DOMPoint(
11
+ (rect.left + rect.right)/2,
12
+ (rect.top + rect.bottom)/2
13
+ );
23
14
  }
24
15
 
25
- if (pt.x >= rectPt.x) { // right side
26
- var maxXy = slope * (rect.right - pt.x) + pt.y;
27
- if (rect.top <= maxXy && maxXy <= rect.bottom)
28
- return new DOMPoint(rect.right, maxXy);
16
+ function pointOnRect(pt, rect) {
17
+ var rectPt = rectCenter(rect);
18
+ var slope = (rectPt.y - pt.y) / (rectPt.x - pt.x);
19
+
20
+ if (pt.x <= rectPt.x) { // left side
21
+ var minXy = slope * (rect.left - pt.x) + pt.y;
22
+ if (rect.top <= minXy && minXy <= rect.bottom)
23
+ return new DOMPoint(rect.left, minXy);
24
+ }
25
+
26
+ if (pt.x >= rectPt.x) { // right side
27
+ var maxXy = slope * (rect.right - pt.x) + pt.y;
28
+ if (rect.top <= maxXy && maxXy <= rect.bottom)
29
+ return new DOMPoint(rect.right, maxXy);
30
+ }
31
+
32
+ if (pt.y <= rectPt.y) { // top side
33
+ var minYx = (rect.top - pt.y) / slope + pt.x;
34
+ if (rect.left <= minYx && minYx <= rect.right)
35
+ return new DOMPoint(minYx, rect.top);
36
+ }
37
+
38
+ if (pt.y >= rectPt.y) { // bottom side
39
+ var maxYx = (rect.bottom - pt.y) / slope + pt.x;
40
+ if (rect.left <= maxYx && maxYx <= rect.right)
41
+ return new DOMPoint(maxYx, rect.bottom);
42
+ }
43
+
44
+ return new DOMPoint(pt.x,pt.y);
29
45
  }
30
46
 
31
- if (pt.y <= rectPt.y) { // top side
32
- var minYx = (rect.top - pt.y) / slope + pt.x;
33
- if (rect.left <= minYx && minYx <= rect.right)
34
- return new DOMPoint(minYx, rect.top);
47
+ function dragEnterTarget() {
48
+ target.scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
49
+ var targetRect = target.getBoundingClientRect();
50
+ var sourceCenter = rectCenter(source.getBoundingClientRect());
51
+
52
+ for (var i = 0; i < drop_modifier_keys.length; i++) {
53
+ var key = drop_modifier_keys[i];
54
+ if (key == "control"){
55
+ key = "ctrl"
56
+ }
57
+ opts[key + 'Key'] = true;
58
+ }
59
+
60
+ var dragEnterEvent = new DragEvent('dragenter', opts);
61
+ target.dispatchEvent(dragEnterEvent);
62
+
63
+ // fire 2 dragover events to simulate dragging with a direction
64
+ var entryPoint = pointOnRect(sourceCenter, targetRect)
65
+ var dragOverOpts = Object.assign({clientX: entryPoint.x, clientY: entryPoint.y}, opts);
66
+ var dragOverEvent = new DragEvent('dragover', dragOverOpts);
67
+ target.dispatchEvent(dragOverEvent);
68
+ window.setTimeout(dragOnTarget, step_delay);
35
69
  }
36
70
 
37
- if (pt.y >= rectPt.y) { // bottom side
38
- var maxYx = (rect.bottom - pt.y) / slope + pt.x;
39
- if (rect.left <= maxYx && maxYx <= rect.right)
40
- return new DOMPoint(maxYx, rect.bottom);
71
+ function dropPoint(rect) {
72
+ if (!drop_offset) return rectCenter(rect);
73
+
74
+ var origin = drop_offset.position == 'center' ? rectCenter(rect) : new DOMPoint(rect.left, rect.top);
75
+ return new DOMPoint(origin.x + drop_offset.x, origin.y + drop_offset.y);
41
76
  }
42
77
 
43
- return new DOMPoint(pt.x,pt.y);
44
- }
78
+ function dragOnTarget() {
79
+ var point = dropPoint(target.getBoundingClientRect());
80
+ var dragOverOpts = Object.assign({clientX: point.x, clientY: point.y}, opts);
81
+ var dragOverEvent = new DragEvent('dragover', dragOverOpts);
82
+ target.dispatchEvent(dragOverEvent);
83
+ window.setTimeout(dragEnd, step_delay, dragOverEvent.defaultPrevented, dragOverOpts);
84
+ }
45
85
 
46
- function dragEnterTarget() {
47
- target.scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
48
- var targetRect = target.getBoundingClientRect();
49
- var sourceCenter = rectCenter(source.getBoundingClientRect());
86
+ function dragEnd(drop, dragOverOpts) {
87
+ var dragEndOptions = Object.assign({}, opts, dragOverOpts);
88
+ var dropOrLeaveEvent = new DragEvent(drop ? 'drop' : 'dragleave', dragEndOptions);
89
+ target.dispatchEvent(dropOrLeaveEvent);
90
+ var dragEndEvent = new DragEvent('dragend', dragEndOptions);
91
+ source.dispatchEvent(dragEndEvent);
92
+ callback.call(true);
93
+ }
50
94
 
51
- for (var i = 0; i < drop_modifier_keys.length; i++) {
52
- var key = drop_modifier_keys[i];
53
- if (key == "control"){
54
- key = "ctrl"
55
- }
56
- opts[key + 'Key'] = true;
95
+ var dt = new DataTransfer();
96
+ var opts = { cancelable: true, bubbles: true, dataTransfer: dt };
97
+
98
+ while (source && !source.draggable) {
99
+ source = source.parentElement;
100
+ }
101
+
102
+ if (source.tagName == 'A'){
103
+ dt.setData('text/uri-list', source.href);
104
+ dt.setData('text', source.href);
57
105
  }
106
+ if (source.tagName == 'IMG'){
107
+ dt.setData('text/uri-list', source.src);
108
+ dt.setData('text', source.src);
109
+ }
110
+
111
+ var dragEvent = new DragEvent('dragstart', opts);
112
+ source.dispatchEvent(dragEvent);
58
113
 
59
- var dragEnterEvent = new DragEvent('dragenter', opts);
60
- target.dispatchEvent(dragEnterEvent);
61
-
62
- // fire 2 dragover events to simulate dragging with a direction
63
- var entryPoint = pointOnRect(sourceCenter, targetRect)
64
- var dragOverOpts = Object.assign({clientX: entryPoint.x, clientY: entryPoint.y}, opts);
65
- var dragOverEvent = new DragEvent('dragover', dragOverOpts);
66
- target.dispatchEvent(dragOverEvent);
67
- window.setTimeout(dragOnTarget, step_delay);
68
- }
69
-
70
- function dropPoint(rect) {
71
- if (!drop_offset) return rectCenter(rect);
72
-
73
- var origin = drop_offset.position == 'center' ? rectCenter(rect) : new DOMPoint(rect.left, rect.top);
74
- return new DOMPoint(origin.x + drop_offset.x, origin.y + drop_offset.y);
75
- }
76
-
77
- function dragOnTarget() {
78
- var point = dropPoint(target.getBoundingClientRect());
79
- var dragOverOpts = Object.assign({clientX: point.x, clientY: point.y}, opts);
80
- var dragOverEvent = new DragEvent('dragover', dragOverOpts);
81
- target.dispatchEvent(dragOverEvent);
82
- window.setTimeout(dragEnd, step_delay, dragOverEvent.defaultPrevented, dragOverOpts);
83
- }
84
-
85
- function dragEnd(drop, dragOverOpts) {
86
- var dragEndOptions = Object.assign({}, opts, dragOverOpts);
87
- var dropOrLeaveEvent = new DragEvent(drop ? 'drop' : 'dragleave', dragEndOptions);
88
- target.dispatchEvent(dropOrLeaveEvent);
89
- var dragEndEvent = new DragEvent('dragend', dragEndOptions);
90
- source.dispatchEvent(dragEndEvent);
91
- callback.call(true);
92
- }
93
-
94
- var source = arguments[0],
95
- target = arguments[1],
96
- step_delay = arguments[2],
97
- drop_modifier_keys = arguments[3],
98
- drop_offset = arguments[4],
99
- callback = arguments[5];
100
-
101
- var dt = new DataTransfer();
102
- var opts = { cancelable: true, bubbles: true, dataTransfer: dt };
103
-
104
- while (source && !source.draggable) {
105
- source = source.parentElement;
106
- }
107
-
108
- if (source.tagName == 'A'){
109
- dt.setData('text/uri-list', source.href);
110
- dt.setData('text', source.href);
111
- }
112
- if (source.tagName == 'IMG'){
113
- dt.setData('text/uri-list', source.src);
114
- dt.setData('text', source.src);
115
- }
116
-
117
- var dragEvent = new DragEvent('dragstart', opts);
118
- source.dispatchEvent(dragEvent);
119
-
120
- window.setTimeout(dragEnterTarget, step_delay);
114
+ window.setTimeout(dragEnterTarget, step_delay);
115
+ }).apply(this, arguments);