Calendar_Function_monthOnly.js 10.9 KB
const _CalendarObject_ = Symbol('_CalendarObject_')
const _nowDate_ = Symbol('_nowDate_')
const _separator_ = Symbol('_separator_')
/** @file monthOnly 模式日历逻辑 — 自 CalendarMonthSingle 保留,供首页绩效月历使用 */
class Calendar {
  [_CalendarObject_] = {};
  [_nowDate_] = new Date();
  [_separator_] = '-'
  constructor(calendarObject) {
    this[_CalendarObject_] = calendarObject
    this[_separator_] = calendarObject.data.separator
    this.setCalenderHeight()
    this.setMonthList()
    let { weekdays, firstDayOfWeek } = calendarObject.data
    let firstDay = weekdays[firstDayOfWeek - 1]
    let [last, first] = weekdays.join('').split(firstDay)
    calendarObject.setData({
      weekdays: [firstDay, ...first, ...last]
    })
  }
  setCalenderHeight() {
    let _this = this[_CalendarObject_]
    let { data: calenderHeight } = _this
    let height = '80%'
    if (typeof calenderHeight === 'number') {
      wx.$util.getSystemInfo(res => {
        let { windowHeight } = res
        if (calenderHeight > windowHeight) calenderHeight = windowHeight
        if (calenderHeight < windowHeight * 0.5) calenderHeight = windowHeight * 0.5
        height = `${calenderHeight}px`
      })
    }
    if (typeof calenderHeight === 'string') {
      if (calenderHeight.includes('%')) {
        height = calenderHeight
        let [num] = calenderHeight.split('%')
        if (num < 50) height = '50%'
        if (num > 100) height = '80%'
      } else {
        if (calenderHeight < 50) calenderHeight = '50'
        if (calenderHeight > 100) calenderHeight = '80'
        height = `${calenderHeight}%`
      }
    }
    _this.setData({
      isFullSucreen: wx.$util.isFullSucreen(_this.data.isComponents),
      height
    })
  }
  setCalenderBodyHeight() {
    let _this = this[_CalendarObject_]
    wx.$util.getViewInfo(
      '.calender-header',
      header => {
        wx.$util.getViewInfo(
          '.calender-footer',
          footer => {
            let headerHeight = header.height
            let footerHeight = (footer && footer.height) || 0
            _this.setData({
              calenderBodyHeight: `${headerHeight + footerHeight}px`
            })
          },
          _this
        )
      },
      _this
    )
  }
  setMonthList() {
    let { separator, previous, minDate, maxDate, next, date } = this[_CalendarObject_].data
    if (maxDate || minDate) {
      if (!minDate) {
        let nowMonth = new Date().getMonth()
        minDate = new Date(new Date().setMonth(nowMonth - previous)).format(`yyyy${separator}MM${separator}dd`)
      }
      if (!maxDate) {
        maxDate = this[_nowDate_].format(`yyyy${separator}MM${separator}dd`)
      }
      previous = this.getMonthDifference(minDate, this[_nowDate_].format(`yyyy${separator}MM${separator}dd`))
      next = this.getMonthDifference(this[_nowDate_].format(`yyyy${separator}MM${separator}dd`), maxDate)
    } else {
      previous = previous < 0 ? 0 : previous
      next = next < 0 ? 0 : next
    }
    let nowMonth = this[_nowDate_].getMonth()
    let now = new Date().setDate(1)
    for (let i = nowMonth - previous; i <= nowMonth + next; i++) {
      this.init(new Date(now).setMonth(i))
    }

    let _this = this[_CalendarObject_]
    let [minYear, minMonth] = minDate.split(separator)
    let [maxYear, maxMonth] = maxDate.split(separator)
    let yearList = []
    let now_Year = new Date().getFullYear()
    let now_Month = new Date().getMonth()
    for (let index = minYear; index <= maxYear; index++) {
      let mothObj = []
      for (let MonIndex = 0; MonIndex < 12; MonIndex++) {
        let month = MonIndex + 1 >= 10 ? MonIndex + 1 : `0${MonIndex + 1}`
        let obj = {
          label: `${MonIndex + 1}月`,
          disable: false,
          start: false,
          end: false,
          select: false,
          rangeDate: `${index}年${month + 1}月`,
          date: `${index}-${month}`
        }
        if ((MonIndex + 1 < minMonth && minYear == index) || (maxYear == index && maxMonth < MonIndex + 1)) {
          obj.disable = true
        }
        if (index == now_Year && MonIndex == now_Month) {
          obj.active = true
          date = obj
        }
        mothObj.push(obj)
      }
      yearList.unshift(mothObj)
    }

    _this.setData({
      yearList,
      rangeMonth: {
        start: new Date().format(`yyyy${separator}MM`),
        end: new Date().format(`yyyy${separator}MM`)
      },
      date
    })
  }
  init(date) {
    let _this = this[_CalendarObject_]
    let { firstDayOfWeek, allowSameDay, multipleDate, monthList, rangeDate, dateList, monPickList, dateType, current, minDate, maxDate, value } = _this.data
    let separator = this[_separator_]
    let [year, month] = new Date(date).format(`yyyy${separator}MM`).split(separator)
    let days = this.getDayNumber(year, Number(month))
    let week = new Date(year, Number(month) - 1, 1).getDay()
    let dateFormat = `yyyy${separator}MM${separator}dd`
    let today = value || this[_nowDate_].format(dateFormat)
    let nextDay = ''
    let arr = []
    dateList.push(`${year}${separator}${month}`)
    monPickList.unshift(year)
    monPickList = [...new Set(monPickList)]

    if (dateType === 'range') {
      nextDay = new Date(new Date().setDate(new Date(new Date(today)).getDate() + 1)).format(dateFormat)
    }
    let [now_year, now_month, now_day] = today.split(separator)
    let [next_year, next_month, next_day] = allowSameDay ? today.split(separator) : nextDay.split(separator)
    for (let i = 1; i <= days; i++) {
      let todayStr = `${year}${separator}${month}${separator}${now_day}`
      let dayStr = `${year}${separator}${month}${separator}${i < 10 ? `0${i}` : i}`
      let obj = {
        day: i,
        data: dayStr,
        active: now_year == year && now_month == month && i == Number(now_day)
      }
      if ((minDate && Date.parse(minDate) > Date.parse(dayStr)) || (maxDate && Date.parse(maxDate) < Date.parse(dayStr))) obj.disable = true
      if (dateType === 'multiple') multipleDate.push(todayStr)
      if (nextDay) {
        if (now_year == year && now_month == month && i == Number(now_day)) {
          obj.start = true
          rangeDate.start = todayStr
        }
        if (next_year == year && next_month == month && i == Number(next_day)) {
          obj.active = true
          obj.end = true
          rangeDate.end = `${year}${separator}${next_month}${separator}${next_day}`
        }
      }
      firstDayOfWeek = firstDayOfWeek == 7 ? 0 : firstDayOfWeek
      if (i === 1) obj.week = `week-${week - firstDayOfWeek}`
      arr.push(obj)
    }
    monthList.push({
      data: arr,
      mark: Number(month)
    })
    if (current <= 1) {
      let [nowYear, nowMonth] = today.split(separator)
      current = dateList.findIndex(e => e === `${nowYear}${separator}${nowMonth}`)
    }
    _this.setData({
      multipleDate,
      rangeDate,
      monthList,
      dateList,
      monPickList,
      current
    })
  }
  getMonthDifference(date1, date2) {
    let separator = this[_separator_]
    let [year1, month1] = date1.split(separator)
    let [year2, month2] = date2.split(separator)
    return (parseInt(year2) - parseInt(year1)) * 12 + (parseInt(month2) - parseInt(month1))
  }
  single(e) {
    let _this = this[_CalendarObject_]
    let { yearindex, index } = e.currentTarget.dataset
    let { showConfirm, yearList } = _this.data
    yearList.forEach(row => {
      row.forEach(item => {
        item.active = false
        item.start = false
        item.end = false
        item.select = false
      })
    })
    yearList[yearindex][index].active = true
    _this.setData({
      yearList,
      date: yearList[yearindex][index]
    })
    if (!showConfirm) this.done()
  }
  multiple(e) {
    let _this = this[_CalendarObject_]
    let { index, ind } = e.currentTarget.dataset
    let { monthList } = _this.data
    let arr = []
    monthList[index].data[ind].active = !monthList[index].data[ind].active
    monthList.map(x =>
      x.data.map(i => {
        if (i.active) arr.push(i.data)
      })
    )
    _this.setData({
      [`monthList[${index}].data`]: monthList[index].data,
      multipleDate: arr
    })
  }
  range(e) {
    const _this = this[_CalendarObject_]
    let { index, data, ind } = e.currentTarget.dataset
    let { allowSameDay, showConfirm, monthList, rangeDate } = _this.data
    const start = Date.parse(rangeDate.start)
    if (rangeDate.end || start > Date.parse(data)) {
      monthList = this.clearStatus({ monthList, index, ind })
      rangeDate.start = data
      rangeDate.end = ''
    } else {
      if (!allowSameDay && Date.parse(data) == Date.parse(rangeDate.start)) return
      monthList[index].data[ind].active = true
      monthList[index].data[ind].end = true
      rangeDate.end = data
      monthList.map(x => {
        x.data.map(i => {
          let day = Date.parse(i.data)
          i.middle = day > start && day < Date.parse(data)
        })
      })
    }
    _this.setData({ monthList, rangeDate })
    if (!showConfirm && rangeDate.end) this.done()
  }
  month(e) {
    const _this = this[_CalendarObject_]
    let { rangeMonth, yearList, showConfirm } = _this.data
    let { index, yearindex, data } = e.currentTarget.dataset
    const start = Date.parse(rangeMonth.start)
    let falseObj = { end: false, start: false, select: false }
    if (rangeMonth.end || start > Date.parse(data.date)) {
      yearList = yearList.map(item => item.map(ele => ({ ...ele, ...falseObj })))
      yearList[yearindex][index]['start'] = true
      rangeMonth.start = data.date
      rangeMonth.end = ''
    } else {
      yearList[yearindex][index]['end'] = true
      rangeMonth.end = data.date
      yearList.map(x => {
        x.map(i => {
          let day = Date.parse(i.date)
          i.select = day > start && day < Date.parse(data.date)
        })
      })
    }
    _this.setData({ yearList, rangeMonth })
    if (!showConfirm && rangeMonth.end) this.done()
  }
  done() {
    let _this = this[_CalendarObject_]
    let { date } = _this.data
    _this.triggerEvent('select', { date })
    _this.showCalendar()
  }
  clearStatus(params) {
    let { monthList, index, ind } = params
    monthList.map(x =>
      x.data.map(i => {
        i.active = false
        i.start = false
        i.end = false
        i.middle = false
      })
    )
    monthList[index].data[ind].active = true
    monthList[index].data[ind].start = true
    return monthList
  }
  getDayNumber(year, month) {
    let days = 31
    switch (month) {
      case 2:
        days = (year % 4 == 0 && year % 100 !== 0) || year % 400 == 0 ? 29 : 28
        break
      case 4:
      case 6:
      case 9:
      case 11:
        days = 30
        break
    }
    return days
  }
  chooseAll() {
    let _this = this[_CalendarObject_]
    let { monthList, minDate, maxDate } = _this.data
    monthList.map((e, index) => {
      e.data.map((i, ind) => {
        let obj = { currentTarget: { dataset: { index, ind, data: i.data } } }
        if (i.data == minDate) this.range(obj)
        if (i.data == maxDate) this.range(obj)
      })
    })
  }
}

export default Calendar