bootstrap-table-auto-refresh.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. var _createClass = function () {
  21. function defineProperties(target, props) {
  22. for (var i = 0; i < props.length; i++) {
  23. var descriptor = props[i];
  24. descriptor.enumerable = descriptor.enumerable || false;
  25. descriptor.configurable = true;
  26. if ("value" in descriptor) descriptor.writable = true;
  27. Object.defineProperty(target, descriptor.key, descriptor);
  28. }
  29. }
  30. return function (Constructor, protoProps, staticProps) {
  31. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  32. if (staticProps) defineProperties(Constructor, staticProps);
  33. return Constructor;
  34. };
  35. }();
  36. function _possibleConstructorReturn(self, call) {
  37. if (!self) {
  38. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  39. }
  40. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  41. }
  42. var _get = function get(object, property, receiver) {
  43. if (object === null) object = Function.prototype;
  44. var desc = Object.getOwnPropertyDescriptor(object, property);
  45. if (desc === undefined) {
  46. var parent = Object.getPrototypeOf(object);
  47. if (parent === null) {
  48. return undefined;
  49. } else {
  50. return get(parent, property, receiver);
  51. }
  52. } else if ("value" in desc) {
  53. return desc.value;
  54. } else {
  55. var getter = desc.get;
  56. if (getter === undefined) {
  57. return undefined;
  58. }
  59. return getter.call(receiver);
  60. }
  61. };
  62. function _inherits(subClass, superClass) {
  63. if (typeof superClass !== "function" && superClass !== null) {
  64. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. enumerable: false,
  70. writable: true,
  71. configurable: true
  72. }
  73. });
  74. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  75. }
  76. /**
  77. * @author: Alec Fenichel
  78. * @webSite: https://fenichelar.com
  79. * @update: zhixin wen <wenzhixin2010@gmail.com>
  80. */
  81. (function ($) {
  82. var Utils = $.fn.bootstrapTable.utils;
  83. $.extend($.fn.bootstrapTable.defaults, {
  84. autoRefresh: false,
  85. autoRefreshInterval: 60,
  86. autoRefreshSilent: true,
  87. autoRefreshStatus: true,
  88. autoRefreshFunction: null
  89. });
  90. $.extend($.fn.bootstrapTable.defaults.icons, {
  91. autoRefresh: Utils.bootstrapVersion === 4 ? 'fa-clock' : 'glyphicon-time icon-time'
  92. });
  93. $.extend($.fn.bootstrapTable.locales, {
  94. formatAutoRefresh: function formatAutoRefresh() {
  95. return 'Auto Refresh';
  96. }
  97. });
  98. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  99. $.BootstrapTable = function (_$$BootstrapTable) {
  100. _inherits(_class, _$$BootstrapTable);
  101. function _class() {
  102. _classCallCheck(this, _class);
  103. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  104. }
  105. _createClass(_class, [{
  106. key: 'init',
  107. value: function init() {
  108. var _get2,
  109. _this2 = this;
  110. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  111. args[_key] = arguments[_key];
  112. }
  113. (_get2 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'init', this)).call.apply(_get2, [this].concat(args));
  114. if (this.options.autoRefresh && this.options.autoRefreshStatus) {
  115. this.options.autoRefreshFunction = setInterval(function () {
  116. _this2.refresh({ silent: _this2.options.autoRefreshSilent });
  117. }, this.options.autoRefreshInterval * 1000);
  118. }
  119. }
  120. }, {
  121. key: 'initToolbar',
  122. value: function initToolbar() {
  123. var _get3;
  124. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  125. args[_key2] = arguments[_key2];
  126. }
  127. (_get3 = _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initToolbar', this)).call.apply(_get3, [this].concat(args));
  128. if (this.options.autoRefresh) {
  129. var $btnGroup = this.$toolbar.find('>.btn-group');
  130. var $btnAutoRefresh = $btnGroup.find('.auto-refresh');
  131. if (!$btnAutoRefresh.length) {
  132. $btnAutoRefresh = $('\n <button class="auto-refresh btn' + Utils.sprintf(' btn-%s', this.options.buttonsClass) + '\n ' + Utils.sprintf(' btn-%s', this.options.iconSize) + '\n ' + (this.options.autoRefreshStatus ? 'active' : '') + '"\n type="button" title="' + this.options.formatAutoRefresh() + '">\n <i class="' + this.options.iconsPrefix + ' ' + this.options.icons.autoRefresh + '"></i>\n </button>\n ').appendTo($btnGroup);
  133. $btnAutoRefresh.on('click', $.proxy(this.toggleAutoRefresh, this));
  134. }
  135. }
  136. }
  137. }, {
  138. key: 'toggleAutoRefresh',
  139. value: function toggleAutoRefresh() {
  140. var _this3 = this;
  141. if (this.options.autoRefresh) {
  142. if (this.options.autoRefreshStatus) {
  143. clearInterval(this.options.autoRefreshFunction);
  144. this.$toolbar.find('>.btn-group').find('.auto-refresh').removeClass('active');
  145. } else {
  146. this.options.autoRefreshFunction = setInterval(function () {
  147. _this3.refresh({ silent: _this3.options.autoRefreshSilent });
  148. }, this.options.autoRefreshInterval * 1000);
  149. this.$toolbar.find('>.btn-group').find('.auto-refresh').addClass('active');
  150. }
  151. this.options.autoRefreshStatus = !this.options.autoRefreshStatus;
  152. }
  153. }
  154. }]);
  155. return _class;
  156. }($.BootstrapTable);
  157. })(jQuery);
  158. });