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.

PushMenu.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE PushMenu.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'PushMenu'
  13. const DATA_KEY = 'lte.pushmenu'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  17. const EVENT_SHOWN = `shown${EVENT_KEY}`
  18. const SELECTOR_TOGGLE_BUTTON = '[data-widget="pushmenu"]'
  19. const SELECTOR_BODY = 'body'
  20. const SELECTOR_OVERLAY = '#sidebar-overlay'
  21. const SELECTOR_WRAPPER = '.wrapper'
  22. const CLASS_NAME_COLLAPSED = 'sidebar-collapse'
  23. const CLASS_NAME_OPEN = 'sidebar-open'
  24. const CLASS_NAME_IS_OPENING = 'sidebar-is-opening'
  25. const CLASS_NAME_CLOSED = 'sidebar-closed'
  26. const Default = {
  27. autoCollapseSize: 992,
  28. enableRemember: false,
  29. noTransitionAfterReload: true
  30. }
  31. /**
  32. * Class Definition
  33. * ====================================================
  34. */
  35. class PushMenu {
  36. constructor(element, options) {
  37. this._element = element
  38. this._options = $.extend({}, Default, options)
  39. if ($(SELECTOR_OVERLAY).length === 0) {
  40. this._addOverlay()
  41. }
  42. this._init()
  43. }
  44. // Public
  45. static _jQueryInterface(operation) {
  46. return this.each(function () {
  47. let data = $(this).data(DATA_KEY)
  48. const _options = $.extend({}, Default, $(this).data())
  49. if (!data) {
  50. data = new PushMenu(this, _options)
  51. $(this).data(DATA_KEY, data)
  52. }
  53. if (typeof operation === 'string' && /collapse|expand|toggle/.test(operation)) {
  54. data[operation]()
  55. }
  56. })
  57. }
  58. expand() {
  59. const $bodySelector = $(SELECTOR_BODY)
  60. if (this._options.autoCollapseSize && $(window).width() <= this._options.autoCollapseSize) {
  61. $bodySelector.addClass(CLASS_NAME_OPEN)
  62. }
  63. $bodySelector.addClass(CLASS_NAME_IS_OPENING).removeClass(`${CLASS_NAME_COLLAPSED} ${CLASS_NAME_CLOSED}`).delay(50).queue(function () {
  64. $bodySelector.removeClass(CLASS_NAME_IS_OPENING)
  65. $(this).dequeue()
  66. })
  67. if (this._options.enableRemember) {
  68. localStorage.setItem(`remember${EVENT_KEY}`, CLASS_NAME_OPEN)
  69. }
  70. $(this._element).trigger($.Event(EVENT_SHOWN))
  71. }
  72. collapse() {
  73. const $bodySelector = $(SELECTOR_BODY)
  74. if (this._options.autoCollapseSize && $(window).width() <= this._options.autoCollapseSize) {
  75. $bodySelector.removeClass(CLASS_NAME_OPEN).addClass(CLASS_NAME_CLOSED)
  76. }
  77. $bodySelector.addClass(CLASS_NAME_COLLAPSED)
  78. if (this._options.enableRemember) {
  79. localStorage.setItem(`remember${EVENT_KEY}`, CLASS_NAME_COLLAPSED)
  80. }
  81. $(this._element).trigger($.Event(EVENT_COLLAPSED))
  82. }
  83. toggle() {
  84. if ($(SELECTOR_BODY).hasClass(CLASS_NAME_COLLAPSED)) {
  85. this.expand()
  86. } else {
  87. this.collapse()
  88. }
  89. }
  90. autoCollapse(resize = false) {
  91. if (!this._options.autoCollapseSize) {
  92. return
  93. }
  94. const $bodySelector = $(SELECTOR_BODY)
  95. if ($(window).width() <= this._options.autoCollapseSize) {
  96. if (!$bodySelector.hasClass(CLASS_NAME_OPEN)) {
  97. this.collapse()
  98. }
  99. } else if (resize === true) {
  100. if ($bodySelector.hasClass(CLASS_NAME_OPEN)) {
  101. $bodySelector.removeClass(CLASS_NAME_OPEN)
  102. } else if ($bodySelector.hasClass(CLASS_NAME_CLOSED)) {
  103. this.expand()
  104. }
  105. }
  106. }
  107. // Private
  108. remember() {
  109. if (!this._options.enableRemember) {
  110. return
  111. }
  112. const $body = $('body')
  113. const toggleState = localStorage.getItem(`remember${EVENT_KEY}`)
  114. if (toggleState === CLASS_NAME_COLLAPSED) {
  115. if (this._options.noTransitionAfterReload) {
  116. $body.addClass('hold-transition').addClass(CLASS_NAME_COLLAPSED).delay(50).queue(function () {
  117. $(this).removeClass('hold-transition')
  118. $(this).dequeue()
  119. })
  120. } else {
  121. $body.addClass(CLASS_NAME_COLLAPSED)
  122. }
  123. } else if (this._options.noTransitionAfterReload) {
  124. $body.addClass('hold-transition').removeClass(CLASS_NAME_COLLAPSED).delay(50).queue(function () {
  125. $(this).removeClass('hold-transition')
  126. $(this).dequeue()
  127. })
  128. } else {
  129. $body.removeClass(CLASS_NAME_COLLAPSED)
  130. }
  131. }
  132. _init() {
  133. this.remember()
  134. this.autoCollapse()
  135. $(window).resize(() => {
  136. this.autoCollapse(true)
  137. })
  138. }
  139. // Static
  140. _addOverlay() {
  141. const overlay = $('<div />', {
  142. id: 'sidebar-overlay'
  143. })
  144. overlay.on('click', () => {
  145. this.collapse()
  146. })
  147. $(SELECTOR_WRAPPER).append(overlay)
  148. }
  149. }
  150. /**
  151. * Data API
  152. * ====================================================
  153. */
  154. $(document).on('click', SELECTOR_TOGGLE_BUTTON, event => {
  155. event.preventDefault()
  156. let button = event.currentTarget
  157. if ($(button).data('widget') !== 'pushmenu') {
  158. button = $(button).closest(SELECTOR_TOGGLE_BUTTON)
  159. }
  160. PushMenu._jQueryInterface.call($(button), 'toggle')
  161. })
  162. $(window).on('load', () => {
  163. PushMenu._jQueryInterface.call($(SELECTOR_TOGGLE_BUTTON))
  164. })
  165. /**
  166. * jQuery API
  167. * ====================================================
  168. */
  169. $.fn[NAME] = PushMenu._jQueryInterface
  170. $.fn[NAME].Constructor = PushMenu
  171. $.fn[NAME].noConflict = function () {
  172. $.fn[NAME] = JQUERY_NO_CONFLICT
  173. return PushMenu._jQueryInterface
  174. }
  175. export default PushMenu