bootstrap-table-auto-refresh.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableAutoRefresh = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: Alec Fenichel
  17. * @webSite: https://fenichelar.com
  18. * @version: v1.0.0
  19. */
  20. (function ($) {
  21. 'use strict';
  22. $.extend($.fn.bootstrapTable.defaults, {
  23. autoRefresh: false,
  24. autoRefreshInterval: 60,
  25. autoRefreshSilent: true,
  26. autoRefreshStatus: true,
  27. autoRefreshFunction: null
  28. });
  29. $.extend($.fn.bootstrapTable.defaults.icons, {
  30. autoRefresh: 'glyphicon-time icon-time'
  31. });
  32. $.extend($.fn.bootstrapTable.locales, {
  33. formatAutoRefresh: function formatAutoRefresh() {
  34. return 'Auto Refresh';
  35. }
  36. });
  37. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  38. var BootstrapTable = $.fn.bootstrapTable.Constructor;
  39. var _init = BootstrapTable.prototype.init;
  40. var _initToolbar = BootstrapTable.prototype.initToolbar;
  41. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  42. BootstrapTable.prototype.init = function () {
  43. _init.apply(this, Array.prototype.slice.apply(arguments));
  44. if (this.options.autoRefresh && this.options.autoRefreshStatus) {
  45. var that = this;
  46. this.options.autoRefreshFunction = setInterval(function () {
  47. that.refresh({ silent: that.options.autoRefreshSilent });
  48. }, this.options.autoRefreshInterval * 1000);
  49. }
  50. };
  51. BootstrapTable.prototype.initToolbar = function () {
  52. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  53. if (this.options.autoRefresh) {
  54. var $btnGroup = this.$toolbar.find('>.btn-group');
  55. var $btnAutoRefresh = $btnGroup.find('.auto-refresh');
  56. if (!$btnAutoRefresh.length) {
  57. $btnAutoRefresh = $([sprintf('<button class="btn btn-default auto-refresh %s" ', this.options.autoRefreshStatus ? 'enabled' : ''), 'type="button" ', sprintf('title="%s">', this.options.formatAutoRefresh()), sprintf('<i class="%s %s"></i>', this.options.iconsPrefix, this.options.icons.autoRefresh), '</button>'].join('')).appendTo($btnGroup);
  58. $btnAutoRefresh.on('click', $.proxy(this.toggleAutoRefresh, this));
  59. }
  60. }
  61. };
  62. BootstrapTable.prototype.toggleAutoRefresh = function () {
  63. if (this.options.autoRefresh) {
  64. if (this.options.autoRefreshStatus) {
  65. clearInterval(this.options.autoRefreshFunction);
  66. this.$toolbar.find('>.btn-group').find('.auto-refresh').removeClass('enabled');
  67. } else {
  68. var that = this;
  69. this.options.autoRefreshFunction = setInterval(function () {
  70. that.refresh({ silent: that.options.autoRefreshSilent });
  71. }, this.options.autoRefreshInterval * 1000);
  72. this.$toolbar.find('>.btn-group').find('.auto-refresh').addClass('enabled');
  73. }
  74. this.options.autoRefreshStatus = !this.options.autoRefreshStatus;
  75. }
  76. };
  77. })(jQuery);
  78. });