treeview.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE treeview.ts
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import {
  8. domReady
  9. } from './util/index'
  10. /**
  11. * ------------------------------------------------------------------------
  12. * Constants
  13. * ------------------------------------------------------------------------
  14. */
  15. const CLASS_NAME_MENU_OPEN = 'menu-open'
  16. const CLASS_NAME_MENU_IS_OPEN = 'menu-is-open'
  17. const SELECTOR_NAV_ITEM = '.nav-item'
  18. const SELECTOR_DATA_TOGGLE = '[data-lte-toggle="treeview"]'
  19. const Defaults = {
  20. transitionDuration: 300,
  21. transitionTimingFuntion: 'linear'
  22. }
  23. /**
  24. * Class Definition
  25. * ====================================================
  26. */
  27. class Treeview {
  28. open(navItem: Element | null, childNavItem: HTMLElement | null | undefined): void {
  29. navItem?.classList.add(CLASS_NAME_MENU_OPEN)
  30. navItem?.classList.add(CLASS_NAME_MENU_IS_OPEN)
  31. const height: number = childNavItem?.scrollHeight ?? 0
  32. childNavItem?.style.setProperty('transition', `height ${Defaults.transitionDuration}ms ${Defaults.transitionTimingFuntion}`)
  33. childNavItem?.style.setProperty('overflow', 'hidden')
  34. childNavItem?.style.setProperty('height', '0px')
  35. childNavItem?.style.setProperty('display', 'block')
  36. setTimeout(() => {
  37. childNavItem?.style.setProperty('height', `${height}px`)
  38. }, 1)
  39. setTimeout(() => {
  40. childNavItem?.style.removeProperty('overflow')
  41. childNavItem?.style.removeProperty('height')
  42. }, Defaults.transitionDuration)
  43. }
  44. close(navItem: Element, childNavItem: HTMLElement | null | undefined): void {
  45. navItem.classList.remove(CLASS_NAME_MENU_IS_OPEN)
  46. navItem.classList.remove(CLASS_NAME_MENU_OPEN)
  47. const height: number = childNavItem?.scrollHeight ?? 0
  48. childNavItem?.style.setProperty('transition', `height ${Defaults.transitionDuration}ms ${Defaults.transitionTimingFuntion}`)
  49. childNavItem?.style.setProperty('overflow', 'hidden')
  50. childNavItem?.style.setProperty('height', `${height}px`)
  51. setTimeout(() => {
  52. childNavItem?.style.setProperty('height', '0px')
  53. }, 1)
  54. setTimeout(() => {
  55. childNavItem?.style.removeProperty('overflow')
  56. childNavItem?.style.removeProperty('height')
  57. childNavItem?.style.removeProperty('display')
  58. }, Defaults.transitionDuration)
  59. }
  60. toggle(treeviewMenu: Element): void {
  61. const navItem: HTMLElement | null = treeviewMenu.closest(SELECTOR_NAV_ITEM)
  62. const childNavItem: HTMLElement | null | undefined = navItem?.querySelector('.nav-treeview')
  63. if (navItem?.classList.contains(CLASS_NAME_MENU_OPEN)) {
  64. this.close(navItem, childNavItem)
  65. } else {
  66. this.open(navItem, childNavItem)
  67. }
  68. }
  69. }
  70. /**
  71. * ------------------------------------------------------------------------
  72. * Data Api implementation
  73. * ------------------------------------------------------------------------
  74. */
  75. domReady(() => {
  76. const button = document.querySelectorAll(SELECTOR_DATA_TOGGLE)
  77. for (const btn of button) {
  78. btn.addEventListener('click', event => {
  79. // event.preventDefault()
  80. const treeviewMenu = event.target as Element
  81. const data = new Treeview()
  82. data.toggle(treeviewMenu)
  83. })
  84. }
  85. })
  86. export default Treeview
  87. //