This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nezha-fronted/nezha-fronted/src/components/common/pickTime.vue

210 lines
6.4 KiB
Vue
Raw Normal View History

<template>
<div class="interval-refresh margin-r-10">
<time-picker v-if="showTimePicker" ref="timePicker" v-model="searchTime" :default-pick="defaultPick" :show-empty="showEmpty" class="time-picker margin-r-10" size="small" @change="dateChange" :sign="sign"></time-picker>
2021-08-02 19:51:53 +08:00
<multipleTime v-if="showMultiple" ref="multipleTime" :stepSearchTime="searchTime" class="multiple-time margin-r-10" @change="dateChange(searchTime)"/>
<chart-unit v-if="useChartUnit" v-model="unit" class="margin-r-10"></chart-unit>
<div v-show="useRefresh" class="top-tool-btn-group">
<button :id="id+'-refresh'" class="top-tool-btn top-tool-btn--text" @click="antiShake" :title="id === 'explore' ? '': $t('dashboard.refresh')">
<i class="global-active-color nz-icon nz-icon-refresh" style="font-size: 14px"></i>&nbsp;
<span><slot name="added-text"></slot></span>
</button>
<button id="browser-go" class="top-tool-btn top-tool-btn--dropdown" @click="dropdownHandler(dropdownShow)" :title="$t('el.datepicker.selectTime')">
<span class="select-refresh-time-label" v-if="interval">{{interLabel}}</span>
<i class="nz-icon nz-icon-arrow-down" style="font-size: 12px;"></i>
<transition name="el-zoom-in-top">
2023-04-03 10:03:21 +08:00
<ul v-show="dropdownShow" class="el-dropdown-menu el-popper el-dropdown-menu--mini nz-dropdown popper-z-index" v-clickoutside="dropdownHandler">
<li v-for="i in $CONSTANTS.intervalList" :key="i.value + i.label" :style="{color:interval === i.value || interval.value === i.value ? theme.themeColor : ''}" class="el-dropdown-menu__item dropdown-content" @click="selectInterval(i,true)">
2021-11-04 18:21:20 +08:00
{{$t(i.label)}}
</li>
<li :key="-1" style="display: none" class="el-dropdown-menu__item dropdown-content" @click="selectInterval({label: 10, value: 10},true)">
{{$t(10)}}
</li>
</ul>
</transition>
</button>
</div>
</div>
</template>
<script>
2021-03-19 18:52:19 +08:00
import bus from '../../libs/bus'
import timePicker from './timePicker'
2021-03-31 10:02:12 +08:00
import multipleTime from './multipleTime'
2021-03-19 18:52:19 +08:00
import chartUnit from './chartUnit'
export default {
name: 'pickTime',
2021-03-19 18:52:19 +08:00
components: {
'time-picker': timePicker,
2021-03-31 10:02:12 +08:00
'chart-unit': chartUnit,
multipleTime
2021-03-19 18:52:19 +08:00
},
model: {
event: 'change',
prop: 'timeRange'
},
props: {
refreshDataFunc: {
required: true,
type: Function
},
2021-03-19 18:52:19 +08:00
timeRange: {
type: Array,
required: true
},
2021-03-19 18:52:19 +08:00
useRefresh: {
type: Boolean,
default: true
},
2021-03-19 18:52:19 +08:00
useChartUnit: {
type: Boolean,
default: true
},
2021-03-19 18:52:19 +08:00
showTimePicker: {
type: Boolean,
default: true
},
2021-03-31 10:02:12 +08:00
showMultiple: {
type: Boolean,
default: false
},
2021-03-19 18:52:19 +08:00
defaultPick: Number,
showEmpty: { type: Boolean, default: false },
id: String,
sign: [Number, String],
from: String
2021-03-19 18:52:19 +08:00
},
data () {
return {
searchTime: [],
visible: false,
intervalTimer: null,
interval: -1,
unit: 2,
dropdownShow: false,
2021-11-01 17:23:01 +08:00
interLabel: '',
theme: {
themeColor: ''
},
2022-01-04 16:26:15 +08:00
timer: ''
2021-03-19 18:52:19 +08:00
}
},
created () {
this.searchTime = this.timeFormate(this.timeRange)
this.$emit('change', this.searchTime)
},
methods: {
selectInterval (val, route) {
2021-03-19 18:52:19 +08:00
this.visible = false
clearInterval(this.intervalTimer)
this.interval = val.value
this.interLabel = val.label
if (!this.showTimePicker && val && val.value != 0) {
2021-03-19 18:52:19 +08:00
this.intervalTimer = setInterval(() => {
this.$emit('change', this.searchTime)
this.refreshDataFunc()
}, val.value * 1000)
return
}
// refresh拼接到地址栏参数中
if (this.from === 'dashboard' && route) {
const newQuery = JSON.parse(JSON.stringify(this.$route.query))
if (val.label == newQuery.refresh) return
val.value ? newQuery.refresh = val.label : delete newQuery.refresh
this.$router.replace({ query: newQuery })
}
if (val && val.value != 0) {
2021-03-19 18:52:19 +08:00
const start = new Date(this.searchTime[1])
const now = bus.getOffsetTimezoneData()
const interval = Math.floor((now - start.getTime()) / 1000) // 计算当前结束时间到现在的间隔(秒)
if (interval >= 60) { // 如果结束时间到现在超过30s
this.getIntervalData(interval)
}
2021-03-19 18:52:19 +08:00
this.intervalTimer = setInterval(() => {
this.getIntervalData(this.interval.value)
}, val.value * 1000)
}
},
dropdownHandler (show) {
if (!show) {
this.dropdownShow = true
} else {
this.dropdownShow = false
}
},
2021-03-19 18:52:19 +08:00
getIntervalData (interval) { // interval:结束时间到现在的秒数
// const start = new Date(this.searchTime[0])
// const end = new Date(this.searchTime[1])
// start.setSeconds(start.getSeconds() + interval)
// end.setSeconds(end.getSeconds() + interval)
//
// this.searchTime = this.timeFormate([start, end])
// this.$refs.timePicker.setCostomTime(this.searchTime)
// this.$emit('change', this.searchTime)
2021-03-19 18:52:19 +08:00
// 刷新数据
this.refreshDataFunc()
2020-04-28 19:23:48 +08:00
},
2021-03-19 18:52:19 +08:00
timeFormate (timeRange) {
if (timeRange && timeRange.length < 2) {
return []
}
2021-12-31 10:10:39 +08:00
const startTime = bus.timeFormate(timeRange[0], this.pickTimer)
const endTime = bus.timeFormate(timeRange[1], this.pickTimer)
2021-03-19 18:52:19 +08:00
return [startTime, endTime]
},
dateChange (time) {
this.searchTime = time
setTimeout(() => {
this.$emit('change', this.searchTime)
this.refreshDataFunc(true)
}, 100)
2021-10-22 10:32:04 +08:00
},
antiShake () {
if (this.timer) {
clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.refreshDataFunc()
2023-04-10 14:26:46 +08:00
this.timer = null
2021-10-22 10:32:04 +08:00
}, 200)
} else {
this.timer = setTimeout(() => {
this.refreshDataFunc()
2023-04-10 14:26:46 +08:00
this.timer = null
2021-10-22 10:32:04 +08:00
}, 200)
}
2021-03-19 18:52:19 +08:00
}
},
watch: {
unit: {
handler (n, o) {
this.$emit('unitChange', n)
2020-04-28 19:23:48 +08:00
}
}
},
beforeDestroy () {
2023-04-10 14:26:46 +08:00
if (this.timer) {
this.timer = setTimeout(() => {
this.refreshDataFunc()
this.timer = null
}, 200)
}
if (this.intervalTimer) {
clearInterval(this.intervalTimer)
this.intervalTimer = null
}
}
2021-03-19 18:52:19 +08:00
}
</script>
2021-11-01 17:23:01 +08:00
<style>
.interval-refresh {
display: flex;
}
2021-08-05 22:25:55 +08:00
.select-refresh-time-label{
margin-left: 5px;
}
.time-picker {
/* padding-left: 8px; */
2021-08-05 22:25:55 +08:00
display: flex;
}
</style>