index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  3. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  4. if (ar || !(i in from)) {
  5. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  6. ar[i] = from[i];
  7. }
  8. }
  9. return to.concat(ar || Array.prototype.slice.call(from));
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var component_1 = require("../common/component");
  16. var utils_1 = require("./utils");
  17. var toast_1 = __importDefault(require("../toast/toast"));
  18. var utils_2 = require("../common/utils");
  19. var initialMinDate = (0, utils_1.getToday)().getTime();
  20. var initialMaxDate = (function () {
  21. var now = (0, utils_1.getToday)();
  22. return new Date(now.getFullYear(), now.getMonth() + 6, now.getDate()).getTime();
  23. })();
  24. (0, component_1.VantComponent)({
  25. props: {
  26. title: {
  27. type: String,
  28. value: '日期选择',
  29. },
  30. color: String,
  31. show: {
  32. type: Boolean,
  33. observer: function (val) {
  34. if (val) {
  35. this.initRect();
  36. this.scrollIntoView();
  37. }
  38. },
  39. },
  40. formatter: null,
  41. confirmText: {
  42. type: String,
  43. value: '确定',
  44. },
  45. confirmDisabledText: {
  46. type: String,
  47. value: '确定',
  48. },
  49. rangePrompt: String,
  50. showRangePrompt: {
  51. type: Boolean,
  52. value: true,
  53. },
  54. defaultDate: {
  55. type: null,
  56. observer: function (val) {
  57. this.setData({ currentDate: val });
  58. this.scrollIntoView();
  59. },
  60. },
  61. allowSameDay: Boolean,
  62. type: {
  63. type: String,
  64. value: 'single',
  65. observer: 'reset',
  66. },
  67. minDate: {
  68. type: Number,
  69. value: initialMinDate,
  70. },
  71. maxDate: {
  72. type: Number,
  73. value: initialMaxDate,
  74. },
  75. position: {
  76. type: String,
  77. value: 'bottom',
  78. },
  79. rowHeight: {
  80. type: null,
  81. value: utils_1.ROW_HEIGHT,
  82. },
  83. round: {
  84. type: Boolean,
  85. value: true,
  86. },
  87. poppable: {
  88. type: Boolean,
  89. value: true,
  90. },
  91. showMark: {
  92. type: Boolean,
  93. value: true,
  94. },
  95. showTitle: {
  96. type: Boolean,
  97. value: true,
  98. },
  99. showConfirm: {
  100. type: Boolean,
  101. value: true,
  102. },
  103. showSubtitle: {
  104. type: Boolean,
  105. value: true,
  106. },
  107. safeAreaInsetBottom: {
  108. type: Boolean,
  109. value: true,
  110. },
  111. closeOnClickOverlay: {
  112. type: Boolean,
  113. value: true,
  114. },
  115. maxRange: {
  116. type: null,
  117. value: null,
  118. },
  119. firstDayOfWeek: {
  120. type: Number,
  121. value: 0,
  122. },
  123. readonly: Boolean,
  124. },
  125. data: {
  126. subtitle: '',
  127. currentDate: null,
  128. scrollIntoView: '',
  129. },
  130. created: function () {
  131. this.setData({
  132. currentDate: this.getInitialDate(this.data.defaultDate),
  133. });
  134. },
  135. mounted: function () {
  136. if (this.data.show || !this.data.poppable) {
  137. this.initRect();
  138. this.scrollIntoView();
  139. }
  140. },
  141. methods: {
  142. reset: function () {
  143. this.setData({ currentDate: this.getInitialDate() });
  144. this.scrollIntoView();
  145. },
  146. initRect: function () {
  147. var _this = this;
  148. if (this.contentObserver != null) {
  149. this.contentObserver.disconnect();
  150. }
  151. var contentObserver = this.createIntersectionObserver({
  152. thresholds: [0, 0.1, 0.9, 1],
  153. observeAll: true,
  154. });
  155. this.contentObserver = contentObserver;
  156. contentObserver.relativeTo('.van-calendar__body');
  157. contentObserver.observe('.month', function (res) {
  158. if (res.boundingClientRect.top <= res.relativeRect.top) {
  159. // @ts-ignore
  160. _this.setData({ subtitle: (0, utils_1.formatMonthTitle)(res.dataset.date) });
  161. }
  162. });
  163. },
  164. limitDateRange: function (date, minDate, maxDate) {
  165. if (minDate === void 0) { minDate = null; }
  166. if (maxDate === void 0) { maxDate = null; }
  167. minDate = minDate || this.data.minDate;
  168. maxDate = maxDate || this.data.maxDate;
  169. if ((0, utils_1.compareDay)(date, minDate) === -1) {
  170. return minDate;
  171. }
  172. if ((0, utils_1.compareDay)(date, maxDate) === 1) {
  173. return maxDate;
  174. }
  175. return date;
  176. },
  177. getInitialDate: function (defaultDate) {
  178. var _this = this;
  179. if (defaultDate === void 0) { defaultDate = null; }
  180. var _a = this.data, type = _a.type, minDate = _a.minDate, maxDate = _a.maxDate;
  181. var now = (0, utils_1.getToday)().getTime();
  182. if (type === 'range') {
  183. if (!Array.isArray(defaultDate)) {
  184. defaultDate = [];
  185. }
  186. var _b = defaultDate || [], startDay = _b[0], endDay = _b[1];
  187. var start = this.limitDateRange(startDay || now, minDate, (0, utils_1.getPrevDay)(new Date(maxDate)).getTime());
  188. var end = this.limitDateRange(endDay || now, (0, utils_1.getNextDay)(new Date(minDate)).getTime());
  189. return [start, end];
  190. }
  191. if (type === 'multiple') {
  192. if (Array.isArray(defaultDate)) {
  193. return defaultDate.map(function (date) { return _this.limitDateRange(date); });
  194. }
  195. return [this.limitDateRange(now)];
  196. }
  197. if (!defaultDate || Array.isArray(defaultDate)) {
  198. defaultDate = now;
  199. }
  200. return this.limitDateRange(defaultDate);
  201. },
  202. scrollIntoView: function () {
  203. var _this = this;
  204. (0, utils_2.requestAnimationFrame)(function () {
  205. var _a = _this.data, currentDate = _a.currentDate, type = _a.type, show = _a.show, poppable = _a.poppable, minDate = _a.minDate, maxDate = _a.maxDate;
  206. // @ts-ignore
  207. var targetDate = type === 'single' ? currentDate : currentDate[0];
  208. var displayed = show || !poppable;
  209. if (!targetDate || !displayed) {
  210. return;
  211. }
  212. var months = (0, utils_1.getMonths)(minDate, maxDate);
  213. months.some(function (month, index) {
  214. if ((0, utils_1.compareMonth)(month, targetDate) === 0) {
  215. _this.setData({ scrollIntoView: "month" + index });
  216. return true;
  217. }
  218. return false;
  219. });
  220. });
  221. },
  222. onOpen: function () {
  223. this.$emit('open');
  224. },
  225. onOpened: function () {
  226. this.$emit('opened');
  227. },
  228. onClose: function () {
  229. this.$emit('close');
  230. },
  231. onClosed: function () {
  232. this.$emit('closed');
  233. },
  234. onClickDay: function (event) {
  235. if (this.data.readonly) {
  236. return;
  237. }
  238. var date = event.detail.date;
  239. var _a = this.data, type = _a.type, currentDate = _a.currentDate, allowSameDay = _a.allowSameDay;
  240. if (type === 'range') {
  241. // @ts-ignore
  242. var startDay = currentDate[0], endDay = currentDate[1];
  243. if (startDay && !endDay) {
  244. var compareToStart = (0, utils_1.compareDay)(date, startDay);
  245. if (compareToStart === 1) {
  246. this.select([startDay, date], true);
  247. }
  248. else if (compareToStart === -1) {
  249. this.select([date, null]);
  250. }
  251. else if (allowSameDay) {
  252. this.select([date, date]);
  253. }
  254. }
  255. else {
  256. this.select([date, null]);
  257. }
  258. }
  259. else if (type === 'multiple') {
  260. var selectedIndex_1;
  261. // @ts-ignore
  262. var selected = currentDate.some(function (dateItem, index) {
  263. var equal = (0, utils_1.compareDay)(dateItem, date) === 0;
  264. if (equal) {
  265. selectedIndex_1 = index;
  266. }
  267. return equal;
  268. });
  269. if (selected) {
  270. // @ts-ignore
  271. var cancelDate = currentDate.splice(selectedIndex_1, 1);
  272. this.setData({ currentDate: currentDate });
  273. this.unselect(cancelDate);
  274. }
  275. else {
  276. // @ts-ignore
  277. this.select(__spreadArray(__spreadArray([], currentDate, true), [date], false));
  278. }
  279. }
  280. else {
  281. this.select(date, true);
  282. }
  283. },
  284. unselect: function (dateArray) {
  285. var date = dateArray[0];
  286. if (date) {
  287. this.$emit('unselect', (0, utils_1.copyDates)(date));
  288. }
  289. },
  290. select: function (date, complete) {
  291. if (complete && this.data.type === 'range') {
  292. var valid = this.checkRange(date);
  293. if (!valid) {
  294. // auto selected to max range if showConfirm
  295. if (this.data.showConfirm) {
  296. this.emit([
  297. date[0],
  298. (0, utils_1.getDayByOffset)(date[0], this.data.maxRange - 1),
  299. ]);
  300. }
  301. else {
  302. this.emit(date);
  303. }
  304. return;
  305. }
  306. }
  307. this.emit(date);
  308. if (complete && !this.data.showConfirm) {
  309. this.onConfirm();
  310. }
  311. },
  312. emit: function (date) {
  313. var getTime = function (date) {
  314. return date instanceof Date ? date.getTime() : date;
  315. };
  316. this.setData({
  317. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  318. });
  319. this.$emit('select', (0, utils_1.copyDates)(date));
  320. },
  321. checkRange: function (date) {
  322. var _a = this.data, maxRange = _a.maxRange, rangePrompt = _a.rangePrompt, showRangePrompt = _a.showRangePrompt;
  323. if (maxRange && (0, utils_1.calcDateNum)(date) > maxRange) {
  324. if (showRangePrompt) {
  325. (0, toast_1.default)({
  326. context: this,
  327. message: rangePrompt || "\u9009\u62E9\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7 " + maxRange + " \u5929",
  328. });
  329. }
  330. this.$emit('over-range');
  331. return false;
  332. }
  333. return true;
  334. },
  335. onConfirm: function () {
  336. var _this = this;
  337. if (this.data.type === 'range' &&
  338. !this.checkRange(this.data.currentDate)) {
  339. return;
  340. }
  341. wx.nextTick(function () {
  342. // @ts-ignore
  343. _this.$emit('confirm', (0, utils_1.copyDates)(_this.data.currentDate));
  344. });
  345. },
  346. onClickSubtitle: function (event) {
  347. this.$emit('click-subtitle', event);
  348. },
  349. },
  350. });