Sin descripción
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.

front-main.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Main - Front Pages
  3. */
  4. 'use strict';
  5. window.isRtl = window.Helpers.isRtl();
  6. window.isDarkStyle = window.Helpers.isDarkStyle();
  7. (function () {
  8. const menu = document.getElementById('navbarSupportedContent'),
  9. nav = document.querySelector('.layout-navbar'),
  10. navItemLink = document.querySelectorAll('.navbar-nav .nav-link');
  11. // Initialised custom options if checked
  12. setTimeout(function () {
  13. window.Helpers.initCustomOptionCheck();
  14. }, 1000);
  15. // Init BS Tooltip
  16. const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
  17. tooltipTriggerList.map(function (tooltipTriggerEl) {
  18. return new bootstrap.Tooltip(tooltipTriggerEl);
  19. });
  20. if (isRtl) {
  21. // If layout is RTL add .dropdown-menu-end class to .dropdown-menu
  22. Helpers._addClass('dropdown-menu-end', document.querySelectorAll('#layout-navbar .dropdown-menu'));
  23. // If layout is RTL add .dropdown-menu-end class to .dropdown-menu
  24. Helpers._addClass('dropdown-menu-end', document.querySelectorAll('.dropdown-menu'));
  25. }
  26. // Navbar
  27. window.addEventListener('scroll', e => {
  28. if (window.scrollY > 10) {
  29. nav.classList.add('navbar-active');
  30. } else {
  31. nav.classList.remove('navbar-active');
  32. }
  33. });
  34. window.addEventListener('load', e => {
  35. if (window.scrollY > 10) {
  36. nav.classList.add('navbar-active');
  37. } else {
  38. nav.classList.remove('navbar-active');
  39. }
  40. });
  41. // Function to close the mobile menu
  42. function closeMenu() {
  43. menu.classList.remove('show');
  44. }
  45. document.addEventListener('click', function (event) {
  46. // Check if the clicked element is inside mobile menu
  47. if (!menu.contains(event.target)) {
  48. closeMenu();
  49. }
  50. });
  51. navItemLink.forEach(link => {
  52. link.addEventListener('click', event => {
  53. if (!link.classList.contains('dropdown-toggle')) {
  54. closeMenu();
  55. } else {
  56. event.preventDefault();
  57. }
  58. });
  59. });
  60. // Mega dropdown
  61. const megaDropdown = document.querySelectorAll('.nav-link.mega-dropdown');
  62. if (megaDropdown) {
  63. megaDropdown.forEach(e => {
  64. new MegaDropdown(e);
  65. });
  66. }
  67. // Get style from local storage or use 'system' as default
  68. let storedStyle =
  69. localStorage.getItem('templateCustomizer-' + templateName + '--Theme') || //if no template style then use Customizer style
  70. (window.templateCustomizer?.settings?.defaultStyle ?? document.documentElement.getAttribute('data-bs-theme')); //!if there is no Customizer then use default style as light
  71. let styleSwitcher = document.querySelector('.dropdown-style-switcher');
  72. const styleSwitcherIcon = styleSwitcher.querySelector('i');
  73. new bootstrap.Tooltip(styleSwitcherIcon, {
  74. title: storedStyle.charAt(0).toUpperCase() + storedStyle.slice(1) + ' Mode',
  75. fallbackPlacements: ['bottom']
  76. });
  77. // Run switchImage function based on the stored style
  78. window.Helpers.switchImage(storedStyle);
  79. // Update light/dark image based on current style
  80. window.Helpers.setTheme(window.Helpers.getPreferredTheme());
  81. window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
  82. const storedTheme = window.Helpers.getStoredTheme();
  83. if (storedTheme !== 'light' && storedTheme !== 'dark') {
  84. window.Helpers.setTheme(window.Helpers.getPreferredTheme());
  85. }
  86. });
  87. function getScrollbarWidth() {
  88. const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
  89. document.body.style.setProperty('--bs-scrollbar-width', `${scrollbarWidth}px`);
  90. }
  91. getScrollbarWidth();
  92. //Style Switcher (Light/Dark/System Mode)
  93. window.addEventListener('DOMContentLoaded', () => {
  94. window.Helpers.showActiveTheme(window.Helpers.getPreferredTheme());
  95. getScrollbarWidth();
  96. document.querySelectorAll('[data-bs-theme-value]').forEach(toggle => {
  97. toggle.addEventListener('click', () => {
  98. const theme = toggle.getAttribute('data-bs-theme-value');
  99. window.Helpers.setStoredTheme(templateName, theme);
  100. window.Helpers.setTheme(theme);
  101. window.Helpers.showActiveTheme(theme, true);
  102. window.Helpers.syncCustomOptions(theme);
  103. let currTheme = theme;
  104. if (theme === 'system') {
  105. currTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
  106. }
  107. new bootstrap.Tooltip(styleSwitcherIcon, {
  108. title: theme.charAt(0).toUpperCase() + theme.slice(1) + ' Mode',
  109. fallbackPlacements: ['bottom']
  110. });
  111. window.Helpers.switchImage(currTheme);
  112. });
  113. });
  114. });
  115. })();