CardRefresh.js 3.6 KB

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