UNPKG

2.29 kBJavaScriptView Raw
1'use strict';
2
3const index$1 = require('./index-a0a08b2a.js');
4const haptic = require('./haptic-9f199ada.js');
5const index = require('./index-a1dd5c93.js');
6
7const createButtonActiveGesture = (el, isButton) => {
8 let currentTouchedButton;
9 let initialTouchedButton;
10 const activateButtonAtPoint = (x, y, hapticFeedbackFn) => {
11 if (typeof document === 'undefined') {
12 return;
13 }
14 const target = document.elementFromPoint(x, y);
15 if (!target || !isButton(target)) {
16 clearActiveButton();
17 return;
18 }
19 if (target !== currentTouchedButton) {
20 clearActiveButton();
21 setActiveButton(target, hapticFeedbackFn);
22 }
23 };
24 const setActiveButton = (button, hapticFeedbackFn) => {
25 currentTouchedButton = button;
26 if (!initialTouchedButton) {
27 initialTouchedButton = currentTouchedButton;
28 }
29 const buttonToModify = currentTouchedButton;
30 index$1.writeTask(() => buttonToModify.classList.add('ion-activated'));
31 hapticFeedbackFn();
32 };
33 const clearActiveButton = (dispatchClick = false) => {
34 if (!currentTouchedButton) {
35 return;
36 }
37 const buttonToModify = currentTouchedButton;
38 index$1.writeTask(() => buttonToModify.classList.remove('ion-activated'));
39 /**
40 * Clicking on one button, but releasing on another button
41 * does not dispatch a click event in browsers, so we
42 * need to do it manually here. Some browsers will
43 * dispatch a click if clicking on one button, dragging over
44 * another button, and releasing on the original button. In that
45 * case, we need to make sure we do not cause a double click there.
46 */
47 if (dispatchClick && initialTouchedButton !== currentTouchedButton) {
48 currentTouchedButton.click();
49 }
50 currentTouchedButton = undefined;
51 };
52 return index.createGesture({
53 el,
54 gestureName: 'buttonActiveDrag',
55 threshold: 0,
56 onStart: ev => activateButtonAtPoint(ev.currentX, ev.currentY, haptic.hapticSelectionStart),
57 onMove: ev => activateButtonAtPoint(ev.currentX, ev.currentY, haptic.hapticSelectionChanged),
58 onEnd: () => {
59 clearActiveButton(true);
60 haptic.hapticSelectionEnd();
61 initialTouchedButton = undefined;
62 }
63 });
64};
65
66exports.createButtonActiveGesture = createButtonActiveGesture;