ControlSidebar.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE ControlSidebar.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'ControlSidebar'
  13. const DATA_KEY = 'lte.controlsidebar'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
  17. const EVENT_EXPANDED = `expanded${EVENT_KEY}`
  18. const SELECTOR_CONTROL_SIDEBAR = '.control-sidebar'
  19. const SELECTOR_CONTROL_SIDEBAR_CONTENT = '.control-sidebar-content'
  20. const SELECTOR_DATA_TOGGLE = '[data-widget="control-sidebar"]'
  21. const SELECTOR_HEADER = '.main-header'
  22. const SELECTOR_FOOTER = '.main-footer'
  23. const CLASS_NAME_CONTROL_SIDEBAR_ANIMATE = 'control-sidebar-animate'
  24. const CLASS_NAME_CONTROL_SIDEBAR_OPEN = 'control-sidebar-open'
  25. const CLASS_NAME_CONTROL_SIDEBAR_SLIDE = 'control-sidebar-slide-open'
  26. const CLASS_NAME_LAYOUT_FIXED = 'layout-fixed'
  27. const CLASS_NAME_NAVBAR_FIXED = 'layout-navbar-fixed'
  28. const CLASS_NAME_NAVBAR_SM_FIXED = 'layout-sm-navbar-fixed'
  29. const CLASS_NAME_NAVBAR_MD_FIXED = 'layout-md-navbar-fixed'
  30. const CLASS_NAME_NAVBAR_LG_FIXED = 'layout-lg-navbar-fixed'
  31. const CLASS_NAME_NAVBAR_XL_FIXED = 'layout-xl-navbar-fixed'
  32. const CLASS_NAME_FOOTER_FIXED = 'layout-footer-fixed'
  33. const CLASS_NAME_FOOTER_SM_FIXED = 'layout-sm-footer-fixed'
  34. const CLASS_NAME_FOOTER_MD_FIXED = 'layout-md-footer-fixed'
  35. const CLASS_NAME_FOOTER_LG_FIXED = 'layout-lg-footer-fixed'
  36. const CLASS_NAME_FOOTER_XL_FIXED = 'layout-xl-footer-fixed'
  37. const Default = {
  38. controlsidebarSlide: true,
  39. scrollbarTheme: 'os-theme-light',
  40. scrollbarAutoHide: 'l',
  41. target: SELECTOR_CONTROL_SIDEBAR
  42. }
  43. /**
  44. * Class Definition
  45. * ====================================================
  46. */
  47. class ControlSidebar {
  48. constructor(element, config) {
  49. this._element = element
  50. this._config = config
  51. }
  52. // Public
  53. collapse() {
  54. const $body = $('body')
  55. const $html = $('html')
  56. const { target } = this._config
  57. // Show the control sidebar
  58. if (this._config.controlsidebarSlide) {
  59. $html.addClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
  60. $body.removeClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
  61. $(target).hide()
  62. $html.removeClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
  63. $(this).dequeue()
  64. })
  65. } else {
  66. $body.removeClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN)
  67. }
  68. $(this._element).trigger($.Event(EVENT_COLLAPSED))
  69. }
  70. show() {
  71. const $body = $('body')
  72. const $html = $('html')
  73. // Collapse the control sidebar
  74. if (this._config.controlsidebarSlide) {
  75. $html.addClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
  76. $(this._config.target).show().delay(10).queue(function () {
  77. $body.addClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE).delay(300).queue(function () {
  78. $html.removeClass(CLASS_NAME_CONTROL_SIDEBAR_ANIMATE)
  79. $(this).dequeue()
  80. })
  81. $(this).dequeue()
  82. })
  83. } else {
  84. $body.addClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN)
  85. }
  86. this._fixHeight()
  87. this._fixScrollHeight()
  88. $(this._element).trigger($.Event(EVENT_EXPANDED))
  89. }
  90. toggle() {
  91. const $body = $('body')
  92. const shouldClose = $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) ||
  93. $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE)
  94. if (shouldClose) {
  95. // Close the control sidebar
  96. this.collapse()
  97. } else {
  98. // Open the control sidebar
  99. this.show()
  100. }
  101. }
  102. // Private
  103. _init() {
  104. const $body = $('body')
  105. const shouldNotHideAll = $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) ||
  106. $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE)
  107. if (shouldNotHideAll) {
  108. $(SELECTOR_CONTROL_SIDEBAR).not(this._config.target).hide()
  109. $(this._config.target).css('display', 'block')
  110. } else {
  111. $(SELECTOR_CONTROL_SIDEBAR).hide()
  112. }
  113. this._fixHeight()
  114. this._fixScrollHeight()
  115. $(window).resize(() => {
  116. this._fixHeight()
  117. this._fixScrollHeight()
  118. })
  119. $(window).scroll(() => {
  120. const $body = $('body')
  121. const shouldFixHeight = $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_OPEN) ||
  122. $body.hasClass(CLASS_NAME_CONTROL_SIDEBAR_SLIDE)
  123. if (shouldFixHeight) {
  124. this._fixScrollHeight()
  125. }
  126. })
  127. }
  128. _isNavbarFixed() {
  129. const $body = $('body')
  130. return (
  131. $body.hasClass(CLASS_NAME_NAVBAR_FIXED) ||
  132. $body.hasClass(CLASS_NAME_NAVBAR_SM_FIXED) ||
  133. $body.hasClass(CLASS_NAME_NAVBAR_MD_FIXED) ||
  134. $body.hasClass(CLASS_NAME_NAVBAR_LG_FIXED) ||
  135. $body.hasClass(CLASS_NAME_NAVBAR_XL_FIXED)
  136. )
  137. }
  138. _isFooterFixed() {
  139. const $body = $('body')
  140. return (
  141. $body.hasClass(CLASS_NAME_FOOTER_FIXED) ||
  142. $body.hasClass(CLASS_NAME_FOOTER_SM_FIXED) ||
  143. $body.hasClass(CLASS_NAME_FOOTER_MD_FIXED) ||
  144. $body.hasClass(CLASS_NAME_FOOTER_LG_FIXED) ||
  145. $body.hasClass(CLASS_NAME_FOOTER_XL_FIXED)
  146. )
  147. }
  148. _fixScrollHeight() {
  149. const $body = $('body')
  150. const $controlSidebar = $(this._config.target)
  151. if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
  152. return
  153. }
  154. const heights = {
  155. scroll: $(document).height(),
  156. window: $(window).height(),
  157. header: $(SELECTOR_HEADER).outerHeight(),
  158. footer: $(SELECTOR_FOOTER).outerHeight()
  159. }
  160. const positions = {
  161. bottom: Math.abs((heights.window + $(window).scrollTop()) - heights.scroll),
  162. top: $(window).scrollTop()
  163. }
  164. const navbarFixed = this._isNavbarFixed() && $(SELECTOR_HEADER).css('position') === 'fixed'
  165. const footerFixed = this._isFooterFixed() && $(SELECTOR_FOOTER).css('position') === 'fixed'
  166. const $controlsidebarContent = $(`${this._config.target}, ${this._config.target} ${SELECTOR_CONTROL_SIDEBAR_CONTENT}`)
  167. if (positions.top === 0 && positions.bottom === 0) {
  168. $controlSidebar.css({
  169. bottom: heights.footer,
  170. top: heights.header
  171. })
  172. $controlsidebarContent.css('height', heights.window - (heights.header + heights.footer))
  173. } else if (positions.bottom <= heights.footer) {
  174. if (footerFixed === false) {
  175. const top = heights.header - positions.top
  176. $controlSidebar.css('bottom', heights.footer - positions.bottom).css('top', top >= 0 ? top : 0)
  177. $controlsidebarContent.css('height', heights.window - (heights.footer - positions.bottom))
  178. } else {
  179. $controlSidebar.css('bottom', heights.footer)
  180. }
  181. } else if (positions.top <= heights.header) {
  182. if (navbarFixed === false) {
  183. $controlSidebar.css('top', heights.header - positions.top)
  184. $controlsidebarContent.css('height', heights.window - (heights.header - positions.top))
  185. } else {
  186. $controlSidebar.css('top', heights.header)
  187. }
  188. } else if (navbarFixed === false) {
  189. $controlSidebar.css('top', 0)
  190. $controlsidebarContent.css('height', heights.window)
  191. } else {
  192. $controlSidebar.css('top', heights.header)
  193. }
  194. if (footerFixed && navbarFixed) {
  195. $controlsidebarContent.css('height', '100%')
  196. $controlSidebar.css('height', '')
  197. } else if (footerFixed || navbarFixed) {
  198. $controlsidebarContent.css('height', '100%')
  199. $controlsidebarContent.css('height', '')
  200. }
  201. }
  202. _fixHeight() {
  203. const $body = $('body')
  204. const $controlSidebar = $(`${this._config.target} ${SELECTOR_CONTROL_SIDEBAR_CONTENT}`)
  205. if (!$body.hasClass(CLASS_NAME_LAYOUT_FIXED)) {
  206. $controlSidebar.attr('style', '')
  207. return
  208. }
  209. const heights = {
  210. window: $(window).height(),
  211. header: $(SELECTOR_HEADER).outerHeight(),
  212. footer: $(SELECTOR_FOOTER).outerHeight()
  213. }
  214. let sidebarHeight = heights.window - heights.header
  215. if (this._isFooterFixed() && $(SELECTOR_FOOTER).css('position') === 'fixed') {
  216. sidebarHeight = heights.window - heights.header - heights.footer
  217. }
  218. $controlSidebar.css('height', sidebarHeight)
  219. if (typeof $.fn.overlayScrollbars !== 'undefined') {
  220. $controlSidebar.overlayScrollbars({
  221. className: this._config.scrollbarTheme,
  222. sizeAutoCapable: true,
  223. scrollbars: {
  224. autoHide: this._config.scrollbarAutoHide,
  225. clickScrolling: true
  226. }
  227. })
  228. }
  229. }
  230. // Static
  231. static _jQueryInterface(operation) {
  232. return this.each(function () {
  233. let data = $(this).data(DATA_KEY)
  234. const _options = $.extend({}, Default, $(this).data())
  235. if (!data) {
  236. data = new ControlSidebar(this, _options)
  237. $(this).data(DATA_KEY, data)
  238. }
  239. if (data[operation] === 'undefined') {
  240. throw new Error(`${operation} is not a function`)
  241. }
  242. data[operation]()
  243. })
  244. }
  245. }
  246. /**
  247. *
  248. * Data Api implementation
  249. * ====================================================
  250. */
  251. $(document).on('click', SELECTOR_DATA_TOGGLE, function (event) {
  252. event.preventDefault()
  253. ControlSidebar._jQueryInterface.call($(this), 'toggle')
  254. })
  255. $(document).ready(() => {
  256. ControlSidebar._jQueryInterface.call($(SELECTOR_DATA_TOGGLE), '_init')
  257. })
  258. /**
  259. * jQuery API
  260. * ====================================================
  261. */
  262. $.fn[NAME] = ControlSidebar._jQueryInterface
  263. $.fn[NAME].Constructor = ControlSidebar
  264. $.fn[NAME].noConflict = function () {
  265. $.fn[NAME] = JQUERY_NO_CONFLICT
  266. return ControlSidebar._jQueryInterface
  267. }
  268. export default ControlSidebar