@oat-sa/tao-core-ui 3.13.3 → 3.13.5

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.
@@ -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