CardWidget.js 6.2 KB

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