gridstack 7.3.0 → 8.0.1

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.
Files changed (196) hide show
  1. package/README.md +9 -2
  2. package/dist/dd-base-impl.d.ts +20 -20
  3. package/dist/dd-base-impl.js +31 -35
  4. package/dist/dd-base-impl.js.map +1 -1
  5. package/dist/dd-draggable.d.ts +28 -28
  6. package/dist/dd-draggable.js +336 -344
  7. package/dist/dd-draggable.js.map +1 -1
  8. package/dist/dd-droppable.d.ts +26 -26
  9. package/dist/dd-droppable.js +146 -148
  10. package/dist/dd-droppable.js.map +1 -1
  11. package/dist/dd-element.d.ts +27 -27
  12. package/dist/dd-element.js +90 -94
  13. package/dist/dd-element.js.map +1 -1
  14. package/dist/dd-gridstack.d.ts +34 -34
  15. package/dist/dd-gridstack.js +127 -124
  16. package/dist/dd-gridstack.js.map +1 -1
  17. package/dist/dd-manager.d.ts +22 -22
  18. package/dist/dd-manager.js +9 -13
  19. package/dist/dd-manager.js.map +1 -1
  20. package/dist/dd-resizable-handle.d.ts +14 -14
  21. package/dist/dd-resizable-handle.js +102 -105
  22. package/dist/dd-resizable-handle.js.map +1 -1
  23. package/dist/dd-resizable.d.ts +28 -28
  24. package/dist/dd-resizable.js +290 -296
  25. package/dist/dd-resizable.js.map +1 -1
  26. package/dist/dd-touch.d.ts +33 -33
  27. package/dist/dd-touch.js +173 -182
  28. package/dist/dd-touch.js.map +1 -1
  29. package/dist/es5/dd-base-impl.d.ts +20 -20
  30. package/dist/es5/dd-base-impl.js +40 -40
  31. package/dist/es5/dd-base-impl.js.map +1 -1
  32. package/dist/es5/dd-draggable.d.ts +28 -28
  33. package/dist/es5/dd-draggable.js +366 -367
  34. package/dist/es5/dd-draggable.js.map +1 -1
  35. package/dist/es5/dd-droppable.d.ts +26 -26
  36. package/dist/es5/dd-droppable.js +181 -179
  37. package/dist/es5/dd-droppable.js.map +1 -1
  38. package/dist/es5/dd-element.d.ts +27 -27
  39. package/dist/es5/dd-element.js +95 -95
  40. package/dist/es5/dd-element.js.map +1 -1
  41. package/dist/es5/dd-gridstack.d.ts +34 -34
  42. package/dist/es5/dd-gridstack.js +144 -144
  43. package/dist/es5/dd-gridstack.js.map +1 -1
  44. package/dist/es5/dd-manager.d.ts +22 -22
  45. package/dist/es5/dd-manager.js +16 -16
  46. package/dist/es5/dd-manager.js.map +1 -1
  47. package/dist/es5/dd-resizable-handle.d.ts +14 -14
  48. package/dist/es5/dd-resizable-handle.js +105 -106
  49. package/dist/es5/dd-resizable-handle.js.map +1 -1
  50. package/dist/es5/dd-resizable.d.ts +28 -28
  51. package/dist/es5/dd-resizable.js +317 -318
  52. package/dist/es5/dd-resizable.js.map +1 -1
  53. package/dist/es5/dd-touch.d.ts +33 -33
  54. package/dist/es5/dd-touch.js +185 -185
  55. package/dist/es5/dd-touch.js.map +1 -1
  56. package/dist/es5/gridstack-all.js +1 -1
  57. package/dist/es5/gridstack-all.js.LICENSE.txt +1 -1
  58. package/dist/es5/gridstack-all.js.map +1 -1
  59. package/dist/es5/gridstack-engine.d.ts +102 -102
  60. package/dist/es5/gridstack-engine.js +1000 -995
  61. package/dist/es5/gridstack-engine.js.map +1 -1
  62. package/dist/es5/gridstack-poly.js +1 -1
  63. package/dist/es5/gridstack.d.ts +392 -376
  64. package/dist/es5/gridstack.js +2254 -2233
  65. package/dist/es5/gridstack.js.map +1 -1
  66. package/dist/es5/types.d.ts +279 -284
  67. package/dist/es5/types.js +47 -35
  68. package/dist/es5/types.js.map +1 -1
  69. package/dist/es5/utils.d.ts +95 -93
  70. package/dist/es5/utils.js +600 -569
  71. package/dist/es5/utils.js.map +1 -1
  72. package/dist/gridstack-all.js +1 -1
  73. package/dist/gridstack-all.js.LICENSE.txt +1 -1
  74. package/dist/gridstack-all.js.map +1 -1
  75. package/dist/gridstack-engine.d.ts +102 -102
  76. package/dist/gridstack-engine.js +950 -954
  77. package/dist/gridstack-engine.js.map +1 -1
  78. package/dist/gridstack-extra.css +0 -390
  79. package/dist/gridstack-extra.min.css +1 -1
  80. package/dist/gridstack.css +19 -97
  81. package/dist/gridstack.d.ts +392 -376
  82. package/dist/gridstack.js +2155 -2157
  83. package/dist/gridstack.js.map +1 -1
  84. package/dist/gridstack.min.css +1 -1
  85. package/dist/ng/README.md +154 -0
  86. package/dist/ng/gridstack-item.component.d.ts +29 -0
  87. package/dist/ng/gridstack-item.component.js +65 -0
  88. package/dist/ng/gridstack-item.component.js.map +1 -0
  89. package/dist/ng/gridstack.component.d.ts +118 -0
  90. package/dist/ng/gridstack.component.js +245 -0
  91. package/dist/ng/gridstack.component.js.map +1 -0
  92. package/dist/src/gridstack-extra.scss +0 -2
  93. package/dist/src/gridstack.scss +14 -9
  94. package/dist/types.d.ts +279 -284
  95. package/dist/types.js +44 -35
  96. package/dist/types.js.map +1 -1
  97. package/dist/utils.d.ts +95 -93
  98. package/dist/utils.js +546 -527
  99. package/dist/utils.js.map +1 -1
  100. package/{dist → dist_save}/angular/gridstack-item.component.ts +5 -3
  101. package/{dist → dist_save}/angular/gridstack.component.ts +32 -18
  102. package/dist_save/dd-base-impl.d.ts +20 -0
  103. package/dist_save/dd-base-impl.js +36 -0
  104. package/dist_save/dd-base-impl.js.map +1 -0
  105. package/dist_save/dd-draggable.d.ts +28 -0
  106. package/dist_save/dd-draggable.js +343 -0
  107. package/dist_save/dd-draggable.js.map +1 -0
  108. package/dist_save/dd-droppable.d.ts +26 -0
  109. package/dist_save/dd-droppable.js +149 -0
  110. package/dist_save/dd-droppable.js.map +1 -0
  111. package/dist_save/dd-element.d.ts +27 -0
  112. package/dist_save/dd-element.js +95 -0
  113. package/dist_save/dd-element.js.map +1 -0
  114. package/dist_save/dd-gridstack.d.ts +34 -0
  115. package/dist_save/dd-gridstack.js +125 -0
  116. package/dist_save/dd-gridstack.js.map +1 -0
  117. package/dist_save/dd-manager.d.ts +22 -0
  118. package/dist_save/dd-manager.js +14 -0
  119. package/dist_save/dd-manager.js.map +1 -0
  120. package/dist_save/dd-resizable-handle.d.ts +14 -0
  121. package/dist_save/dd-resizable-handle.js +106 -0
  122. package/dist_save/dd-resizable-handle.js.map +1 -0
  123. package/dist_save/dd-resizable.d.ts +28 -0
  124. package/dist_save/dd-resizable.js +294 -0
  125. package/dist_save/dd-resizable.js.map +1 -0
  126. package/dist_save/dd-touch.d.ts +33 -0
  127. package/dist_save/dd-touch.js +183 -0
  128. package/dist_save/dd-touch.js.map +1 -0
  129. package/dist_save/es5/dd-base-impl.d.ts +20 -0
  130. package/dist_save/es5/dd-base-impl.js +41 -0
  131. package/dist_save/es5/dd-base-impl.js.map +1 -0
  132. package/dist_save/es5/dd-draggable.d.ts +28 -0
  133. package/dist_save/es5/dd-draggable.js +366 -0
  134. package/dist_save/es5/dd-draggable.js.map +1 -0
  135. package/dist_save/es5/dd-droppable.d.ts +26 -0
  136. package/dist_save/es5/dd-droppable.js +180 -0
  137. package/dist_save/es5/dd-droppable.js.map +1 -0
  138. package/dist_save/es5/dd-element.d.ts +27 -0
  139. package/dist_save/es5/dd-element.js +96 -0
  140. package/dist_save/es5/dd-element.js.map +1 -0
  141. package/dist_save/es5/dd-gridstack.d.ts +34 -0
  142. package/dist_save/es5/dd-gridstack.js +145 -0
  143. package/dist_save/es5/dd-gridstack.js.map +1 -0
  144. package/dist_save/es5/dd-manager.d.ts +22 -0
  145. package/dist_save/es5/dd-manager.js +17 -0
  146. package/dist_save/es5/dd-manager.js.map +1 -0
  147. package/dist_save/es5/dd-resizable-handle.d.ts +14 -0
  148. package/dist_save/es5/dd-resizable-handle.js +107 -0
  149. package/dist_save/es5/dd-resizable-handle.js.map +1 -0
  150. package/dist_save/es5/dd-resizable.d.ts +28 -0
  151. package/dist_save/es5/dd-resizable.js +316 -0
  152. package/dist_save/es5/dd-resizable.js.map +1 -0
  153. package/dist_save/es5/dd-touch.d.ts +33 -0
  154. package/dist_save/es5/dd-touch.js +186 -0
  155. package/dist_save/es5/dd-touch.js.map +1 -0
  156. package/dist_save/es5/gridstack-all.js +3 -0
  157. package/dist_save/es5/gridstack-all.js.LICENSE.txt +7 -0
  158. package/dist_save/es5/gridstack-all.js.map +1 -0
  159. package/dist_save/es5/gridstack-engine.d.ts +102 -0
  160. package/dist_save/es5/gridstack-engine.js +997 -0
  161. package/dist_save/es5/gridstack-engine.js.map +1 -0
  162. package/dist_save/es5/gridstack-poly.js +356 -0
  163. package/dist_save/es5/gridstack.d.ts +376 -0
  164. package/dist_save/es5/gridstack.js +2238 -0
  165. package/dist_save/es5/gridstack.js.map +1 -0
  166. package/dist_save/es5/types.d.ts +284 -0
  167. package/dist_save/es5/types.js +36 -0
  168. package/dist_save/es5/types.js.map +1 -0
  169. package/dist_save/es5/utils.d.ts +95 -0
  170. package/dist_save/es5/utils.js +590 -0
  171. package/dist_save/es5/utils.js.map +1 -0
  172. package/dist_save/gridstack-all.js +3 -0
  173. package/dist_save/gridstack-all.js.LICENSE.txt +7 -0
  174. package/dist_save/gridstack-all.js.map +1 -0
  175. package/dist_save/gridstack-engine.d.ts +102 -0
  176. package/dist_save/gridstack-engine.js +956 -0
  177. package/dist_save/gridstack-engine.js.map +1 -0
  178. package/dist_save/gridstack-extra.css +433 -0
  179. package/dist_save/gridstack-extra.min.css +1 -0
  180. package/dist_save/gridstack.css +226 -0
  181. package/dist_save/gridstack.d.ts +376 -0
  182. package/dist_save/gridstack.js +2162 -0
  183. package/dist_save/gridstack.js.map +1 -0
  184. package/dist_save/gridstack.min.css +1 -0
  185. package/dist_save/src/gridstack-extra.scss +27 -0
  186. package/dist_save/src/gridstack.scss +131 -0
  187. package/dist_save/types.d.ts +284 -0
  188. package/dist_save/types.js +36 -0
  189. package/dist_save/types.js.map +1 -0
  190. package/dist_save/utils.d.ts +95 -0
  191. package/dist_save/utils.js +548 -0
  192. package/dist_save/utils.js.map +1 -0
  193. package/doc/CHANGES.md +20 -0
  194. package/doc/README.md +21 -14
  195. package/package.json +25 -24
  196. /package/{dist → dist_save}/angular/README.md +0 -0
@@ -1,345 +1,337 @@
1
- "use strict";
2
- /**
3
- * dd-draggable.ts 7.3.0
4
- * Copyright (c) 2021-2022 Alain Dumesny - see GridStack root license
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.DDDraggable = void 0;
8
- const dd_manager_1 = require("./dd-manager");
9
- const utils_1 = require("./utils");
10
- const dd_base_impl_1 = require("./dd-base-impl");
11
- const dd_touch_1 = require("./dd-touch");
12
- // let count = 0; // TEST
13
- class DDDraggable extends dd_base_impl_1.DDBaseImplement {
14
- constructor(el, option = {}) {
15
- super();
16
- this.el = el;
17
- this.option = option;
18
- // get the element that is actually supposed to be dragged by
19
- let handleName = option.handle.substring(1);
20
- this.dragEl = el.classList.contains(handleName) ? el : el.querySelector(option.handle) || el;
21
- // create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
22
- this._mouseDown = this._mouseDown.bind(this);
23
- this._mouseMove = this._mouseMove.bind(this);
24
- this._mouseUp = this._mouseUp.bind(this);
25
- this.enable();
26
- }
27
- on(event, callback) {
28
- super.on(event, callback);
29
- }
30
- off(event) {
31
- super.off(event);
32
- }
33
- enable() {
34
- if (this.disabled === false)
35
- return;
36
- super.enable();
37
- this.dragEl.addEventListener('mousedown', this._mouseDown);
38
- if (dd_touch_1.isTouch) {
39
- this.dragEl.addEventListener('touchstart', dd_touch_1.touchstart);
40
- this.dragEl.addEventListener('pointerdown', dd_touch_1.pointerdown);
41
- // this.dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
42
- }
43
- this.el.classList.remove('ui-draggable-disabled');
44
- this.el.classList.add('ui-draggable');
45
- }
46
- disable(forDestroy = false) {
47
- if (this.disabled === true)
48
- return;
49
- super.disable();
50
- this.dragEl.removeEventListener('mousedown', this._mouseDown);
51
- if (dd_touch_1.isTouch) {
52
- this.dragEl.removeEventListener('touchstart', dd_touch_1.touchstart);
53
- this.dragEl.removeEventListener('pointerdown', dd_touch_1.pointerdown);
54
- }
55
- this.el.classList.remove('ui-draggable');
56
- if (!forDestroy)
57
- this.el.classList.add('ui-draggable-disabled');
58
- }
59
- destroy() {
60
- if (this.dragTimeout)
61
- window.clearTimeout(this.dragTimeout);
62
- delete this.dragTimeout;
63
- if (this.dragging)
64
- this._mouseUp(this.mouseDownEvent);
65
- this.disable(true);
66
- delete this.el;
67
- delete this.helper;
68
- delete this.option;
69
- super.destroy();
70
- }
71
- updateOption(opts) {
72
- Object.keys(opts).forEach(key => this.option[key] = opts[key]);
73
- return this;
74
- }
75
- /** @internal call when mouse goes down before a dragstart happens */
76
- _mouseDown(e) {
77
- // don't let more than one widget handle mouseStart
78
- if (dd_manager_1.DDManager.mouseHandled)
79
- return;
80
- if (e.button !== 0)
81
- return true; // only left click
82
- // make sure we are not clicking on known object that handles mouseDown (TODO: make this extensible ?) #2054
83
- const skipMouseDown = ['input', 'textarea', 'button', 'select', 'option'];
84
- const name = e.target.nodeName.toLowerCase();
85
- if (skipMouseDown.find(skip => skip === name))
86
- return true;
87
- // also check for content editable
88
- if (e.target.closest('[contenteditable="true"]'))
89
- return true;
90
- // REMOVE: why would we get the event if it wasn't for us or child ?
91
- // make sure we are clicking on a drag handle or child of it...
92
- // Note: we don't need to check that's handle is an immediate child, as mouseHandled will prevent parents from also handling it (lowest wins)
93
- // let className = this.option.handle.substring(1);
94
- // let el = e.target as HTMLElement;
95
- // while (el && !el.classList.contains(className)) { el = el.parentElement; }
96
- // if (!el) return;
97
- this.mouseDownEvent = e;
98
- delete this.dragging;
99
- delete dd_manager_1.DDManager.dragElement;
100
- delete dd_manager_1.DDManager.dropElement;
101
- // document handler so we can continue receiving moves as the item is 'fixed' position, and capture=true so WE get a first crack
102
- document.addEventListener('mousemove', this._mouseMove, true); // true=capture, not bubble
103
- document.addEventListener('mouseup', this._mouseUp, true);
104
- if (dd_touch_1.isTouch) {
105
- this.dragEl.addEventListener('touchmove', dd_touch_1.touchmove);
106
- this.dragEl.addEventListener('touchend', dd_touch_1.touchend);
107
- }
108
- e.preventDefault();
109
- // preventDefault() prevents blur event which occurs just after mousedown event.
110
- // if an editable content has focus, then blur must be call
111
- if (document.activeElement)
112
- document.activeElement.blur();
113
- dd_manager_1.DDManager.mouseHandled = true;
114
- return true;
115
- }
116
- /** @internal method to call actual drag event */
117
- _callDrag(e) {
118
- if (!this.dragging)
119
- return;
120
- const ev = utils_1.Utils.initEvent(e, { target: this.el, type: 'drag' });
121
- if (this.option.drag) {
122
- this.option.drag(ev, this.ui());
123
- }
124
- this.triggerEvent('drag', ev);
125
- }
126
- /** @internal called when the main page (after successful mousedown) receives a move event to drag the item around the screen */
127
- _mouseMove(e) {
128
- var _a;
129
- // console.log(`${count++} move ${e.x},${e.y}`)
130
- let s = this.mouseDownEvent;
131
- if (this.dragging) {
132
- this._dragFollow(e);
133
- // delay actual grid handling drag until we pause for a while if set
134
- if (dd_manager_1.DDManager.pauseDrag) {
135
- const pause = Number.isInteger(dd_manager_1.DDManager.pauseDrag) ? dd_manager_1.DDManager.pauseDrag : 100;
136
- if (this.dragTimeout)
137
- window.clearTimeout(this.dragTimeout);
138
- this.dragTimeout = window.setTimeout(() => this._callDrag(e), pause);
139
- }
140
- else {
141
- this._callDrag(e);
142
- }
143
- }
144
- else if (Math.abs(e.x - s.x) + Math.abs(e.y - s.y) > 3) {
145
- /**
146
- * don't start unless we've moved at least 3 pixels
147
- */
148
- this.dragging = true;
149
- dd_manager_1.DDManager.dragElement = this;
150
- // if we're dragging an actual grid item, set the current drop as the grid (to detect enter/leave)
151
- let grid = (_a = this.el.gridstackNode) === null || _a === void 0 ? void 0 : _a.grid;
152
- if (grid) {
153
- dd_manager_1.DDManager.dropElement = grid.el.ddElement.ddDroppable;
154
- }
155
- else {
156
- delete dd_manager_1.DDManager.dropElement;
157
- }
158
- this.helper = this._createHelper(e);
159
- this._setupHelperContainmentStyle();
160
- this.dragOffset = this._getDragOffset(e, this.el, this.helperContainment);
161
- const ev = utils_1.Utils.initEvent(e, { target: this.el, type: 'dragstart' });
162
- this._setupHelperStyle(e);
163
- if (this.option.start) {
164
- this.option.start(ev, this.ui());
165
- }
166
- this.triggerEvent('dragstart', ev);
167
- }
168
- e.preventDefault(); // needed otherwise we get text sweep text selection as we drag around
169
- return true;
170
- }
171
- /** @internal call when the mouse gets released to drop the item at current location */
172
- _mouseUp(e) {
173
- var _a;
174
- document.removeEventListener('mousemove', this._mouseMove, true);
175
- document.removeEventListener('mouseup', this._mouseUp, true);
176
- if (dd_touch_1.isTouch) {
177
- this.dragEl.removeEventListener('touchmove', dd_touch_1.touchmove, true);
178
- this.dragEl.removeEventListener('touchend', dd_touch_1.touchend, true);
179
- }
180
- if (this.dragging) {
181
- delete this.dragging;
182
- // reset the drop target if dragging over ourself (already parented, just moving during stop callback below)
183
- if (((_a = dd_manager_1.DDManager.dropElement) === null || _a === void 0 ? void 0 : _a.el) === this.el.parentElement) {
184
- delete dd_manager_1.DDManager.dropElement;
185
- }
186
- this.helperContainment.style.position = this.parentOriginStylePosition || null;
187
- if (this.helper === this.el) {
188
- this._removeHelperStyle();
189
- }
190
- else {
191
- this.helper.remove();
192
- }
193
- const ev = utils_1.Utils.initEvent(e, { target: this.el, type: 'dragstop' });
194
- if (this.option.stop) {
195
- this.option.stop(ev); // NOTE: destroy() will be called when removing item, so expect NULL ptr after!
196
- }
197
- this.triggerEvent('dragstop', ev);
198
- // call the droppable method to receive the item
199
- if (dd_manager_1.DDManager.dropElement) {
200
- dd_manager_1.DDManager.dropElement.drop(e);
201
- }
202
- }
203
- delete this.helper;
204
- delete this.mouseDownEvent;
205
- delete dd_manager_1.DDManager.dragElement;
206
- delete dd_manager_1.DDManager.dropElement;
207
- delete dd_manager_1.DDManager.mouseHandled;
208
- e.preventDefault();
209
- }
210
- /** @internal create a clone copy (or user defined method) of the original drag item if set */
211
- _createHelper(event) {
212
- let helper = this.el;
213
- if (typeof this.option.helper === 'function') {
214
- helper = this.option.helper(event);
215
- }
216
- else if (this.option.helper === 'clone') {
217
- helper = utils_1.Utils.cloneNode(this.el);
218
- }
219
- if (!document.body.contains(helper)) {
220
- utils_1.Utils.appendTo(helper, this.option.appendTo === 'parent' ? this.el.parentNode : this.option.appendTo);
221
- }
222
- if (helper === this.el) {
223
- this.dragElementOriginStyle = DDDraggable.originStyleProp.map(prop => this.el.style[prop]);
224
- }
225
- return helper;
226
- }
227
- /** @internal set the fix position of the dragged item */
228
- _setupHelperStyle(e) {
229
- this.helper.classList.add('ui-draggable-dragging');
230
- // TODO: set all at once with style.cssText += ... ? https://stackoverflow.com/questions/3968593
231
- const style = this.helper.style;
232
- style.pointerEvents = 'none'; // needed for over items to get enter/leave
233
- // style.cursor = 'move'; // TODO: can't set with pointerEvents=none ! (done in CSS as well)
234
- style['min-width'] = 0; // since we no longer relative to our parent and we don't resize anyway (normally 100/#column %)
235
- style.width = this.dragOffset.width + 'px';
236
- style.height = this.dragOffset.height + 'px';
237
- style.willChange = 'left, top';
238
- style.position = 'fixed'; // let us drag between grids by not clipping as parent .grid-stack is position: 'relative'
239
- this._dragFollow(e); // now position it
240
- style.transition = 'none'; // show up instantly
241
- setTimeout(() => {
242
- if (this.helper) {
243
- style.transition = null; // recover animation
244
- }
245
- }, 0);
246
- return this;
247
- }
248
- /** @internal restore back the original style before dragging */
249
- _removeHelperStyle() {
250
- var _a;
251
- this.helper.classList.remove('ui-draggable-dragging');
252
- let node = (_a = this.helper) === null || _a === void 0 ? void 0 : _a.gridstackNode;
253
- // don't bother restoring styles if we're gonna remove anyway...
254
- if (!(node === null || node === void 0 ? void 0 : node._isAboutToRemove) && this.dragElementOriginStyle) {
255
- let helper = this.helper;
256
- // don't animate, otherwise we animate offseted when switching back to 'absolute' from 'fixed'.
257
- // TODO: this also removes resizing animation which doesn't have this issue, but others.
258
- // Ideally both would animate ('move' would immediately restore 'absolute' and adjust coordinate to match,
259
- // then trigger a delay (repaint) to restore to final dest with animate) but then we need to make sure 'resizestop'
260
- // is called AFTER 'transitionend' event is received (see https://github.com/gridstack/gridstack.js/issues/2033)
261
- let transition = this.dragElementOriginStyle['transition'] || null;
262
- helper.style.transition = this.dragElementOriginStyle['transition'] = 'none'; // can't be NULL #1973
263
- DDDraggable.originStyleProp.forEach(prop => helper.style[prop] = this.dragElementOriginStyle[prop] || null);
264
- setTimeout(() => helper.style.transition = transition, 50); // recover animation from saved vars after a pause (0 isn't enough #1973)
265
- }
266
- delete this.dragElementOriginStyle;
267
- return this;
268
- }
269
- /** @internal updates the top/left position to follow the mouse */
270
- _dragFollow(e) {
271
- let containmentRect = { left: 0, top: 0 };
272
- // if (this.helper.style.position === 'absolute') { // we use 'fixed'
273
- // const { left, top } = this.helperContainment.getBoundingClientRect();
274
- // containmentRect = { left, top };
275
- // }
276
- const style = this.helper.style;
277
- const offset = this.dragOffset;
278
- style.left = e.clientX + offset.offsetLeft - containmentRect.left + 'px';
279
- style.top = e.clientY + offset.offsetTop - containmentRect.top + 'px';
280
- }
281
- /** @internal */
282
- _setupHelperContainmentStyle() {
283
- this.helperContainment = this.helper.parentElement;
284
- if (this.helper.style.position !== 'fixed') {
285
- this.parentOriginStylePosition = this.helperContainment.style.position;
286
- if (window.getComputedStyle(this.helperContainment).position.match(/static/)) {
287
- this.helperContainment.style.position = 'relative';
288
- }
289
- }
290
- return this;
291
- }
292
- /** @internal */
293
- _getDragOffset(event, el, parent) {
294
- // in case ancestor has transform/perspective css properties that change the viewpoint
295
- let xformOffsetX = 0;
296
- let xformOffsetY = 0;
297
- if (parent) {
298
- const testEl = document.createElement('div');
299
- utils_1.Utils.addElStyles(testEl, {
300
- opacity: '0',
301
- position: 'fixed',
302
- top: 0 + 'px',
303
- left: 0 + 'px',
304
- width: '1px',
305
- height: '1px',
306
- zIndex: '-999999',
307
- });
308
- parent.appendChild(testEl);
309
- const testElPosition = testEl.getBoundingClientRect();
310
- parent.removeChild(testEl);
311
- xformOffsetX = testElPosition.left;
312
- xformOffsetY = testElPosition.top;
313
- // TODO: scale ?
314
- }
315
- const targetOffset = el.getBoundingClientRect();
316
- return {
317
- left: targetOffset.left,
318
- top: targetOffset.top,
319
- offsetLeft: -event.clientX + targetOffset.left - xformOffsetX,
320
- offsetTop: -event.clientY + targetOffset.top - xformOffsetY,
321
- width: targetOffset.width,
322
- height: targetOffset.height
323
- };
324
- }
325
- /** @internal TODO: set to public as called by DDDroppable! */
326
- ui() {
327
- const containmentEl = this.el.parentElement;
328
- const containmentRect = containmentEl.getBoundingClientRect();
329
- const offset = this.helper.getBoundingClientRect();
330
- return {
331
- position: {
332
- top: offset.top - containmentRect.top,
333
- left: offset.left - containmentRect.left
334
- }
335
- /* not used by GridStack for now...
336
- helper: [this.helper], //The object arr representing the helper that's being dragged.
337
- offset: { top: offset.top, left: offset.left } // Current offset position of the helper as { top, left } object.
338
- */
339
- };
340
- }
341
- }
342
- exports.DDDraggable = DDDraggable;
343
- /** @internal properties we change during dragging, and restore back */
344
- DDDraggable.originStyleProp = ['transition', 'pointerEvents', 'position', 'left', 'top', 'minWidth', 'willChange'];
1
+ /**
2
+ * dd-draggable.ts 8.0.1
3
+ * Copyright (c) 2021-2022 Alain Dumesny - see GridStack root license
4
+ */
5
+ import { DDManager } from './dd-manager';
6
+ import { Utils } from './utils';
7
+ import { DDBaseImplement } from './dd-base-impl';
8
+ import { isTouch, touchend, touchmove, touchstart, pointerdown } from './dd-touch';
9
+ // let count = 0; // TEST
10
+ class DDDraggable extends DDBaseImplement {
11
+ constructor(el, option = {}) {
12
+ super();
13
+ this.el = el;
14
+ this.option = option;
15
+ // get the element that is actually supposed to be dragged by
16
+ let handleName = option.handle.substring(1);
17
+ this.dragEl = el.classList.contains(handleName) ? el : el.querySelector(option.handle) || el;
18
+ // create var event binding so we can easily remove and still look like TS methods (unlike anonymous functions)
19
+ this._mouseDown = this._mouseDown.bind(this);
20
+ this._mouseMove = this._mouseMove.bind(this);
21
+ this._mouseUp = this._mouseUp.bind(this);
22
+ this.enable();
23
+ }
24
+ on(event, callback) {
25
+ super.on(event, callback);
26
+ }
27
+ off(event) {
28
+ super.off(event);
29
+ }
30
+ enable() {
31
+ if (this.disabled === false)
32
+ return;
33
+ super.enable();
34
+ this.dragEl.addEventListener('mousedown', this._mouseDown);
35
+ if (isTouch) {
36
+ this.dragEl.addEventListener('touchstart', touchstart);
37
+ this.dragEl.addEventListener('pointerdown', pointerdown);
38
+ // this.dragEl.style.touchAction = 'none'; // not needed unlike pointerdown doc comment
39
+ }
40
+ this.el.classList.remove('ui-draggable-disabled');
41
+ }
42
+ disable(forDestroy = false) {
43
+ if (this.disabled === true)
44
+ return;
45
+ super.disable();
46
+ this.dragEl.removeEventListener('mousedown', this._mouseDown);
47
+ if (isTouch) {
48
+ this.dragEl.removeEventListener('touchstart', touchstart);
49
+ this.dragEl.removeEventListener('pointerdown', pointerdown);
50
+ }
51
+ if (!forDestroy)
52
+ this.el.classList.add('ui-draggable-disabled');
53
+ }
54
+ destroy() {
55
+ if (this.dragTimeout)
56
+ window.clearTimeout(this.dragTimeout);
57
+ delete this.dragTimeout;
58
+ if (this.dragging)
59
+ this._mouseUp(this.mouseDownEvent);
60
+ this.disable(true);
61
+ delete this.el;
62
+ delete this.helper;
63
+ delete this.option;
64
+ super.destroy();
65
+ }
66
+ updateOption(opts) {
67
+ Object.keys(opts).forEach(key => this.option[key] = opts[key]);
68
+ return this;
69
+ }
70
+ /** @internal call when mouse goes down before a dragstart happens */
71
+ _mouseDown(e) {
72
+ // don't let more than one widget handle mouseStart
73
+ if (DDManager.mouseHandled)
74
+ return;
75
+ if (e.button !== 0)
76
+ return true; // only left click
77
+ // make sure we are not clicking on known object that handles mouseDown (TODO: make this extensible ?) #2054
78
+ const skipMouseDown = ['input', 'textarea', 'button', 'select', 'option'];
79
+ const name = e.target.nodeName.toLowerCase();
80
+ if (skipMouseDown.find(skip => skip === name))
81
+ return true;
82
+ // also check for content editable
83
+ if (e.target.closest('[contenteditable="true"]'))
84
+ return true;
85
+ // REMOVE: why would we get the event if it wasn't for us or child ?
86
+ // make sure we are clicking on a drag handle or child of it...
87
+ // Note: we don't need to check that's handle is an immediate child, as mouseHandled will prevent parents from also handling it (lowest wins)
88
+ // let className = this.option.handle.substring(1);
89
+ // let el = e.target as HTMLElement;
90
+ // while (el && !el.classList.contains(className)) { el = el.parentElement; }
91
+ // if (!el) return;
92
+ this.mouseDownEvent = e;
93
+ delete this.dragging;
94
+ delete DDManager.dragElement;
95
+ delete DDManager.dropElement;
96
+ // document handler so we can continue receiving moves as the item is 'fixed' position, and capture=true so WE get a first crack
97
+ document.addEventListener('mousemove', this._mouseMove, true); // true=capture, not bubble
98
+ document.addEventListener('mouseup', this._mouseUp, true);
99
+ if (isTouch) {
100
+ this.dragEl.addEventListener('touchmove', touchmove);
101
+ this.dragEl.addEventListener('touchend', touchend);
102
+ }
103
+ e.preventDefault();
104
+ // preventDefault() prevents blur event which occurs just after mousedown event.
105
+ // if an editable content has focus, then blur must be call
106
+ if (document.activeElement)
107
+ document.activeElement.blur();
108
+ DDManager.mouseHandled = true;
109
+ return true;
110
+ }
111
+ /** @internal method to call actual drag event */
112
+ _callDrag(e) {
113
+ if (!this.dragging)
114
+ return;
115
+ const ev = Utils.initEvent(e, { target: this.el, type: 'drag' });
116
+ if (this.option.drag) {
117
+ this.option.drag(ev, this.ui());
118
+ }
119
+ this.triggerEvent('drag', ev);
120
+ }
121
+ /** @internal called when the main page (after successful mousedown) receives a move event to drag the item around the screen */
122
+ _mouseMove(e) {
123
+ // console.log(`${count++} move ${e.x},${e.y}`)
124
+ let s = this.mouseDownEvent;
125
+ if (this.dragging) {
126
+ this._dragFollow(e);
127
+ // delay actual grid handling drag until we pause for a while if set
128
+ if (DDManager.pauseDrag) {
129
+ const pause = Number.isInteger(DDManager.pauseDrag) ? DDManager.pauseDrag : 100;
130
+ if (this.dragTimeout)
131
+ window.clearTimeout(this.dragTimeout);
132
+ this.dragTimeout = window.setTimeout(() => this._callDrag(e), pause);
133
+ }
134
+ else {
135
+ this._callDrag(e);
136
+ }
137
+ }
138
+ else if (Math.abs(e.x - s.x) + Math.abs(e.y - s.y) > 3) {
139
+ /**
140
+ * don't start unless we've moved at least 3 pixels
141
+ */
142
+ this.dragging = true;
143
+ DDManager.dragElement = this;
144
+ // if we're dragging an actual grid item, set the current drop as the grid (to detect enter/leave)
145
+ let grid = this.el.gridstackNode?.grid;
146
+ if (grid) {
147
+ DDManager.dropElement = grid.el.ddElement.ddDroppable;
148
+ }
149
+ else {
150
+ delete DDManager.dropElement;
151
+ }
152
+ this.helper = this._createHelper(e);
153
+ this._setupHelperContainmentStyle();
154
+ this.dragOffset = this._getDragOffset(e, this.el, this.helperContainment);
155
+ const ev = Utils.initEvent(e, { target: this.el, type: 'dragstart' });
156
+ this._setupHelperStyle(e);
157
+ if (this.option.start) {
158
+ this.option.start(ev, this.ui());
159
+ }
160
+ this.triggerEvent('dragstart', ev);
161
+ }
162
+ e.preventDefault(); // needed otherwise we get text sweep text selection as we drag around
163
+ return true;
164
+ }
165
+ /** @internal call when the mouse gets released to drop the item at current location */
166
+ _mouseUp(e) {
167
+ document.removeEventListener('mousemove', this._mouseMove, true);
168
+ document.removeEventListener('mouseup', this._mouseUp, true);
169
+ if (isTouch) {
170
+ this.dragEl.removeEventListener('touchmove', touchmove, true);
171
+ this.dragEl.removeEventListener('touchend', touchend, true);
172
+ }
173
+ if (this.dragging) {
174
+ delete this.dragging;
175
+ // reset the drop target if dragging over ourself (already parented, just moving during stop callback below)
176
+ if (DDManager.dropElement?.el === this.el.parentElement) {
177
+ delete DDManager.dropElement;
178
+ }
179
+ this.helperContainment.style.position = this.parentOriginStylePosition || null;
180
+ if (this.helper === this.el) {
181
+ this._removeHelperStyle();
182
+ }
183
+ else {
184
+ this.helper.remove();
185
+ }
186
+ const ev = Utils.initEvent(e, { target: this.el, type: 'dragstop' });
187
+ if (this.option.stop) {
188
+ this.option.stop(ev); // NOTE: destroy() will be called when removing item, so expect NULL ptr after!
189
+ }
190
+ this.triggerEvent('dragstop', ev);
191
+ // call the droppable method to receive the item
192
+ if (DDManager.dropElement) {
193
+ DDManager.dropElement.drop(e);
194
+ }
195
+ }
196
+ delete this.helper;
197
+ delete this.mouseDownEvent;
198
+ delete DDManager.dragElement;
199
+ delete DDManager.dropElement;
200
+ delete DDManager.mouseHandled;
201
+ e.preventDefault();
202
+ }
203
+ /** @internal create a clone copy (or user defined method) of the original drag item if set */
204
+ _createHelper(event) {
205
+ let helper = this.el;
206
+ if (typeof this.option.helper === 'function') {
207
+ helper = this.option.helper(event);
208
+ }
209
+ else if (this.option.helper === 'clone') {
210
+ helper = Utils.cloneNode(this.el);
211
+ }
212
+ if (!document.body.contains(helper)) {
213
+ Utils.appendTo(helper, this.option.appendTo === 'parent' ? this.el.parentElement : this.option.appendTo);
214
+ }
215
+ if (helper === this.el) {
216
+ this.dragElementOriginStyle = DDDraggable.originStyleProp.map(prop => this.el.style[prop]);
217
+ }
218
+ return helper;
219
+ }
220
+ /** @internal set the fix position of the dragged item */
221
+ _setupHelperStyle(e) {
222
+ this.helper.classList.add('ui-draggable-dragging');
223
+ // TODO: set all at once with style.cssText += ... ? https://stackoverflow.com/questions/3968593
224
+ const style = this.helper.style;
225
+ style.pointerEvents = 'none'; // needed for over items to get enter/leave
226
+ // style.cursor = 'move'; // TODO: can't set with pointerEvents=none ! (done in CSS as well)
227
+ style['min-width'] = 0; // since we no longer relative to our parent and we don't resize anyway (normally 100/#column %)
228
+ style.width = this.dragOffset.width + 'px';
229
+ style.height = this.dragOffset.height + 'px';
230
+ style.willChange = 'left, top';
231
+ style.position = 'fixed'; // let us drag between grids by not clipping as parent .grid-stack is position: 'relative'
232
+ this._dragFollow(e); // now position it
233
+ style.transition = 'none'; // show up instantly
234
+ setTimeout(() => {
235
+ if (this.helper) {
236
+ style.transition = null; // recover animation
237
+ }
238
+ }, 0);
239
+ return this;
240
+ }
241
+ /** @internal restore back the original style before dragging */
242
+ _removeHelperStyle() {
243
+ this.helper.classList.remove('ui-draggable-dragging');
244
+ let node = this.helper?.gridstackNode;
245
+ // don't bother restoring styles if we're gonna remove anyway...
246
+ if (!node?._isAboutToRemove && this.dragElementOriginStyle) {
247
+ let helper = this.helper;
248
+ // don't animate, otherwise we animate offseted when switching back to 'absolute' from 'fixed'.
249
+ // TODO: this also removes resizing animation which doesn't have this issue, but others.
250
+ // Ideally both would animate ('move' would immediately restore 'absolute' and adjust coordinate to match,
251
+ // then trigger a delay (repaint) to restore to final dest with animate) but then we need to make sure 'resizestop'
252
+ // is called AFTER 'transitionend' event is received (see https://github.com/gridstack/gridstack.js/issues/2033)
253
+ let transition = this.dragElementOriginStyle['transition'] || null;
254
+ helper.style.transition = this.dragElementOriginStyle['transition'] = 'none'; // can't be NULL #1973
255
+ DDDraggable.originStyleProp.forEach(prop => helper.style[prop] = this.dragElementOriginStyle[prop] || null);
256
+ setTimeout(() => helper.style.transition = transition, 50); // recover animation from saved vars after a pause (0 isn't enough #1973)
257
+ }
258
+ delete this.dragElementOriginStyle;
259
+ return this;
260
+ }
261
+ /** @internal updates the top/left position to follow the mouse */
262
+ _dragFollow(e) {
263
+ let containmentRect = { left: 0, top: 0 };
264
+ // if (this.helper.style.position === 'absolute') { // we use 'fixed'
265
+ // const { left, top } = this.helperContainment.getBoundingClientRect();
266
+ // containmentRect = { left, top };
267
+ // }
268
+ const style = this.helper.style;
269
+ const offset = this.dragOffset;
270
+ style.left = e.clientX + offset.offsetLeft - containmentRect.left + 'px';
271
+ style.top = e.clientY + offset.offsetTop - containmentRect.top + 'px';
272
+ }
273
+ /** @internal */
274
+ _setupHelperContainmentStyle() {
275
+ this.helperContainment = this.helper.parentElement;
276
+ if (this.helper.style.position !== 'fixed') {
277
+ this.parentOriginStylePosition = this.helperContainment.style.position;
278
+ if (window.getComputedStyle(this.helperContainment).position.match(/static/)) {
279
+ this.helperContainment.style.position = 'relative';
280
+ }
281
+ }
282
+ return this;
283
+ }
284
+ /** @internal */
285
+ _getDragOffset(event, el, parent) {
286
+ // in case ancestor has transform/perspective css properties that change the viewpoint
287
+ let xformOffsetX = 0;
288
+ let xformOffsetY = 0;
289
+ if (parent) {
290
+ const testEl = document.createElement('div');
291
+ Utils.addElStyles(testEl, {
292
+ opacity: '0',
293
+ position: 'fixed',
294
+ top: 0 + 'px',
295
+ left: 0 + 'px',
296
+ width: '1px',
297
+ height: '1px',
298
+ zIndex: '-999999',
299
+ });
300
+ parent.appendChild(testEl);
301
+ const testElPosition = testEl.getBoundingClientRect();
302
+ parent.removeChild(testEl);
303
+ xformOffsetX = testElPosition.left;
304
+ xformOffsetY = testElPosition.top;
305
+ // TODO: scale ?
306
+ }
307
+ const targetOffset = el.getBoundingClientRect();
308
+ return {
309
+ left: targetOffset.left,
310
+ top: targetOffset.top,
311
+ offsetLeft: -event.clientX + targetOffset.left - xformOffsetX,
312
+ offsetTop: -event.clientY + targetOffset.top - xformOffsetY,
313
+ width: targetOffset.width,
314
+ height: targetOffset.height
315
+ };
316
+ }
317
+ /** @internal TODO: set to public as called by DDDroppable! */
318
+ ui() {
319
+ const containmentEl = this.el.parentElement;
320
+ const containmentRect = containmentEl.getBoundingClientRect();
321
+ const offset = this.helper.getBoundingClientRect();
322
+ return {
323
+ position: {
324
+ top: offset.top - containmentRect.top,
325
+ left: offset.left - containmentRect.left
326
+ }
327
+ /* not used by GridStack for now...
328
+ helper: [this.helper], //The object arr representing the helper that's being dragged.
329
+ offset: { top: offset.top, left: offset.left } // Current offset position of the helper as { top, left } object.
330
+ */
331
+ };
332
+ }
333
+ }
334
+ /** @internal properties we change during dragging, and restore back */
335
+ DDDraggable.originStyleProp = ['transition', 'pointerEvents', 'position', 'left', 'top', 'minWidth', 'willChange'];
336
+ export { DDDraggable };
345
337
  //# sourceMappingURL=dd-draggable.js.map