CalendarMonth.js 5.81 KB
// components/CalendarMonth/CalendarMonth.js
import Calendar from './Calendar_Function'
import CalendarMonthOnly from './Calendar_Function_monthOnly'
import _component_ from './Static_Properties'
import { CLOSE_ICON_URL } from '~/utils/imageBase.js'
import {
  getInitialDayType,
  resolveMonthOnly,
  resolveDayOnly,
  buildMonthSetDataPayload,
  shouldUseSingleMonthView,
  shouldRebuildMonthListOnBoundsChange
} from './calendarMonth.helpers.js'
const CalendarFunction = Symbol('CalendarFunction')

Component({
  properties: _component_.properties,
  data: _component_.data,
  methods: {
    noScroll() {},

    /** 确保月份数据已 lazy 构建 */
    ensureMonthListReady() {
      if (this._monthListReady || this._monthOnlyMode) return
      this[CalendarFunction].setMonthList()
      this._monthListReady = true
    },

    /** 按外部回显值重置当前选中日期 */
    resetData(date) {
      this.ensureMonthListReady()
      let { monthList, current } = this.data
      monthList.map(x => {
        x.data.map(i => {
          i.active = false
          i.rangeLabel = ''
        })
      })
      monthList.map((item, index) => {
        item.data.map(ele => {
          if (ele.data == date) {
            ele.active = true
            current = index
          }
        })
      })
      this.setData(buildMonthSetDataPayload(this.data, { monthList, current }))
    },

    setCalenderBodyHeight() {
      this[CalendarFunction].setCalenderBodyHeight()
    },

    showCalendar(e) {
      if (this.data.disabled) return false
      let type = (e && e.currentTarget && e.currentTarget.dataset.type) || ''
      let result = type === 'show'
      if (!result) {
        this.setData({ hideCalendar: true })
        setTimeout(() => {
          this.setData({
            show: result,
            hideCalendar: false
          })
        }, 300)
      } else {
        if (!this._monthOnlyMode) {
          this.ensureMonthListReady()
        }
        this.setData({ show: result })
        if (!this.data.calenderBodyHeight) {
          this[CalendarFunction].setCalenderBodyHeight()
        }
      }
      this.triggerEvent('tapCalendar')
    },

    dateChange(e) {
      let { current, source } = e.detail
      let { dayType, dayOnly } = this.data
      if (source !== 'touch') return

      if (dayType) {
        if (!shouldUseSingleMonthView(dayOnly, false, false)) {
          this[CalendarFunction].onSwiperDateChange(current)
        }
      } else {
        this.setData({ yearCurrent: current })
      }
    },

    bindDateChange: function (e) {
      var date = e.detail.value
      this.setData({ date: date })
      this.post_data.year = date
      this.renderHtml()
    },

    choose(e) {
      let { dateType, minDate, maxDate } = this.data
      let { data } = e.currentTarget.dataset
      if (
        (minDate && Date.parse(minDate) > Date.parse(data)) ||
        (maxDate && Date.parse(maxDate) < Date.parse(data))
      ) {
        return
      }
      this[CalendarFunction][dateType](e)
    },

    monChoose(e) {
      const { data, type } = e.currentTarget.dataset
      if (data.disable) return false
      if (this._monthOnlyMode) {
        this[CalendarFunction][this.properties.dateType](e)
        return
      }
      this[CalendarFunction][type](e)
    },

    done() {
      if (this._monthOnlyMode) {
        this[CalendarFunction].done()
        return
      }
      this[CalendarFunction].done(this.data.dateType)
    },

    changeDate(e) {
      let { value } = e.detail
      if (this._monthOnlyMode) {
        this.setData({ current: value })
        return
      }
      this[CalendarFunction].onPickerMonthChange(Number(value))
    },

    bindYearChange(e) {
      let { value } = e.detail
      this.setData({ yearCurrent: value })
    },

    showShortcutPicker() {
      this.setData({ showShortcut: !this.data.showShortcut })
    },

    showShortcutPickerYear() {
      this.setData({ showShortcutYear: !this.data.showShortcutYear })
    },

    chooseAll() {
      this.ensureMonthListReady()
      this[CalendarFunction].chooseAll()
    },

    changeModel() {
      let { dayType } = this.data
      this.setData({ dayType: dayType == 0 ? 1 : 0 })
    },

    /**
     * minDate/maxDate 变更时失效 lazy monthList,弹层打开时立即重建
     * @param {string} minDate
     * @param {string} maxDate
     */
    onCalendarBoundsChange(minDate, maxDate) {
      if (!this[CalendarFunction]) return
      const prevMin = this._lastBoundsMin
      const prevMax = this._lastBoundsMax
      this._lastBoundsMin = minDate
      this._lastBoundsMax = maxDate
      if (prevMin === undefined && prevMax === undefined) return
      if (
        !shouldRebuildMonthListOnBoundsChange(prevMin, prevMax, minDate, maxDate)
      ) {
        return
      }
      this._monthListReady = false
      if (this.data.show && !this._monthOnlyMode) {
        this[CalendarFunction].setMonthList()
        this._monthListReady = true
      }
    }
  },

  observers: {
    'minDate, maxDate': function (minDate, maxDate) {
      this.onCalendarBoundsChange(minDate, maxDate)
    }
  },

  attached() {
    const monthOnly = resolveMonthOnly(
      this.properties.monthOnly,
      this.properties.monType
    )
    const dayOnly = resolveDayOnly(this.properties.dayOnly, monthOnly)
    this._monthOnlyMode = monthOnly
    this._dayOnlyMode = dayOnly
    this._monthListReady = false

    if (monthOnly) {
      this.setData({
        dayType: getInitialDayType({ monthOnly: true }),
        closeIconUrl: CLOSE_ICON_URL,
        dayOnly: false
      })
      this[CalendarFunction] = new CalendarMonthOnly(this)
      this._monthListReady = true
    } else {
      this.setData({
        dayType: getInitialDayType({ monthOnly: false, dayOnly }),
        closeIconUrl: CLOSE_ICON_URL,
        dayOnly
      })
      this[CalendarFunction] = new Calendar(this)
    }
  }
})