You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 regels
1.6 KiB

  1. (function() {
  2. // ignore if routing is enabled
  3. if (dmx.routing) return;
  4. dmx.config.mapping['a.nav-link:not([href^="#"])'] = 'nav-link';
  5. dmx.Component('nav-link', {
  6. init (node) {
  7. window.addEventListener("popstate", this._stateHandler);
  8. window.addEventListener("pushstate", this._stateHandler);
  9. window.addEventListener("replacestate", this._stateHandler);
  10. window.addEventListener('hashchange', this._stateHandler);
  11. this._stateHandler();
  12. },
  13. destroy () {
  14. window.removeEventListener("popstate", this._stateHandler);
  15. window.removeEventListener("pushstate", this._stateHandler);
  16. window.removeEventListener("replacestate", this._stateHandler);
  17. window.removeEventListener('hashchange', this._stateHandler);
  18. },
  19. _stateHandler () {
  20. const node = this.$node;
  21. const active = node.href == window.location.href || node.href == window.location.href.split("?")[0].split("#")[0];
  22. node.classList.toggle('active', active);
  23. if (node.classList.contains('dropdown-item')) {
  24. const items = node.parentNode.querySelectorAll('.dropdown-item');
  25. node.classList.remove('active');
  26. for (let i = 0; i < items.length; i++) {
  27. const match = items[i].href == window.location.href || items[i].href == window.location.href.split("?")[0].split("#")[0];
  28. if (match) {
  29. items[i].classList.add('active');
  30. node.classList.add('active');
  31. } else {
  32. items[i].classList.remove('active');
  33. }
  34. }
  35. }
  36. },
  37. });
  38. })()