You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CardRefresh.js 3.9KB

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. static _jQueryInterface(config) {
  52. let data = $(this).data(DATA_KEY)
  53. const _options = $.extend({}, Default, $(this).data())
  54. if (!data) {
  55. data = new CardRefresh($(this), _options)
  56. $(this).data(DATA_KEY, typeof config === 'string' ? data : config)
  57. }
  58. if (typeof config === 'string' && /load/.test(config)) {
  59. data[config]()
  60. } else {
  61. data._init($(this))
  62. }
  63. }
  64. load() {
  65. this._addOverlay()
  66. this._settings.onLoadStart.call($(this))
  67. $.get(this._settings.source, this._settings.params, response => {
  68. if (this._settings.loadInContent) {
  69. if (this._settings.sourceSelector !== '') {
  70. response = $(response).find(this._settings.sourceSelector).html()
  71. }
  72. this._parent.find(this._settings.content).html(response)
  73. }
  74. this._settings.onLoadDone.call($(this), response)
  75. this._removeOverlay()
  76. }, this._settings.responseType !== '' && this._settings.responseType)
  77. $(this._element).trigger($.Event(EVENT_LOADED))
  78. }
  79. _addOverlay() {
  80. this._parent.append(this._overlay)
  81. $(this._element).trigger($.Event(EVENT_OVERLAY_ADDED))
  82. }
  83. // Private
  84. _removeOverlay() {
  85. this._parent.find(this._overlay).remove()
  86. $(this._element).trigger($.Event(EVENT_OVERLAY_REMOVED))
  87. }
  88. // Static
  89. _init() {
  90. $(this).find(this._settings.trigger).on('click', () => {
  91. this.load()
  92. })
  93. if (this._settings.loadOnInit) {
  94. this.load()
  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