index.js 12 KB

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