button-active-43e2b419.js 2.5 KB

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