index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. "use strict";
  2. var _component = require("../common/component");
  3. var _utils = require("./utils");
  4. var _toast = _interopRequireDefault(require("../toast/toast"));
  5. var _utils2 = require("../common/utils");
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  7. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  8. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  9. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  10. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  11. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  12. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  13. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  14. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  15. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  16. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  17. var _my = require("../../__antmove/api/index.js")(my);
  18. var wx = _my;
  19. (0, _component.VantComponent)({
  20. props: {
  21. title: {
  22. type: String,
  23. value: "日期选择"
  24. },
  25. color: String,
  26. show: {
  27. type: Boolean,
  28. observer: function observer(val) {
  29. if (val) {
  30. this.initRect();
  31. this.scrollIntoView();
  32. }
  33. }
  34. },
  35. formatter: null,
  36. confirmText: {
  37. type: String,
  38. value: "确定"
  39. },
  40. rangePrompt: String,
  41. defaultDate: {
  42. type: [Number, Array],
  43. observer: function observer(val) {
  44. this.setData({
  45. currentDate: val
  46. });
  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(new Date().getFullYear(), new Date().getMonth() + 6, new Date().getDate()).getTime()
  64. },
  65. position: {
  66. type: String,
  67. value: "bottom"
  68. },
  69. rowHeight: {
  70. type: [Number, String],
  71. value: _utils.ROW_HEIGHT
  72. },
  73. round: {
  74. type: Boolean,
  75. value: true
  76. },
  77. poppable: {
  78. type: Boolean,
  79. value: true
  80. },
  81. showMark: {
  82. type: Boolean,
  83. value: true
  84. },
  85. showTitle: {
  86. type: Boolean,
  87. value: true
  88. },
  89. showConfirm: {
  90. type: Boolean,
  91. value: true
  92. },
  93. showSubtitle: {
  94. type: Boolean,
  95. value: true
  96. },
  97. safeAreaInsetBottom: {
  98. type: Boolean,
  99. value: true
  100. },
  101. closeOnClickOverlay: {
  102. type: Boolean,
  103. value: true
  104. },
  105. maxRange: {
  106. type: [Number, String],
  107. value: null
  108. }
  109. },
  110. data: {
  111. subtitle: "",
  112. currentDate: null,
  113. scrollIntoView: ""
  114. },
  115. created: function created() {
  116. this.setData({
  117. currentDate: this.getInitialDate()
  118. });
  119. },
  120. mounted: function mounted() {
  121. if (this.data.show || !this.data.poppable) {
  122. this.initRect();
  123. this.scrollIntoView();
  124. }
  125. },
  126. methods: {
  127. onFormatter: function onFormatter(opts) {
  128. return this.props.onFormatter ? this.props.onFormatter(opts) : opts;
  129. },
  130. reset: function reset() {
  131. this.setData({
  132. currentDate: this.getInitialDate()
  133. });
  134. this.scrollIntoView();
  135. },
  136. initRect: function initRect() {
  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: (0, _utils.formatMonthTitle)(res.dataset.date)
  152. });
  153. }
  154. });
  155. },
  156. getInitialDate: function getInitialDate() {
  157. var _this$data = this.data,
  158. type = _this$data.type,
  159. defaultDate = _this$data.defaultDate,
  160. minDate = _this$data.minDate;
  161. if (type === "range") {
  162. var _ref = defaultDate || [],
  163. _ref2 = _slicedToArray(_ref, 2),
  164. startDay = _ref2[0],
  165. endDay = _ref2[1];
  166. return [startDay || minDate, endDay || (0, _utils.getNextDay)(new Date(minDate)).getTime()];
  167. }
  168. if (type === "multiple") {
  169. return defaultDate || [minDate];
  170. }
  171. return defaultDate || minDate;
  172. },
  173. scrollIntoView: function scrollIntoView() {
  174. var _this2 = this;
  175. (0, _utils2.requestAnimationFrame)(function () {
  176. var _this2$data = _this2.data,
  177. currentDate = _this2$data.currentDate,
  178. type = _this2$data.type,
  179. show = _this2$data.show,
  180. poppable = _this2$data.poppable,
  181. minDate = _this2$data.minDate,
  182. maxDate = _this2$data.maxDate; // @ts-ignore
  183. var targetDate = type === "single" ? currentDate : currentDate[0];
  184. var displayed = show || !poppable;
  185. if (!targetDate || !displayed) {
  186. return;
  187. }
  188. var months = (0, _utils.getMonths)(minDate, maxDate);
  189. months.some(function (month, index) {
  190. if ((0, _utils.compareMonth)(month, targetDate) === 0) {
  191. _this2.setData({
  192. scrollIntoView: "month".concat(index)
  193. });
  194. return true;
  195. }
  196. return false;
  197. });
  198. });
  199. },
  200. onOpen: function onOpen() {
  201. this.$emit("open");
  202. },
  203. onOpened: function onOpened() {
  204. this.$emit("opened");
  205. },
  206. onClose: function onClose() {
  207. this.$emit("close");
  208. },
  209. onClosed: function onClosed() {
  210. this.$emit("closed");
  211. },
  212. onClickDay: function onClickDay(event) {
  213. var date = event.detail.date;
  214. var _this$data2 = this.data,
  215. type = _this$data2.type,
  216. currentDate = _this$data2.currentDate,
  217. allowSameDay = _this$data2.allowSameDay;
  218. if (type === "range") {
  219. // @ts-ignore
  220. var _currentDate = _slicedToArray(currentDate, 2),
  221. startDay = _currentDate[0],
  222. endDay = _currentDate[1];
  223. if (startDay && !endDay) {
  224. var compareToStart = (0, _utils.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; // @ts-ignore
  237. var selected = currentDate.some(function (dateItem, index) {
  238. var equal = (0, _utils.compareDay)(dateItem, date) === 0;
  239. if (equal) {
  240. selectedIndex = index;
  241. }
  242. return equal;
  243. });
  244. if (selected) {
  245. // @ts-ignore
  246. var cancelDate = currentDate.splice(selectedIndex, 1);
  247. this.setData({
  248. currentDate: currentDate
  249. });
  250. this.unselect(cancelDate);
  251. } else {
  252. // @ts-ignore
  253. this.select([].concat(_toConsumableArray(currentDate), [date]));
  254. }
  255. } else {
  256. this.select(date, true);
  257. }
  258. },
  259. unselect: function unselect(dateArray) {
  260. var date = dateArray[0];
  261. if (date) {
  262. this.$emit("unselect", (0, _utils.copyDates)(date));
  263. }
  264. },
  265. select: function select(date, complete) {
  266. if (complete && this.data.type === "range") {
  267. var valid = this.checkRange(date);
  268. if (!valid) {
  269. // auto selected to max range if showConfirm
  270. if (this.data.showConfirm) {
  271. this.emit([date[0], (0, _utils.getDayByOffset)(date[0], this.data.maxRange - 1)]);
  272. } else {
  273. this.emit(date);
  274. }
  275. return;
  276. }
  277. }
  278. this.emit(date);
  279. if (complete && !this.data.showConfirm) {
  280. this.onConfirm();
  281. }
  282. },
  283. emit: function emit(date) {
  284. var getTime = function getTime(date) {
  285. return date instanceof Date ? date.getTime() : date;
  286. };
  287. this.setData({
  288. currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date)
  289. });
  290. this.$emit("select", (0, _utils.copyDates)(date));
  291. },
  292. checkRange: function checkRange(date) {
  293. var _this$data3 = this.data,
  294. maxRange = _this$data3.maxRange,
  295. rangePrompt = _this$data3.rangePrompt;
  296. if (maxRange && (0, _utils.calcDateNum)(date) > maxRange) {
  297. (0, _toast["default"])({
  298. context: this,
  299. message: rangePrompt || "\u9009\u62E9\u5929\u6570\u4E0D\u80FD\u8D85\u8FC7 ".concat(maxRange, " \u5929")
  300. });
  301. return false;
  302. }
  303. return true;
  304. },
  305. onConfirm: function onConfirm() {
  306. var _this3 = this;
  307. if (this.data.type === "range" && !this.checkRange(this.data.currentDate)) {
  308. return;
  309. }
  310. wx.nextTick(function () {
  311. // @ts-ignore
  312. _this3.$emit("confirm", (0, _utils.copyDates)(_this3.data.currentDate));
  313. });
  314. }
  315. }
  316. });