@oat-sa/tao-core-ui 3.13.3 → 3.13.4
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.
- package/dist/interactUtils.js +176 -0
- package/package.json +1 -1
- package/src/interactUtils.js +184 -0
package/dist/interactUtils.js
CHANGED
|
@@ -119,6 +119,182 @@ define(['jquery', 'lodash', 'interact', 'core/mouseEvent'], function ($, _, inte
|
|
|
119
119
|
domElement.style.transform = 'translate(0px, 0px) translateZ(0px)';
|
|
120
120
|
domElement.setAttribute('data-x', 0);
|
|
121
121
|
domElement.setAttribute('data-y', 0);
|
|
122
|
+
},
|
|
123
|
+
/**
|
|
124
|
+
* Improve touch devices support:
|
|
125
|
+
* - prevent native scroll while dragging
|
|
126
|
+
* - start drag only after longpress:
|
|
127
|
+
* when user is scrolling through the long page, he can accidentally get his finger on the draggable element:
|
|
128
|
+
* this will cause unwanted, unnoticed drag and can mess up his response.
|
|
129
|
+
* @example
|
|
130
|
+
* touchPatch = interactUtils.touchPatchFactory();
|
|
131
|
+
* interact(selector)
|
|
132
|
+
* .draggable({
|
|
133
|
+
onstart: () => {
|
|
134
|
+
touchPatch.onstart();
|
|
135
|
+
},
|
|
136
|
+
onend: () => {
|
|
137
|
+
touchPatch.onend();
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
.actionChecker(touchPatch.actionChecker);
|
|
141
|
+
...
|
|
142
|
+
function destroy() {
|
|
143
|
+
` touchPatch.destroy()
|
|
144
|
+
interact(selector).unset();
|
|
145
|
+
}
|
|
146
|
+
* @returns {Object}
|
|
147
|
+
*/
|
|
148
|
+
touchPatchFactory: function touchPatchFactory() {
|
|
149
|
+
const delayBefore = 300;
|
|
150
|
+
const distanceTolerance = 20; //while waiting for delayBefore, finger can move a little bit
|
|
151
|
+
|
|
152
|
+
interact.pointerMoveTolerance(distanceTolerance);
|
|
153
|
+
let isDragging = false;
|
|
154
|
+
|
|
155
|
+
// webKit requires cancelable `touchmove` events to be added as early as possible
|
|
156
|
+
// alternative: `touch-action: pinch-zoom` css: add it after drag start [?]; or have it always - worse ux
|
|
157
|
+
function touchmoveListener(e) {
|
|
158
|
+
if (isDragging) {
|
|
159
|
+
e.preventDefault();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
window.addEventListener('touchmove', touchmoveListener, {
|
|
163
|
+
passive: false
|
|
164
|
+
});
|
|
165
|
+
function contextmenuListener(e) {
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
/**
|
|
170
|
+
* @param {PointerEvent} pointer
|
|
171
|
+
* @param {PointerEvent} event
|
|
172
|
+
* @param {Object} action
|
|
173
|
+
* @param {Object} interactable
|
|
174
|
+
* @returns {Object}
|
|
175
|
+
*/
|
|
176
|
+
actionChecker: (pointer, event, action, interactable, element) => {
|
|
177
|
+
if (event && action && action.name === 'drag') {
|
|
178
|
+
const isTouch = event.pointerType === 'touch';
|
|
179
|
+
interactable.options[action.name].delay = isTouch ? delayBefore : 0;
|
|
180
|
+
if (isTouch && !element.dataset.noContextMenu) {
|
|
181
|
+
//prevent context menu on longpress
|
|
182
|
+
//this listener can stay forever until the element is destroyed
|
|
183
|
+
element.addEventListener('contextmenu', contextmenuListener);
|
|
184
|
+
element.dataset.noContextMenu = true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return action;
|
|
188
|
+
},
|
|
189
|
+
onstart: () => {
|
|
190
|
+
isDragging = true;
|
|
191
|
+
},
|
|
192
|
+
onend: () => {
|
|
193
|
+
isDragging = false;
|
|
194
|
+
},
|
|
195
|
+
destroy: () => {
|
|
196
|
+
window.removeEventListener('touchmove', touchmoveListener);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
/**
|
|
201
|
+
* Builds a scroll observer that will make sure the dragged element keeps an accurate positioning
|
|
202
|
+
* @example
|
|
203
|
+
* scrollObserver = interactUtils.scrollObserverFactory($container);
|
|
204
|
+
* dragOptions = {
|
|
205
|
+
autoScroll: {
|
|
206
|
+
container: scrollObserver.getScrollContainer().get(0)
|
|
207
|
+
},
|
|
208
|
+
onstart: function (e) {
|
|
209
|
+
scrollObserver.start($activeChoice);
|
|
210
|
+
},
|
|
211
|
+
onend: function (e) {
|
|
212
|
+
scrollObserver.stop();
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
* @param {jQuery} $scrollContainer
|
|
216
|
+
* @returns {scrollObserver}
|
|
217
|
+
*/
|
|
218
|
+
scrollObserverFactory: function scrollObserverFactory($scrollContainer) {
|
|
219
|
+
let currentDraggable = null;
|
|
220
|
+
let beforeY = 0;
|
|
221
|
+
let beforeX = 0;
|
|
222
|
+
let afterY = 0;
|
|
223
|
+
let afterX = 0;
|
|
224
|
+
|
|
225
|
+
// reset the scroll observer context
|
|
226
|
+
function resetScrollObserver() {
|
|
227
|
+
currentDraggable = null;
|
|
228
|
+
beforeY = 0;
|
|
229
|
+
beforeX = 0;
|
|
230
|
+
afterY = 0;
|
|
231
|
+
afterX = 0;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// keep the position of the dragged element accurate with the scroll position
|
|
235
|
+
function onScrollCb() {
|
|
236
|
+
let x;
|
|
237
|
+
let y;
|
|
238
|
+
if (currentDraggable) {
|
|
239
|
+
beforeY = afterY;
|
|
240
|
+
beforeX = afterX;
|
|
241
|
+
if (afterY === 0 && beforeY === 0) beforeY = this.scrollTop;
|
|
242
|
+
if (afterX === 0 && beforeX === 0) beforeX = this.scrollLeft;
|
|
243
|
+
afterY = this.scrollTop;
|
|
244
|
+
afterX = this.scrollLeft;
|
|
245
|
+
y = (parseInt(currentDraggable.getAttribute('data-y'), 10) || 0) + (afterY - beforeY);
|
|
246
|
+
x = (parseInt(currentDraggable.getAttribute('data-x'), 10) || 0) + (afterX - beforeX);
|
|
247
|
+
|
|
248
|
+
// translate the element
|
|
249
|
+
currentDraggable.style.webkitTransform = currentDraggable.style.transform = `translate(${x}px, ${y}px)`;
|
|
250
|
+
|
|
251
|
+
// update the position attributes
|
|
252
|
+
currentDraggable.setAttribute('data-x', x);
|
|
253
|
+
currentDraggable.setAttribute('data-y', y);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// find the scroll container within the parents if any
|
|
258
|
+
$scrollContainer.parents().each(function findScrollContainer() {
|
|
259
|
+
const $el = $(this);
|
|
260
|
+
const ovf = $el.css('overflow');
|
|
261
|
+
if (ovf !== 'hidden' && ovf !== 'visible') {
|
|
262
|
+
$scrollContainer = $el;
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// make sure the drop zones will follow the scroll
|
|
268
|
+
interact.dynamicDrop(true);
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @typedef {Object} scrollObserver
|
|
272
|
+
*/
|
|
273
|
+
return {
|
|
274
|
+
/**
|
|
275
|
+
* Gets the scroll container
|
|
276
|
+
* @returns {jQuery}
|
|
277
|
+
*/
|
|
278
|
+
getScrollContainer: function getScrollContainer() {
|
|
279
|
+
return $scrollContainer;
|
|
280
|
+
},
|
|
281
|
+
/**
|
|
282
|
+
* Initializes the scroll observer while dragging a choice
|
|
283
|
+
* @param {HTMLElement|jQuery} draggedElement
|
|
284
|
+
*/
|
|
285
|
+
start: function start(draggedElement) {
|
|
286
|
+
resetScrollObserver();
|
|
287
|
+
currentDraggable = draggedElement instanceof $ ? draggedElement.get(0) : draggedElement;
|
|
288
|
+
$scrollContainer.on('scroll.scrollObserver', _.throttle(onScrollCb, 50));
|
|
289
|
+
},
|
|
290
|
+
/**
|
|
291
|
+
* Tears down the the scroll observer once the dragging is done
|
|
292
|
+
*/
|
|
293
|
+
stop: function stop() {
|
|
294
|
+
$scrollContainer.off('.scrollObserver');
|
|
295
|
+
resetScrollObserver();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
122
298
|
}
|
|
123
299
|
};
|
|
124
300
|
var interactHelper$1 = interactHelper;
|
package/package.json
CHANGED
package/src/interactUtils.js
CHANGED
|
@@ -134,6 +134,190 @@ interactHelper = {
|
|
|
134
134
|
|
|
135
135
|
domElement.setAttribute('data-x', 0);
|
|
136
136
|
domElement.setAttribute('data-y', 0);
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Improve touch devices support:
|
|
141
|
+
* - prevent native scroll while dragging
|
|
142
|
+
* - start drag only after longpress:
|
|
143
|
+
* when user is scrolling through the long page, he can accidentally get his finger on the draggable element:
|
|
144
|
+
* this will cause unwanted, unnoticed drag and can mess up his response.
|
|
145
|
+
* @example
|
|
146
|
+
* touchPatch = interactUtils.touchPatchFactory();
|
|
147
|
+
* interact(selector)
|
|
148
|
+
* .draggable({
|
|
149
|
+
onstart: () => {
|
|
150
|
+
touchPatch.onstart();
|
|
151
|
+
},
|
|
152
|
+
onend: () => {
|
|
153
|
+
touchPatch.onend();
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
.actionChecker(touchPatch.actionChecker);
|
|
157
|
+
...
|
|
158
|
+
function destroy() {
|
|
159
|
+
` touchPatch.destroy()
|
|
160
|
+
interact(selector).unset();
|
|
161
|
+
}
|
|
162
|
+
* @returns {Object}
|
|
163
|
+
*/
|
|
164
|
+
touchPatchFactory: function touchPatchFactory() {
|
|
165
|
+
const delayBefore = 300;
|
|
166
|
+
const distanceTolerance = 20; //while waiting for delayBefore, finger can move a little bit
|
|
167
|
+
|
|
168
|
+
interact.pointerMoveTolerance(distanceTolerance);
|
|
169
|
+
let isDragging = false;
|
|
170
|
+
|
|
171
|
+
// webKit requires cancelable `touchmove` events to be added as early as possible
|
|
172
|
+
// alternative: `touch-action: pinch-zoom` css: add it after drag start [?]; or have it always - worse ux
|
|
173
|
+
function touchmoveListener(e) {
|
|
174
|
+
if (isDragging) {
|
|
175
|
+
e.preventDefault();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
window.addEventListener('touchmove', touchmoveListener, { passive: false });
|
|
179
|
+
|
|
180
|
+
function contextmenuListener(e) {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
/**
|
|
186
|
+
* @param {PointerEvent} pointer
|
|
187
|
+
* @param {PointerEvent} event
|
|
188
|
+
* @param {Object} action
|
|
189
|
+
* @param {Object} interactable
|
|
190
|
+
* @returns {Object}
|
|
191
|
+
*/
|
|
192
|
+
actionChecker: (pointer, event, action, interactable, element) => {
|
|
193
|
+
if (event && action && action.name === 'drag') {
|
|
194
|
+
const isTouch = event.pointerType === 'touch';
|
|
195
|
+
interactable.options[action.name].delay = isTouch ? delayBefore : 0;
|
|
196
|
+
|
|
197
|
+
if (isTouch && !element.dataset.noContextMenu) {
|
|
198
|
+
//prevent context menu on longpress
|
|
199
|
+
//this listener can stay forever until the element is destroyed
|
|
200
|
+
element.addEventListener('contextmenu', contextmenuListener);
|
|
201
|
+
element.dataset.noContextMenu = true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return action;
|
|
205
|
+
},
|
|
206
|
+
onstart: () => {
|
|
207
|
+
isDragging = true;
|
|
208
|
+
},
|
|
209
|
+
onend: () => {
|
|
210
|
+
isDragging = false;
|
|
211
|
+
},
|
|
212
|
+
destroy: () => {
|
|
213
|
+
window.removeEventListener('touchmove', touchmoveListener);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Builds a scroll observer that will make sure the dragged element keeps an accurate positioning
|
|
220
|
+
* @example
|
|
221
|
+
* scrollObserver = interactUtils.scrollObserverFactory($container);
|
|
222
|
+
* dragOptions = {
|
|
223
|
+
autoScroll: {
|
|
224
|
+
container: scrollObserver.getScrollContainer().get(0)
|
|
225
|
+
},
|
|
226
|
+
onstart: function (e) {
|
|
227
|
+
scrollObserver.start($activeChoice);
|
|
228
|
+
},
|
|
229
|
+
onend: function (e) {
|
|
230
|
+
scrollObserver.stop();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
* @param {jQuery} $scrollContainer
|
|
234
|
+
* @returns {scrollObserver}
|
|
235
|
+
*/
|
|
236
|
+
scrollObserverFactory: function scrollObserverFactory($scrollContainer) {
|
|
237
|
+
let currentDraggable = null;
|
|
238
|
+
let beforeY = 0;
|
|
239
|
+
let beforeX = 0;
|
|
240
|
+
let afterY = 0;
|
|
241
|
+
let afterX = 0;
|
|
242
|
+
|
|
243
|
+
// reset the scroll observer context
|
|
244
|
+
function resetScrollObserver() {
|
|
245
|
+
currentDraggable = null;
|
|
246
|
+
beforeY = 0;
|
|
247
|
+
beforeX = 0;
|
|
248
|
+
afterY = 0;
|
|
249
|
+
afterX = 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// keep the position of the dragged element accurate with the scroll position
|
|
253
|
+
function onScrollCb() {
|
|
254
|
+
let x;
|
|
255
|
+
let y;
|
|
256
|
+
if (currentDraggable) {
|
|
257
|
+
beforeY = afterY;
|
|
258
|
+
beforeX = afterX;
|
|
259
|
+
|
|
260
|
+
if (afterY === 0 && beforeY === 0) beforeY = this.scrollTop;
|
|
261
|
+
if (afterX === 0 && beforeX === 0) beforeX = this.scrollLeft;
|
|
262
|
+
|
|
263
|
+
afterY = this.scrollTop;
|
|
264
|
+
afterX = this.scrollLeft;
|
|
265
|
+
|
|
266
|
+
y = (parseInt(currentDraggable.getAttribute('data-y'), 10) || 0) + (afterY - beforeY);
|
|
267
|
+
x = (parseInt(currentDraggable.getAttribute('data-x'), 10) || 0) + (afterX - beforeX);
|
|
268
|
+
|
|
269
|
+
// translate the element
|
|
270
|
+
currentDraggable.style.webkitTransform = currentDraggable.style.transform = `translate(${x}px, ${y}px)`;
|
|
271
|
+
|
|
272
|
+
// update the position attributes
|
|
273
|
+
currentDraggable.setAttribute('data-x', x);
|
|
274
|
+
currentDraggable.setAttribute('data-y', y);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// find the scroll container within the parents if any
|
|
279
|
+
$scrollContainer.parents().each(function findScrollContainer() {
|
|
280
|
+
const $el = $(this);
|
|
281
|
+
const ovf = $el.css('overflow');
|
|
282
|
+
if (ovf !== 'hidden' && ovf !== 'visible') {
|
|
283
|
+
$scrollContainer = $el;
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// make sure the drop zones will follow the scroll
|
|
289
|
+
interact.dynamicDrop(true);
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @typedef {Object} scrollObserver
|
|
293
|
+
*/
|
|
294
|
+
return {
|
|
295
|
+
/**
|
|
296
|
+
* Gets the scroll container
|
|
297
|
+
* @returns {jQuery}
|
|
298
|
+
*/
|
|
299
|
+
getScrollContainer: function getScrollContainer() {
|
|
300
|
+
return $scrollContainer;
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Initializes the scroll observer while dragging a choice
|
|
305
|
+
* @param {HTMLElement|jQuery} draggedElement
|
|
306
|
+
*/
|
|
307
|
+
start: function start(draggedElement) {
|
|
308
|
+
resetScrollObserver();
|
|
309
|
+
currentDraggable = draggedElement instanceof $ ? draggedElement.get(0) : draggedElement;
|
|
310
|
+
$scrollContainer.on('scroll.scrollObserver', _.throttle(onScrollCb, 50));
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Tears down the the scroll observer once the dragging is done
|
|
315
|
+
*/
|
|
316
|
+
stop: function stop() {
|
|
317
|
+
$scrollContainer.off('.scrollObserver');
|
|
318
|
+
resetScrollObserver();
|
|
319
|
+
}
|
|
320
|
+
};
|
|
137
321
|
}
|
|
138
322
|
};
|
|
139
323
|
|