refresh.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div style="display: inline;">
  3. <a-tooltip placement="top">
  4. <template slot="title" v-show="tooltips && lastSync !== ''">
  5. <span>{{ $t('refresh.last_sync_at', [lastSync]) }}</span>
  6. </template>
  7. <a-button style="width: 45px;padding-left: 15px;padding-right: 15px;" :icon="loading ? 'loading':'sync'" @click="emitRefresh" />
  8. </a-tooltip>
  9. <a-select v-if="showSelect" v-model="syncConfig.duration" @change="handleChange" style="width: 90px">
  10. <a-select-option :dropdownMatchSelectWidth="false" v-for="d of durations" :key="d.label" :value="d.value">
  11. {{ d.label }}
  12. </a-select-option>
  13. </a-select>
  14. </div>
  15. </template>
  16. <script>
  17. import i18n from '@/locales'
  18. export default {
  19. name: 'refresh',
  20. props: {
  21. value: {
  22. type: Number,
  23. default: 0,
  24. },
  25. loading: {
  26. type: Boolean,
  27. default: false,
  28. },
  29. tooltips: {
  30. type: Boolean,
  31. default: true,
  32. },
  33. showSelect: {
  34. type: Boolean,
  35. default: false,
  36. },
  37. durations: {
  38. type: Array,
  39. default: () => {
  40. return [
  41. { label: i18n.t('refresh.auto.disable'), value: 0 },
  42. { label: i18n.t('refresh.duration.seconds', [5]), value: 5 },
  43. { label: i18n.t('refresh.duration.seconds', [10]), value: 10 },
  44. { label: i18n.t('refresh.duration.seconds', [30]), value: 30 },
  45. { label: i18n.t('refresh.duration.minutes', [1]), value: 60 },
  46. { label: i18n.t('refresh.duration.minutes', [5]), value: 300 },
  47. { label: i18n.t('refresh.duration.minutes', [15]), value: 900 },
  48. { label: i18n.t('refresh.duration.minutes', [30]), value: 1800 },
  49. { label: i18n.t('refresh.duration.hours', [1]), value: 3600 },
  50. { label: i18n.t('refresh.duration.hours', [2]), value: 7200 },
  51. { label: i18n.t('refresh.duration.days', [1]), value: 86400 },
  52. ]
  53. },
  54. },
  55. },
  56. data () {
  57. let timer
  58. if (this.value > 0) {
  59. timer = setInterval(this.emitRefresh, this.value * 1000)
  60. }
  61. return {
  62. lastSync: '',
  63. timer: timer,
  64. syncConfig: {
  65. enable: false,
  66. duration: this.value,
  67. },
  68. }
  69. },
  70. beforeDestroy () {
  71. this.cancelAutoRefresh()
  72. },
  73. methods: {
  74. emitRefresh () {
  75. this.lastSync = new Date().toLocaleTimeString()
  76. if (this.loading) {
  77. return
  78. }
  79. this.$emit('refresh')
  80. },
  81. handleChange (v) {
  82. this.syncConfig.duration = v
  83. this.syncConfig.enable = v > 0
  84. this.syncConfig.enable ? this.resetAutoRefresh(v) : this.cancelAutoRefresh()
  85. this.emitRefresh()
  86. },
  87. resetAutoRefresh (v) {
  88. if (this.timer) {
  89. clearInterval(this.timer)
  90. }
  91. this.timer = setInterval(this.emitRefresh, v * 1000)
  92. },
  93. cancelAutoRefresh () {
  94. if (this.timer) {
  95. clearInterval(this.timer)
  96. }
  97. },
  98. },
  99. }
  100. </script>