index.js 8.3 KB

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