use-slider-button.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var core = require('@vueuse/core');
  6. var constants = require('../constants.js');
  7. var aria = require('../../../../constants/aria.js');
  8. var event = require('../../../../constants/event.js');
  9. const useTooltip = (props, formatTooltip, showTooltip) => {
  10. const tooltip = vue.ref();
  11. const tooltipVisible = vue.ref(false);
  12. const enableFormat = vue.computed(() => {
  13. return formatTooltip.value instanceof Function;
  14. });
  15. const formatValue = vue.computed(() => {
  16. return enableFormat.value && formatTooltip.value(props.modelValue) || props.modelValue;
  17. });
  18. const displayTooltip = lodashUnified.debounce(() => {
  19. showTooltip.value && (tooltipVisible.value = true);
  20. }, 50);
  21. const hideTooltip = lodashUnified.debounce(() => {
  22. showTooltip.value && (tooltipVisible.value = false);
  23. }, 50);
  24. return {
  25. tooltip,
  26. tooltipVisible,
  27. formatValue,
  28. displayTooltip,
  29. hideTooltip
  30. };
  31. };
  32. const useSliderButton = (props, initData, emit) => {
  33. const {
  34. disabled,
  35. min,
  36. max,
  37. step,
  38. showTooltip,
  39. persistent,
  40. precision,
  41. sliderSize,
  42. formatTooltip,
  43. emitChange,
  44. resetSize,
  45. updateDragging
  46. } = vue.inject(constants.sliderContextKey);
  47. const { tooltip, tooltipVisible, formatValue, displayTooltip, hideTooltip } = useTooltip(props, formatTooltip, showTooltip);
  48. const button = vue.ref();
  49. const currentPosition = vue.computed(() => {
  50. return `${(props.modelValue - min.value) / (max.value - min.value) * 100}%`;
  51. });
  52. const wrapperStyle = vue.computed(() => {
  53. return props.vertical ? { bottom: currentPosition.value } : { left: currentPosition.value };
  54. });
  55. const handleMouseEnter = () => {
  56. initData.hovering = true;
  57. displayTooltip();
  58. };
  59. const handleMouseLeave = () => {
  60. initData.hovering = false;
  61. if (!initData.dragging) {
  62. hideTooltip();
  63. }
  64. };
  65. const onButtonDown = (event) => {
  66. if (disabled.value)
  67. return;
  68. event.preventDefault();
  69. onDragStart(event);
  70. window.addEventListener("mousemove", onDragging);
  71. window.addEventListener("touchmove", onDragging);
  72. window.addEventListener("mouseup", onDragEnd);
  73. window.addEventListener("touchend", onDragEnd);
  74. window.addEventListener("contextmenu", onDragEnd);
  75. button.value.focus();
  76. };
  77. const incrementPosition = (amount) => {
  78. if (disabled.value)
  79. return;
  80. initData.newPosition = Number.parseFloat(currentPosition.value) + amount / (max.value - min.value) * 100;
  81. setPosition(initData.newPosition);
  82. emitChange();
  83. };
  84. const onLeftKeyDown = () => {
  85. incrementPosition(-step.value);
  86. };
  87. const onRightKeyDown = () => {
  88. incrementPosition(step.value);
  89. };
  90. const onPageDownKeyDown = () => {
  91. incrementPosition(-step.value * 4);
  92. };
  93. const onPageUpKeyDown = () => {
  94. incrementPosition(step.value * 4);
  95. };
  96. const onHomeKeyDown = () => {
  97. if (disabled.value)
  98. return;
  99. setPosition(0);
  100. emitChange();
  101. };
  102. const onEndKeyDown = () => {
  103. if (disabled.value)
  104. return;
  105. setPosition(100);
  106. emitChange();
  107. };
  108. const onKeyDown = (event) => {
  109. let isPreventDefault = true;
  110. switch (event.code) {
  111. case aria.EVENT_CODE.left:
  112. case aria.EVENT_CODE.down:
  113. onLeftKeyDown();
  114. break;
  115. case aria.EVENT_CODE.right:
  116. case aria.EVENT_CODE.up:
  117. onRightKeyDown();
  118. break;
  119. case aria.EVENT_CODE.home:
  120. onHomeKeyDown();
  121. break;
  122. case aria.EVENT_CODE.end:
  123. onEndKeyDown();
  124. break;
  125. case aria.EVENT_CODE.pageDown:
  126. onPageDownKeyDown();
  127. break;
  128. case aria.EVENT_CODE.pageUp:
  129. onPageUpKeyDown();
  130. break;
  131. default:
  132. isPreventDefault = false;
  133. break;
  134. }
  135. isPreventDefault && event.preventDefault();
  136. };
  137. const getClientXY = (event) => {
  138. let clientX;
  139. let clientY;
  140. if (event.type.startsWith("touch")) {
  141. clientY = event.touches[0].clientY;
  142. clientX = event.touches[0].clientX;
  143. } else {
  144. clientY = event.clientY;
  145. clientX = event.clientX;
  146. }
  147. return {
  148. clientX,
  149. clientY
  150. };
  151. };
  152. const onDragStart = (event) => {
  153. initData.dragging = true;
  154. initData.isClick = true;
  155. const { clientX, clientY } = getClientXY(event);
  156. if (props.vertical) {
  157. initData.startY = clientY;
  158. } else {
  159. initData.startX = clientX;
  160. }
  161. initData.startPosition = Number.parseFloat(currentPosition.value);
  162. initData.newPosition = initData.startPosition;
  163. };
  164. const onDragging = (event) => {
  165. if (initData.dragging) {
  166. initData.isClick = false;
  167. displayTooltip();
  168. resetSize();
  169. let diff;
  170. const { clientX, clientY } = getClientXY(event);
  171. if (props.vertical) {
  172. initData.currentY = clientY;
  173. diff = (initData.startY - initData.currentY) / sliderSize.value * 100;
  174. } else {
  175. initData.currentX = clientX;
  176. diff = (initData.currentX - initData.startX) / sliderSize.value * 100;
  177. }
  178. initData.newPosition = initData.startPosition + diff;
  179. setPosition(initData.newPosition);
  180. }
  181. };
  182. const onDragEnd = () => {
  183. if (initData.dragging) {
  184. setTimeout(() => {
  185. initData.dragging = false;
  186. if (!initData.hovering) {
  187. hideTooltip();
  188. }
  189. if (!initData.isClick) {
  190. setPosition(initData.newPosition);
  191. }
  192. emitChange();
  193. }, 0);
  194. window.removeEventListener("mousemove", onDragging);
  195. window.removeEventListener("touchmove", onDragging);
  196. window.removeEventListener("mouseup", onDragEnd);
  197. window.removeEventListener("touchend", onDragEnd);
  198. window.removeEventListener("contextmenu", onDragEnd);
  199. }
  200. };
  201. const setPosition = async (newPosition) => {
  202. if (newPosition === null || Number.isNaN(+newPosition))
  203. return;
  204. if (newPosition < 0) {
  205. newPosition = 0;
  206. } else if (newPosition > 100) {
  207. newPosition = 100;
  208. }
  209. const lengthPerStep = 100 / ((max.value - min.value) / step.value);
  210. const steps = Math.round(newPosition / lengthPerStep);
  211. let value = steps * lengthPerStep * (max.value - min.value) * 0.01 + min.value;
  212. value = Number.parseFloat(value.toFixed(precision.value));
  213. if (value !== props.modelValue) {
  214. emit(event.UPDATE_MODEL_EVENT, value);
  215. }
  216. if (!initData.dragging && props.modelValue !== initData.oldValue) {
  217. initData.oldValue = props.modelValue;
  218. }
  219. await vue.nextTick();
  220. initData.dragging && displayTooltip();
  221. tooltip.value.updatePopper();
  222. };
  223. vue.watch(() => initData.dragging, (val) => {
  224. updateDragging(val);
  225. });
  226. core.useEventListener(button, "touchstart", onButtonDown, { passive: false });
  227. return {
  228. disabled,
  229. button,
  230. tooltip,
  231. tooltipVisible,
  232. showTooltip,
  233. persistent,
  234. wrapperStyle,
  235. formatValue,
  236. handleMouseEnter,
  237. handleMouseLeave,
  238. onButtonDown,
  239. onKeyDown,
  240. setPosition
  241. };
  242. };
  243. exports.useSliderButton = useSliderButton;
  244. //# sourceMappingURL=use-slider-button.js.map