CardRefresh.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE CardRefresh.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  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_LOADED = `loaded${EVENT_KEY}`
  17. const EVENT_OVERLAY_ADDED = `overlay.added${EVENT_KEY}`
  18. const EVENT_OVERLAY_REMOVED = `overlay.removed${EVENT_KEY}`
  19. const CLASS_NAME_CARD = 'card'
  20. const SELECTOR_CARD = `.${CLASS_NAME_CARD}`
  21. const SELECTOR_DATA_REFRESH = '[data-card-widget="card-refresh"]'
  22. const Default = {
  23. source: '',
  24. sourceSelector: '',
  25. params: {},
  26. trigger: SELECTOR_DATA_REFRESH,
  27. content: '.card-body',
  28. loadInContent: true,
  29. loadOnInit: true,
  30. loadErrorTemplate: true,
  31. responseType: '',
  32. overlayTemplate: '<div class="overlay"><i class="fas fa-2x fa-sync-alt fa-spin"></i></div>',
  33. errorTemplate: '<span class="text-danger"></span>',
  34. onLoadStart() {},
  35. onLoadDone(response) {
  36. return response
  37. },
  38. onLoadFail(_jqXHR, _textStatus, _errorThrown) {}
  39. }
  40. class CardRefresh {
  41. constructor(element, settings) {
  42. this._element = element
  43. this._parent = element.parents(SELECTOR_CARD).first()
  44. this._settings = $.extend({}, Default, settings)
  45. this._overlay = $(this._settings.overlayTemplate)
  46. if (element.hasClass(CLASS_NAME_CARD)) {
  47. this._parent = element
  48. }
  49. if (this._settings.source === '') {
  50. throw new Error('Source url was not defined. Please specify a url in your CardRefresh source option.')
  51. }
  52. }
  53. load() {
  54. this._addOverlay()
  55. this._settings.onLoadStart.call($(this))
  56. $.get(this._settings.source, this._settings.params, response => {
  57. if (this._settings.loadInContent) {
  58. if (this._settings.sourceSelector !== '') {
  59. response = $(response).find(this._settings.sourceSelector).html()
  60. }
  61. this._parent.find(this._settings.content).html(response)
  62. }
  63. this._settings.onLoadDone.call($(this), response)
  64. this._removeOverlay()
  65. }, this._settings.responseType !== '' && this._settings.responseType)
  66. .fail((jqXHR, textStatus, errorThrown) => {
  67. this._removeOverlay()
  68. if (this._settings.loadErrorTemplate) {
  69. const msg = $(this._settings.errorTemplate).text(errorThrown)
  70. this._parent.find(this._settings.content).empty().append(msg)
  71. }
  72. this._settings.onLoadFail.call($(this), jqXHR, textStatus, errorThrown)
  73. })
  74. $(this._element).trigger($.Event(EVENT_LOADED))
  75. }
  76. _addOverlay() {
  77. this._parent.append(this._overlay)
  78. $(this._element).trigger($.Event(EVENT_OVERLAY_ADDED))
  79. }
  80. _removeOverlay() {
  81. this._parent.find(this._overlay).remove()
  82. $(this._element).trigger($.Event(EVENT_OVERLAY_REMOVED))
  83. }
  84. // Private
  85. _init() {
  86. $(this).find(this._settings.trigger).on('click', () => {
  87. this.load()
  88. })
  89. if (this._settings.loadOnInit) {
  90. this.load()
  91. }
  92. }
  93. // Static
  94. static _jQueryInterface(config) {
  95. return this.each(function () {
  96. let data = $(this).data(DATA_KEY)
  97. const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
  98. if (!data) {
  99. data = new CardRefresh($(this), _config)
  100. $(this).data(DATA_KEY, data)
  101. data._init()
  102. } else if (typeof config === 'string') {
  103. if (typeof data[config] === 'undefined') {
  104. throw new TypeError(`No method named "${config}"`)
  105. }
  106. data[config]()
  107. } else if (typeof config === 'undefined') {
  108. data._init()
  109. }
  110. })
  111. }
  112. }
  113. /**
  114. * Data API
  115. * ====================================================
  116. */
  117. $(document).on('click', SELECTOR_DATA_REFRESH, function (event) {
  118. if (event) {
  119. event.preventDefault()
  120. }
  121. CardRefresh._jQueryInterface.call($(this), 'load')
  122. })
  123. $(() => {
  124. $(SELECTOR_DATA_REFRESH).each(function () {
  125. CardRefresh._jQueryInterface.call($(this))
  126. })
  127. })
  128. /**
  129. * jQuery API
  130. * ====================================================
  131. */
  132. $.fn[NAME] = CardRefresh._jQueryInterface
  133. $.fn[NAME].Constructor = CardRefresh
  134. $.fn[NAME].noConflict = function () {
  135. $.fn[NAME] = JQUERY_NO_CONFLICT
  136. return CardRefresh._jQueryInterface
  137. }
  138. export default CardRefresh