CardRefresh.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE CardRefresh.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. const CardRefresh = (($) => {
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'CardRefresh'
  13. const DATA_KEY = 'lte.cardrefresh'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const Event = {
  17. LOADED: `loaded${EVENT_KEY}`,
  18. OVERLAY_ADDED: `overlay.added${EVENT_KEY}`,
  19. OVERLAY_REMOVED: `overlay.removed${EVENT_KEY}`,
  20. }
  21. const ClassName = {
  22. CARD: 'card',
  23. }
  24. const Selector = {
  25. CARD: `.${ClassName.CARD}`,
  26. DATA_REFRESH: '[data-card-widget="card-refresh"]',
  27. }
  28. const Default = {
  29. source: '',
  30. sourceSelector: '',
  31. params: {},
  32. trigger: Selector.DATA_REFRESH,
  33. content: '.card-body',
  34. loadInContent: true,
  35. loadOnInit: true,
  36. responseType: '',
  37. overlayTemplate: '<div class="overlay"><i class="fas fa-2x fa-sync-alt fa-spin"></i></div>',
  38. onLoadStart: function () {
  39. },
  40. onLoadDone: function (response) {
  41. return response;
  42. }
  43. }
  44. class CardRefresh {
  45. constructor(element, settings) {
  46. this._element = element
  47. this._parent = element.parents(Selector.CARD).first()
  48. this._settings = $.extend({}, Default, settings)
  49. this._overlay = $(this._settings.overlayTemplate)
  50. if (element.hasClass(ClassName.CARD)) {
  51. this._parent = element
  52. }
  53. if (this._settings.source === '') {
  54. throw new Error('Source url was not defined. Please specify a url in your CardRefresh source option.');
  55. }
  56. }
  57. load() {
  58. this._addOverlay()
  59. this._settings.onLoadStart.call($(this))
  60. $.get(this._settings.source, this._settings.params, function (response) {
  61. if (this._settings.loadInContent) {
  62. if (this._settings.sourceSelector != '') {
  63. response = $(response).find(this._settings.sourceSelector).html()
  64. }
  65. this._parent.find(this._settings.content).html(response)
  66. }
  67. this._settings.onLoadDone.call($(this), response)
  68. this._removeOverlay();
  69. }.bind(this), this._settings.responseType !== '' && this._settings.responseType)
  70. const loadedEvent = $.Event(Event.LOADED)
  71. $(this._element).trigger(loadedEvent)
  72. }
  73. _addOverlay() {
  74. this._parent.append(this._overlay)
  75. const overlayAddedEvent = $.Event(Event.OVERLAY_ADDED)
  76. $(this._element).trigger(overlayAddedEvent)
  77. };
  78. _removeOverlay() {
  79. this._parent.find(this._overlay).remove()
  80. const overlayRemovedEvent = $.Event(Event.OVERLAY_REMOVED)
  81. $(this._element).trigger(overlayRemovedEvent)
  82. };
  83. // Private
  84. _init(card) {
  85. $(this).find(this._settings.trigger).on('click', () => {
  86. this.load()
  87. })
  88. if (this._settings.loadOnInit) {
  89. this.load()
  90. }
  91. }
  92. // Static
  93. static _jQueryInterface(config) {
  94. let data = $(this).data(DATA_KEY)
  95. const _options = $.extend({}, Default, $(this).data())
  96. if (!data) {
  97. data = new CardRefresh($(this), _options)
  98. $(this).data(DATA_KEY, typeof config === 'string' ? data: config)
  99. }
  100. if (typeof config === 'string' && config.match(/load/)) {
  101. data[config]()
  102. } else {
  103. data._init($(this))
  104. }
  105. }
  106. }
  107. /**
  108. * Data API
  109. * ====================================================
  110. */
  111. $(document).on('click', Selector.DATA_REFRESH, function (event) {
  112. if (event) {
  113. event.preventDefault()
  114. }
  115. CardRefresh._jQueryInterface.call($(this), 'load')
  116. })
  117. $(document).ready(function () {
  118. $(Selector.DATA_REFRESH).each(function() {
  119. CardRefresh._jQueryInterface.call($(this))
  120. })
  121. })
  122. /**
  123. * jQuery API
  124. * ====================================================
  125. */
  126. $.fn[NAME] = CardRefresh._jQueryInterface
  127. $.fn[NAME].Constructor = CardRefresh
  128. $.fn[NAME].noConflict = function () {
  129. $.fn[NAME] = JQUERY_NO_CONFLICT
  130. return CardRefresh._jQueryInterface
  131. }
  132. return CardRefresh
  133. })(jQuery)
  134. export default CardRefresh