date-picker.vue 6.4 KB
Newer Older
A
Amy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
<template>
  <cube-cascade-picker
    ref="cascadePicker"
    :data="data"
    :selectedIndex="selectedIndex"
    :title="title"
    :cancel-txt="cancelTxt"
    :confirm-txt="confirmTxt"
    :swipe-time="swipeTime"
    :z-index="zIndex"
    @select="_select"
    @cancel="_cancel"
    @change="_change">
  </cube-cascade-picker>
</template>

<script>
  import apiMixin from '../../common/mixins/api'
  import pickerMixin from '../../common/mixins/picker'

  const COMPONENT_NAME = 'date-picker'
  const EVENT_SELECT = 'select'
  const EVENT_CANCEL = 'cancel'
  const EVENT_CHANGE = 'change'

  const UNIT_LIST = ['year', 'month', 'date', 'hour', 'minute', 'second']
  const UNIT_RELATED_LIST = [
    {
      txt: '',
      pad: false
    },
    {
      txt: '',
      natureMin: 1,
      natureMax: 12,
      pad: false
    },
    {
      txt: '',
      natureMin: 1,
      natureMax: 31,
      pad: false
    },
    {
      txt: '',
      natureMin: 0,
      natureMax: 23,
      pad: false,
      natureRange: range(0, 23, false, '')
    },
    {
      txt: '',
      natureMin: 0,
      natureMax: 59,
      pad: true,
      natureRange: range(0, 59, true, '')
    },
    {
      txt: '',
      natureMin: 0,
      natureMax: 59,
      pad: true,
      natureRange: range(0, 59, true, '')
    }
  ]

  export default {
    name: COMPONENT_NAME,
    mixins: [apiMixin, pickerMixin],
    props: {
      min: {
        type: [Date, Array],
        default() {
          return new Date(2010, 1, 1)
        }
      },
      max: {
        type: [Date, Array],
        default() {
          return new Date(2020, 12, 31)
        }
      },
      startColumn: {
        type: String,
        default() {
          return 'year'
        }
      },
      columnCount: {
        type: Number,
        default: 3
      },
      value: {
        type: [Date, Array],
        default() {
          return this.min
        }
      }
    },
    computed: {
      startIndex() {
        let startIndex = UNIT_LIST.indexOf(this.startColumn)
        return startIndex < 0 ? 0 : startIndex
      },
      minArray() {
        return this.min instanceof Date
                ? dateToArray(this.min).slice(this.startIndex, this.startIndex + this.columnCount)
                : this.min
      },
      maxArray() {
        return this.max instanceof Date
                ? dateToArray(this.max).slice(this.startIndex, this.startIndex + this.columnCount)
                : this.max
      },
      valueArray() {
        return this.value instanceof Date
                ? dateToArray(this.value).slice(this.startIndex, this.startIndex + this.columnCount)
                : this.value
      },
      data() {
        let data = []
        this._generateData(this.startIndex, 0, data)

        return data
      },
      selectedIndex() {
        let selectedIndex = []
        let data = this.data
        let findIndex

        for (let i = 0; i < this.columnCount && i < 6 - this.startIndex; i++) {
          findIndex = data.findIndex((item) => {
            return this.valueArray[i] && item.value === this.valueArray[i]
          })
          selectedIndex[i] = findIndex !== -1 ? findIndex : 0
          data = data[selectedIndex[i]].children
        }

        return selectedIndex
      }
    },
    methods: {
      show() {
        this.$refs.cascadePicker.show()
      },
      hide() {
        this.$refs.cascadePicker.hide()
      },
      _select(selectedVal, selectedIndex, selectedText) {
        this.$emit(EVENT_SELECT, this._arrayToDate(selectedVal), selectedVal, selectedText)
      },
      _cancel() {
        this.$emit(EVENT_CANCEL)
      },
      _change(i, newIndex) {
        this.$emit(EVENT_CHANGE, i, newIndex)
      },
      _generateData(i, count, item) {
        if (count === 0) {
          let min = i === 0 ? this.minArray[0] : Math.max(this.minArray[0], UNIT_RELATED_LIST[i].natureMin)
          let max = i === 0 ? this.maxArray[0] : Math.min(this.maxArray[0], UNIT_RELATED_LIST[i].natureMax)
          item.push(...range(min, max, UNIT_RELATED_LIST[i].pad, UNIT_RELATED_LIST[i].txt, true, true))
        } else {
          if (i < 3 || item.isMin || item.isMax) {
            let natureMax = i === 2 ? computeNatrueMaxDay(item.value, item.year) : UNIT_RELATED_LIST[i].natureMax
            let min = item.isMin ? Math.max(this.minArray[count], UNIT_RELATED_LIST[i].natureMin) : UNIT_RELATED_LIST[i].natureMin
            let max = item.isMax ? Math.min(this.maxArray[count], natureMax) : natureMax

            let storageYear = i === 1 && this.startIndex === 0 && this.columnCount >= 3 && item.value
            item.children = range(min, max, UNIT_RELATED_LIST[i].pad, UNIT_RELATED_LIST[i].txt, item.isMin, item.isMax, storageYear)
          } else {
            item.children = UNIT_RELATED_LIST[i].natureRange
          }
        }
        if (count < this.columnCount - 1 && i < 5) {
          (item.children || item).forEach(subItem => {
            this._generateData(i + 1, count + 1, subItem)
          })
        }
      },
      _arrayToDate(selectedVal) {
        const args = []
        const defaultDateArray = dateToArray(new Date(0))

        for (let i = 0; i < 6; i++) {
          if (i < this.startIndex) {
            args[i] = defaultDateArray[i]
          } else if (i >= this.startIndex + this.columnCount) {
            args[i] = UNIT_RELATED_LIST[i].natureMin
          } else {
            args[i] = selectedVal[i - this.startIndex]
          }
        }
        // Month need to subtract 1.
        args[1]--

        return new Date(...args)
      }
    }
  }

  function range(min, max, pad = false, unit = '', fatherIsMin, fatherIsMax, year) {
    let arr = []
    for (let i = min; i <= max; i++) {
      const value = (pad && i < 10 ? '0' + i : i) + unit
      const object = {
        text: value,
        value: i
      }

      if (fatherIsMin && i === min) object.isMin = true
      if (fatherIsMax && i === max) object.isMax = true
      if (year) object.year = year

      arr.push(object)
    }
    return arr
  }

  function computeNatrueMaxDay(month, year) {
    let natureMaxDay = 30
    if ([1, 3, 5, 7, 8, 10, 12].indexOf(month) > -1) {
      natureMaxDay = 31
    } else {
      if (month === 2) {
        natureMaxDay = !year || (!(year % 400) || (!(year % 4) && year % 100)) ? 29 : 28
      }
    }

    return natureMaxDay
  }

  function dateToArray(date) {
    return [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()]
  }
</script>