Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. const TRANSITION_EVENTS = ['transitionend', 'webkitTransitionEnd', 'oTransitionEnd']
  2. const DELTA = 5
  3. class Menu {
  4. constructor(el, config = {}, _PS = null) {
  5. this._el = el
  6. this._horizontal = config.orientation === 'horizontal'
  7. this._animate = config.animate !== false
  8. this._accordion = config.accordion !== false
  9. this._showDropdownOnHover = Boolean(config.showDropdownOnHover)
  10. this._closeChildren = Boolean(config.closeChildren)
  11. this._rtl = document.documentElement.getAttribute('dir') === 'rtl' || document.body.getAttribute('dir') === 'rtl'
  12. this._onOpen = config.onOpen || (() => {})
  13. this._onOpened = config.onOpened || (() => {})
  14. this._onClose = config.onClose || (() => {})
  15. this._onClosed = config.onClosed || (() => {})
  16. this._psScroll = null
  17. this._topParent = null
  18. this._menuBgClass = null
  19. el.classList.add('menu')
  20. el.classList[this._animate ? 'remove' : 'add']('menu-no-animation')
  21. if (!this._horizontal) {
  22. el.classList.add('menu-vertical')
  23. el.classList.remove('menu-horizontal')
  24. const PerfectScrollbarLib = _PS || window.PerfectScrollbar
  25. if (PerfectScrollbarLib) {
  26. this._scrollbar = new PerfectScrollbarLib(el.querySelector('.menu-inner'), {
  27. suppressScrollX: true,
  28. wheelPropagation: !Menu._hasClass('layout-menu-fixed layout-menu-fixed-offcanvas')
  29. })
  30. window.Helpers.menuPsScroll = this._scrollbar
  31. } else {
  32. el.querySelector('.menu-inner').classList.add('overflow-auto')
  33. }
  34. } else {
  35. el.classList.add('menu-horizontal')
  36. el.classList.remove('menu-vertical')
  37. this._inner = el.querySelector('.menu-inner')
  38. const container = this._inner.parentNode
  39. this._prevBtn = el.querySelector('.menu-horizontal-prev')
  40. if (!this._prevBtn) {
  41. this._prevBtn = document.createElement('a')
  42. this._prevBtn.href = '#'
  43. this._prevBtn.className = 'menu-horizontal-prev'
  44. container.appendChild(this._prevBtn)
  45. }
  46. this._wrapper = el.querySelector('.menu-horizontal-wrapper')
  47. if (!this._wrapper) {
  48. this._wrapper = document.createElement('div')
  49. this._wrapper.className = 'menu-horizontal-wrapper'
  50. this._wrapper.appendChild(this._inner)
  51. container.appendChild(this._wrapper)
  52. }
  53. this._nextBtn = el.querySelector('.menu-horizontal-next')
  54. if (!this._nextBtn) {
  55. this._nextBtn = document.createElement('a')
  56. this._nextBtn.href = '#'
  57. this._nextBtn.className = 'menu-horizontal-next'
  58. container.appendChild(this._nextBtn)
  59. }
  60. this._innerPosition = 0
  61. this.update()
  62. }
  63. // Switch to vertical menu on small screen for horizontal menu layout on page load
  64. if (this._horizontal && window.innerWidth < window.Helpers.LAYOUT_BREAKPOINT) {
  65. this.switchMenu('vertical')
  66. } else {
  67. this._bindEvents()
  68. }
  69. // Link menu instance to element
  70. el.menuInstance = this
  71. const semiDarkEl = localStorage.getItem(`templateCustomizer-${templateName}--SemiDark`)
  72. if (semiDarkEl === 'true') {
  73. document.querySelector('#layout-menu').setAttribute('data-bs-theme', 'dark')
  74. }
  75. }
  76. _bindEvents() {
  77. // Click Event
  78. this._evntElClick = e => {
  79. // Find top parent element
  80. if (e.target.closest('ul') && e.target.closest('ul').classList.contains('menu-inner')) {
  81. const menuItem = Menu._findParent(e.target, 'menu-item', false)
  82. // eslint-disable-next-line prefer-destructuring
  83. if (menuItem) this._topParent = menuItem.childNodes[0]
  84. }
  85. const toggleLink = e.target.classList.contains('menu-toggle')
  86. ? e.target
  87. : Menu._findParent(e.target, 'menu-toggle', false)
  88. if (toggleLink) {
  89. e.preventDefault()
  90. if (toggleLink.getAttribute('data-hover') !== 'true') {
  91. this.toggle(toggleLink)
  92. }
  93. }
  94. }
  95. if ((!this._showDropdownOnHover && this._horizontal) || !this._horizontal || window.Helpers.isMobileDevice)
  96. this._el.addEventListener('click', this._evntElClick)
  97. this._evntWindowResize = () => {
  98. this.update()
  99. if (this._lastWidth !== window.innerWidth) {
  100. this._lastWidth = window.innerWidth
  101. this.update()
  102. }
  103. const horizontalMenuTemplate = document.querySelector("[data-template^='horizontal-menu']")
  104. if (!this._horizontal && !horizontalMenuTemplate) this.manageScroll()
  105. }
  106. window.addEventListener('resize', this._evntWindowResize)
  107. if (this._horizontal) {
  108. this._evntPrevBtnClick = e => {
  109. e.preventDefault()
  110. if (this._prevBtn.classList.contains('disabled')) return
  111. this._slide('prev')
  112. }
  113. this._prevBtn.addEventListener('click', this._evntPrevBtnClick)
  114. this._evntNextBtnClick = e => {
  115. e.preventDefault()
  116. if (this._nextBtn.classList.contains('disabled')) return
  117. this._slide('next')
  118. }
  119. this._nextBtn.addEventListener('click', this._evntNextBtnClick)
  120. this._evntBodyClick = e => {
  121. if (!this._inner.contains(e.target) && this._el.querySelectorAll('.menu-inner > .menu-item.open').length)
  122. this.closeAll()
  123. }
  124. document.body.addEventListener('click', this._evntBodyClick)
  125. if (this._showDropdownOnHover) {
  126. /** ***********************************************
  127. * Horizontal Menu Mouse Over Event
  128. * ? e.target !== e.currentTarget condition to disable mouseover event on whole menu navbar
  129. * ? !e.target.parentNode.classList.contains('open') to disable mouseover events on icon, text and dropdown arrow
  130. */
  131. this._evntElMouseOver = e => {
  132. if (e.target !== e.currentTarget && !e.target.parentNode.classList.contains('open')) {
  133. const toggleLink = e.target.classList.contains('menu-toggle') ? e.target : null
  134. if (toggleLink) {
  135. e.preventDefault()
  136. if (toggleLink.getAttribute('data-hover') !== 'true') {
  137. this.toggle(toggleLink)
  138. }
  139. }
  140. }
  141. e.stopPropagation()
  142. }
  143. if (this._horizontal && window.screen.width > window.Helpers.LAYOUT_BREAKPOINT) {
  144. this._el.addEventListener('mouseover', this._evntElMouseOver)
  145. }
  146. /** ***********************************************
  147. * Horizontal Menu Mouse Out Event
  148. * ? e.target !== e.currentTarget condition to disable mouseout event on whole menu navbar
  149. * ? mouseOutEl.parentNode.classList.contains('open') to check if the mouseout element has open class or not
  150. * ? !mouseOutEl.classList.contains('menu-toggle') to check if mouseout was from single menu item and not from the one which has submenu
  151. * ? !mouseOverEl.parentNode.classList.contains('menu-link') to disable mouseout event for icon, text and dropdown arrow
  152. */
  153. this._evntElMouseOut = e => {
  154. const mainEl = e.currentTarget
  155. const mouseOutEl = e.target
  156. const mouseOverEl = e.toElement || e.relatedTarget
  157. // Find absolute parent of any menu item from which mouseout event triggered
  158. if (mouseOutEl.closest('ul') && mouseOutEl.closest('ul').classList.contains('menu-inner')) {
  159. this._topParent = mouseOutEl
  160. }
  161. if (
  162. mouseOutEl !== mainEl &&
  163. (mouseOutEl.parentNode.classList.contains('open') || !mouseOutEl.classList.contains('menu-toggle')) &&
  164. mouseOverEl &&
  165. mouseOverEl.parentNode &&
  166. !mouseOverEl.parentNode.classList.contains('menu-link')
  167. ) {
  168. // When mouse goes totally out of menu items, check mouse over element to confirm it's not the child of menu, once confirmed close the menu
  169. if (this._topParent && !Menu.childOf(mouseOverEl, this._topParent.parentNode)) {
  170. const toggleLink = this._topParent.classList.contains('menu-toggle') ? this._topParent : null
  171. if (toggleLink) {
  172. e.preventDefault()
  173. if (toggleLink.getAttribute('data-hover') !== 'true') {
  174. this.toggle(toggleLink)
  175. this._topParent = null
  176. }
  177. }
  178. }
  179. // When mouse enter the sub menu, check if it's child of the initially mouse overed menu item(Actual Parent),
  180. // if it's the parent do not close the sub menu else close the sub menu
  181. if (Menu.childOf(mouseOverEl, mouseOutEl.parentNode)) {
  182. return
  183. }
  184. const toggleLink = mouseOutEl.classList.contains('menu-toggle') ? mouseOutEl : null
  185. if (toggleLink) {
  186. e.preventDefault()
  187. if (toggleLink.getAttribute('data-hover') !== 'true') {
  188. this.toggle(toggleLink)
  189. }
  190. }
  191. }
  192. e.stopPropagation()
  193. }
  194. if (this._horizontal && window.screen.width > window.Helpers.LAYOUT_BREAKPOINT) {
  195. this._el.addEventListener('mouseout', this._evntElMouseOut)
  196. }
  197. }
  198. }
  199. }
  200. static childOf(/* child node */ c, /* parent node */ p) {
  201. // returns boolean
  202. if (c.parentNode) {
  203. while ((c = c.parentNode) && c !== p);
  204. return !!c
  205. }
  206. return false
  207. }
  208. _unbindEvents() {
  209. if (this._evntElClick) {
  210. this._el.removeEventListener('click', this._evntElClick)
  211. this._evntElClick = null
  212. }
  213. if (this._evntElMouseOver) {
  214. this._el.removeEventListener('mouseover', this._evntElMouseOver)
  215. this._evntElMouseOver = null
  216. }
  217. if (this._evntElMouseOut) {
  218. this._el.removeEventListener('mouseout', this._evntElMouseOut)
  219. this._evntElMouseOut = null
  220. }
  221. if (this._evntWindowResize) {
  222. window.removeEventListener('resize', this._evntWindowResize)
  223. this._evntWindowResize = null
  224. }
  225. if (this._evntBodyClick) {
  226. document.body.removeEventListener('click', this._evntBodyClick)
  227. this._evntBodyClick = null
  228. }
  229. if (this._evntInnerMousemove) {
  230. this._inner.removeEventListener('mousemove', this._evntInnerMousemove)
  231. this._evntInnerMousemove = null
  232. }
  233. if (this._evntInnerMouseleave) {
  234. this._inner.removeEventListener('mouseleave', this._evntInnerMouseleave)
  235. this._evntInnerMouseleave = null
  236. }
  237. }
  238. static _isRoot(item) {
  239. return !Menu._findParent(item, 'menu-item', false)
  240. }
  241. static _findParent(el, cls, throwError = true) {
  242. if (el.tagName.toUpperCase() === 'BODY') return null
  243. el = el.parentNode
  244. while (el.tagName.toUpperCase() !== 'BODY' && !el.classList.contains(cls)) {
  245. el = el.parentNode
  246. }
  247. el = el.tagName.toUpperCase() !== 'BODY' ? el : null
  248. if (!el && throwError) throw new Error(`Cannot find \`.${cls}\` parent element`)
  249. return el
  250. }
  251. static _findChild(el, cls) {
  252. const items = el.childNodes
  253. const found = []
  254. for (let i = 0, l = items.length; i < l; i++) {
  255. if (items[i].classList) {
  256. let passed = 0
  257. for (let j = 0; j < cls.length; j++) {
  258. if (items[i].classList.contains(cls[j])) passed += 1
  259. }
  260. if (cls.length === passed) found.push(items[i])
  261. }
  262. }
  263. return found
  264. }
  265. static _findMenu(item) {
  266. let curEl = item.childNodes[0]
  267. let menu = null
  268. while (curEl && !menu) {
  269. if (curEl.classList && curEl.classList.contains('menu-sub')) menu = curEl
  270. curEl = curEl.nextSibling
  271. }
  272. if (!menu) throw new Error('Cannot find `.menu-sub` element for the current `.menu-toggle`')
  273. return menu
  274. }
  275. // Has class
  276. static _hasClass(cls, el = window.Helpers.ROOT_EL) {
  277. let result = false
  278. cls.split(' ').forEach(c => {
  279. if (el.classList.contains(c)) result = true
  280. })
  281. return result
  282. }
  283. open(el, closeChildren = this._closeChildren) {
  284. const item = this._findUnopenedParent(Menu._getItem(el, true), closeChildren)
  285. if (!item) return
  286. const toggleLink = Menu._getLink(item, true)
  287. Menu._promisify(this._onOpen, this, item, toggleLink, Menu._findMenu(item))
  288. .then(() => {
  289. if (!this._horizontal || !Menu._isRoot(item)) {
  290. if (this._animate && !this._horizontal) {
  291. window.requestAnimationFrame(() => this._toggleAnimation(true, item, false))
  292. if (this._accordion) this._closeOther(item, closeChildren)
  293. } else if (this._animate) {
  294. this._toggleDropdown(true, item, closeChildren)
  295. // eslint-disable-next-line no-unused-expressions
  296. this._onOpened && this._onOpened(this, item, toggleLink, Menu._findMenu(item))
  297. } else {
  298. item.classList.add('open')
  299. // eslint-disable-next-line no-unused-expressions
  300. this._onOpened && this._onOpened(this, item, toggleLink, Menu._findMenu(item))
  301. if (this._accordion) this._closeOther(item, closeChildren)
  302. }
  303. } else {
  304. this._toggleDropdown(true, item, closeChildren)
  305. // eslint-disable-next-line no-unused-expressions
  306. this._onOpened && this._onOpened(this, item, toggleLink, Menu._findMenu(item))
  307. }
  308. })
  309. .catch(() => {})
  310. }
  311. close(el, closeChildren = this._closeChildren, _autoClose = false) {
  312. const item = Menu._getItem(el, true)
  313. const toggleLink = Menu._getLink(el, true)
  314. if (!item.classList.contains('open') || item.classList.contains('disabled')) return
  315. Menu._promisify(this._onClose, this, item, toggleLink, Menu._findMenu(item), _autoClose)
  316. .then(() => {
  317. if (!this._horizontal || !Menu._isRoot(item)) {
  318. if (this._animate && !this._horizontal) {
  319. window.requestAnimationFrame(() => this._toggleAnimation(false, item, closeChildren))
  320. } else {
  321. item.classList.remove('open')
  322. if (closeChildren) {
  323. const opened = item.querySelectorAll('.menu-item.open')
  324. for (let i = 0, l = opened.length; i < l; i++) opened[i].classList.remove('open')
  325. }
  326. // eslint-disable-next-line no-unused-expressions
  327. this._onClosed && this._onClosed(this, item, toggleLink, Menu._findMenu(item))
  328. }
  329. } else {
  330. this._toggleDropdown(false, item, closeChildren)
  331. // eslint-disable-next-line no-unused-expressions
  332. this._onClosed && this._onClosed(this, item, toggleLink, Menu._findMenu(item))
  333. }
  334. })
  335. .catch(() => {})
  336. }
  337. _closeOther(item, closeChildren) {
  338. const opened = Menu._findChild(item.parentNode, ['menu-item', 'open'])
  339. for (let i = 0, l = opened.length; i < l; i++) {
  340. if (opened[i] !== item) this.close(opened[i], closeChildren)
  341. }
  342. }
  343. toggle(el, closeChildren = this._closeChildren) {
  344. const item = Menu._getItem(el, true)
  345. if (item.classList.contains('open')) this.close(item, closeChildren)
  346. else this.open(item, closeChildren)
  347. }
  348. _toggleDropdown(show, item, closeChildren) {
  349. const menu = Menu._findMenu(item)
  350. const actualItem = item
  351. let subMenuItem = false
  352. if (show) {
  353. if (Menu._findParent(item, 'menu-sub', false)) {
  354. subMenuItem = true
  355. item = this._topParent ? this._topParent.parentNode : item
  356. }
  357. const wrapperWidth = Math.round(this._wrapper.getBoundingClientRect().width)
  358. const position = this._innerPosition
  359. const itemOffset = this._getItemOffset(item)
  360. const itemWidth = Math.round(item.getBoundingClientRect().width)
  361. if (itemOffset - DELTA <= -1 * position) {
  362. this._innerPosition = -1 * itemOffset
  363. } else if (itemOffset + position + itemWidth + DELTA >= wrapperWidth) {
  364. if (itemWidth > wrapperWidth) {
  365. this._innerPosition = -1 * itemOffset
  366. } else {
  367. this._innerPosition = -1 * (itemOffset + itemWidth - wrapperWidth)
  368. }
  369. }
  370. actualItem.classList.add('open')
  371. const menuWidth = Math.round(menu.getBoundingClientRect().width)
  372. if (subMenuItem) {
  373. if (
  374. itemOffset + this._innerPosition + menuWidth * 2 > wrapperWidth &&
  375. menuWidth < wrapperWidth &&
  376. menuWidth >= itemWidth
  377. ) {
  378. menu.style.left = [this._rtl ? '100%' : '-100%']
  379. }
  380. } else if (
  381. itemOffset + this._innerPosition + menuWidth > wrapperWidth &&
  382. menuWidth < wrapperWidth &&
  383. menuWidth > itemWidth
  384. ) {
  385. menu.style[this._rtl ? 'marginRight' : 'marginLeft'] = `-${menuWidth - itemWidth}px`
  386. }
  387. this._closeOther(actualItem, closeChildren)
  388. this._updateSlider()
  389. } else {
  390. const toggle = Menu._findChild(item, ['menu-toggle'])
  391. // eslint-disable-next-line no-unused-expressions
  392. toggle.length && toggle[0].removeAttribute('data-hover', 'true')
  393. item.classList.remove('open')
  394. menu.style[this._rtl ? 'marginRight' : 'marginLeft'] = null
  395. if (closeChildren) {
  396. const opened = menu.querySelectorAll('.menu-item.open')
  397. for (let i = 0, l = opened.length; i < l; i++) opened[i].classList.remove('open')
  398. }
  399. }
  400. }
  401. _slide(direction) {
  402. const wrapperWidth = Math.round(this._wrapper.getBoundingClientRect().width)
  403. const innerWidth = this._innerWidth
  404. let newPosition
  405. if (direction === 'next') {
  406. newPosition = this._getSlideNextPos()
  407. if (innerWidth + newPosition < wrapperWidth) {
  408. newPosition = wrapperWidth - innerWidth
  409. }
  410. } else {
  411. newPosition = this._getSlidePrevPos()
  412. if (newPosition > 0) newPosition = 0
  413. }
  414. this._innerPosition = newPosition
  415. this.update()
  416. }
  417. _getSlideNextPos() {
  418. const wrapperWidth = Math.round(this._wrapper.getBoundingClientRect().width)
  419. const position = this._innerPosition
  420. let curItem = this._inner.childNodes[0]
  421. let left = 0
  422. while (curItem) {
  423. if (curItem.tagName) {
  424. const curItemWidth = Math.round(curItem.getBoundingClientRect().width)
  425. if (left + position - DELTA <= wrapperWidth && left + position + curItemWidth + DELTA >= wrapperWidth) {
  426. if (curItemWidth > wrapperWidth && left === -1 * position) left += curItemWidth
  427. break
  428. }
  429. left += curItemWidth
  430. }
  431. curItem = curItem.nextSibling
  432. }
  433. return -1 * left
  434. }
  435. _getSlidePrevPos() {
  436. const wrapperWidth = Math.round(this._wrapper.getBoundingClientRect().width)
  437. const position = this._innerPosition
  438. let curItem = this._inner.childNodes[0]
  439. let left = 0
  440. while (curItem) {
  441. if (curItem.tagName) {
  442. const curItemWidth = Math.round(curItem.getBoundingClientRect().width)
  443. if (left - DELTA <= -1 * position && left + curItemWidth + DELTA >= -1 * position) {
  444. if (curItemWidth <= wrapperWidth) left = left + curItemWidth - wrapperWidth
  445. break
  446. }
  447. left += curItemWidth
  448. }
  449. curItem = curItem.nextSibling
  450. }
  451. return -1 * left
  452. }
  453. static _getItem(el, toggle) {
  454. let item = null
  455. const selector = toggle ? 'menu-toggle' : 'menu-link'
  456. if (el.classList.contains('menu-item')) {
  457. if (Menu._findChild(el, [selector]).length) item = el
  458. } else if (el.classList.contains(selector)) {
  459. item = el.parentNode.classList.contains('menu-item') ? el.parentNode : null
  460. }
  461. if (!item) {
  462. throw new Error(`${toggle ? 'Toggable ' : ''}\`.menu-item\` element not found.`)
  463. }
  464. return item
  465. }
  466. static _getLink(el, toggle) {
  467. let found = []
  468. const selector = toggle ? 'menu-toggle' : 'menu-link'
  469. if (el.classList.contains(selector)) found = [el]
  470. else if (el.classList.contains('menu-item')) found = Menu._findChild(el, [selector])
  471. if (!found.length) throw new Error(`\`${selector}\` element not found.`)
  472. return found[0]
  473. }
  474. _findUnopenedParent(item, closeChildren) {
  475. let tree = []
  476. let parentItem = null
  477. while (item) {
  478. if (item.classList.contains('disabled')) {
  479. parentItem = null
  480. tree = []
  481. } else {
  482. if (!item.classList.contains('open')) parentItem = item
  483. tree.push(item)
  484. }
  485. item = Menu._findParent(item, 'menu-item', false)
  486. }
  487. if (!parentItem) return null
  488. if (tree.length === 1) return parentItem
  489. tree = tree.slice(0, tree.indexOf(parentItem))
  490. for (let i = 0, l = tree.length; i < l; i++) {
  491. tree[i].classList.add('open')
  492. if (this._accordion) {
  493. const openedItems = Menu._findChild(tree[i].parentNode, ['menu-item', 'open'])
  494. for (let j = 0, k = openedItems.length; j < k; j++) {
  495. if (openedItems[j] !== tree[i]) {
  496. openedItems[j].classList.remove('open')
  497. if (closeChildren) {
  498. const openedChildren = openedItems[j].querySelectorAll('.menu-item.open')
  499. for (let x = 0, z = openedChildren.length; x < z; x++) {
  500. openedChildren[x].classList.remove('open')
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. return parentItem
  508. }
  509. _toggleAnimation(open, item, closeChildren) {
  510. const toggleLink = Menu._getLink(item, true)
  511. const menu = Menu._findMenu(item)
  512. Menu._unbindAnimationEndEvent(item)
  513. const linkHeight = Math.round(toggleLink.getBoundingClientRect().height)
  514. item.style.overflow = 'hidden'
  515. const clearItemStyle = () => {
  516. item.classList.remove('menu-item-animating')
  517. item.classList.remove('menu-item-closing')
  518. item.style.overflow = null
  519. item.style.height = null
  520. if (!this._horizontal) this.update()
  521. }
  522. if (open) {
  523. item.style.height = `${linkHeight}px`
  524. item.classList.add('menu-item-animating')
  525. item.classList.add('open')
  526. Menu._bindAnimationEndEvent(item, () => {
  527. clearItemStyle()
  528. this._onOpened(this, item, toggleLink, menu)
  529. })
  530. setTimeout(() => {
  531. item.style.height = `${linkHeight + Math.round(menu.getBoundingClientRect().height)}px`
  532. }, 50)
  533. } else {
  534. item.style.height = `${linkHeight + Math.round(menu.getBoundingClientRect().height)}px`
  535. item.classList.add('menu-item-animating')
  536. item.classList.add('menu-item-closing')
  537. Menu._bindAnimationEndEvent(item, () => {
  538. item.classList.remove('open')
  539. clearItemStyle()
  540. if (closeChildren) {
  541. const opened = item.querySelectorAll('.menu-item.open')
  542. for (let i = 0, l = opened.length; i < l; i++) opened[i].classList.remove('open')
  543. }
  544. this._onClosed(this, item, toggleLink, menu)
  545. })
  546. setTimeout(() => {
  547. item.style.height = `${linkHeight}px`
  548. }, 50)
  549. }
  550. }
  551. static _bindAnimationEndEvent(el, handler) {
  552. const cb = e => {
  553. if (e.target !== el) return
  554. Menu._unbindAnimationEndEvent(el)
  555. handler(e)
  556. }
  557. let duration = window.getComputedStyle(el).transitionDuration
  558. duration = parseFloat(duration) * (duration.indexOf('ms') !== -1 ? 1 : 1000)
  559. el._menuAnimationEndEventCb = cb
  560. TRANSITION_EVENTS.forEach(ev => el.addEventListener(ev, el._menuAnimationEndEventCb, false))
  561. el._menuAnimationEndEventTimeout = setTimeout(() => {
  562. cb({ target: el })
  563. }, duration + 50)
  564. }
  565. _getItemOffset(item) {
  566. let curItem = this._inner.childNodes[0]
  567. let left = 0
  568. while (curItem !== item) {
  569. if (curItem.tagName) {
  570. left += Math.round(curItem.getBoundingClientRect().width)
  571. }
  572. curItem = curItem.nextSibling
  573. }
  574. return left
  575. }
  576. _updateSlider(wrapperWidth = null, innerWidth = null, position = null) {
  577. const _wrapperWidth = wrapperWidth !== null ? wrapperWidth : Math.round(this._wrapper.getBoundingClientRect().width)
  578. const _innerWidth = innerWidth !== null ? innerWidth : this._innerWidth
  579. const _position = position !== null ? position : this._innerPosition
  580. if (_innerWidth < _wrapperWidth || window.innerWidth < window.Helpers.LAYOUT_BREAKPOINT) {
  581. this._prevBtn.classList.add('d-none')
  582. this._nextBtn.classList.add('d-none')
  583. } else {
  584. this._prevBtn.classList.remove('d-none')
  585. this._nextBtn.classList.remove('d-none')
  586. }
  587. if (_innerWidth > _wrapperWidth && window.innerWidth > window.Helpers.LAYOUT_BREAKPOINT) {
  588. if (_position === 0) this._prevBtn.classList.add('disabled')
  589. else this._prevBtn.classList.remove('disabled')
  590. if (_innerWidth + _position <= _wrapperWidth) this._nextBtn.classList.add('disabled')
  591. else this._nextBtn.classList.remove('disabled')
  592. }
  593. }
  594. static _promisify(fn, ...args) {
  595. const result = fn(...args)
  596. if (result instanceof Promise) {
  597. return result
  598. }
  599. if (result === false) {
  600. return Promise.reject()
  601. }
  602. return Promise.resolve()
  603. }
  604. get _innerWidth() {
  605. const items = this._inner.childNodes
  606. let width = 0
  607. for (let i = 0, l = items.length; i < l; i++) {
  608. if (items[i].tagName) {
  609. width += Math.round(items[i].getBoundingClientRect().width)
  610. }
  611. }
  612. return width
  613. }
  614. get _innerPosition() {
  615. return parseInt(this._inner.style[this._rtl ? 'marginRight' : 'marginLeft'] || '0px', 10)
  616. }
  617. set _innerPosition(value) {
  618. this._inner.style[this._rtl ? 'marginRight' : 'marginLeft'] = `${value}px`
  619. return value
  620. }
  621. static _unbindAnimationEndEvent(el) {
  622. const cb = el._menuAnimationEndEventCb
  623. if (el._menuAnimationEndEventTimeout) {
  624. clearTimeout(el._menuAnimationEndEventTimeout)
  625. el._menuAnimationEndEventTimeout = null
  626. }
  627. if (!cb) return
  628. TRANSITION_EVENTS.forEach(ev => el.removeEventListener(ev, cb, false))
  629. el._menuAnimationEndEventCb = null
  630. }
  631. closeAll(closeChildren = this._closeChildren) {
  632. const opened = this._el.querySelectorAll('.menu-inner > .menu-item.open')
  633. for (let i = 0, l = opened.length; i < l; i++) this.close(opened[i], closeChildren)
  634. }
  635. static setDisabled(el, disabled) {
  636. Menu._getItem(el, false).classList[disabled ? 'add' : 'remove']('disabled')
  637. }
  638. static isActive(el) {
  639. return Menu._getItem(el, false).classList.contains('active')
  640. }
  641. static isOpened(el) {
  642. return Menu._getItem(el, false).classList.contains('open')
  643. }
  644. static isDisabled(el) {
  645. return Menu._getItem(el, false).classList.contains('disabled')
  646. }
  647. update() {
  648. if (!this._horizontal) {
  649. if (this._scrollbar) {
  650. this._scrollbar.update()
  651. }
  652. } else {
  653. this.closeAll()
  654. const wrapperWidth = Math.round(this._wrapper.getBoundingClientRect().width)
  655. const innerWidth = this._innerWidth
  656. let position = this._innerPosition
  657. if (wrapperWidth - position > innerWidth) {
  658. position = wrapperWidth - innerWidth
  659. if (position > 0) position = 0
  660. this._innerPosition = position
  661. }
  662. this._updateSlider(wrapperWidth, innerWidth, position)
  663. }
  664. }
  665. manageScroll() {
  666. const { PerfectScrollbar } = window
  667. const menuInner = document.querySelector('.menu-inner')
  668. if (window.innerWidth < window.Helpers.LAYOUT_BREAKPOINT) {
  669. if (this._scrollbar !== null) {
  670. this._scrollbar.destroy()
  671. this._scrollbar = null
  672. }
  673. menuInner.classList.add('overflow-auto')
  674. } else {
  675. if (this._scrollbar === null) {
  676. const menuScroll = new PerfectScrollbar(document.querySelector('.menu-inner'), {
  677. suppressScrollX: true,
  678. wheelPropagation: false
  679. })
  680. this._scrollbar = menuScroll
  681. }
  682. menuInner.classList.remove('overflow-auto')
  683. }
  684. }
  685. switchMenu(menu) {
  686. // Unbind Events
  687. this._unbindEvents()
  688. // const html = document.documentElement
  689. const navbar = document.querySelector('nav.layout-navbar')
  690. const navbarCollapse = document.querySelector('#navbar-collapse')
  691. const asideMenuWrapper = document.querySelector('#layout-menu div')
  692. const asideMenu = document.querySelector('#layout-menu')
  693. const horzMenuClasses = ['layout-menu-horizontal', 'menu', 'menu-horizontal', 'container-fluid', 'flex-grow-0']
  694. const vertMenuClasses = ['layout-menu', 'menu', 'menu-vertical']
  695. const horzMenuWrapper = document.querySelector('.menu-horizontal-wrapper')
  696. const menuInner = document.querySelector('.menu-inner')
  697. const brand = document.querySelector('.app-brand')
  698. const menuToggler = document.querySelector('.layout-menu-toggle')
  699. const activeMenuItems = document.querySelectorAll('.menu-inner .active')
  700. const { PerfectScrollbar } = window
  701. if (menu === 'vertical') {
  702. this._horizontal = false
  703. asideMenuWrapper.insertBefore(brand, horzMenuWrapper)
  704. asideMenuWrapper.insertBefore(menuInner, horzMenuWrapper)
  705. asideMenuWrapper.classList.add('flex-column', 'p-0')
  706. asideMenu.classList.remove(...asideMenu.classList)
  707. asideMenu.classList.add(...vertMenuClasses, this._menuBgClass)
  708. brand.classList.remove('d-none', 'd-lg-flex')
  709. menuToggler.classList.remove('d-none')
  710. if (PerfectScrollbar !== undefined) {
  711. this._psScroll = new PerfectScrollbar(document.querySelector('.menu-inner'), {
  712. suppressScrollX: true,
  713. wheelPropagation: !Menu._hasClass('layout-menu-fixed layout-menu-fixed-offcanvas')
  714. })
  715. }
  716. menuInner.classList.add('overflow-auto')
  717. // Add open class to active items
  718. for (let i = 0; i < activeMenuItems.length - 1; ++i) {
  719. activeMenuItems[i].classList.add('open')
  720. }
  721. } else {
  722. this._horizontal = true
  723. navbar.children[0].insertBefore(brand, navbarCollapse)
  724. brand.classList.add('d-none', 'd-lg-flex')
  725. horzMenuWrapper.appendChild(menuInner)
  726. asideMenuWrapper.classList.remove('flex-column', 'p-0')
  727. asideMenu.classList.remove(...asideMenu.classList)
  728. asideMenu.classList.add(...horzMenuClasses, this._menuBgClass)
  729. menuToggler.classList.add('d-none')
  730. menuInner.classList.remove('overflow-auto')
  731. // Remove open class from active items
  732. for (let i = 0; i < activeMenuItems.length; ++i) {
  733. activeMenuItems[i].classList.remove('open')
  734. }
  735. }
  736. const semiDarkEl = localStorage.getItem(`templateCustomizer-${templateName}--SemiDark`)
  737. if (semiDarkEl) {
  738. asideMenu.setAttribute('data-bs-theme', 'dark')
  739. }
  740. this._bindEvents()
  741. }
  742. destroy() {
  743. if (!this._el) return
  744. this._unbindEvents()
  745. const items = this._el.querySelectorAll('.menu-item')
  746. for (let i = 0, l = items.length; i < l; i++) {
  747. Menu._unbindAnimationEndEvent(items[i])
  748. items[i].classList.remove('menu-item-animating')
  749. items[i].classList.remove('open')
  750. items[i].style.overflow = null
  751. items[i].style.height = null
  752. }
  753. const menus = this._el.querySelectorAll('.menu-menu')
  754. for (let i2 = 0, l2 = menus.length; i2 < l2; i2++) {
  755. menus[i2].style.marginRight = null
  756. menus[i2].style.marginLeft = null
  757. }
  758. this._el.classList.remove('menu-no-animation')
  759. if (this._wrapper) {
  760. this._prevBtn.parentNode.removeChild(this._prevBtn)
  761. this._nextBtn.parentNode.removeChild(this._nextBtn)
  762. this._wrapper.parentNode.insertBefore(this._inner, this._wrapper)
  763. this._wrapper.parentNode.removeChild(this._wrapper)
  764. this._inner.style.marginLeft = null
  765. this._inner.style.marginRight = null
  766. }
  767. this._el.menuInstance = null
  768. delete this._el.menuInstance
  769. this._el = null
  770. this._horizontal = null
  771. this._animate = null
  772. this._accordion = null
  773. this._showDropdownOnHover = null
  774. this._closeChildren = null
  775. this._rtl = null
  776. this._onOpen = null
  777. this._onOpened = null
  778. this._onClose = null
  779. this._onClosed = null
  780. if (this._scrollbar) {
  781. this._scrollbar.destroy()
  782. this._scrollbar = null
  783. }
  784. this._inner = null
  785. this._prevBtn = null
  786. this._wrapper = null
  787. this._nextBtn = null
  788. }
  789. }
  790. window.Menu = Menu
  791. export { Menu }