index.js 8.2 KB

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