index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. rootPortal: {
  133. type: Boolean,
  134. value: false,
  135. },
  136. },
  137. data: {
  138. subtitle: '',
  139. currentDate: null,
  140. scrollIntoView: '',
  141. },
  142. watch: {
  143. minDate: function () {
  144. this.initRect();
  145. },
  146. maxDate: function () {
  147. this.initRect();
  148. },
  149. },
  150. created: function () {
  151. this.setData({
  152. currentDate: this.getInitialDate(this.data.defaultDate),
  153. });
  154. },
  155. mounted: function () {
  156. if (this.data.show || !this.data.poppable) {
  157. this.initRect();
  158. this.scrollIntoView();
  159. }
  160. },
  161. methods: {
  162. reset: function () {
  163. this.setData({ currentDate: this.getInitialDate(this.data.defaultDate) });
  164. this.scrollIntoView();
  165. },
  166. initRect: function () {
  167. var _this = this;
  168. if (this.contentObserver != null) {
  169. this.contentObserver.disconnect();
  170. }
  171. var contentObserver = this.createIntersectionObserver({
  172. thresholds: [0, 0.1, 0.9, 1],
  173. observeAll: true,
  174. });
  175. this.contentObserver = contentObserver;
  176. contentObserver.relativeTo('.van-calendar__body');
  177. contentObserver.observe('.month', function (res) {
  178. if (res.boundingClientRect.top <= res.relativeRect.top) {
  179. // @ts-ignore
  180. _this.setData({ subtitle: (0, utils_1.formatMonthTitle)(res.dataset.date) });
  181. }
  182. });
  183. },
  184. limitDateRange: function (date, minDate, maxDate) {
  185. if (minDate === void 0) { minDate = null; }
  186. if (maxDate === void 0) { maxDate = null; }
  187. minDate = minDate || this.data.minDate;
  188. maxDate = maxDate || this.data.maxDate;
  189. if ((0, utils_1.compareDay)(date, minDate) === -1) {
  190. return minDate;
  191. }
  192. if ((0, utils_1.compareDay)(date, maxDate) === 1) {
  193. return maxDate;
  194. }
  195. return date;
  196. },
  197. getInitialDate: function (defaultDate) {
  198. var _this = this;
  199. if (defaultDate === void 0) { defaultDate = null; }
  200. var _a = this.data, type = _a.type, minDate = _a.minDate, maxDate = _a.maxDate, allowSameDay = _a.allowSameDay;
  201. if (!defaultDate)
  202. return [];
  203. var now = (0, utils_1.getToday)().getTime();
  204. if (type === 'range') {
  205. if (!Array.isArray(defaultDate)) {
  206. defaultDate = [];
  207. }
  208. var _b = defaultDate || [], startDay = _b[0], endDay = _b[1];
  209. var startDate = getTime(startDay || now);
  210. var start = this.limitDateRange(startDate, minDate, allowSameDay ? startDate : (0, utils_1.getPrevDay)(new Date(maxDate)).getTime());
  211. var date = getTime(endDay || now);
  212. var end = this.limitDateRange(date, allowSameDay ? date : (0, utils_1.getNextDay)(new Date(minDate)).getTime());
  213. return [start, end];
  214. }
  215. if (type === 'multiple') {
  216. if (Array.isArray(defaultDate)) {
  217. return defaultDate.map(function (date) { return _this.limitDateRange(date); });
  218. }
  219. return [this.limitDateRange(now)];
  220. }
  221. if (!defaultDate || Array.isArray(defaultDate)) {
  222. defaultDate = now;
  223. }
  224. return this.limitDateRange(defaultDate);
  225. },
  226. scrollIntoView: function () {
  227. var _this = this;
  228. (0, utils_2.requestAnimationFrame)(function () {
  229. var _a = _this.data, currentDate = _a.currentDate, type = _a.type, show = _a.show, poppable = _a.poppable, minDate = _a.minDate, maxDate = _a.maxDate;
  230. if (!currentDate)
  231. return;
  232. // @ts-ignore
  233. var targetDate = type === 'single' ? currentDate : currentDate[0];
  234. var displayed = show || !poppable;
  235. if (!targetDate || !displayed) {
  236. return;
  237. }
  238. var months = (0, utils_1.getMonths)(minDate, maxDate);
  239. months.some(function (month, index) {
  240. if ((0, utils_1.compareMonth)(month, targetDate) === 0) {
  241. _this.setData({ scrollIntoView: "month".concat(index) });
  242. return true;
  243. }
  244. return false;
  245. });
  246. });
  247. },
  248. onOpen: function () {
  249. this.$emit('open');
  250. },
  251. onOpened: function () {
  252. this.$emit('opened');
  253. },
  254. onClose: function () {
  255. this.$emit('close');
  256. },
  257. onClosed: function () {
  258. this.$emit('closed');
  259. },
  260. onClickDay: function (event) {
  261. if (this.data.readonly) {
  262. return;
  263. }
  264. var date = event.detail.date;
  265. var _a = this.data, type = _a.type, currentDate = _a.currentDate, allowSameDay = _a.allowSameDay;
  266. if (type === 'range') {
  267. // @ts-ignore
  268. var startDay_1 = currentDate[0], endDay = currentDate[1];
  269. if (startDay_1 && !endDay) {
  270. var compareToStart = (0, utils_1.compareDay)(date, startDay_1);
  271. if (compareToStart === 1) {
  272. var days_1 = this.selectComponent('.month').data.days;
  273. days_1.some(function (day, index) {
  274. var isDisabled = day.type === 'disabled' &&
  275. getTime(startDay_1) < getTime(day.date) &&
  276. getTime(day.date) < getTime(date);
  277. if (isDisabled) {
  278. (date = days_1[index - 1].date);
  279. }
  280. return isDisabled;
  281. });
  282. this.select([startDay_1, date], true);
  283. }
  284. else if (compareToStart === -1) {
  285. this.select([date, null]);
  286. }
  287. else if (allowSameDay) {
  288. this.select([date, date], true);
  289. }
  290. }
  291. else {
  292. this.select([date, null]);
  293. }
  294. }
  295. else if (type === 'multiple') {
  296. var selectedIndex_1;
  297. // @ts-ignore
  298. var selected = currentDate.some(function (dateItem, index) {
  299. var equal = (0, utils_1.compareDay)(dateItem, date) === 0;
  300. if (equal) {
  301. selectedIndex_1 = index;
  302. }
  303. return equal;
  304. });
  305. if (selected) {
  306. // @ts-ignore
  307. var cancelDate = currentDate.splice(selectedIndex_1, 1);
  308. this.setData({ currentDate: currentDate });
  309. this.unselect(cancelDate);
  310. }
  311. else {
  312. // @ts-ignore
  313. this.select(__spreadArray(__spreadArray([], currentDate, true), [date], false));
  314. }
  315. }
  316. else {
  317. this.select(date, true);
  318. }
  319. },
  320. unselect: function (dateArray) {
  321. var date = dateArray[0];
  322. if (date) {
  323. this.$emit('unselect', (0, utils_1.copyDates)(date));
  324. }
  325. },
  326. select: function (date, complete) {
  327. if (complete && this.data.type === 'range') {
  328. var valid = this.checkRange(date);
  329. if (!valid) {
  330. // auto selected to max range if showConfirm
  331. if (this.data.showConfirm) {
  332. this.emit([
  333. date[0],
  334. (0, utils_1.getDayByOffset)(date[0], this.data.maxRange - 1),
  335. ]);
  336. }
  337. else {
  338. this.emit(date);
  339. }
  340. return;
  341. }
  342. }
  343. this.emit(date);
  344. if (complete && !this.data.showConfirm) {
  345. this.onConfirm();
  346. }
  347. },
  348. emit: function (date) {
  349. this.setData({
  350. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date),
  351. });
  352. this.$emit('select', (0, utils_1.copyDates)(date));
  353. },
  354. checkRange: function (date) {
  355. var _a = this.data, maxRange = _a.maxRange, rangePrompt = _a.rangePrompt, showRangePrompt = _a.showRangePrompt;
  356. if (maxRange && (0, utils_1.calcDateNum)(date) > maxRange) {
  357. if (showRangePrompt) {
  358. (0, toast_1.default)({
  359. context: this,
  360. message: rangePrompt || "\u9009\u62E9\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7 ".concat(maxRange, " \u5929"),
  361. });
  362. }
  363. this.$emit('over-range');
  364. return false;
  365. }
  366. return true;
  367. },
  368. onConfirm: function () {
  369. var _this = this;
  370. if (this.data.type === 'range' &&
  371. !this.checkRange(this.data.currentDate)) {
  372. return;
  373. }
  374. wx.nextTick(function () {
  375. // @ts-ignore
  376. _this.$emit('confirm', (0, utils_1.copyDates)(_this.data.currentDate));
  377. });
  378. },
  379. onClickSubtitle: function (event) {
  380. this.$emit('click-subtitle', event);
  381. },
  382. },
  383. });