CardWidget.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE CardWidget.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'CardWidget'
  13. const DATA_KEY = 'lte.cardwidget'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const EVENT_EXPANDED = `expanded${EVENT_KEY}`
  17. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  18. const EVENT_MAXIMIZED = `maximized${EVENT_KEY}`
  19. const EVENT_MINIMIZED = `minimized${EVENT_KEY}`
  20. const EVENT_REMOVED = `removed${EVENT_KEY}`
  21. const CLASS_NAME_CARD = 'card'
  22. const CLASS_NAME_COLLAPSED = 'collapsed-card'
  23. const CLASS_NAME_COLLAPSING = 'collapsing-card'
  24. const CLASS_NAME_EXPANDING = 'expanding-card'
  25. const CLASS_NAME_WAS_COLLAPSED = 'was-collapsed'
  26. const CLASS_NAME_MAXIMIZED = 'maximized-card'
  27. const SELECTOR_DATA_REMOVE = '[data-card-widget="remove"]'
  28. const SELECTOR_DATA_COLLAPSE = '[data-card-widget="collapse"]'
  29. const SELECTOR_DATA_MAXIMIZE = '[data-card-widget="maximize"]'
  30. const SELECTOR_CARD = `.${CLASS_NAME_CARD}`
  31. const SELECTOR_CARD_HEADER = '.card-header'
  32. const SELECTOR_CARD_BODY = '.card-body'
  33. const SELECTOR_CARD_FOOTER = '.card-footer'
  34. const Default = {
  35. animationSpeed: 'normal',
  36. collapseTrigger: SELECTOR_DATA_COLLAPSE,
  37. removeTrigger: SELECTOR_DATA_REMOVE,
  38. maximizeTrigger: SELECTOR_DATA_MAXIMIZE,
  39. collapseIcon: 'fa-minus',
  40. expandIcon: 'fa-plus',
  41. maximizeIcon: 'fa-expand',
  42. minimizeIcon: 'fa-compress'
  43. }
  44. class CardWidget {
  45. constructor(element, settings) {
  46. this._element = element
  47. this._parent = element.parents(SELECTOR_CARD).first()
  48. if (element.hasClass(CLASS_NAME_CARD)) {
  49. this._parent = element
  50. }
  51. this._settings = $.extend({}, Default, settings)
  52. }
  53. collapse() {
  54. this._parent.addClass(CLASS_NAME_COLLAPSING).children(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
  55. .slideUp(this._settings.animationSpeed, () => {
  56. this._parent.addClass(CLASS_NAME_COLLAPSED).removeClass(CLASS_NAME_COLLAPSING)
  57. })
  58. this._parent.find(`> ${SELECTOR_CARD_HEADER} ${this._settings.collapseTrigger} .${this._settings.collapseIcon}`)
  59. .addClass(this._settings.expandIcon)
  60. .removeClass(this._settings.collapseIcon)
  61. this._element.trigger($.Event(EVENT_COLLAPSED), this._parent)
  62. }
  63. expand() {
  64. this._parent.addClass(CLASS_NAME_EXPANDING).children(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
  65. .slideDown(this._settings.animationSpeed, () => {
  66. this._parent.removeClass(CLASS_NAME_COLLAPSED).removeClass(CLASS_NAME_EXPANDING)
  67. })
  68. this._parent.find(`> ${SELECTOR_CARD_HEADER} ${this._settings.collapseTrigger} .${this._settings.expandIcon}`)
  69. .addClass(this._settings.collapseIcon)
  70. .removeClass(this._settings.expandIcon)
  71. this._element.trigger($.Event(EVENT_EXPANDED), this._parent)
  72. }
  73. remove() {
  74. this._parent.slideUp()
  75. this._element.trigger($.Event(EVENT_REMOVED), this._parent)
  76. }
  77. toggle() {
  78. if (this._parent.hasClass(CLASS_NAME_COLLAPSED)) {
  79. this.expand()
  80. return
  81. }
  82. this.collapse()
  83. }
  84. maximize() {
  85. this._parent.find(`${this._settings.maximizeTrigger} .${this._settings.maximizeIcon}`)
  86. .addClass(this._settings.minimizeIcon)
  87. .removeClass(this._settings.maximizeIcon)
  88. this._parent.css({
  89. height: this._parent.height(),
  90. width: this._parent.width(),
  91. position: 'fixed',
  92. transition: 'all .15s'
  93. }).delay(150).queue(function () {
  94. const $element = $(this)
  95. $element.addClass(CLASS_NAME_MAXIMIZED)
  96. $('html').addClass(CLASS_NAME_MAXIMIZED)
  97. if ($element.hasClass(CLASS_NAME_COLLAPSED)) {
  98. $element.addClass(CLASS_NAME_WAS_COLLAPSED)
  99. }
  100. $element.dequeue()
  101. })
  102. this._element.trigger($.Event(EVENT_MAXIMIZED), this._parent)
  103. }
  104. minimize() {
  105. this._parent.find(`${this._settings.maximizeTrigger} .${this._settings.minimizeIcon}`)
  106. .addClass(this._settings.maximizeIcon)
  107. .removeClass(this._settings.minimizeIcon)
  108. this._parent.css('cssText', `height: ${this._parent[0].style.height} !important; width: ${this._parent[0].style.width} !important; transition: all .15s;`
  109. ).delay(10).queue(function () {
  110. const $element = $(this)
  111. $element.removeClass(CLASS_NAME_MAXIMIZED)
  112. $('html').removeClass(CLASS_NAME_MAXIMIZED)
  113. $element.css({
  114. height: 'inherit',
  115. width: 'inherit'
  116. })
  117. if ($element.hasClass(CLASS_NAME_WAS_COLLAPSED)) {
  118. $element.removeClass(CLASS_NAME_WAS_COLLAPSED)
  119. }
  120. $element.dequeue()
  121. })
  122. this._element.trigger($.Event(EVENT_MINIMIZED), this._parent)
  123. }
  124. toggleMaximize() {
  125. if (this._parent.hasClass(CLASS_NAME_MAXIMIZED)) {
  126. this.minimize()
  127. return
  128. }
  129. this.maximize()
  130. }
  131. // Private
  132. _init(card) {
  133. this._parent = card
  134. $(this).find(this._settings.collapseTrigger).click(() => {
  135. this.toggle()
  136. })
  137. $(this).find(this._settings.maximizeTrigger).click(() => {
  138. this.toggleMaximize()
  139. })
  140. $(this).find(this._settings.removeTrigger).click(() => {
  141. this.remove()
  142. })
  143. }
  144. // Static
  145. static _jQueryInterface(config) {
  146. let data = $(this).data(DATA_KEY)
  147. const _config = $.extend({}, Default, $(this).data())
  148. if (!data) {
  149. data = new CardWidget($(this), _config)
  150. $(this).data(DATA_KEY, typeof config === 'string' ? data : config)
  151. }
  152. if (typeof config === 'string' && /collapse|expand|remove|toggle|maximize|minimize|toggleMaximize/.test(config)) {
  153. data[config]()
  154. } else if (typeof config === 'object') {
  155. data._init($(this))
  156. }
  157. }
  158. }
  159. /**
  160. * Data API
  161. * ====================================================
  162. */
  163. $(document).on('click', SELECTOR_DATA_COLLAPSE, function (event) {
  164. if (event) {
  165. event.preventDefault()
  166. }
  167. CardWidget._jQueryInterface.call($(this), 'toggle')
  168. })
  169. $(document).on('click', SELECTOR_DATA_REMOVE, function (event) {
  170. if (event) {
  171. event.preventDefault()
  172. }
  173. CardWidget._jQueryInterface.call($(this), 'remove')
  174. })
  175. $(document).on('click', SELECTOR_DATA_MAXIMIZE, function (event) {
  176. if (event) {
  177. event.preventDefault()
  178. }
  179. CardWidget._jQueryInterface.call($(this), 'toggleMaximize')
  180. })
  181. /**
  182. * jQuery API
  183. * ====================================================
  184. */
  185. $.fn[NAME] = CardWidget._jQueryInterface
  186. $.fn[NAME].Constructor = CardWidget
  187. $.fn[NAME].noConflict = function () {
  188. $.fn[NAME] = JQUERY_NO_CONFLICT
  189. return CardWidget._jQueryInterface
  190. }
  191. export default CardWidget