mixin.js 3.8 KB
Newer Older
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
// 主要用于日和星期的互斥使用
const TYPE_NOT_SET = 'TYPE_NOT_SET'
const TYPE_EVERY = 'TYPE_EVERY'
const TYPE_RANGE = 'TYPE_RANGE'
const TYPE_LOOP = 'TYPE_LOOP'
const TYPE_WORK = 'TYPE_WORK'
const TYPE_LAST = 'TYPE_LAST'
const TYPE_SPECIFY = 'TYPE_SPECIFY'

const DEFAULT_VALUE = '?'

export default {
  model: {
    prop: 'prop',
    event: 'change'
  },
  props: {
    prop: {
      type: String,
      default: DEFAULT_VALUE
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    const type = TYPE_EVERY
    return {
      DEFAULT_VALUE,
      // 类型
      type,
      // 启用日或者星期互斥用
      TYPE_NOT_SET,
      TYPE_EVERY,
      TYPE_RANGE,
      TYPE_LOOP,
      TYPE_WORK,
      TYPE_LAST,
      TYPE_SPECIFY,
      // 对于不同的类型,所定义的值也有所不同
      valueRange: {
        start: 0,
        end: 0
      },
      valueLoop: {
        start: 0,
        interval: 1
      },
      valueWeek: {
        start: 0,
        end: 0
      },
      valueList: [],
      valueWork: 1,
      maxValue: 0,
      minValue: 0
    }
  },
  watch: {
    prop (newVal, oldVal) {
      if (newVal === this.value_c) {
        // console.info('skip ' + newVal)
        return
      }
      this.parseProp(newVal)
    }
  },
  computed: {
    value_c () {
      let result = []
      switch (this.type) {
        case TYPE_NOT_SET:
          result.push('?')
          break
        case TYPE_EVERY:
          result.push('*')
          break
        case TYPE_RANGE:
          result.push(`${this.valueRange.start}-${this.valueRange.end}`)
          break
        case TYPE_LOOP:
          result.push(`${this.valueLoop.start}/${this.valueLoop.interval}`)
          break
        case TYPE_WORK:
          result.push(`${this.valueWork}W`)
          break
        case TYPE_LAST:
          result.push('L')
          break
        case TYPE_SPECIFY:
          result.push(this.valueList.join(','))
          break
        default:
          result.push(this.DEFAULT_VALUE)
          break
      }
      return result.length > 0 ? result.join('') : this.DEFAULT_VALUE
    }
  },
  methods: {
    parseProp (value) {
      if (value === this.value_c) {
        // console.info('same ' + value)
        return
      }
      if (typeof (this.preProcessProp) === 'function') {
        value = this.preProcessProp(value)
      }
      try {
        if (!value || value === this.DEFAULT_VALUE) {
          this.type = TYPE_EVERY
        } else if (value.indexOf('?') >= 0) {
          this.type = TYPE_NOT_SET
        } else if (value.indexOf('-') >= 0) {
          this.type = TYPE_RANGE
          const values = value.split('-')
          if (values.length >= 2) {
            this.valueRange.start = parseInt(values[0])
            this.valueRange.end = parseInt(values[1])
          }
        } else if (value.indexOf('/') >= 0) {
          this.type = TYPE_LOOP
          const values = value.split('/')
          if (values.length >= 2) {
            this.valueLoop.start = value[0] === '*' ? 0 : parseInt(values[0])
            this.valueLoop.interval = parseInt(values[1])
          }
        } else if (value.indexOf('W') >= 0) {
          this.type = TYPE_WORK
          const values = value.split('W')
          if (!values[0] && !isNaN(values[0])) {
            this.valueWork = parseInt(values[0])
          }
        } else if (value.indexOf('L') >= 0) {
          this.type = TYPE_LAST
          const values = value.split('L')
          this.valueLast = parseInt(values[0])
        } else if (value.indexOf(',') >= 0 || !isNaN(value)) {
          this.type = TYPE_SPECIFY
          this.valueList = value.split(',').map(item => parseInt(item))
        } else {
          this.type = TYPE_EVERY
        }
      } catch (e) {
        // console.info(e)
        this.type = TYPE_EVERY
      }
    }
  }
}