basic-time-spinner.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var index$1 = require('../../../scrollbar/index.js');
  6. var index$2 = require('../../../icon/index.js');
  7. var iconsVue = require('@element-plus/icons-vue');
  8. var constants = require('../constants.js');
  9. var utils = require('../utils.js');
  10. var basicTimeSpinner = require('../props/basic-time-spinner.js');
  11. var useTimePicker = require('../composables/use-time-picker.js');
  12. var pluginVue_exportHelper = require('../../../../_virtual/plugin-vue_export-helper.js');
  13. var index$3 = require('../../../../directives/repeat-click/index.js');
  14. var event = require('../../../../constants/event.js');
  15. var index = require('../../../../hooks/use-namespace/index.js');
  16. var style = require('../../../../utils/dom/style.js');
  17. var types = require('../../../../utils/types.js');
  18. const _hoisted_1 = ["onClick"];
  19. const _hoisted_2 = ["onMouseenter"];
  20. const _sfc_main = vue.defineComponent({
  21. __name: "basic-time-spinner",
  22. props: basicTimeSpinner.basicTimeSpinnerProps,
  23. emits: [event.CHANGE_EVENT, "select-range", "set-option"],
  24. setup(__props, { emit: __emit }) {
  25. const props = __props;
  26. const pickerBase = vue.inject(constants.PICKER_BASE_INJECTION_KEY);
  27. const { isRange, format } = pickerBase.props;
  28. const emit = __emit;
  29. const ns = index.useNamespace("time");
  30. const { getHoursList, getMinutesList, getSecondsList } = useTimePicker.getTimeLists(
  31. props.disabledHours,
  32. props.disabledMinutes,
  33. props.disabledSeconds
  34. );
  35. let isScrolling = false;
  36. const currentScrollbar = vue.ref();
  37. const listHoursRef = vue.ref();
  38. const listMinutesRef = vue.ref();
  39. const listSecondsRef = vue.ref();
  40. const listRefsMap = {
  41. hours: listHoursRef,
  42. minutes: listMinutesRef,
  43. seconds: listSecondsRef
  44. };
  45. const spinnerItems = vue.computed(() => {
  46. return props.showSeconds ? constants.timeUnits : constants.timeUnits.slice(0, 2);
  47. });
  48. const timePartials = vue.computed(() => {
  49. const { spinnerDate } = props;
  50. const hours = spinnerDate.hour();
  51. const minutes = spinnerDate.minute();
  52. const seconds = spinnerDate.second();
  53. return { hours, minutes, seconds };
  54. });
  55. const timeList = vue.computed(() => {
  56. const { hours, minutes } = vue.unref(timePartials);
  57. const { role, spinnerDate } = props;
  58. const compare = !isRange ? spinnerDate : void 0;
  59. return {
  60. hours: getHoursList(role, compare),
  61. minutes: getMinutesList(hours, role, compare),
  62. seconds: getSecondsList(hours, minutes, role, compare)
  63. };
  64. });
  65. const arrowControlTimeList = vue.computed(() => {
  66. const { hours, minutes, seconds } = vue.unref(timePartials);
  67. return {
  68. hours: utils.buildTimeList(hours, 23),
  69. minutes: utils.buildTimeList(minutes, 59),
  70. seconds: utils.buildTimeList(seconds, 59)
  71. };
  72. });
  73. const debouncedResetScroll = lodashUnified.debounce((type) => {
  74. isScrolling = false;
  75. adjustCurrentSpinner(type);
  76. }, 200);
  77. const getAmPmFlag = (hour) => {
  78. const shouldShowAmPm = !!props.amPmMode;
  79. if (!shouldShowAmPm)
  80. return "";
  81. const isCapital = props.amPmMode === "A";
  82. let content = hour < 12 ? " am" : " pm";
  83. if (isCapital)
  84. content = content.toUpperCase();
  85. return content;
  86. };
  87. const emitSelectRange = (type) => {
  88. let range = [0, 0];
  89. const actualFormat = format || constants.DEFAULT_FORMATS_TIME;
  90. const hourIndex = actualFormat.indexOf("HH");
  91. const minuteIndex = actualFormat.indexOf("mm");
  92. const secondIndex = actualFormat.indexOf("ss");
  93. switch (type) {
  94. case "hours":
  95. if (hourIndex !== -1) {
  96. range = [hourIndex, hourIndex + 2];
  97. }
  98. break;
  99. case "minutes":
  100. if (minuteIndex !== -1) {
  101. range = [minuteIndex, minuteIndex + 2];
  102. }
  103. break;
  104. case "seconds":
  105. if (secondIndex !== -1) {
  106. range = [secondIndex, secondIndex + 2];
  107. }
  108. break;
  109. }
  110. const [left, right] = range;
  111. emit("select-range", left, right);
  112. currentScrollbar.value = type;
  113. };
  114. const adjustCurrentSpinner = (type) => {
  115. adjustSpinner(type, vue.unref(timePartials)[type]);
  116. };
  117. const adjustSpinners = () => {
  118. adjustCurrentSpinner("hours");
  119. adjustCurrentSpinner("minutes");
  120. adjustCurrentSpinner("seconds");
  121. };
  122. const getScrollbarElement = (el) => el.querySelector(`.${ns.namespace.value}-scrollbar__wrap`);
  123. const adjustSpinner = (type, value) => {
  124. if (props.arrowControl)
  125. return;
  126. const scrollbar = vue.unref(listRefsMap[type]);
  127. if (scrollbar && scrollbar.$el) {
  128. getScrollbarElement(scrollbar.$el).scrollTop = Math.max(
  129. 0,
  130. value * typeItemHeight(type)
  131. );
  132. }
  133. };
  134. const typeItemHeight = (type) => {
  135. const scrollbar = vue.unref(listRefsMap[type]);
  136. const listItem = scrollbar == null ? void 0 : scrollbar.$el.querySelector("li");
  137. if (listItem) {
  138. return Number.parseFloat(style.getStyle(listItem, "height")) || 0;
  139. }
  140. return 0;
  141. };
  142. const onIncrement = () => {
  143. scrollDown(1);
  144. };
  145. const onDecrement = () => {
  146. scrollDown(-1);
  147. };
  148. const scrollDown = (step) => {
  149. if (!currentScrollbar.value) {
  150. emitSelectRange("hours");
  151. }
  152. const label = currentScrollbar.value;
  153. const now = vue.unref(timePartials)[label];
  154. const total = currentScrollbar.value === "hours" ? 24 : 60;
  155. const next = findNextUnDisabled(label, now, step, total);
  156. modifyDateField(label, next);
  157. adjustSpinner(label, next);
  158. vue.nextTick(() => emitSelectRange(label));
  159. };
  160. const findNextUnDisabled = (type, now, step, total) => {
  161. let next = (now + step + total) % total;
  162. const list = vue.unref(timeList)[type];
  163. while (list[next] && next !== now) {
  164. next = (next + step + total) % total;
  165. }
  166. return next;
  167. };
  168. const modifyDateField = (type, value) => {
  169. const list = vue.unref(timeList)[type];
  170. const isDisabled = list[value];
  171. if (isDisabled)
  172. return;
  173. const { hours, minutes, seconds } = vue.unref(timePartials);
  174. let changeTo;
  175. switch (type) {
  176. case "hours":
  177. changeTo = props.spinnerDate.hour(value).minute(minutes).second(seconds);
  178. break;
  179. case "minutes":
  180. changeTo = props.spinnerDate.hour(hours).minute(value).second(seconds);
  181. break;
  182. case "seconds":
  183. changeTo = props.spinnerDate.hour(hours).minute(minutes).second(value);
  184. break;
  185. }
  186. emit(event.CHANGE_EVENT, changeTo);
  187. };
  188. const handleClick = (type, { value, disabled }) => {
  189. if (!disabled) {
  190. modifyDateField(type, value);
  191. emitSelectRange(type);
  192. adjustSpinner(type, value);
  193. }
  194. };
  195. const handleScroll = (type) => {
  196. const scrollbar = vue.unref(listRefsMap[type]);
  197. if (!scrollbar)
  198. return;
  199. isScrolling = true;
  200. debouncedResetScroll(type);
  201. const value = Math.min(
  202. Math.round(
  203. (getScrollbarElement(scrollbar.$el).scrollTop - (scrollBarHeight(type) * 0.5 - 10) / typeItemHeight(type) + 3) / typeItemHeight(type)
  204. ),
  205. type === "hours" ? 23 : 59
  206. );
  207. modifyDateField(type, value);
  208. };
  209. const scrollBarHeight = (type) => {
  210. return vue.unref(listRefsMap[type]).$el.offsetHeight;
  211. };
  212. const bindScrollEvent = () => {
  213. const bindFunction = (type) => {
  214. const scrollbar = vue.unref(listRefsMap[type]);
  215. if (scrollbar && scrollbar.$el) {
  216. getScrollbarElement(scrollbar.$el).onscroll = () => {
  217. handleScroll(type);
  218. };
  219. }
  220. };
  221. bindFunction("hours");
  222. bindFunction("minutes");
  223. bindFunction("seconds");
  224. };
  225. vue.onMounted(() => {
  226. vue.nextTick(() => {
  227. !props.arrowControl && bindScrollEvent();
  228. adjustSpinners();
  229. if (props.role === "start")
  230. emitSelectRange("hours");
  231. });
  232. });
  233. const setRef = (scrollbar, type) => {
  234. listRefsMap[type].value = scrollbar != null ? scrollbar : void 0;
  235. };
  236. emit("set-option", [`${props.role}_scrollDown`, scrollDown]);
  237. emit("set-option", [`${props.role}_emitSelectRange`, emitSelectRange]);
  238. vue.watch(
  239. () => props.spinnerDate,
  240. () => {
  241. if (isScrolling)
  242. return;
  243. adjustSpinners();
  244. }
  245. );
  246. return (_ctx, _cache) => {
  247. return vue.openBlock(), vue.createElementBlock(
  248. "div",
  249. {
  250. class: vue.normalizeClass([vue.unref(ns).b("spinner"), { "has-seconds": _ctx.showSeconds }])
  251. },
  252. [
  253. !_ctx.arrowControl ? (vue.openBlock(true), vue.createElementBlock(
  254. vue.Fragment,
  255. { key: 0 },
  256. vue.renderList(spinnerItems.value, (item) => {
  257. return vue.openBlock(), vue.createBlock(vue.unref(index$1.ElScrollbar), {
  258. key: item,
  259. ref_for: true,
  260. ref: (scrollbar) => setRef(scrollbar, item),
  261. class: vue.normalizeClass(vue.unref(ns).be("spinner", "wrapper")),
  262. "wrap-style": "max-height: inherit;",
  263. "view-class": vue.unref(ns).be("spinner", "list"),
  264. noresize: "",
  265. tag: "ul",
  266. onMouseenter: ($event) => emitSelectRange(item),
  267. onMousemove: ($event) => adjustCurrentSpinner(item)
  268. }, {
  269. default: vue.withCtx(() => [
  270. (vue.openBlock(true), vue.createElementBlock(
  271. vue.Fragment,
  272. null,
  273. vue.renderList(timeList.value[item], (disabled, key) => {
  274. return vue.openBlock(), vue.createElementBlock("li", {
  275. key,
  276. class: vue.normalizeClass([
  277. vue.unref(ns).be("spinner", "item"),
  278. vue.unref(ns).is("active", key === timePartials.value[item]),
  279. vue.unref(ns).is("disabled", disabled)
  280. ]),
  281. onClick: ($event) => handleClick(item, { value: key, disabled })
  282. }, [
  283. item === "hours" ? (vue.openBlock(), vue.createElementBlock(
  284. vue.Fragment,
  285. { key: 0 },
  286. [
  287. vue.createTextVNode(
  288. vue.toDisplayString(("0" + (_ctx.amPmMode ? key % 12 || 12 : key)).slice(-2)) + vue.toDisplayString(getAmPmFlag(key)),
  289. 1
  290. )
  291. ],
  292. 64
  293. )) : (vue.openBlock(), vue.createElementBlock(
  294. vue.Fragment,
  295. { key: 1 },
  296. [
  297. vue.createTextVNode(
  298. vue.toDisplayString(("0" + key).slice(-2)),
  299. 1
  300. )
  301. ],
  302. 64
  303. ))
  304. ], 10, _hoisted_1);
  305. }),
  306. 128
  307. ))
  308. ]),
  309. _: 2
  310. }, 1032, ["class", "view-class", "onMouseenter", "onMousemove"]);
  311. }),
  312. 128
  313. )) : vue.createCommentVNode("v-if", true),
  314. _ctx.arrowControl ? (vue.openBlock(true), vue.createElementBlock(
  315. vue.Fragment,
  316. { key: 1 },
  317. vue.renderList(spinnerItems.value, (item) => {
  318. return vue.openBlock(), vue.createElementBlock("div", {
  319. key: item,
  320. class: vue.normalizeClass([vue.unref(ns).be("spinner", "wrapper"), vue.unref(ns).is("arrow")]),
  321. onMouseenter: ($event) => emitSelectRange(item)
  322. }, [
  323. vue.withDirectives((vue.openBlock(), vue.createBlock(vue.unref(index$2.ElIcon), {
  324. class: vue.normalizeClass(["arrow-up", vue.unref(ns).be("spinner", "arrow")])
  325. }, {
  326. default: vue.withCtx(() => [
  327. vue.createVNode(vue.unref(iconsVue.ArrowUp))
  328. ]),
  329. _: 1
  330. }, 8, ["class"])), [
  331. [vue.unref(index$3.vRepeatClick), onDecrement]
  332. ]),
  333. vue.withDirectives((vue.openBlock(), vue.createBlock(vue.unref(index$2.ElIcon), {
  334. class: vue.normalizeClass(["arrow-down", vue.unref(ns).be("spinner", "arrow")])
  335. }, {
  336. default: vue.withCtx(() => [
  337. vue.createVNode(vue.unref(iconsVue.ArrowDown))
  338. ]),
  339. _: 1
  340. }, 8, ["class"])), [
  341. [vue.unref(index$3.vRepeatClick), onIncrement]
  342. ]),
  343. vue.createElementVNode(
  344. "ul",
  345. {
  346. class: vue.normalizeClass(vue.unref(ns).be("spinner", "list"))
  347. },
  348. [
  349. (vue.openBlock(true), vue.createElementBlock(
  350. vue.Fragment,
  351. null,
  352. vue.renderList(arrowControlTimeList.value[item], (time, key) => {
  353. return vue.openBlock(), vue.createElementBlock(
  354. "li",
  355. {
  356. key,
  357. class: vue.normalizeClass([
  358. vue.unref(ns).be("spinner", "item"),
  359. vue.unref(ns).is("active", time === timePartials.value[item]),
  360. vue.unref(ns).is("disabled", timeList.value[item][time])
  361. ])
  362. },
  363. [
  364. vue.unref(types.isNumber)(time) ? (vue.openBlock(), vue.createElementBlock(
  365. vue.Fragment,
  366. { key: 0 },
  367. [
  368. item === "hours" ? (vue.openBlock(), vue.createElementBlock(
  369. vue.Fragment,
  370. { key: 0 },
  371. [
  372. vue.createTextVNode(
  373. vue.toDisplayString(("0" + (_ctx.amPmMode ? time % 12 || 12 : time)).slice(-2)) + vue.toDisplayString(getAmPmFlag(time)),
  374. 1
  375. )
  376. ],
  377. 64
  378. )) : (vue.openBlock(), vue.createElementBlock(
  379. vue.Fragment,
  380. { key: 1 },
  381. [
  382. vue.createTextVNode(
  383. vue.toDisplayString(("0" + time).slice(-2)),
  384. 1
  385. )
  386. ],
  387. 64
  388. ))
  389. ],
  390. 64
  391. )) : vue.createCommentVNode("v-if", true)
  392. ],
  393. 2
  394. );
  395. }),
  396. 128
  397. ))
  398. ],
  399. 2
  400. )
  401. ], 42, _hoisted_2);
  402. }),
  403. 128
  404. )) : vue.createCommentVNode("v-if", true)
  405. ],
  406. 2
  407. );
  408. };
  409. }
  410. });
  411. var TimeSpinner = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/time-picker/src/time-picker-com/basic-time-spinner.vue"]]);
  412. exports["default"] = TimeSpinner;
  413. //# sourceMappingURL=basic-time-spinner.js.map