ControlSidebar.js 3.0 KB

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