Pu Zhibing
2025-02-19 81e2eb4dd2e27da3c4cc447d6aeb9150a5ff7464
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
/**
 * @author: Alec Fenichel
 * @webSite: https://fenichelar.com
 * @update: zhixin wen <wenzhixin2010@gmail.com>
 */
 
var Utils = $.fn.bootstrapTable.utils
 
$.extend($.fn.bootstrapTable.defaults, {
  autoRefresh: false,
  showAutoRefresh: true,
  autoRefreshInterval: 60,
  autoRefreshSilent: true,
  autoRefreshStatus: true,
  autoRefreshFunction: null
})
 
$.extend($.fn.bootstrapTable.defaults.icons, {
  autoRefresh: {
    bootstrap3: 'glyphicon-time icon-time',
    bootstrap5: 'bi-clock',
    materialize: 'access_time',
    'bootstrap-table': 'icon-clock'
  }[$.fn.bootstrapTable.theme] || 'fa-clock'
})
 
$.extend($.fn.bootstrapTable.locales, {
  formatAutoRefresh () {
    return 'Auto Refresh'
  }
})
 
$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales)
 
$.BootstrapTable = class extends $.BootstrapTable {
  init (...args) {
    super.init(...args)
 
    if (this.options.autoRefresh && this.options.autoRefreshStatus) {
      this.setupRefreshInterval()
    }
  }
 
  initToolbar (...args) {
    if (this.options.autoRefresh) {
      this.buttons = Object.assign(this.buttons, {
        autoRefresh: {
          html: `
            <button class="auto-refresh ${this.constants.buttonsClass}
              ${this.options.autoRefreshStatus ? ` ${this.constants.classes.buttonActive}` : ''}"
              type="button" name="autoRefresh" title="${this.options.formatAutoRefresh()}">
              ${this.options.showButtonIcons ? Utils.sprintf(this.constants.html.icon, this.options.iconsPrefix, this.options.icons.autoRefresh) : ''}
              ${this.options.showButtonText ? this.options.formatAutoRefresh() : ''}
            </button>
           `,
          event: this.toggleAutoRefresh
        }
      })
    }
 
    super.initToolbar(...args)
  }
 
  toggleAutoRefresh () {
    if (this.options.autoRefresh) {
      if (this.options.autoRefreshStatus) {
        clearInterval(this.options.autoRefreshFunction)
        this.$toolbar.find('>.columns .auto-refresh')
          .removeClass(this.constants.classes.buttonActive)
      } else {
        this.setupRefreshInterval()
        this.$toolbar.find('>.columns .auto-refresh')
          .addClass(this.constants.classes.buttonActive)
      }
      this.options.autoRefreshStatus = !this.options.autoRefreshStatus
    }
  }
 
  destroy () {
    if (this.options.autoRefresh && this.options.autoRefreshStatus) {
      clearInterval(this.options.autoRefreshFunction)
    }
 
    super.destroy()
  }
 
  setupRefreshInterval () {
    this.options.autoRefreshFunction = setInterval(() => {
      if (!this.options.autoRefresh || !this.options.autoRefreshStatus) {
        return
      }
      this.refresh({ silent: this.options.autoRefreshSilent })
    }, this.options.autoRefreshInterval * 1000)
  }
}