Widget.js 5.9 KB

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