panel-time-pick.mjs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { defineComponent, inject, ref, computed, openBlock, createBlock, Transition, unref, withCtx, createElementBlock, normalizeClass, createElementVNode, createVNode, toDisplayString, createCommentVNode, nextTick } from 'vue';
  2. import dayjs from 'dayjs';
  3. import { PICKER_BASE_INJECTION_KEY } from '../constants.mjs';
  4. import { panelTimePickerProps } from '../props/panel-time-picker.mjs';
  5. import { useTimePanel } from '../composables/use-time-panel.mjs';
  6. import { useOldValue, buildAvailableTimeSlotGetter } from '../composables/use-time-picker.mjs';
  7. import TimeSpinner from './basic-time-spinner.mjs';
  8. import _export_sfc from '../../../../_virtual/plugin-vue_export-helper.mjs';
  9. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  10. import { useLocale } from '../../../../hooks/use-locale/index.mjs';
  11. import { isUndefined } from '../../../../utils/types.mjs';
  12. import { getEventCode } from '../../../../utils/dom/event.mjs';
  13. import { EVENT_CODE } from '../../../../constants/aria.mjs';
  14. const _sfc_main = /* @__PURE__ */ defineComponent({
  15. __name: "panel-time-pick",
  16. props: panelTimePickerProps,
  17. emits: ["pick", "select-range", "set-picker-option"],
  18. setup(__props, { emit }) {
  19. const props = __props;
  20. const pickerBase = inject(PICKER_BASE_INJECTION_KEY);
  21. const {
  22. arrowControl,
  23. disabledHours,
  24. disabledMinutes,
  25. disabledSeconds,
  26. defaultValue
  27. } = pickerBase.props;
  28. const { getAvailableHours, getAvailableMinutes, getAvailableSeconds } = buildAvailableTimeSlotGetter(disabledHours, disabledMinutes, disabledSeconds);
  29. const ns = useNamespace("time");
  30. const { t, lang } = useLocale();
  31. const selectionRange = ref([0, 2]);
  32. const oldValue = useOldValue(props);
  33. const transitionName = computed(() => {
  34. return isUndefined(props.actualVisible) ? `${ns.namespace.value}-zoom-in-top` : "";
  35. });
  36. const showSeconds = computed(() => {
  37. return props.format.includes("ss");
  38. });
  39. const amPmMode = computed(() => {
  40. if (props.format.includes("A"))
  41. return "A";
  42. if (props.format.includes("a"))
  43. return "a";
  44. return "";
  45. });
  46. const isValidValue = (_date) => {
  47. const parsedDate = dayjs(_date).locale(lang.value);
  48. const result = getRangeAvailableTime(parsedDate);
  49. return parsedDate.isSame(result);
  50. };
  51. const handleCancel = () => {
  52. const old = oldValue.value;
  53. emit("pick", old, false);
  54. nextTick(() => {
  55. oldValue.value = old;
  56. });
  57. };
  58. const handleConfirm = (visible = false, first = false) => {
  59. if (first)
  60. return;
  61. emit("pick", props.parsedValue, visible);
  62. };
  63. const handleChange = (_date) => {
  64. if (!props.visible) {
  65. return;
  66. }
  67. const result = getRangeAvailableTime(_date).millisecond(0);
  68. emit("pick", result, true);
  69. };
  70. const setSelectionRange = (start, end) => {
  71. emit("select-range", start, end);
  72. selectionRange.value = [start, end];
  73. };
  74. const changeSelectionRange = (step) => {
  75. const actualFormat = props.format;
  76. const hourIndex = actualFormat.indexOf("HH");
  77. const minuteIndex = actualFormat.indexOf("mm");
  78. const secondIndex = actualFormat.indexOf("ss");
  79. const list = [];
  80. const mapping = [];
  81. if (hourIndex !== -1) {
  82. list.push(hourIndex);
  83. mapping.push("hours");
  84. }
  85. if (minuteIndex !== -1) {
  86. list.push(minuteIndex);
  87. mapping.push("minutes");
  88. }
  89. if (secondIndex !== -1 && showSeconds.value) {
  90. list.push(secondIndex);
  91. mapping.push("seconds");
  92. }
  93. const index = list.indexOf(selectionRange.value[0]);
  94. const next = (index + step + list.length) % list.length;
  95. timePickerOptions["start_emitSelectRange"](mapping[next]);
  96. };
  97. const handleKeydown = (event) => {
  98. const code = getEventCode(event);
  99. const { left, right, up, down } = EVENT_CODE;
  100. if ([left, right].includes(code)) {
  101. const step = code === left ? -1 : 1;
  102. changeSelectionRange(step);
  103. event.preventDefault();
  104. return;
  105. }
  106. if ([up, down].includes(code)) {
  107. const step = code === up ? -1 : 1;
  108. timePickerOptions["start_scrollDown"](step);
  109. event.preventDefault();
  110. return;
  111. }
  112. };
  113. const { timePickerOptions, onSetOption, getAvailableTime } = useTimePanel({
  114. getAvailableHours,
  115. getAvailableMinutes,
  116. getAvailableSeconds
  117. });
  118. const getRangeAvailableTime = (date) => {
  119. return getAvailableTime(date, props.datetimeRole || "", true);
  120. };
  121. const parseUserInput = (value) => {
  122. if (!value)
  123. return null;
  124. return dayjs(value, props.format).locale(lang.value);
  125. };
  126. const getDefaultValue = () => {
  127. return dayjs(defaultValue).locale(lang.value);
  128. };
  129. emit("set-picker-option", ["isValidValue", isValidValue]);
  130. emit("set-picker-option", ["parseUserInput", parseUserInput]);
  131. emit("set-picker-option", ["handleKeydownInput", handleKeydown]);
  132. emit("set-picker-option", ["getRangeAvailableTime", getRangeAvailableTime]);
  133. emit("set-picker-option", ["getDefaultValue", getDefaultValue]);
  134. return (_ctx, _cache) => {
  135. return openBlock(), createBlock(Transition, { name: unref(transitionName) }, {
  136. default: withCtx(() => [
  137. _ctx.actualVisible || _ctx.visible ? (openBlock(), createElementBlock("div", {
  138. key: 0,
  139. class: normalizeClass(unref(ns).b("panel"))
  140. }, [
  141. createElementVNode("div", {
  142. class: normalizeClass([unref(ns).be("panel", "content"), { "has-seconds": unref(showSeconds) }])
  143. }, [
  144. createVNode(TimeSpinner, {
  145. ref: "spinner",
  146. role: _ctx.datetimeRole || "start",
  147. "arrow-control": unref(arrowControl),
  148. "show-seconds": unref(showSeconds),
  149. "am-pm-mode": unref(amPmMode),
  150. "spinner-date": _ctx.parsedValue,
  151. "disabled-hours": unref(disabledHours),
  152. "disabled-minutes": unref(disabledMinutes),
  153. "disabled-seconds": unref(disabledSeconds),
  154. onChange: handleChange,
  155. onSetOption: unref(onSetOption),
  156. onSelectRange: setSelectionRange
  157. }, null, 8, ["role", "arrow-control", "show-seconds", "am-pm-mode", "spinner-date", "disabled-hours", "disabled-minutes", "disabled-seconds", "onSetOption"])
  158. ], 2),
  159. createElementVNode("div", {
  160. class: normalizeClass(unref(ns).be("panel", "footer"))
  161. }, [
  162. createElementVNode("button", {
  163. type: "button",
  164. class: normalizeClass([unref(ns).be("panel", "btn"), "cancel"]),
  165. onClick: handleCancel
  166. }, toDisplayString(unref(t)("el.datepicker.cancel")), 3),
  167. createElementVNode("button", {
  168. type: "button",
  169. class: normalizeClass([unref(ns).be("panel", "btn"), "confirm"]),
  170. onClick: ($event) => handleConfirm()
  171. }, toDisplayString(unref(t)("el.datepicker.confirm")), 11, ["onClick"])
  172. ], 2)
  173. ], 2)) : createCommentVNode("v-if", true)
  174. ]),
  175. _: 1
  176. }, 8, ["name"]);
  177. };
  178. }
  179. });
  180. var TimePickPanel = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "panel-time-pick.vue"]]);
  181. export { TimePickPanel as default };
  182. //# sourceMappingURL=panel-time-pick.mjs.map