CardRefresh.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. responseType: '',
  31. overlayTemplate: '<div class="overlay"><i class="fas fa-2x fa-sync-alt fa-spin"></i></div>',
  32. onLoadStart() {},
  33. onLoadDone(response) {
  34. return response
  35. }
  36. }
  37. class CardRefresh {
  38. constructor(element, settings) {
  39. this._element = element
  40. this._parent = element.parents(SELECTOR_CARD).first()
  41. this._settings = $.extend({}, Default, settings)
  42. this._overlay = $(this._settings.overlayTemplate)
  43. if (element.hasClass(CLASS_NAME_CARD)) {
  44. this._parent = element
  45. }
  46. if (this._settings.source === '') {
  47. throw new Error('Source url was not defined. Please specify a url in your CardRefresh source option.')
  48. }
  49. }
  50. load() {
  51. this._addOverlay()
  52. this._settings.onLoadStart.call($(this))
  53. $.get(this._settings.source, this._settings.params, response => {
  54. if (this._settings.loadInContent) {
  55. if (this._settings.sourceSelector !== '') {
  56. response = $(response).find(this._settings.sourceSelector).html()
  57. }
  58. this._parent.find(this._settings.content).html(response)
  59. }
  60. this._settings.onLoadDone.call($(this), response)
  61. this._removeOverlay()
  62. }, this._settings.responseType !== '' && this._settings.responseType)
  63. $(this._element).trigger($.Event(EVENT_LOADED))
  64. }
  65. _addOverlay() {
  66. this._parent.append(this._overlay)
  67. $(this._element).trigger($.Event(EVENT_OVERLAY_ADDED))
  68. }
  69. _removeOverlay() {
  70. this._parent.find(this._overlay).remove()
  71. $(this._element).trigger($.Event(EVENT_OVERLAY_REMOVED))
  72. }
  73. // Private
  74. _init() {
  75. $(this).find(this._settings.trigger).on('click', () => {
  76. this.load()
  77. })
  78. if (this._settings.loadOnInit) {
  79. this.load()
  80. }
  81. }
  82. // Static
  83. static _jQueryInterface(config) {
  84. let data = $(this).data(DATA_KEY)
  85. const _options = $.extend({}, Default, $(this).data())
  86. if (!data) {
  87. data = new CardRefresh($(this), _options)
  88. $(this).data(DATA_KEY, typeof config === 'string' ? data : config)
  89. }
  90. if (typeof config === 'string' && config.match(/load/)) {
  91. data[config]()
  92. } else {
  93. data._init($(this))
  94. }
  95. }
  96. }
  97. /**
  98. * Data API
  99. * ====================================================
  100. */
  101. $(document).on('click', SELECTOR_DATA_REFRESH, function (event) {
  102. if (event) {
  103. event.preventDefault()
  104. }
  105. CardRefresh._jQueryInterface.call($(this), 'load')
  106. })
  107. $(() => {
  108. $(SELECTOR_DATA_REFRESH).each(function () {
  109. CardRefresh._jQueryInterface.call($(this))
  110. })
  111. })
  112. /**
  113. * jQuery API
  114. * ====================================================
  115. */
  116. $.fn[NAME] = CardRefresh._jQueryInterface
  117. $.fn[NAME].Constructor = CardRefresh
  118. $.fn[NAME].noConflict = function () {
  119. $.fn[NAME] = JQUERY_NO_CONFLICT
  120. return CardRefresh._jQueryInterface
  121. }
  122. export default CardRefresh