Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

152 wiersze
3.8 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'underscore',
  8. 'jquery-ui-modules/slider'
  9. ], function ($, _) {
  10. 'use strict';
  11. /**
  12. * Adds support for touch events for regular jQuery UI slider.
  13. */
  14. $.widget('mage.touchSlider', $.ui.slider, {
  15. /**
  16. * Creates instance of widget.
  17. *
  18. * @override
  19. */
  20. _create: function () {
  21. _.bindAll(
  22. this,
  23. '_mouseDown',
  24. '_mouseMove',
  25. '_onTouchEnd'
  26. );
  27. return this._superApply(arguments);
  28. },
  29. /**
  30. * Initializes mouse events on element.
  31. * @override
  32. */
  33. _mouseInit: function () {
  34. var result = this._superApply(arguments);
  35. this.element
  36. .off('mousedown.' + this.widgetName)
  37. .on('touchstart.' + this.widgetName, this._mouseDown);
  38. return result;
  39. },
  40. /**
  41. * Elements' 'mousedown' event handler polyfill.
  42. * @override
  43. */
  44. _mouseDown: function (event) {
  45. var prevDelegate = this._mouseMoveDelegate,
  46. result;
  47. event = this._touchToMouse(event);
  48. result = this._super(event);
  49. if (prevDelegate === this._mouseMoveDelegate) {
  50. return result;
  51. }
  52. $(document)
  53. .off('mousemove.' + this.widgetName)
  54. .off('mouseup.' + this.widgetName);
  55. $(document)
  56. .on('touchmove.' + this.widgetName, this._mouseMove)
  57. .on('touchend.' + this.widgetName, this._onTouchEnd)
  58. .on('tochleave.' + this.widgetName, this._onTouchEnd);
  59. return result;
  60. },
  61. /**
  62. * Documents' 'mousemove' event handler polyfill.
  63. *
  64. * @override
  65. * @param {Event} event - Touch event object.
  66. */
  67. _mouseMove: function (event) {
  68. event = this._touchToMouse(event);
  69. return this._super(event);
  70. },
  71. /**
  72. * Documents' 'touchend' event handler.
  73. */
  74. _onTouchEnd: function (event) {
  75. $(document).trigger('mouseup');
  76. return this._mouseUp(event);
  77. },
  78. /**
  79. * Removes previously assigned touch handlers.
  80. *
  81. * @override
  82. */
  83. _mouseUp: function () {
  84. this._removeTouchHandlers();
  85. return this._superApply(arguments);
  86. },
  87. /**
  88. * Removes previously assigned touch handlers.
  89. *
  90. * @override
  91. */
  92. _mouseDestroy: function () {
  93. this._removeTouchHandlers();
  94. return this._superApply(arguments);
  95. },
  96. /**
  97. * Removes touch events from document object.
  98. */
  99. _removeTouchHandlers: function () {
  100. $(document)
  101. .off('touchmove.' + this.widgetName)
  102. .off('touchend.' + this.widgetName)
  103. .off('touchleave.' + this.widgetName);
  104. },
  105. /**
  106. * Adds properties to the touch event to mimic mouse event.
  107. *
  108. * @param {Event} event - Touch event object.
  109. * @returns {Event}
  110. */
  111. _touchToMouse: function (event) {
  112. var orig = event.originalEvent,
  113. touch = orig.touches[0];
  114. return _.extend(event, {
  115. which: 1,
  116. pageX: touch.pageX,
  117. pageY: touch.pageY,
  118. clientX: touch.clientX,
  119. clientY: touch.clientY,
  120. screenX: touch.screenX,
  121. screenY: touch.screenY
  122. });
  123. }
  124. });
  125. return $.mage.touchSlider;
  126. });