CardRefresh.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = {
  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() {
  39. },
  40. onLoadDone(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, 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. }, this._settings.responseType !== '' && this._settings.responseType)
  70. $(this._element).trigger($.Event(Event.LOADED))
  71. }
  72. _addOverlay() {
  73. this._parent.append(this._overlay)
  74. $(this._element).trigger($.Event(Event.OVERLAY_ADDED))
  75. }
  76. _removeOverlay() {
  77. this._parent.find(this._overlay).remove()
  78. $(this._element).trigger($.Event(Event.OVERLAY_REMOVED))
  79. }
  80. // Private
  81. _init() {
  82. $(this).find(this._settings.trigger).on('click', () => {
  83. this.load()
  84. })
  85. if (this._settings.loadOnInit) {
  86. this.load()
  87. }
  88. }
  89. // Static
  90. static _jQueryInterface(config) {
  91. let data = $(this).data(DATA_KEY)
  92. const _options = $.extend({}, Default, $(this).data())
  93. if (!data) {
  94. data = new CardRefresh($(this), _options)
  95. $(this).data(DATA_KEY, typeof config === 'string' ? data : config)
  96. }
  97. if (typeof config === 'string' && config.match(/load/)) {
  98. data[config]()
  99. } else {
  100. data._init($(this))
  101. }
  102. }
  103. }
  104. /**
  105. * Data API
  106. * ====================================================
  107. */
  108. $(document).on('click', Selector.DATA_REFRESH, function (event) {
  109. if (event) {
  110. event.preventDefault()
  111. }
  112. CardRefresh._jQueryInterface.call($(this), 'load')
  113. })
  114. $(() => {
  115. $(Selector.DATA_REFRESH).each(function () {
  116. CardRefresh._jQueryInterface.call($(this))
  117. })
  118. })
  119. /**
  120. * jQuery API
  121. * ====================================================
  122. */
  123. $.fn[NAME] = CardRefresh._jQueryInterface
  124. $.fn[NAME].Constructor = CardRefresh
  125. $.fn[NAME].noConflict = function () {
  126. $.fn[NAME] = JQUERY_NO_CONFLICT
  127. return CardRefresh._jQueryInterface
  128. }
  129. export default CardRefresh