index.js 13 KB

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