Calendar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name2 in all)
  9. __defProp(target, name2, { get: all[name2], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  21. mod
  22. ));
  23. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  24. var stdin_exports = {};
  25. __export(stdin_exports, {
  26. default: () => stdin_default
  27. });
  28. module.exports = __toCommonJS(stdin_exports);
  29. var import_vue = require("vue");
  30. var import_vue2 = require("vue");
  31. var import_utils = require("../utils");
  32. var import_utils2 = require("./utils");
  33. var import_use = require("@vant/use");
  34. var import_use_refs = require("../composables/use-refs");
  35. var import_use_expose = require("../composables/use-expose");
  36. var import_popup = require("../popup");
  37. var import_button = require("../button");
  38. var import_toast = require("../toast");
  39. var import_CalendarMonth = __toESM(require("./CalendarMonth"));
  40. var import_CalendarHeader = __toESM(require("./CalendarHeader"));
  41. const calendarProps = {
  42. show: Boolean,
  43. type: (0, import_utils.makeStringProp)("single"),
  44. title: String,
  45. color: String,
  46. round: import_utils.truthProp,
  47. readonly: Boolean,
  48. poppable: import_utils.truthProp,
  49. maxRange: (0, import_utils.makeNumericProp)(null),
  50. position: (0, import_utils.makeStringProp)("bottom"),
  51. teleport: [String, Object],
  52. showMark: import_utils.truthProp,
  53. showTitle: import_utils.truthProp,
  54. formatter: Function,
  55. rowHeight: import_utils.numericProp,
  56. confirmText: String,
  57. rangePrompt: String,
  58. lazyRender: import_utils.truthProp,
  59. showConfirm: import_utils.truthProp,
  60. defaultDate: [Date, Array],
  61. allowSameDay: Boolean,
  62. showSubtitle: import_utils.truthProp,
  63. closeOnPopstate: import_utils.truthProp,
  64. showRangePrompt: import_utils.truthProp,
  65. confirmDisabledText: String,
  66. closeOnClickOverlay: import_utils.truthProp,
  67. safeAreaInsetTop: Boolean,
  68. safeAreaInsetBottom: import_utils.truthProp,
  69. minDate: {
  70. type: Date,
  71. validator: import_utils.isDate,
  72. default: import_utils2.getToday
  73. },
  74. maxDate: {
  75. type: Date,
  76. validator: import_utils.isDate,
  77. default: () => {
  78. const now = (0, import_utils2.getToday)();
  79. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate());
  80. }
  81. },
  82. firstDayOfWeek: {
  83. type: import_utils.numericProp,
  84. default: 0,
  85. validator: (val) => val >= 0 && val <= 6
  86. }
  87. };
  88. var stdin_default = (0, import_vue2.defineComponent)({
  89. name: import_utils2.name,
  90. props: calendarProps,
  91. emits: ["select", "confirm", "unselect", "month-show", "over-range", "update:show", "click-subtitle"],
  92. setup(props, {
  93. emit,
  94. slots
  95. }) {
  96. const limitDateRange = (date, minDate = props.minDate, maxDate = props.maxDate) => {
  97. if ((0, import_utils2.compareDay)(date, minDate) === -1) {
  98. return minDate;
  99. }
  100. if ((0, import_utils2.compareDay)(date, maxDate) === 1) {
  101. return maxDate;
  102. }
  103. return date;
  104. };
  105. const getInitialDate = (defaultDate = props.defaultDate) => {
  106. const {
  107. type,
  108. minDate,
  109. maxDate,
  110. allowSameDay
  111. } = props;
  112. if (defaultDate === null) {
  113. return defaultDate;
  114. }
  115. const now = (0, import_utils2.getToday)();
  116. if (type === "range") {
  117. if (!Array.isArray(defaultDate)) {
  118. defaultDate = [];
  119. }
  120. const start = limitDateRange(defaultDate[0] || now, minDate, allowSameDay ? maxDate : (0, import_utils2.getPrevDay)(maxDate));
  121. const end = limitDateRange(defaultDate[1] || now, allowSameDay ? minDate : (0, import_utils2.getNextDay)(minDate));
  122. return [start, end];
  123. }
  124. if (type === "multiple") {
  125. if (Array.isArray(defaultDate)) {
  126. return defaultDate.map((date) => limitDateRange(date));
  127. }
  128. return [limitDateRange(now)];
  129. }
  130. if (!defaultDate || Array.isArray(defaultDate)) {
  131. defaultDate = now;
  132. }
  133. return limitDateRange(defaultDate);
  134. };
  135. let bodyHeight;
  136. const bodyRef = (0, import_vue2.ref)();
  137. const subtitle = (0, import_vue2.ref)("");
  138. const currentDate = (0, import_vue2.ref)(getInitialDate());
  139. const [monthRefs, setMonthRefs] = (0, import_use_refs.useRefs)();
  140. const dayOffset = (0, import_vue2.computed)(() => props.firstDayOfWeek ? +props.firstDayOfWeek % 7 : 0);
  141. const months = (0, import_vue2.computed)(() => {
  142. const months2 = [];
  143. const cursor = new Date(props.minDate);
  144. cursor.setDate(1);
  145. do {
  146. months2.push(new Date(cursor));
  147. cursor.setMonth(cursor.getMonth() + 1);
  148. } while ((0, import_utils2.compareMonth)(cursor, props.maxDate) !== 1);
  149. return months2;
  150. });
  151. const buttonDisabled = (0, import_vue2.computed)(() => {
  152. if (currentDate.value) {
  153. if (props.type === "range") {
  154. return !currentDate.value[0] || !currentDate.value[1];
  155. }
  156. if (props.type === "multiple") {
  157. return !currentDate.value.length;
  158. }
  159. }
  160. return !currentDate.value;
  161. });
  162. const getSelectedDate = () => currentDate.value;
  163. const onScroll = () => {
  164. const top = (0, import_utils.getScrollTop)(bodyRef.value);
  165. const bottom = top + bodyHeight;
  166. const heights = months.value.map((item, index) => monthRefs.value[index].getHeight());
  167. const heightSum = heights.reduce((a, b) => a + b, 0);
  168. if (bottom > heightSum && top > 0) {
  169. return;
  170. }
  171. let height = 0;
  172. let currentMonth;
  173. const visibleRange = [-1, -1];
  174. for (let i = 0; i < months.value.length; i++) {
  175. const month = monthRefs.value[i];
  176. const visible = height <= bottom && height + heights[i] >= top;
  177. if (visible) {
  178. visibleRange[1] = i;
  179. if (!currentMonth) {
  180. currentMonth = month;
  181. visibleRange[0] = i;
  182. }
  183. if (!monthRefs.value[i].showed) {
  184. monthRefs.value[i].showed = true;
  185. emit("month-show", {
  186. date: month.date,
  187. title: month.getTitle()
  188. });
  189. }
  190. }
  191. height += heights[i];
  192. }
  193. months.value.forEach((month, index) => {
  194. const visible = index >= visibleRange[0] - 1 && index <= visibleRange[1] + 1;
  195. monthRefs.value[index].setVisible(visible);
  196. });
  197. if (currentMonth) {
  198. subtitle.value = currentMonth.getTitle();
  199. }
  200. };
  201. const scrollToDate = (targetDate) => {
  202. (0, import_use.raf)(() => {
  203. months.value.some((month, index) => {
  204. if ((0, import_utils2.compareMonth)(month, targetDate) === 0) {
  205. if (bodyRef.value) {
  206. monthRefs.value[index].scrollToDate(bodyRef.value, targetDate);
  207. }
  208. return true;
  209. }
  210. return false;
  211. });
  212. onScroll();
  213. });
  214. };
  215. const scrollToCurrentDate = () => {
  216. if (props.poppable && !props.show) {
  217. return;
  218. }
  219. if (currentDate.value) {
  220. const targetDate = props.type === "single" ? currentDate.value : currentDate.value[0];
  221. if ((0, import_utils.isDate)(targetDate)) {
  222. scrollToDate(targetDate);
  223. }
  224. } else {
  225. (0, import_use.raf)(onScroll);
  226. }
  227. };
  228. const init = () => {
  229. if (props.poppable && !props.show) {
  230. return;
  231. }
  232. (0, import_use.raf)(() => {
  233. bodyHeight = Math.floor((0, import_use.useRect)(bodyRef).height);
  234. });
  235. scrollToCurrentDate();
  236. };
  237. const reset = (date = getInitialDate()) => {
  238. currentDate.value = date;
  239. scrollToCurrentDate();
  240. };
  241. const checkRange = (date) => {
  242. const {
  243. maxRange,
  244. rangePrompt,
  245. showRangePrompt
  246. } = props;
  247. if (maxRange && (0, import_utils2.calcDateNum)(date) > maxRange) {
  248. if (showRangePrompt) {
  249. (0, import_toast.Toast)(rangePrompt || (0, import_utils2.t)("rangePrompt", maxRange));
  250. }
  251. emit("over-range");
  252. return false;
  253. }
  254. return true;
  255. };
  256. const onConfirm = () => {
  257. var _a;
  258. return emit("confirm", (_a = currentDate.value) != null ? _a : (0, import_utils2.cloneDates)(currentDate.value));
  259. };
  260. const select = (date, complete) => {
  261. const setCurrentDate = (date2) => {
  262. currentDate.value = date2;
  263. emit("select", (0, import_utils2.cloneDates)(date2));
  264. };
  265. if (complete && props.type === "range") {
  266. const valid = checkRange(date);
  267. if (!valid) {
  268. setCurrentDate([date[0], (0, import_utils2.getDayByOffset)(date[0], +props.maxRange - 1)]);
  269. return;
  270. }
  271. }
  272. setCurrentDate(date);
  273. if (complete && !props.showConfirm) {
  274. onConfirm();
  275. }
  276. };
  277. const getDisabledDate = (disabledDays2, startDay, date) => {
  278. var _a;
  279. return (_a = disabledDays2.find((day) => (0, import_utils2.compareDay)(startDay, day.date) === -1 && (0, import_utils2.compareDay)(day.date, date) === -1)) == null ? void 0 : _a.date;
  280. };
  281. const disabledDays = (0, import_vue2.computed)(() => monthRefs.value.reduce((arr, ref2) => {
  282. var _a, _b;
  283. arr.push(...(_b = (_a = ref2.disabledDays) == null ? void 0 : _a.value) != null ? _b : []);
  284. return arr;
  285. }, []));
  286. const onClickDay = (item) => {
  287. if (props.readonly || !item.date) {
  288. return;
  289. }
  290. const {
  291. date
  292. } = item;
  293. const {
  294. type
  295. } = props;
  296. if (type === "range") {
  297. if (!currentDate.value) {
  298. select([date]);
  299. return;
  300. }
  301. const [startDay, endDay] = currentDate.value;
  302. if (startDay && !endDay) {
  303. const compareToStart = (0, import_utils2.compareDay)(date, startDay);
  304. if (compareToStart === 1) {
  305. const disabledDay = getDisabledDate(disabledDays.value, startDay, date);
  306. if (disabledDay) {
  307. const endDay2 = (0, import_utils2.getPrevDay)(disabledDay);
  308. if ((0, import_utils2.compareDay)(startDay, endDay2) === -1) {
  309. select([startDay, endDay2]);
  310. } else {
  311. select([date]);
  312. }
  313. } else {
  314. select([startDay, date], true);
  315. }
  316. } else if (compareToStart === -1) {
  317. select([date]);
  318. } else if (props.allowSameDay) {
  319. select([date, date], true);
  320. }
  321. } else {
  322. select([date]);
  323. }
  324. } else if (type === "multiple") {
  325. if (!currentDate.value) {
  326. select([date]);
  327. return;
  328. }
  329. const dates = currentDate.value;
  330. const selectedIndex = dates.findIndex((dateItem) => (0, import_utils2.compareDay)(dateItem, date) === 0);
  331. if (selectedIndex !== -1) {
  332. const [unselectedDate] = dates.splice(selectedIndex, 1);
  333. emit("unselect", (0, import_utils2.cloneDate)(unselectedDate));
  334. } else if (props.maxRange && dates.length >= props.maxRange) {
  335. (0, import_toast.Toast)(props.rangePrompt || (0, import_utils2.t)("rangePrompt", props.maxRange));
  336. } else {
  337. select([...dates, date]);
  338. }
  339. } else {
  340. select(date, true);
  341. }
  342. };
  343. const updateShow = (value) => emit("update:show", value);
  344. const renderMonth = (date, index) => {
  345. const showMonthTitle = index !== 0 || !props.showSubtitle;
  346. return (0, import_vue.createVNode)(import_CalendarMonth.default, (0, import_vue.mergeProps)({
  347. "ref": setMonthRefs(index),
  348. "date": date,
  349. "currentDate": currentDate.value,
  350. "showMonthTitle": showMonthTitle,
  351. "firstDayOfWeek": dayOffset.value
  352. }, (0, import_utils.pick)(props, ["type", "color", "minDate", "maxDate", "showMark", "formatter", "rowHeight", "lazyRender", "showSubtitle", "allowSameDay"]), {
  353. "onClick": onClickDay
  354. }), (0, import_utils.pick)(slots, ["top-info", "bottom-info"]));
  355. };
  356. const renderFooterButton = () => {
  357. if (slots.footer) {
  358. return slots.footer();
  359. }
  360. if (props.showConfirm) {
  361. const slot = slots["confirm-text"];
  362. const disabled = buttonDisabled.value;
  363. const text = disabled ? props.confirmDisabledText : props.confirmText;
  364. return (0, import_vue.createVNode)(import_button.Button, {
  365. "round": true,
  366. "block": true,
  367. "type": "danger",
  368. "color": props.color,
  369. "class": (0, import_utils2.bem)("confirm"),
  370. "disabled": disabled,
  371. "nativeType": "button",
  372. "onClick": onConfirm
  373. }, {
  374. default: () => [slot ? slot({
  375. disabled
  376. }) : text || (0, import_utils2.t)("confirm")]
  377. });
  378. }
  379. };
  380. const renderFooter = () => (0, import_vue.createVNode)("div", {
  381. "class": [(0, import_utils2.bem)("footer"), {
  382. "van-safe-area-bottom": props.safeAreaInsetBottom
  383. }]
  384. }, [renderFooterButton()]);
  385. const renderCalendar = () => (0, import_vue.createVNode)("div", {
  386. "class": (0, import_utils2.bem)()
  387. }, [(0, import_vue.createVNode)(import_CalendarHeader.default, {
  388. "title": props.title,
  389. "subtitle": subtitle.value,
  390. "showTitle": props.showTitle,
  391. "showSubtitle": props.showSubtitle,
  392. "firstDayOfWeek": dayOffset.value,
  393. "onClick-subtitle": (event) => emit("click-subtitle", event)
  394. }, (0, import_utils.pick)(slots, ["title", "subtitle"])), (0, import_vue.createVNode)("div", {
  395. "ref": bodyRef,
  396. "class": (0, import_utils2.bem)("body"),
  397. "onScroll": onScroll
  398. }, [months.value.map(renderMonth)]), renderFooter()]);
  399. (0, import_vue2.watch)(() => props.show, init);
  400. (0, import_vue2.watch)(() => [props.type, props.minDate, props.maxDate], () => reset(getInitialDate(currentDate.value)));
  401. (0, import_vue2.watch)(() => props.defaultDate, (value = null) => {
  402. currentDate.value = value;
  403. scrollToCurrentDate();
  404. });
  405. (0, import_use_expose.useExpose)({
  406. reset,
  407. scrollToDate,
  408. getSelectedDate
  409. });
  410. (0, import_use.onMountedOrActivated)(init);
  411. return () => {
  412. if (props.poppable) {
  413. return (0, import_vue.createVNode)(import_popup.Popup, {
  414. "show": props.show,
  415. "class": (0, import_utils2.bem)("popup"),
  416. "round": props.round,
  417. "position": props.position,
  418. "closeable": props.showTitle || props.showSubtitle,
  419. "teleport": props.teleport,
  420. "closeOnPopstate": props.closeOnPopstate,
  421. "safeAreaInsetTop": props.safeAreaInsetTop,
  422. "closeOnClickOverlay": props.closeOnClickOverlay,
  423. "onUpdate:show": updateShow
  424. }, {
  425. default: renderCalendar
  426. });
  427. }
  428. return renderCalendar();
  429. };
  430. }
  431. });