CardRefresh.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. this._init();
  57. if (this._settings.loadOnInit) {
  58. this.load();
  59. }
  60. }
  61. load() {
  62. this._addOverlay()
  63. this._settings.onLoadStart.call($(this))
  64. $.get(this._settings.source, this._settings.params, function (response) {
  65. if (this._settings.loadInContent) {
  66. if (this._settings.sourceSelector != '') {
  67. response = $(response).find(this._settings.sourceSelector).html()
  68. }
  69. this._parent.find(this._settings.content).html(response)
  70. }
  71. this._settings.onLoadDone.call($(this), response)
  72. this._removeOverlay();
  73. }.bind(this), this._settings.responseType !== '' && this._settings.responseType)
  74. const loadedEvent = $.Event(Event.LOADED)
  75. $(this._element).trigger(loadedEvent)
  76. }
  77. _addOverlay() {
  78. this._parent.append(this._overlay)
  79. const overlayAddedEvent = $.Event(Event.OVERLAY_ADDED)
  80. $(this._element).trigger(overlayAddedEvent)
  81. };
  82. _removeOverlay() {
  83. this._parent.find(this._overlay).remove()
  84. const overlayRemovedEvent = $.Event(Event.OVERLAY_REMOVED)
  85. $(this._element).trigger(overlayRemovedEvent)
  86. };
  87. // Private
  88. _init(card) {
  89. $(this).find(this._settings.trigger).on('click', () => {
  90. this.load()
  91. })
  92. }
  93. // Static
  94. static _jQueryInterface(config) {
  95. let data = $(this).data(DATA_KEY)
  96. let options = $(this).data()
  97. if (!data) {
  98. data = new CardRefresh($(this), options)
  99. $(this).data(DATA_KEY, typeof config === 'string' ? data: config)
  100. }
  101. if (typeof config === 'string' && config.match(/load/)) {
  102. data[config]()
  103. } else if (typeof config === 'object') {
  104. data._init($(this))
  105. }
  106. }
  107. }
  108. /**
  109. * Data API
  110. * ====================================================
  111. */
  112. $(document).on('click', Selector.DATA_REFRESH, function (event) {
  113. if (event) {
  114. event.preventDefault()
  115. }
  116. CardRefresh._jQueryInterface.call($(this), 'load')
  117. })
  118. /**
  119. * jQuery API
  120. * ====================================================
  121. */
  122. $.fn[NAME] = CardRefresh._jQueryInterface
  123. $.fn[NAME].Constructor = CardRefresh
  124. $.fn[NAME].noConflict = function () {
  125. $.fn[NAME] = JQUERY_NO_CONFLICT
  126. return CardRefresh._jQueryInterface
  127. }
  128. return CardRefresh
  129. })(jQuery)
  130. export default CardRefresh