ControlSidebar.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_OPEN : 'control-sidebar-open',
  27. CONTROL_SIDEBAR_SLIDE: 'control-sidebar-slide-open'
  28. }
  29. const Default = {
  30. slide: true
  31. }
  32. /**
  33. * Class Definition
  34. * ====================================================
  35. */
  36. class ControlSidebar {
  37. constructor(element, config) {
  38. this._element = element
  39. this._config = this._getConfig(config)
  40. }
  41. // Public
  42. show() {
  43. // Show the control sidebar
  44. if (this._config.slide) {
  45. $('body').removeClass(ClassName.CONTROL_SIDEBAR_SLIDE)
  46. } else {
  47. $('body').removeClass(ClassName.CONTROL_SIDEBAR_OPEN)
  48. }
  49. }
  50. collapse() {
  51. // Collapse the control sidebar
  52. if (this._config.slide) {
  53. $('body').addClass(ClassName.CONTROL_SIDEBAR_SLIDE)
  54. } else {
  55. $('body').addClass(ClassName.CONTROL_SIDEBAR_OPEN)
  56. }
  57. }
  58. toggle() {
  59. this._setMargin()
  60. const shouldOpen = $('body').hasClass(ClassName.CONTROL_SIDEBAR_OPEN) || $('body')
  61. .hasClass(ClassName.CONTROL_SIDEBAR_SLIDE)
  62. if (shouldOpen) {
  63. // Open the control sidebar
  64. this.show()
  65. } else {
  66. // Close the control sidebar
  67. this.collapse()
  68. }
  69. }
  70. // Private
  71. _getConfig(config) {
  72. return $.extend({}, Default, config)
  73. }
  74. _setMargin() {
  75. $(Selector.CONTROL_SIDEBAR).css({
  76. top: $(Selector.MAIN_HEADER).innerHeight()
  77. })
  78. }
  79. // Static
  80. static _jQueryInterface(operation) {
  81. return this.each(function () {
  82. let data = $(this).data(DATA_KEY)
  83. if (!data) {
  84. data = new ControlSidebar(this, $(this).data())
  85. $(this).data(DATA_KEY, data)
  86. }
  87. if (data[operation] === 'undefined') {
  88. throw new Error(`${operation} is not a function`)
  89. }
  90. data[operation]()
  91. })
  92. }
  93. }
  94. /**
  95. *
  96. * Data Api implementation
  97. * ====================================================
  98. */
  99. $(document).on('click', Selector.DATA_TOGGLE, function (event) {
  100. event.preventDefault()
  101. ControlSidebar._jQueryInterface.call($(this), 'toggle')
  102. })
  103. /**
  104. * jQuery API
  105. * ====================================================
  106. */
  107. $.fn[NAME] = ControlSidebar._jQueryInterface
  108. $.fn[NAME].Constructor = ControlSidebar
  109. $.fn[NAME].noConflict = function () {
  110. $.fn[NAME] = JQUERY_NO_CONFLICT
  111. return ControlSidebar._jQueryInterface
  112. }
  113. return ControlSidebar
  114. })(jQuery)
  115. export default ControlSidebar