index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. const defaults = {
  4. prefixCls: 'wux-calendar',
  5. monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  6. monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  7. dayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
  8. dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
  9. firstDay: 1, // First day of the week, Monday
  10. weekendDays: [0, 6], // Sunday and Saturday
  11. multiple: false,
  12. dateFormat: 'yyyy-mm-dd',
  13. direction: 'horizontal', // or 'vertical'
  14. minDate: null,
  15. maxDate: null,
  16. touchMove: true,
  17. animate: true,
  18. closeOnSelect: true,
  19. weekHeader: true,
  20. toolbar: true,
  21. value: [],
  22. onMonthAdd() {},
  23. onChange() {},
  24. onOpen() {},
  25. onClose() {},
  26. onDayClick() {},
  27. onMonthYearChangeStart() {},
  28. onMonthYearChangeEnd() {},
  29. }
  30. // 获取手指触摸点坐标
  31. const getTouchPosition = (e) => {
  32. const touches = e.touches[0] || e.changedTouches[0]
  33. return {
  34. x: touches.pageX,
  35. y: touches.pageY,
  36. }
  37. }
  38. // 获取元素旋转属性
  39. const getTransform = (translate, isH) => `transform: translate3d(${isH ? translate : 0}%, ${isH ? 0 : translate}%, 0)`
  40. // 判断两个日期是否在同一天
  41. const isSameDate = (a, b) => {
  42. const prev = new Date(a)
  43. const next = new Date(b)
  44. return prev.getFullYear() === next.getFullYear() && prev.getMonth() === next.getMonth() && prev.getDate() === next.getDate()
  45. }
  46. baseComponent({
  47. useFunc: true,
  48. data: defaults,
  49. computed: {
  50. classes: ['prefixCls, direction', function(prefixCls, direction) {
  51. const wrap = classNames(prefixCls, {
  52. [`${prefixCls}--${direction}`]: direction,
  53. })
  54. const content = `${prefixCls}__content`
  55. const hd = `${prefixCls}__hd`
  56. const toolbar = `${prefixCls}__toolbar`
  57. const picker = `${prefixCls}__picker`
  58. const link = `${prefixCls}__link`
  59. const prev = classNames(`${prefixCls}__icon`, {
  60. [`${prefixCls}__icon--prev`]: true,
  61. })
  62. const next = classNames(`${prefixCls}__icon`, {
  63. [`${prefixCls}__icon--next`]: true,
  64. })
  65. const value = `${prefixCls}__value`
  66. const bd = `${prefixCls}__bd`
  67. const weekdays = `${prefixCls}__weekdays`
  68. const weekday = `${prefixCls}__weekday`
  69. const months = `${prefixCls}__months`
  70. const monthsContent = `${prefixCls}__months-content`
  71. const month = `${prefixCls}__month`
  72. const days = `${prefixCls}__days`
  73. const day = `${prefixCls}__day`
  74. const text = `${prefixCls}__text`
  75. return {
  76. wrap,
  77. content,
  78. hd,
  79. toolbar,
  80. picker,
  81. link,
  82. prev,
  83. next,
  84. value,
  85. bd,
  86. weekdays,
  87. weekday,
  88. months,
  89. monthsContent,
  90. month,
  91. days,
  92. day,
  93. text,
  94. }
  95. }],
  96. },
  97. methods: {
  98. /**
  99. * 打开日历
  100. * @param {Object} opts
  101. */
  102. open(opts = {}) {
  103. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts))
  104. this.monthsTranslate = 0
  105. this.isH = options.direction === 'horizontal'
  106. this.$$setData({ in: true, ...options }).then(() => this.init())
  107. this.setValue(options.value)
  108. if (typeof this.fns.onOpen === 'function') {
  109. this.fns.onOpen.call(this)
  110. }
  111. },
  112. /**
  113. * 关闭日历
  114. */
  115. close() {
  116. this.$$setData({ in: false })
  117. if (typeof this.fns.onClose === 'function') {
  118. this.fns.onClose.call(this)
  119. }
  120. },
  121. /**
  122. * 初始化
  123. */
  124. init() {
  125. const weeks = this.setWeekHeader()
  126. const months = this.setMonthsHTML()
  127. const monthsTranslate = this.setMonthsTranslate()
  128. if (typeof this.fns.onMonthAdd === 'function') {
  129. months.forEach((month) => this.fns.onMonthAdd.call(this, month))
  130. }
  131. return this.$$setData({ weeks, months, monthsTranslate, wrapperTranslate: '' }).then(() => this.$$setData({...this.updateCurrentMonthYear() }))
  132. },
  133. /**
  134. * 设置月份的位置信息
  135. * @param {Number} translate
  136. */
  137. setMonthsTranslate(translate = this.monthsTranslate) {
  138. const prevMonthTranslate = -(translate + 1) * 100
  139. const currentMonthTranslate = -translate * 100
  140. const nextMonthTranslate = -(translate - 1) * 100
  141. return [
  142. getTransform(prevMonthTranslate, this.isH),
  143. getTransform(currentMonthTranslate, this.isH),
  144. getTransform(nextMonthTranslate, this.isH),
  145. ]
  146. },
  147. /**
  148. * 更新当前年月
  149. * @param {String} dir 方向
  150. */
  151. updateCurrentMonthYear(dir) {
  152. const { months, monthNames } = this.data
  153. if (typeof dir === 'undefined') {
  154. const currentMonth = parseInt(months[1].month, 10)
  155. const currentYear = parseInt(months[1].year, 10)
  156. const currentMonthName = monthNames[currentMonth]
  157. return {
  158. currentMonth,
  159. currentYear,
  160. currentMonthName,
  161. }
  162. }
  163. const currentMonth = parseInt(months[dir === 'next' ? (months.length - 1) : 0].month, 10)
  164. const currentYear = parseInt(months[dir === 'next' ? (months.length - 1) : 0].year, 10)
  165. const currentMonthName = monthNames[currentMonth]
  166. return {
  167. currentMonth,
  168. currentYear,
  169. currentMonthName,
  170. }
  171. },
  172. /**
  173. * 手指触摸动作开始
  174. * @param {Object} e 事件对象
  175. */
  176. onTouchStart(e) {
  177. if (!this.data.touchMove || this.isMoved || this.isRendered) return
  178. this.start = getTouchPosition(e)
  179. this.move = {}
  180. this.touchesDiff = 0
  181. this.allowItemClick = true
  182. this.isMoved = false
  183. },
  184. /**
  185. * 手指触摸后移动
  186. * @param {Object} e 事件对象
  187. */
  188. onTouchMove(e) {
  189. if (!this.data.touchMove || this.isRendered) return
  190. this.allowItemClick = false
  191. if (!this.isMoved) {
  192. this.isMoved = true
  193. }
  194. this.$$setData({ swiping: true })
  195. const { prefixCls } = this.data
  196. const query = wx.createSelectorQuery().in(this)
  197. query.select(`.${prefixCls}__months-content`).boundingClientRect((rect) => {
  198. // 由于 boundingClientRect 为异步方法,某些情况下其回调函数在 onTouchEnd 之后触发,导致 wrapperTranslate 计算错误
  199. // 所以判断 this.isMoved = false 时阻止回调函数的执行
  200. if (!rect || !this.isMoved) return
  201. this.move = getTouchPosition(e)
  202. this.touchesDiff = this.isH ? this.move.x - this.start.x : this.move.y - this.start.y
  203. const { width, height } = rect
  204. const percentage = this.touchesDiff / (this.isH ? width : height)
  205. const currentTranslate = (this.monthsTranslate + percentage) * 100
  206. const transform = getTransform(currentTranslate, this.isH)
  207. this.$$setData({
  208. wrapperTranslate: `transition-duration: 0s; ${transform}`,
  209. })
  210. })
  211. query.exec()
  212. },
  213. /**
  214. * 手指触摸动作结束
  215. */
  216. onTouchEnd() {
  217. if (!this.data.touchMove || !this.isMoved || this.isRendered) return
  218. this.isMoved = false
  219. this.$$setData({ swiping: false })
  220. if (Math.abs(this.touchesDiff) < 30) {
  221. this.resetMonth()
  222. } else if (this.touchesDiff >= 30) {
  223. this.prevMonth()
  224. } else {
  225. this.nextMonth()
  226. }
  227. // Allow click
  228. setTimeout(() => (this.allowItemClick = true), 100)
  229. },
  230. /**
  231. * 日期的点击事件
  232. * @param {Object} e 事件对象
  233. */
  234. onDayClick(e) {
  235. if (this.allowItemClick) {
  236. const dataset = e.currentTarget.dataset
  237. const dateYear = dataset.year
  238. const dateMonth = dataset.month
  239. const dateDay = dataset.day
  240. const dateType = dataset.type
  241. if (dateType.selected && !this.data.multiple) return
  242. if (dateType.disabled) return
  243. if (dateType.next) this.nextMonth()
  244. if (dateType.prev) this.prevMonth()
  245. if (typeof this.fns.onDayClick === 'function') {
  246. this.fns.onDayClick.call(this, dateYear, dateMonth, dateDay)
  247. }
  248. this.addValue(new Date(dateYear, dateMonth, dateDay).getTime())
  249. if (this.data.closeOnSelect && !this.data.multiple) {
  250. this.close()
  251. }
  252. }
  253. },
  254. /**
  255. * 重置月份的位置信息
  256. */
  257. resetMonth() {
  258. const translate = this.monthsTranslate * 100
  259. const transform = getTransform(translate, this.isH)
  260. this.$$setData({
  261. wrapperTranslate: `transition-duration: 0s; ${transform}`,
  262. })
  263. },
  264. /**
  265. * 设置年月
  266. * @param {String} year 年份
  267. * @param {String} month 月份
  268. */
  269. setYearMonth(year = this.data.currentYear, month = this.data.currentMonth) {
  270. const { months, monthsTranslate, maxDate, minDate, currentYear, currentMonth } = this.data
  271. const targetDate = year < currentYear ? new Date(year, month + 1, -1).getTime() : new Date(year, month).getTime()
  272. // 判断是否存在最大日期
  273. if (maxDate && targetDate > new Date(maxDate).getTime()) return
  274. // 判断是否存在最小日期
  275. if (minDate && targetDate < new Date(minDate).getTime()) return
  276. const currentDate = new Date(currentYear, currentMonth).getTime()
  277. const dir = targetDate > currentDate ? 'next' : 'prev'
  278. const newMonthHTML = this.monthHTML(new Date(year, month))
  279. const prevTranslate = this.monthsTranslate = this.monthsTranslate || 0
  280. if (targetDate > currentDate) {
  281. this.monthsTranslate = this.monthsTranslate - 1
  282. const translate = -(prevTranslate - 1) * 100
  283. const nextMonthTranslate = getTransform(translate, this.isH)
  284. this.$$setData({
  285. months: [months[1], months[2], newMonthHTML],
  286. monthsTranslate: [monthsTranslate[1], monthsTranslate[2], nextMonthTranslate],
  287. })
  288. } else {
  289. this.monthsTranslate = this.monthsTranslate + 1
  290. const translate = -(prevTranslate + 1) * 100
  291. const prevMonthTranslate = getTransform(translate, this.isH)
  292. this.$$setData({
  293. months: [newMonthHTML, months[0], months[1]],
  294. monthsTranslate: [prevMonthTranslate, monthsTranslate[0], monthsTranslate[1]],
  295. })
  296. }
  297. this.onMonthChangeStart(dir)
  298. const transform = getTransform(this.monthsTranslate * 100, this.isH)
  299. const duration = this.data.animate ? .3 : 0
  300. const wrapperTranslate = `transition-duration: ${duration}s; ${transform}`
  301. this.$$setData({
  302. wrapperTranslate,
  303. })
  304. setTimeout(() => this.onMonthChangeEnd(dir, true), duration)
  305. },
  306. /**
  307. * 下一年
  308. */
  309. nextYear() {
  310. this.setYearMonth(this.data.currentYear + 1)
  311. },
  312. /**
  313. * 上一年
  314. */
  315. prevYear() {
  316. this.setYearMonth(this.data.currentYear - 1)
  317. },
  318. /**
  319. * 下一月
  320. */
  321. nextMonth() {
  322. const { months, monthsTranslate, maxDate, currentMonth } = this.data
  323. const nextMonth = parseInt(months[months.length - 1].month, 10)
  324. const nextYear = parseInt(months[months.length - 1].year, 10)
  325. const nextDate = new Date(nextYear, nextMonth)
  326. const nextDateTime = nextDate.getTime()
  327. // 判断是否存在最大日期
  328. if (maxDate && nextDateTime > new Date(maxDate).getTime()) {
  329. return this.resetMonth()
  330. }
  331. this.monthsTranslate = this.monthsTranslate - 1
  332. if (nextMonth === currentMonth) {
  333. const translate = -(this.monthsTranslate) * 100
  334. const nextMonthHTML = this.monthHTML(nextDateTime, 'next')
  335. const nextMonthTranslate = getTransform(translate, this.isH)
  336. const months = [this.data.months[1], this.data.months[2], nextMonthHTML]
  337. this.$$setData({
  338. months,
  339. monthsTranslate: [monthsTranslate[1], monthsTranslate[2], nextMonthTranslate],
  340. })
  341. if (typeof this.fns.onMonthAdd === 'function') {
  342. this.fns.onMonthAdd.call(this, months[months.length - 1])
  343. }
  344. }
  345. this.onMonthChangeStart('next')
  346. const transform = getTransform(this.monthsTranslate * 100, this.isH)
  347. const duration = this.data.animate ? .3 : 0
  348. const wrapperTranslate = `transition-duration: ${duration}s; ${transform}`
  349. this.$$setData({
  350. wrapperTranslate,
  351. })
  352. setTimeout(() => this.onMonthChangeEnd('next'), duration)
  353. },
  354. /**
  355. * 上一月
  356. */
  357. prevMonth() {
  358. const { months, monthsTranslate, minDate, currentMonth } = this.data
  359. const prevMonth = parseInt(months[0].month, 10)
  360. const prevYear = parseInt(months[0].year, 10)
  361. const prevDate = new Date(prevYear, prevMonth + 1, -1)
  362. const prevDateTime = prevDate.getTime()
  363. // 判断是否存在最小日期
  364. if (minDate && prevDateTime < new Date(minDate).getTime()) {
  365. return this.resetMonth()
  366. }
  367. this.monthsTranslate = this.monthsTranslate + 1
  368. if (prevMonth === currentMonth) {
  369. const translate = -(this.monthsTranslate) * 100
  370. const prevMonthHTML = this.monthHTML(prevDateTime, 'prev')
  371. const prevMonthTranslate = getTransform(translate, this.isH)
  372. const months = [prevMonthHTML, this.data.months[0], this.data.months[1]]
  373. this.$$setData({
  374. months,
  375. monthsTranslate: [prevMonthTranslate, monthsTranslate[0], monthsTranslate[1]],
  376. })
  377. if (typeof this.fns.onMonthAdd === 'function') {
  378. this.fns.onMonthAdd.call(this, months[0])
  379. }
  380. }
  381. this.onMonthChangeStart('prev')
  382. const transform = getTransform(this.monthsTranslate * 100, this.isH)
  383. const duration = this.data.animate ? .3 : 0
  384. const wrapperTranslate = `transition-duration: ${duration}s; ${transform}`
  385. this.$$setData({
  386. wrapperTranslate,
  387. })
  388. setTimeout(() => this.onMonthChangeEnd('prev'), duration)
  389. },
  390. /**
  391. * 月份变化开始时的回调函数
  392. * @param {String} dir 方向
  393. */
  394. onMonthChangeStart(dir) {
  395. const params = this.updateCurrentMonthYear(dir)
  396. this.$$setData(params)
  397. if (typeof this.fns.onMonthYearChangeStart === 'function') {
  398. this.fns.onMonthYearChangeStart.call(this, params.currentYear, params.currentMonth)
  399. }
  400. },
  401. /**
  402. * 月份变化完成时的回调函数
  403. * @param {String} dir 方向
  404. * @param {Boolean} rebuildBoth 重置
  405. */
  406. onMonthChangeEnd(dir = 'next', rebuildBoth = false) {
  407. const { currentYear, currentMonth } = this.data
  408. let nextMonthHTML, prevMonthHTML, newMonthHTML, months = [...this.data.months]
  409. if (!rebuildBoth) {
  410. newMonthHTML = this.monthHTML(new Date(currentYear, currentMonth), dir)
  411. if (dir === 'next') {
  412. months = [months[1], months[2], newMonthHTML]
  413. } else if (dir === 'prev') {
  414. months = [newMonthHTML, months[0], months[1]]
  415. }
  416. } else {
  417. prevMonthHTML = this.monthHTML(new Date(currentYear, currentMonth), 'prev')
  418. nextMonthHTML = this.monthHTML(new Date(currentYear, currentMonth), 'next')
  419. months = [prevMonthHTML, months[dir === 'next' ? months.length - 1 : 0], nextMonthHTML]
  420. }
  421. const monthsTranslate = this.setMonthsTranslate(this.monthsTranslate)
  422. this.isRendered = true
  423. this.$$setData({ months, monthsTranslate }).then(() => (this.isRendered = false))
  424. if (typeof this.fns.onMonthAdd === 'function') {
  425. this.fns.onMonthAdd.call(this, dir === 'next' ? months[months.length - 1] : months[0])
  426. }
  427. if (typeof this.fns.onMonthYearChangeEnd === 'function') {
  428. this.fns.onMonthYearChangeEnd.call(this, currentYear, currentMonth)
  429. }
  430. },
  431. /**
  432. * 设置星期
  433. */
  434. setWeekHeader() {
  435. const { weekHeader, firstDay, dayNamesShort, weekendDays } = this.data
  436. const weeks = []
  437. if (weekHeader) {
  438. for (let i = 0; i < 7; i++) {
  439. const weekDayIndex = (i + firstDay > 6) ? (i - 7 + firstDay) : (i + firstDay)
  440. const dayName = dayNamesShort[weekDayIndex]
  441. const weekend = weekendDays.indexOf(weekDayIndex) >= 0
  442. weeks.push({
  443. weekend,
  444. dayName,
  445. })
  446. }
  447. }
  448. return weeks
  449. },
  450. /**
  451. * 判断日期是否存在
  452. */
  453. daysInMonth(date) {
  454. const d = new Date(date)
  455. return new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate()
  456. },
  457. /**
  458. * 设置月份数据
  459. */
  460. monthHTML(date, offset) {
  461. date = new Date(date)
  462. let year = date.getFullYear(),
  463. month = date.getMonth(),
  464. time = date.getTime()
  465. const monthHTML = {
  466. year,
  467. month,
  468. time,
  469. items: [],
  470. }
  471. if (offset === 'next') {
  472. if (month === 11) date = new Date(year + 1, 0)
  473. else date = new Date(year, month + 1, 1)
  474. }
  475. if (offset === 'prev') {
  476. if (month === 0) date = new Date(year - 1, 11)
  477. else date = new Date(year, month - 1, 1)
  478. }
  479. if (offset === 'next' || offset === 'prev') {
  480. month = date.getMonth()
  481. year = date.getFullYear()
  482. time = date.getTime()
  483. }
  484. let daysInPrevMonth = this.daysInMonth(new Date(date.getFullYear(), date.getMonth()).getTime() - 10 * 24 * 60 * 60 * 1000),
  485. daysInMonth = this.daysInMonth(date),
  486. firstDayOfMonthIndex = new Date(date.getFullYear(), date.getMonth()).getDay()
  487. if (firstDayOfMonthIndex === 0) firstDayOfMonthIndex = 7
  488. let dayDate, currentValues = [],
  489. i, j,
  490. rows = 6,
  491. cols = 7,
  492. dayIndex = 0 + (this.data.firstDay - 1),
  493. today = new Date().setHours(0, 0, 0, 0),
  494. minDate = this.data.minDate ? new Date(this.data.minDate).getTime() : null,
  495. maxDate = this.data.maxDate ? new Date(this.data.maxDate).getTime() : null
  496. if (this.data.value && this.data.value.length) {
  497. for (let i = 0; i < this.data.value.length; i++) {
  498. currentValues.push(new Date(this.data.value[i]).setHours(0, 0, 0, 0))
  499. }
  500. }
  501. for (let i = 1; i <= rows; i++) {
  502. let rowHTML = []
  503. let row = i
  504. for (let j = 1; j <= cols; j++) {
  505. let col = j
  506. dayIndex++
  507. let dayNumber = dayIndex - firstDayOfMonthIndex
  508. let type = {}
  509. if (dayNumber < 0) {
  510. dayNumber = daysInPrevMonth + dayNumber + 1
  511. type.prev = true
  512. dayDate = new Date(month - 1 < 0 ? year - 1 : year, month - 1 < 0 ? 11 : month - 1, dayNumber).getTime()
  513. } else {
  514. dayNumber = dayNumber + 1
  515. if (dayNumber > daysInMonth) {
  516. dayNumber = dayNumber - daysInMonth
  517. type.next = true
  518. dayDate = new Date(month + 1 > 11 ? year + 1 : year, month + 1 > 11 ? 0 : month + 1, dayNumber).getTime()
  519. } else {
  520. dayDate = new Date(year, month, dayNumber).getTime()
  521. }
  522. }
  523. // Today
  524. if (dayDate === today) type.today = true
  525. // Selected
  526. if (currentValues.indexOf(dayDate) >= 0) type.selected = true
  527. // Weekend
  528. if (this.data.weekendDays.indexOf(col - 1) >= 0) {
  529. type.weekend = true
  530. }
  531. // Disabled
  532. if ((minDate && dayDate < minDate) || (maxDate && dayDate > maxDate)) {
  533. type.disabled = true
  534. }
  535. dayDate = new Date(dayDate)
  536. const dayYear = dayDate.getFullYear()
  537. const dayMonth = dayDate.getMonth()
  538. rowHTML.push({
  539. type,
  540. year: dayYear,
  541. month: dayMonth,
  542. day: dayNumber,
  543. date: `${dayYear}-${dayMonth + 1}-${dayNumber}`,
  544. })
  545. }
  546. monthHTML.year = year
  547. monthHTML.month = month
  548. monthHTML.time = time
  549. monthHTML.items.push(rowHTML)
  550. }
  551. return monthHTML
  552. },
  553. /**
  554. * 设置月份
  555. */
  556. setMonthsHTML() {
  557. const layoutDate = this.data.value && this.data.value.length ? this.data.value[0] : new Date().setHours(0, 0, 0, 0)
  558. const prevMonthHTML = this.monthHTML(layoutDate, 'prev')
  559. const currentMonthHTML = this.monthHTML(layoutDate)
  560. const nextMonthHTML = this.monthHTML(layoutDate, 'next')
  561. return [prevMonthHTML, currentMonthHTML, nextMonthHTML]
  562. },
  563. /**
  564. * 格式化日期
  565. */
  566. formatDate(date) {
  567. date = new Date(date)
  568. const year = date.getFullYear()
  569. const month = date.getMonth()
  570. const month1 = month + 1
  571. const day = date.getDate()
  572. const weekDay = date.getDay()
  573. return this.data.dateFormat
  574. .replace(/yyyy/g, year)
  575. .replace(/yy/g, (year + '').substring(2))
  576. .replace(/mm/g, month1 < 10 ? '0' + month1 : month1)
  577. .replace(/m/g, month1)
  578. .replace(/MM/g, this.data.monthNames[month])
  579. .replace(/M/g, this.data.monthNamesShort[month])
  580. .replace(/dd/g, day < 10 ? '0' + day : day)
  581. .replace(/d/g, day)
  582. .replace(/DD/g, this.data.dayNames[weekDay])
  583. .replace(/D/g, this.data.dayNamesShort[weekDay])
  584. },
  585. /**
  586. * 添加选中值
  587. */
  588. addValue(value) {
  589. if (this.data.multiple) {
  590. let arrValues = this.data.value || []
  591. let inValuesIndex = -1
  592. for (let i = 0; i < arrValues.length; i++) {
  593. if (isSameDate(value, arrValues[i])) {
  594. inValuesIndex = i
  595. }
  596. }
  597. if (inValuesIndex === -1) {
  598. arrValues.push(value)
  599. } else {
  600. arrValues.splice(inValuesIndex, 1)
  601. }
  602. this.setValue(arrValues)
  603. } else {
  604. this.setValue([value])
  605. }
  606. },
  607. /**
  608. * 设置选择值
  609. */
  610. setValue(value) {
  611. this.$$setData({ value }).then(() => this.updateValue())
  612. },
  613. /**
  614. * 更新日历
  615. */
  616. updateValue() {
  617. const changedPath = {}
  618. this.data.months.forEach((n, i) => {
  619. n.items.forEach((v, k) => {
  620. v.forEach((p, j) => {
  621. if (p.type.selected) {
  622. changedPath[`months[${i}].items[${k}][${j}].type.selected`] = false
  623. }
  624. })
  625. })
  626. })
  627. for (let ii = 0; ii < this.data.value.length; ii++) {
  628. const valueDate = new Date(this.data.value[ii])
  629. const valueYear = valueDate.getFullYear()
  630. const valueMonth = valueDate.getMonth()
  631. const valueDay = valueDate.getDate()
  632. this.data.months.forEach((n, i) => {
  633. if (n.year === valueYear && n.month === valueMonth) {
  634. n.items.forEach((v, k) => {
  635. v.forEach((p, j) => {
  636. if (p.year === valueYear && p.month === valueMonth && p.day === valueDay) {
  637. changedPath[`months[${i}].items[${k}][${j}].type.selected`] = true
  638. }
  639. })
  640. })
  641. }
  642. })
  643. }
  644. this.$$setData(changedPath)
  645. if (typeof this.fns.onChange === 'function') {
  646. this.fns.onChange.call(this, this.data.value, this.data.value.map((n) => this.formatDate(n)))
  647. }
  648. },
  649. noop() {},
  650. },
  651. })