DeDate.vue 11.9 KB
Newer Older
Tippi.Rao committed
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
<template>
  <el-date-picker
    v-if="element.options!== null && element.options.attrs!==null && show"
    ref="dateRef"
    v-model="values"
    :popper-class="'coustom-date-picker' + ' ' + extPoperClass"
    :type="componentType"
    :range-separator="$t(element.options.attrs.rangeSeparator)"
    :start-placeholder="$t(element.options.attrs.startPlaceholder)"
    :end-placeholder="$t(element.options.attrs.endPlaceholder)"
    :placeholder="$t(element.options.attrs.placeholder)"
    :append-to-body="inScreen"
    value-format="timestamp"
    :format="labelFormat"
    :size="size"
    :editable="false"
    :picker-options="pickerOptions"
    :default-time="defaultRangeTime"
    @change="dateChange"
    @focus="toFocus"
    @blur="onBlur"
  />
</template>

<script>
import { ApplicationContext } from '@/utils/ApplicationContext'
import { timeSection } from '@/utils'
import bus from '@/utils/bus'
import customInput from '@/components/widget/deWidget/customInput'
import { mapState } from 'vuex'
export default {
  mixins: [customInput],
  props: {
    canvasId: {
      type: String,
      required: true
    },
    element: {
      type: Object,
      default: null
    },
    inDraw: {
      type: Boolean,
      default: true
    },
    inScreen: {
      type: Boolean,
      required: false,
      default: true
    },
    size: String,
    isRelation: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      operator: 'between',
      values: null,
      onFocus: false,
      show: true,
Tippi.Rao committed
63 64
      outTimer: null,
      innerTimer: null
Tippi.Rao committed
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
    }
  },
  computed: {
    extPoperClass() {
      if (this.labelFormat && this.labelFormat.includes('HH') && !this.labelFormat.includes('HH:mm')) {
        return 'de-no-minite'
      }
      return ''
    },
    defaultoptions() {
      if (!this.element || !this.element.options || !this.element.options.attrs.default) return ''
      return JSON.stringify(this.element.options.attrs.default)
    },
    defaultValueStr() {
      if (!this.element || !this.element.options || !this.element.options.value) return ''
      return this.element.options.value.toString()
    },
    viewIds() {
      if (!this.element || !this.element.options || !this.element.options.attrs.viewIds) return ''
      return this.element.options.attrs.viewIds.toString()
    },
    manualModify() {
      return !!this.element.options.manualModify
    },
    isTimeWidget() {
      const widget = ApplicationContext.getService(this.element.serviceName)
      return widget.isTimeWidget && widget.isTimeWidget()
    },
    componentType() {
      let result = this.element.options.attrs.type || 'date'
      if (this.isTimeWidget && this.element.options.attrs.showTime) {
        result = this.element.serviceName === 'timeDateWidget' ? 'datetime' : 'datetimerange'
      }
      return result
    },
    labelFormat() {
      const result = 'yyyy-MM-dd'
      if (this.isTimeWidget && this.element.options.attrs.showTime && this.element.options.attrs.accuracy) {
        return result + ' ' + this.element.options.attrs.accuracy
      }
      return null
    },
    pickerOptions() {
      const widget = ApplicationContext.getService(this.element.serviceName)
      if (this.element.options.attrs.type === 'daterange' && widget.shortcuts) {
        const cuts = widget.shortcuts()
        const result = cuts.map(cut => {
          return {
            text: this.$t(cut.text),
            onClick: picker => {
              const param = cut.callBack()
              picker.$emit('pick', param)
            }
          }
        })
        return {
          shortcuts: result
        }
      }
      return null
    },
    defaultRangeTime() {
      if (this.element.options.attrs.type === 'daterange' && this.element.options.attrs.showTime) {
        return ['00:00:00', '23:59:59']
      }
      return null
    },
    ...mapState([
      'canvasStyleData'
    ])

  },
  watch: {
Tippi.Rao committed
138

Tippi.Rao committed
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
    'viewIds': function(value, old) {
      if (typeof value === 'undefined' || value === old) return
      this.setCondition()
    },
    'defaultValueStr': function(value, old) {
      if (this.element.options.attrs.default.isDynamic) {
        return
      }
      if (value === old) return
      this.values = this.fillValueDerfault()
      this.dateChange(value)
    },
    'defaultoptions': function(val, old) {
      if (!this.element.options.attrs.default.isDynamic) {
        this.values = this.fillValueDerfault()
        this.dateChange(this.values)
        return
      }
      if (val === old) return
      const widget = ApplicationContext.getService(this.element.serviceName)
      this.values = widget.dynamicDateFormNow(this.element)
      this.dateChange(this.values)
    },
    'labelFormat': function(val, old) {
      if (val !== old) {
        this.show = false
        this.$nextTick(() => {
          this.show = true
        })
      }
    }
  },
  created() {
    this.loadInit()
Tippi.Rao committed
173 174 175
    this.$nextTick(() => {
      this.dynamicRefresh()
    })
Tippi.Rao committed
176 177 178 179 180 181 182 183
  },
  mounted() {
    bus.$on('onScroll', this.onScroll)
    if (this.inDraw) {
      bus.$on('reset-default-value', this.resetDefaultValue)
    }
  },
  beforeDestroy() {
Tippi.Rao committed
184
    this.clearTime()
Tippi.Rao committed
185 186 187 188 189
    bus.$off('onScroll', this.onScroll)
    bus.$off('reset-default-value', this.resetDefaultValue)
  },
  methods: {
    loadInit() {
Tippi.Rao committed
190 191
      this.clearTime()
      if (this.refreshHandler()) {
Tippi.Rao committed
192 193 194 195 196 197 198
        return
      }
      if (this.element.options.value) {
        this.values = this.fillValueDerfault()
        this.dateChange(this.values)
      }
    },
Tippi.Rao committed
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
    refreshHandler() {
      if (this.element.options.attrs.default?.isDynamic) {
        const widget = ApplicationContext.getService(this.element.serviceName)
        this.values = widget.dynamicDateFormNow(this.element)
        this.dateChange(this.values)
        return true
      }
      return false
    },
    clearTime() {
      if (this.outTimer) {
        clearTimeout(this.outTimer)
        this.outTimer = null
      }
      if (this.innerTimer) {
        clearInterval(this.innerTimer)
        this.innerTimer = null
      }
    },
    dynamicRefresh() {
      if (this.inDraw && this.element.options.attrs.default?.isDynamic) {
        const nowDate = new Date()
        const nowTime = nowDate.getTime()
        const tomorrow = new Date(`${nowDate.getFullYear()}-${nowDate.getMonth() + 1}-${nowDate.getDate() + 1} 00:00:01`)
        const tomorrowTime = tomorrow.getTime()
        this.clearTime()
        this.outTimer = setTimeout(() => {
          if (this.inDraw) {
            this.refreshHandler()
Tippi.Rao committed
228
          }
Tippi.Rao committed
229 230 231 232 233 234
          this.innerTimer = setInterval(() => {
            if (this.inDraw) {
              this.refreshHandler()
            }
          }, 24 * 3600 * 1000)
        }, tomorrowTime - nowTime)
Tippi.Rao committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      }
    },
    clearHandler() {
      this.values = null
    },
    onScroll() {
      if (this.onFocus) {
        this.$refs.dateRef.hidePicker()
      }
    },
    resetDefaultValue(id) {
      if (this.inDraw && this.manualModify && this.element.id === id) {
        if (!this.element.options.attrs.default.isDynamic) {
          this.values = this.fillValueDerfault()
          this.dateChange(this.values)
          return
        }
        const widget = ApplicationContext.getService(this.element.serviceName)
        this.values = widget.dynamicDateFormNow(this.element)
        this.dateChange(this.values)
      }
    },
    onBlur() {
      this.onFocus = false
    },
    toFocus() {
      this.onFocus = true
      this.$nextTick(() => {
        this.handleCoustomStyle()
      })
    },
    search() {
      this.setCondition()
    },
    getCondition() {
      const param = {
        canvasId: this.canvasId,
        component: this.element,
        value: this.formatFilterValue(),
        operator: this.operator
      }
      param.value = this.formatValues(param.value)
      return param
    },
    setCondition() {
      const param = this.getCondition()
      !this.isRelation && this.inDraw && this.$store.commit('addViewFilter', param)
    },
    dateChange(value) {
      if (!this.inDraw) {
        if (value === null) {
          this.element.options.value = ''
        } else {
          this.element.options.value = Array.isArray(value) ? value.join() : value.toString()
        }
        this.element.options.manualModify = false
      } else {
        this.element.options.manualModify = true
      }
      this.setCondition()
    },
    formatFilterValue() {
      if (this.values === null) return []
      if (Array.isArray(this.values)) return this.values
      return [this.values]
    },
    formatValues(values) {
      if (!values || values.length === 0) {
        return []
      }
      if (this.element.options.attrs.type === 'daterange') {
        if (values.length !== 2) {
          return null
        }
        let start = values[0]
        let end = values[1]
        start = timeSection(start, 'datetime', this.labelFormat)[0]
        end = timeSection(end, 'datetime', this.labelFormat)[1]

        const results = [start, end]
        return results
      } else {
        const value = values[0]
        return timeSection(parseFloat(value), this.componentType || this.element.options.attrs.type, this.labelFormat)
      }
    },
    fillValueDerfault() {
      const defaultV = this.element.options.value === null ? '' : this.element.options.value.toString()
      if (this.element.options.attrs.type === 'daterange') {
        if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV ===
          '[object Object]') {
          return []
        }
        return defaultV.split(',').map(item => parseFloat(item))
      } else {
        if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV ===
          '[object Object]') {
          return null
        }
        return parseFloat(defaultV.split(',')[0])
      }
    }
  }
}

</script>

<style lang="scss">
.coustom-date-picker {
呼呼啦啦 committed
344
  right: 0px;
Tippi.Rao committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  border: 1px solid var(--BrDateColor, #dfe4ed) !important;
  background: var(--BgDateColor, #FFFFFF) !important;

  .el-picker-panel__sidebar {
    background: var(--BgDateColor, #FFFFFF) !important;
    border-right: 1px solid var(--BrDateColor, #dfe4ed) !important;

    .el-picker-panel__shortcut {
      color: var(--DateColor, #606266);
    }
  }

  .el-date-range-picker__time-header,
  .el-date-picker__time-header {
    border-bottom: 1px solid var(--BrDateColor, #dfe4ed) !important;
  }

  .el-picker-panel__footer {
    border-top: 1px solid var(--BrDateColor, #dfe4ed) !important;
    background: var(--BgDateColor, #FFFFFF) !important;
  }

  .el-date-range-picker__time-picker-wrap,
  .el-date-picker__time-header {
    .el-input__inner {
      border: 1px solid var(--BrDateColor, #dfe4ed) !important;
      color: var(--DateColor, #606266);
      background: var(--BgDateColor, #FFFFFF) !important;
    }
  }

  .el-picker-panel__link-btn:nth-child(2) {
    color: var(--DateColor, #409eff);
    background: var(--BgDateColor, #FFFFFF) !important;
    border: 1px solid var(--BrDateColor, #dfe4ed) !important;
  }

  .popper__arrow,
  .popper__arrow::after {
    display: none !important;
  }

  .el-date-range-picker__content.is-left {
    border-right: 1px solid var(--BrDateColor, #e6ebf5) !important;
  }

  .el-date-table th,
  .el-date-picker__header--bordered {
    border-bottom: 1px solid var(--BrDateColor, #e6ebf5) !important;
  }

  .el-date-table td.in-range:not(.end-date):not(.start-date) div span {
    color: #3370ff;
  }

  .el-date-range-picker__header,
  .el-date-table th,
  .el-date-table__row,
  .el-month-table td .cell,
  .el-year-table td .cell,
  .el-picker-panel__icon-btn,
  .el-date-picker__header-label {
    color: var(--DateColor, #606266);

  }

  .el-month-table td.current:not(.disabled) .cell,
  .el-month-table td.today:not(.disabled) .cell,
  .el-year-table td.current:not(.disabled) .cell,
  .el-year-table td.today:not(.disabled) .cell {
    color: #409EFF;
  }
}

.de-no-minite {
  .el-time-spinner__wrapper {
    width: 100% !important;
  }

  .el-scrollbar:nth-of-type(2) {
    display: none;
  }
}
</style>