ControlSidebar.js 8.3 KB

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