scripts.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // Sticky header position on page scrolling up
  2. const header = document.querySelector('.js-header');
  3. const stickyClass = 'sticky';
  4. let lastScrollTop = 0;
  5. let isWaiting = false;
  6. window.addEventListener('scroll', () => {
  7. if (!isWaiting) {
  8. window.requestAnimationFrame(() => {
  9. let currentScroll = window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
  10. if (currentScroll > lastScrollTop) {
  11. header.classList.remove(stickyClass);
  12. } else if (currentScroll < lastScrollTop && currentScroll > 0) {
  13. header.classList.add(stickyClass);
  14. } else if (currentScroll <= 0) {
  15. header.classList.remove(stickyClass);
  16. }
  17. lastScrollTop = currentScroll;
  18. isWaiting = false;
  19. });
  20. isWaiting = true;
  21. }
  22. }, false);
  23. // Dropdown menu
  24. (function (menuConfig) {
  25. /**
  26. * Merge default config with the theme overrided ones
  27. */
  28. var defaultConfig = {
  29. // behaviour
  30. mobileMenuMode: 'sidebar', // 'overlay' or 'sidebar'
  31. animationSpeed: 300,
  32. submenuWidth: 300,
  33. doubleClickTime: 500,
  34. mobileMenuExpandableSubmenus: true,
  35. isHoverMenu: true,
  36. // selectors
  37. wrapperSelector: '.navbar',
  38. buttonSelector: '.navbar__toggle',
  39. menuSelector: '.navbar__menu',
  40. submenuSelector: '.navbar__submenu',
  41. mobileMenuSidebarLogoSelector: null,
  42. mobileMenuSidebarLogoUrl: null,
  43. relatedContainerForOverlayMenuSelector: null,
  44. // attributes
  45. ariaButtonAttribute: 'aria-expanded',
  46. mobileMenuId: 'js-navbar-mobile',
  47. // CSS classes
  48. separatorItemClass: 'is-separator',
  49. parentItemClass: 'has-submenu',
  50. submenuLeftPositionClass: 'is-left-submenu',
  51. submenuRightPositionClass: 'is-right-submenu',
  52. mobileMenuOverlayClass: 'navbar_mobile_overlay',
  53. mobileMenuSubmenuWrapperClass: 'navbar__submenu_wrapper',
  54. mobileMenuSidebarClass: 'navbar_mobile_sidebar',
  55. mobileMenuSidebarOverlayClass: 'navbar_mobile_sidebar__overlay',
  56. hiddenElementClass: 'is-hidden',
  57. openedMenuClass: 'is-active',
  58. noScrollClass: 'no-scroll',
  59. relatedContainerForOverlayMenuClass: 'is-visible'
  60. };
  61. var config = {};
  62. Object.keys(defaultConfig).forEach(function (key) {
  63. config[key] = defaultConfig[key];
  64. });
  65. if (typeof menuConfig === 'object') {
  66. Object.keys(menuConfig).forEach(function (key) {
  67. config[key] = menuConfig[key];
  68. });
  69. }
  70. /**
  71. * Calculate and apply correct submenu position (left or right)
  72. */
  73. function positionSubmenu(parentItem) {
  74. var submenu = parentItem.querySelector(config.submenuSelector);
  75. if (!submenu) return;
  76. var itemPosition = parentItem.getBoundingClientRect().left;
  77. var widthMultiplier = 2;
  78. if (parentItem.parentNode === document.querySelector(config.menuSelector)) {
  79. widthMultiplier = 1;
  80. }
  81. if (config.submenuWidth !== 'auto') {
  82. var submenuPotentialPosition = itemPosition + (config.submenuWidth * widthMultiplier);
  83. if (window.innerWidth < submenuPotentialPosition) {
  84. submenu.classList.remove(config.submenuLeftPositionClass);
  85. submenu.classList.add(config.submenuRightPositionClass);
  86. } else {
  87. submenu.classList.remove(config.submenuRightPositionClass);
  88. submenu.classList.add(config.submenuLeftPositionClass);
  89. }
  90. } else {
  91. var submenuPotentialPosition = 0;
  92. var submenuPosition = 0;
  93. if (widthMultiplier === 1) {
  94. submenuPotentialPosition = itemPosition + submenu.clientWidth;
  95. } else {
  96. submenuPotentialPosition = itemPosition + parentItem.clientWidth + submenu.clientWidth;
  97. }
  98. if (window.innerWidth < submenuPotentialPosition) {
  99. submenu.classList.remove(config.submenuLeftPositionClass);
  100. submenu.classList.add(config.submenuRightPositionClass);
  101. submenuPosition = -1 * submenu.clientWidth;
  102. submenu.removeAttribute('style');
  103. if (widthMultiplier === 1) {
  104. submenuPosition = 0;
  105. submenu.style.right = submenuPosition + 'px';
  106. } else {
  107. submenu.style.right = parentItem.clientWidth + 'px';
  108. }
  109. } else {
  110. submenu.classList.remove(config.submenuRightPositionClass);
  111. submenu.classList.add(config.submenuLeftPositionClass);
  112. submenuPosition = parentItem.clientWidth;
  113. if (widthMultiplier === 1) {
  114. submenuPosition = 0;
  115. }
  116. submenu.removeAttribute('style');
  117. submenu.style.left = submenuPosition + 'px';
  118. }
  119. }
  120. submenu.setAttribute('aria-hidden', false);
  121. }
  122. /**
  123. * Keyboard navigation for submenus
  124. */
  125. function initKeyboardNavigation() {
  126. // Make separator spans inside has-submenu focusable (fallback if HBS didn't set it)
  127. var separatorSpans = document.querySelectorAll(
  128. config.wrapperSelector + ' .' + config.parentItemClass + ' > span.' + config.separatorItemClass
  129. );
  130. for (var i = 0; i < separatorSpans.length; i++) {
  131. if (!separatorSpans[i].hasAttribute('tabindex')) {
  132. separatorSpans[i].setAttribute('tabindex', '0');
  133. separatorSpans[i].setAttribute('role', 'button');
  134. }
  135. }
  136. // Keyboard events on all submenu parents (desktop menu)
  137. var allParents = document.querySelectorAll(
  138. config.wrapperSelector + ' ' + config.menuSelector + ' .' + config.parentItemClass
  139. );
  140. for (var i = 0; i < allParents.length; i++) {
  141. (function (parent) {
  142. parent.addEventListener('keydown', function (e) {
  143. var target = e.target;
  144. var submenu = parent.querySelector(config.submenuSelector);
  145. if (!submenu) return;
  146. // Only handle events on direct trigger (a or span) of this parent
  147. if (target.parentNode !== parent) return;
  148. // Enter or Space — toggle submenu
  149. if (e.key === 'Enter' || e.key === ' ') {
  150. if (target.tagName === 'SPAN' && target.classList.contains(config.separatorItemClass)) {
  151. e.preventDefault();
  152. e.stopPropagation();
  153. var isHidden = submenu.getAttribute('aria-hidden') !== 'false';
  154. if (isHidden) {
  155. // Use the same positioning logic as hover
  156. parent.classList.remove('is-submenu-closed');
  157. positionSubmenu(parent);
  158. target.setAttribute('aria-expanded', 'true');
  159. // Focus first link in submenu
  160. var firstLink = submenu.querySelector('a, span[tabindex]');
  161. if (firstLink) firstLink.focus();
  162. } else {
  163. parent.classList.add('is-submenu-closed');
  164. submenu.setAttribute('aria-hidden', 'true');
  165. submenu.removeAttribute('style');
  166. target.setAttribute('aria-expanded', 'false');
  167. }
  168. }
  169. }
  170. // Escape — close submenu and return focus
  171. if (e.key === 'Escape') {
  172. if (submenu.getAttribute('aria-hidden') !== 'true') {
  173. parent.classList.add('is-submenu-closed');
  174. submenu.setAttribute('aria-hidden', 'true');
  175. submenu.removeAttribute('style');
  176. var trigger = parent.querySelector('a, span[tabindex="0"]');
  177. if (trigger) {
  178. trigger.setAttribute('aria-expanded', 'false');
  179. trigger.focus();
  180. }
  181. e.stopPropagation();
  182. }
  183. }
  184. });
  185. // Focus listener: show submenu and position on keyboard focus
  186. var trigger = parent.querySelector('a[aria-haspopup], span[aria-haspopup]');
  187. if (trigger) {
  188. trigger.addEventListener('focus', function() {
  189. parent.classList.remove('is-submenu-closed');
  190. positionSubmenu(parent);
  191. trigger.setAttribute('aria-expanded', 'true');
  192. });
  193. // ArrowDown: open submenu and focus first item
  194. trigger.addEventListener('keydown', function(e) {
  195. if (e.key === 'ArrowDown') {
  196. e.preventDefault();
  197. var submenu = parent.querySelector(config.submenuSelector);
  198. if (submenu) {
  199. parent.classList.remove('is-submenu-closed');
  200. positionSubmenu(parent);
  201. trigger.setAttribute('aria-expanded', 'true');
  202. var firstLink = submenu.querySelector('a, span[tabindex]');
  203. if (firstLink) firstLink.focus();
  204. }
  205. }
  206. });
  207. }
  208. // Clear keyboard-close override on mouseenter (hover after Escape)
  209. parent.addEventListener('mouseenter', function() {
  210. parent.classList.remove('is-submenu-closed');
  211. });
  212. // Clean up when focus leaves parent entirely
  213. parent.addEventListener('focusout', function() {
  214. setTimeout(function() {
  215. if (!parent.contains(document.activeElement)) {
  216. parent.classList.remove('is-submenu-closed');
  217. var submenu = parent.querySelector(config.submenuSelector);
  218. if (submenu) {
  219. submenu.setAttribute('aria-hidden', 'true');
  220. submenu.removeAttribute('style');
  221. }
  222. var t = parent.querySelector('a[aria-haspopup], span[aria-haspopup]');
  223. if (t) t.setAttribute('aria-expanded', 'false');
  224. }
  225. }, 0);
  226. });
  227. })(allParents[i]);
  228. }
  229. // Escape from inside submenu — bubble up and close
  230. var allSubmenus = document.querySelectorAll(
  231. config.wrapperSelector + ' ' + config.menuSelector + ' ' + config.submenuSelector
  232. );
  233. for (var i = 0; i < allSubmenus.length; i++) {
  234. (function (submenu) {
  235. submenu.addEventListener('keydown', function (e) {
  236. if (e.key === 'Escape') {
  237. submenu.setAttribute('aria-hidden', 'true');
  238. submenu.removeAttribute('style');
  239. var parentItem = submenu.closest('.' + config.parentItemClass);
  240. if (parentItem) {
  241. parentItem.classList.add('is-submenu-closed');
  242. var trigger = parentItem.querySelector('a, span[tabindex="0"]');
  243. if (trigger) {
  244. trigger.setAttribute('aria-expanded', 'false');
  245. trigger.focus();
  246. }
  247. }
  248. e.stopPropagation();
  249. }
  250. });
  251. })(allSubmenus[i]);
  252. }
  253. }
  254. /**
  255. * Menu initializer
  256. */
  257. function init() {
  258. if (!document.querySelectorAll(config.wrapperSelector).length) {
  259. return;
  260. }
  261. initSubmenuPositions();
  262. if (config.mobileMenuMode === 'overlay') {
  263. initMobileMenuOverlay();
  264. } else if (config.mobileMenuMode === 'sidebar') {
  265. initMobileMenuSidebar();
  266. }
  267. initClosingMenuOnClickLink();
  268. initMobileMenuEscape();
  269. initMobileMenuFocusLeave();
  270. if (!config.isHoverMenu) {
  271. initAriaAttributes();
  272. }
  273. initKeyboardNavigation();
  274. };
  275. /**
  276. * Function responsible for the submenu positions
  277. */
  278. function initSubmenuPositions() {
  279. var submenuParents = document.querySelectorAll(config.wrapperSelector + ' .' + config.parentItemClass);
  280. for (var i = 0; i < submenuParents.length; i++) {
  281. var eventTrigger = config.isHoverMenu ? 'mouseenter' : 'click';
  282. submenuParents[i].addEventListener(eventTrigger, function () {
  283. positionSubmenu(this);
  284. });
  285. if (config.isHoverMenu) {
  286. submenuParents[i].addEventListener('mouseleave', function () {
  287. var submenu = this.querySelector(config.submenuSelector);
  288. submenu.removeAttribute('style');
  289. submenu.setAttribute('aria-hidden', true);
  290. });
  291. }
  292. // Keyboard: position submenu on focusin (Tab navigation)
  293. submenuParents[i].addEventListener('focusin', function () {
  294. positionSubmenu(this);
  295. });
  296. submenuParents[i].addEventListener('focusout', function (e) {
  297. var self = this;
  298. // Delay to check if focus moved to another child inside this parent
  299. setTimeout(function () {
  300. if (!self.contains(document.activeElement)) {
  301. var submenu = self.querySelector(config.submenuSelector);
  302. if (submenu) {
  303. submenu.removeAttribute('style');
  304. submenu.setAttribute('aria-hidden', true);
  305. // Reset aria-expanded on trigger
  306. var trigger = self.querySelector('a, span[tabindex="0"]');
  307. if (trigger) {
  308. trigger.setAttribute('aria-expanded', 'false');
  309. }
  310. }
  311. }
  312. }, 0);
  313. });
  314. }
  315. }
  316. /**
  317. * Function used to init mobile menu - overlay mode
  318. */
  319. /**
  320. * The mobile menu (overlay or sidebar) is a copy of the header menu appended to the body, so it
  321. * gets its own landmark: a nav named by the toggle that opens it (its aria-label, "Menu"); the
  322. * template names the header nav differently ("Main navigation"), so the two landmarks stay
  323. * distinct while the menu is open. While closed it is inert, out of reach of
  324. * the keyboard and the screen reader whatever the theme's CSS does to hide it; the toggle points
  325. * at it (aria-controls, aria-expanded); opening moves the focus to its first item, Escape
  326. * closes it and gives the focus back to the toggle, and the focus leaving it (Tab past the last
  327. * item, to anything but the toggle) closes it too, so nobody tabs on under the overlay.
  328. */
  329. function prepareMobileMenu(menuWrapper, button) {
  330. var headerNav = document.querySelector(config.wrapperSelector);
  331. var label = (button && button.getAttribute('aria-label')) || (headerNav && headerNav.getAttribute('aria-label')) || 'Menu';
  332. var withId = menuWrapper.querySelectorAll('[id]');
  333. for (var i = 0; i < withId.length; i++) {
  334. withId[i].removeAttribute('id');
  335. }
  336. menuWrapper.id = config.mobileMenuId;
  337. menuWrapper.setAttribute('aria-label', label);
  338. menuWrapper.inert = true;
  339. if (button) {
  340. button.setAttribute('aria-controls', config.mobileMenuId);
  341. button.setAttribute(config.ariaButtonAttribute, 'false');
  342. }
  343. }
  344. function setMobileMenuOpen(menuWrapper, open) {
  345. menuWrapper.inert = !open;
  346. if (open) {
  347. var first = menuWrapper.querySelector('a[href], button, [role="button"][tabindex]');
  348. if (first) {
  349. first.focus({ preventScroll: true });
  350. }
  351. }
  352. }
  353. function initMobileMenuEscape() {
  354. document.addEventListener('keydown', function (e) {
  355. var button = document.querySelector(config.buttonSelector);
  356. if (e.key !== 'Escape' || !button || !button.classList.contains(config.openedMenuClass)) {
  357. return;
  358. }
  359. closeMenu(null, true);
  360. button.focus();
  361. });
  362. }
  363. function initMobileMenuFocusLeave() {
  364. document.addEventListener('focusin', function (e) {
  365. var button = document.querySelector(config.buttonSelector);
  366. var menuWrapper = document.getElementById(config.mobileMenuId);
  367. if (!button || !menuWrapper || !button.classList.contains(config.openedMenuClass)) {
  368. return;
  369. }
  370. if (menuWrapper.contains(e.target) || button.contains(e.target)) {
  371. return;
  372. }
  373. closeMenu(null, true);
  374. });
  375. }
  376. function initMobileMenuOverlay() {
  377. var menuWrapper = document.createElement('nav');
  378. menuWrapper.classList.add(config.mobileMenuOverlayClass);
  379. menuWrapper.classList.add(config.hiddenElementClass);
  380. var menuContentHTML = document.querySelector(config.menuSelector).outerHTML;
  381. menuWrapper.innerHTML = menuContentHTML;
  382. document.body.appendChild(menuWrapper);
  383. // Init toggle submenus
  384. if (config.mobileMenuExpandableSubmenus) {
  385. wrapSubmenusIntoContainer(menuWrapper);
  386. initToggleSubmenu(menuWrapper);
  387. } else {
  388. setAriaForSubmenus(menuWrapper);
  389. }
  390. // Init button events
  391. var button = document.querySelector(config.buttonSelector);
  392. prepareMobileMenu(menuWrapper, button);
  393. button.addEventListener('click', function () {
  394. var relatedContainer = document.querySelector(config.relatedContainerForOverlayMenuSelector);
  395. menuWrapper.classList.toggle(config.hiddenElementClass);
  396. button.classList.toggle(config.openedMenuClass);
  397. button.setAttribute(config.ariaButtonAttribute, button.classList.contains(config.openedMenuClass));
  398. setMobileMenuOpen(menuWrapper, button.classList.contains(config.openedMenuClass));
  399. if (button.classList.contains(config.openedMenuClass)) {
  400. document.documentElement.classList.add(config.noScrollClass);
  401. if (relatedContainer) {
  402. relatedContainer.classList.add(config.relatedContainerForOverlayMenuClass);
  403. }
  404. } else {
  405. document.documentElement.classList.remove(config.noScrollClass);
  406. if (relatedContainer) {
  407. relatedContainer.classList.remove(config.relatedContainerForOverlayMenuClass);
  408. }
  409. }
  410. });
  411. }
  412. /**
  413. * Function used to init mobile menu - sidebar mode
  414. */
  415. function initMobileMenuSidebar() {
  416. // Create menu structure
  417. var menuWrapper = document.createElement('nav');
  418. menuWrapper.classList.add(config.mobileMenuSidebarClass);
  419. menuWrapper.classList.add(config.hiddenElementClass);
  420. var menuContentHTML = '';
  421. if (config.mobileMenuSidebarLogoSelector !== null) {
  422. menuContentHTML = document.querySelector(config.mobileMenuSidebarLogoSelector).outerHTML;
  423. } else if (config.mobileMenuSidebarLogoUrl !== null) {
  424. menuContentHTML = '<img src="' + config.mobileMenuSidebarLogoUrl + '" alt="" />';
  425. }
  426. menuContentHTML += document.querySelector(config.menuSelector).outerHTML;
  427. menuWrapper.innerHTML = menuContentHTML;
  428. var menuOverlay = document.createElement('div');
  429. menuOverlay.classList.add(config.mobileMenuSidebarOverlayClass);
  430. menuOverlay.classList.add(config.hiddenElementClass);
  431. document.body.appendChild(menuOverlay);
  432. document.body.appendChild(menuWrapper);
  433. // Init toggle submenus
  434. if (config.mobileMenuExpandableSubmenus) {
  435. wrapSubmenusIntoContainer(menuWrapper);
  436. initToggleSubmenu(menuWrapper);
  437. } else {
  438. setAriaForSubmenus(menuWrapper);
  439. }
  440. // Menu events
  441. menuWrapper.addEventListener('click', function (e) {
  442. e.stopPropagation();
  443. });
  444. menuOverlay.addEventListener('click', function () {
  445. menuWrapper.classList.add(config.hiddenElementClass);
  446. menuOverlay.classList.add(config.hiddenElementClass);
  447. button.classList.remove(config.openedMenuClass);
  448. button.setAttribute(config.ariaButtonAttribute, false);
  449. document.documentElement.classList.remove(config.noScrollClass);
  450. setMobileMenuOpen(menuWrapper, false);
  451. });
  452. // Init button events
  453. var button = document.querySelector(config.buttonSelector);
  454. prepareMobileMenu(menuWrapper, button);
  455. button.addEventListener('click', function () {
  456. menuWrapper.classList.toggle(config.hiddenElementClass);
  457. menuOverlay.classList.toggle(config.hiddenElementClass);
  458. button.classList.toggle(config.openedMenuClass);
  459. button.setAttribute(config.ariaButtonAttribute, button.classList.contains(config.openedMenuClass));
  460. document.documentElement.classList.toggle(config.noScrollClass);
  461. setMobileMenuOpen(menuWrapper, button.classList.contains(config.openedMenuClass));
  462. });
  463. }
  464. /**
  465. * Set aria-hidden="false" for submenus
  466. */
  467. function setAriaForSubmenus(menuWrapper) {
  468. var submenus = menuWrapper.querySelectorAll(config.submenuSelector);
  469. for (var i = 0; i < submenus.length; i++) {
  470. submenus[i].setAttribute('aria-hidden', false);
  471. }
  472. }
  473. /**
  474. * Wrap all submenus into div wrappers
  475. */
  476. function wrapSubmenusIntoContainer(menuWrapper) {
  477. var submenus = menuWrapper.querySelectorAll(config.submenuSelector);
  478. for (var i = 0; i < submenus.length; i++) {
  479. var submenuWrapper = document.createElement('div');
  480. submenuWrapper.classList.add(config.mobileMenuSubmenuWrapperClass);
  481. submenus[i].parentNode.insertBefore(submenuWrapper, submenus[i]);
  482. submenuWrapper.appendChild(submenus[i]);
  483. }
  484. }
  485. /**
  486. * Initialize submenu toggle events
  487. */
  488. function initToggleSubmenu(menuWrapper) {
  489. // Init parent menu item events
  490. var parents = menuWrapper.querySelectorAll('.' + config.parentItemClass);
  491. for (var i = 0; i < parents.length; i++) {
  492. // Add toggle events
  493. parents[i].addEventListener('click', function (e) {
  494. e.stopPropagation();
  495. var submenu = this.querySelector('.' + config.mobileMenuSubmenuWrapperClass);
  496. var content = submenu.firstElementChild;
  497. if (submenu.classList.contains(config.openedMenuClass)) {
  498. var height = content.clientHeight;
  499. submenu.style.height = height + 'px';
  500. setTimeout(function () {
  501. submenu.style.height = '0px';
  502. }, 0);
  503. setTimeout(function () {
  504. submenu.removeAttribute('style');
  505. submenu.classList.remove(config.openedMenuClass);
  506. }, config.animationSpeed);
  507. content.setAttribute('aria-hidden', true);
  508. this.children[0].setAttribute('aria-expanded', false);
  509. } else {
  510. var height = content.clientHeight;
  511. submenu.classList.add(config.openedMenuClass);
  512. submenu.style.height = '0px';
  513. setTimeout(function () {
  514. submenu.style.height = height + 'px';
  515. }, 0);
  516. setTimeout(function () {
  517. submenu.removeAttribute('style');
  518. }, config.animationSpeed);
  519. content.setAttribute('aria-hidden', false);
  520. this.children[0].setAttribute('aria-expanded', true);
  521. }
  522. });
  523. // Block links
  524. var childNodes = parents[i].children;
  525. for (var j = 0; j < childNodes.length; j++) {
  526. if (childNodes[j].tagName === 'A') {
  527. childNodes[j].addEventListener('click', function (e) {
  528. var lastClick = parseInt(this.getAttribute('data-last-click'), 10);
  529. var currentTime = +new Date();
  530. if (isNaN(lastClick)) {
  531. e.preventDefault();
  532. this.setAttribute('data-last-click', currentTime);
  533. } else if (lastClick + config.doubleClickTime <= currentTime) {
  534. e.preventDefault();
  535. this.setAttribute('data-last-click', currentTime);
  536. } else if (lastClick + config.doubleClickTime > currentTime) {
  537. e.stopPropagation();
  538. closeMenu(this, true);
  539. }
  540. });
  541. }
  542. }
  543. }
  544. }
  545. /**
  546. * Set aria-* attributes according to the current activity state
  547. */
  548. function initAriaAttributes() {
  549. var allAriaElements = document.querySelectorAll(config.wrapperSelector + ' ' + '*[aria-hidden]');
  550. for (var i = 0; i < allAriaElements.length; i++) {
  551. var ariaElement = allAriaElements[i];
  552. if (
  553. ariaElement.parentNode.classList.contains('active') ||
  554. ariaElement.parentNode.classList.contains('active-parent')
  555. ) {
  556. ariaElement.setAttribute('aria-hidden', 'false');
  557. ariaElement.parentNode.firstElementChild.setAttribute('aria-expanded', true);
  558. } else {
  559. ariaElement.setAttribute('aria-hidden', 'true');
  560. ariaElement.parentNode.firstElementChild.setAttribute('aria-expanded', false);
  561. }
  562. }
  563. }
  564. /**
  565. * Close menu on click link
  566. */
  567. function initClosingMenuOnClickLink() {
  568. var links = document.querySelectorAll(config.menuSelector + ' a');
  569. for (var i = 0; i < links.length; i++) {
  570. if (links[i].parentNode.classList.contains(config.parentItemClass)) {
  571. continue;
  572. }
  573. links[i].addEventListener('click', function (e) {
  574. closeMenu(this, false);
  575. });
  576. }
  577. }
  578. /**
  579. * Close menu
  580. */
  581. function closeMenu(clickedLink, forceClose) {
  582. if (forceClose === false) {
  583. if (clickedLink.parentNode.classList.contains(config.parentItemClass)) {
  584. return;
  585. }
  586. }
  587. var relatedContainer = document.querySelector(config.relatedContainerForOverlayMenuSelector);
  588. var button = document.querySelector(config.buttonSelector);
  589. var menuWrapper = document.querySelector('.' + config.mobileMenuOverlayClass);
  590. if (!menuWrapper) {
  591. menuWrapper = document.querySelector('.' + config.mobileMenuSidebarClass);
  592. }
  593. menuWrapper.classList.add(config.hiddenElementClass);
  594. button.classList.remove(config.openedMenuClass);
  595. button.setAttribute(config.ariaButtonAttribute, false);
  596. document.documentElement.classList.remove(config.noScrollClass);
  597. setMobileMenuOpen(menuWrapper, false);
  598. if (relatedContainer) {
  599. relatedContainer.classList.remove(config.relatedContainerForOverlayMenuClass);
  600. }
  601. var menuOverlay = document.querySelector('.' + config.mobileMenuSidebarOverlayClass);
  602. if (menuOverlay) {
  603. menuOverlay.classList.add(config.hiddenElementClass);
  604. }
  605. }
  606. /**
  607. * Run menu scripts
  608. */
  609. init();
  610. })(window.publiiThemeMenuConfig);
  611. // Load search input area
  612. const searchButton = document.querySelector('.js-search-btn');
  613. const searchOverlay = document.querySelector('.js-search-overlay');
  614. if (searchButton && searchOverlay) {
  615. searchButton.addEventListener('click', (event) => {
  616. event.stopPropagation();
  617. searchOverlay.classList.toggle('expanded');
  618. if (searchOverlay.classList.contains('expanded')) {
  619. setTimeout(() => {
  620. const element = searchOverlay.querySelector('input, button');
  621. if (element) {
  622. element.focus();
  623. }
  624. }, 60);
  625. }
  626. });
  627. searchOverlay.addEventListener('click', (event) => {
  628. event.stopPropagation();
  629. });
  630. document.body.addEventListener('click', () => {
  631. searchOverlay.classList.remove('expanded');
  632. });
  633. }
  634. // Share buttons pop-up
  635. (function () {
  636. // share popup
  637. const shareButton = document.querySelector('.js-content__share-button');
  638. const sharePopup = document.querySelector('.js-content__share-popup');
  639. if (shareButton && sharePopup) {
  640. sharePopup.addEventListener('click', function (e) {
  641. e.stopPropagation();
  642. });
  643. shareButton.addEventListener('click', function (e) {
  644. e.preventDefault();
  645. e.stopPropagation();
  646. sharePopup.classList.toggle('is-visible');
  647. });
  648. document.body.addEventListener('click', function () {
  649. sharePopup.classList.remove('is-visible');
  650. });
  651. }
  652. // link selector and pop-up window size
  653. const Config = {
  654. Link: ".js-share",
  655. Width: 500,
  656. Height: 500
  657. };
  658. // add handler to links
  659. const shareLinks = document.querySelectorAll(Config.Link);
  660. shareLinks.forEach(link => {
  661. link.addEventListener('click', PopupHandler);
  662. });
  663. // create popup
  664. function PopupHandler(e) {
  665. e.preventDefault();
  666. const target = e.target.closest(Config.Link);
  667. if (!target) return;
  668. // hide share popup
  669. if (sharePopup) {
  670. sharePopup.classList.remove('is-visible');
  671. }
  672. // popup position
  673. const px = Math.floor((window.innerWidth - Config.Width) / 2);
  674. const py = Math.floor((window.innerHeight - Config.Height) / 2);
  675. // open popup
  676. const linkHref = target.href;
  677. const popup = window.open(linkHref, "social", `
  678. width=${Config.Width},
  679. height=${Config.Height},
  680. left=${px},
  681. top=${py},
  682. location=0,
  683. menubar=0,
  684. toolbar=0,
  685. status=0,
  686. scrollbars=1,
  687. resizable=1
  688. `);
  689. if (popup) {
  690. popup.focus();
  691. }
  692. }
  693. })();
  694. // Back to top
  695. document.addEventListener('DOMContentLoaded', () => {
  696. const backToTopButton = document.getElementById('backToTop');
  697. if (backToTopButton) {
  698. const backToTopScrollFunction = () => {
  699. if (document.body.scrollTop > 400 || document.documentElement.scrollTop > 400) {
  700. backToTopButton.classList.add('is-visible');
  701. } else {
  702. backToTopButton.classList.remove('is-visible');
  703. }
  704. };
  705. const backToTopFunction = () => {
  706. window.scrollTo({
  707. top: 0,
  708. behavior: 'smooth'
  709. });
  710. };
  711. window.addEventListener('scroll', backToTopScrollFunction);
  712. backToTopButton.addEventListener('click', backToTopFunction);
  713. }
  714. });
  715. // Responsive embeds script
  716. (function () {
  717. let wrappers = document.querySelectorAll('.post__video, .post__iframe');
  718. for (let i = 0; i < wrappers.length; i++) {
  719. let embed = wrappers[i].querySelector('iframe, embed, video, object');
  720. if (!embed) {
  721. continue;
  722. }
  723. if (embed.getAttribute('data-responsive') === 'false') {
  724. continue;
  725. }
  726. let w = embed.getAttribute('width');
  727. let h = embed.getAttribute('height');
  728. let ratio = false;
  729. if (!w || !h) {
  730. continue;
  731. }
  732. if (w.indexOf('%') > -1 && h.indexOf('%') > -1) { // percentage mode
  733. w = parseFloat(w.replace('%', ''));
  734. h = parseFloat(h.replace('%', ''));
  735. ratio = h / w;
  736. } else if (w.indexOf('%') === -1 && h.indexOf('%') === -1) { // pixels mode
  737. w = parseInt(w, 10);
  738. h = parseInt(h, 10);
  739. ratio = h / w;
  740. }
  741. if (ratio !== false) {
  742. let ratioValue = (ratio * 100) + '%';
  743. wrappers[i].setAttribute('style', '--embed-aspect-ratio:' + ratioValue);
  744. }
  745. }
  746. })();