ControlSidebar.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE ControlSidebar.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. const ControlSidebar = (($) => {
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'ControlSidebar'
  13. const DATA_KEY = 'lte.control.sidebar'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const DATA_API_KEY = '.data-api'
  17. const Event = {
  18. CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`
  19. }
  20. const Selector = {
  21. CONTROL_SIDEBAR: '.control-sidebar',
  22. DATA_TOGGLE : '[data-widget="control-sidebar"]',
  23. MAIN_HEADER : '.main-header'
  24. }
  25. const ClassName = {
  26. CONTROL_SIDEBAR_ANIMATE: 'control-sidebar-animate',
  27. CONTROL_SIDEBAR_OPEN : 'control-sidebar-open',
  28. CONTROL_SIDEBAR_SLIDE : 'control-sidebar-slide-open'
  29. }
  30. const Default = {
  31. slide: true
  32. }
  33. /**
  34. * Class Definition
  35. * ====================================================
  36. */
  37. class ControlSidebar {
  38. constructor(element, config) {
  39. this._element = element
  40. this._config = this._getConfig(config)
  41. }
  42. // Public
  43. show() {
  44. // Show the control sidebar
  45. if (this._config.slide) {
  46. $('html').addClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
  47. $('body').removeClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function(){
  48. $(Selector.CONTROL_SIDEBAR).hide()
  49. $('html').removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
  50. $(this).dequeue()
  51. })
  52. } else {
  53. $('body').removeClass(ClassName.CONTROL_SIDEBAR_OPEN)
  54. }
  55. }
  56. collapse() {
  57. // Collapse the control sidebar
  58. if (this._config.slide) {
  59. $('html').addClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
  60. $(Selector.CONTROL_SIDEBAR).show().delay(100).queue(function(){
  61. $('body').addClass(ClassName.CONTROL_SIDEBAR_SLIDE).delay(300).queue(function(){
  62. $('html').removeClass(ClassName.CONTROL_SIDEBAR_ANIMATE)
  63. $(this).dequeue()
  64. })
  65. $(this).dequeue()
  66. })
  67. } else {
  68. $('body').addClass(ClassName.CONTROL_SIDEBAR_OPEN)
  69. }
  70. }
  71. toggle() {
  72. this._setMargin()
  73. const shouldOpen = $('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || $('body')
  74. .hasClass(ClassName.CONTROL_SIDEBAR_SLIDE)
  75. if (shouldOpen) {
  76. // Open the control sidebar
  77. this.show()
  78. } else {
  79. // Close the control sidebar
  80. this.collapse()
  81. }
  82. }
  83. // Private
  84. _getConfig(config) {
  85. return $.extend({}, Default, config)
  86. }
  87. _setMargin() {
  88. $(Selector.CONTROL_SIDEBAR).css({
  89. top: $(Selector.MAIN_HEADER).innerHeight()
  90. })
  91. }
  92. // Static
  93. static _jQueryInterface(operation) {
  94. return this.each(function () {
  95. let data = $(this).data(DATA_KEY)
  96. if (!data) {
  97. data = new ControlSidebar(this, $(this).data())
  98. $(this).data(DATA_KEY, data)
  99. }
  100. if (data[operation] === 'undefined') {
  101. throw new Error(`${operation} is not a function`)
  102. }
  103. data[operation]()
  104. })
  105. }
  106. }
  107. /**
  108. *
  109. * Data Api implementation
  110. * ====================================================
  111. */
  112. $(document).on('click', Selector.DATA_TOGGLE, function (event) {
  113. event.preventDefault()
  114. ControlSidebar._jQueryInterface.call($(this), 'toggle')
  115. })
  116. /**
  117. * jQuery API
  118. * ====================================================
  119. */
  120. $.fn[NAME] = ControlSidebar._jQueryInterface
  121. $.fn[NAME].Constructor = ControlSidebar
  122. $.fn[NAME].noConflict = function () {
  123. $.fn[NAME] = JQUERY_NO_CONFLICT
  124. return ControlSidebar._jQueryInterface
  125. }
  126. return ControlSidebar
  127. })(jQuery)
  128. export default ControlSidebar