Newer
Older
tonitalia-theme / assets / js / singlepage.js
(function ($) {
	$(function () {
		var $nav = $('.singlepage-nav');

		function updateFixedHeaderHeight() {
			if (!$('body').hasClass('has-tents-fixed-header')) {
				document.documentElement.style.removeProperty('--tents-fixed-header-height');
				return;
			}

			var $fixedNavigation = $('.navigation-top');
			var height           = $fixedNavigation.length ? $fixedNavigation.outerHeight() : 0;

			document.documentElement.style.setProperty('--tents-fixed-header-height', height + 'px');
		}

		function setMenuOpen(isOpen) {
			$nav.toggleClass('is-open', isOpen);
			$nav.find('.singlepage-menu-toggle').attr('aria-expanded', isOpen ? 'true' : 'false');
			updateFixedHeaderHeight();
		}

		function closeMenu() {
			setMenuOpen(false);
		}

		function isMobileMenuViewport() {
			return window.matchMedia('(max-width: 782px)').matches;
		}

		updateFixedHeaderHeight();
		$(window).on('load resize orientationchange', updateFixedHeaderHeight);

		if ($('body').hasClass('has-tents-fixed-header')) {
			$('.navigation-top').addClass('site-navigation-fixed');
		}

		$nav.on('click', '.singlepage-menu-toggle', function () {
			setMenuOpen(!$nav.hasClass('is-open'));
		});

		$('#top-menu a').on('click', function () {
			if (isMobileMenuViewport()) {
				closeMenu();
			}
		});

		$(document).on('keydown', function (e) {
			if (e.key === 'Escape' && $nav.hasClass('is-open')) {
				closeMenu();
			}
		});

		$(document).on('click', function (e) {
			if (!$nav.hasClass('is-open') || !isMobileMenuViewport()) {
				return;
			}

			if ($(e.target).closest('.singlepage-nav').length) {
				return;
			}

			closeMenu();
		});

		function getSectionHashFromHref(href) {
			if (!href) {
				return '';
			}

			var hashIndex = href.indexOf('#');

			if (hashIndex === -1) {
				return '';
			}

			var hash = href.slice(hashIndex);

			return hash.indexOf('#section-') === 0 ? hash : '';
		}

		function scrollToSection(hash, animate) {
			var $target = $(hash);

			if (!$target.length) {
				return false;
			}

			updateFixedHeaderHeight();

			var navHeight  = $('.navigation-top').outerHeight() || 0;
			var offset     = navHeight + 20;
			var targetTop  = $target.offset().top - offset;
			var $scrollEl  = $('html, body');

			if (animate) {
				$scrollEl.animate({ scrollTop: targetTop }, 500);
			} else {
				$scrollEl.scrollTop(targetTop);
			}

			return true;
		}

		$('#top-menu > li > a').on('click', function (e) {
			var sectionHash = getSectionHashFromHref(this.getAttribute('href'));

			if (!sectionHash) {
				return;
			}

			if (!$('body').hasClass('home')) {
				return;
			}

			if (scrollToSection(sectionHash, true)) {
				e.preventDefault();

				if (window.history && window.history.replaceState) {
					window.history.replaceState(null, '', sectionHash);
				}
			}
		});

		function scrollToSectionHashOnLoad() {
			var sectionHash = getSectionHashFromHref(window.location.hash);

			if (!sectionHash || !$('body').hasClass('home')) {
				return;
			}

			window.setTimeout(function () {
				scrollToSection(sectionHash, false);
			}, 0);
		}

		scrollToSectionHashOnLoad();
		$(window).on('load', scrollToSectionHashOnLoad);
	});
})(jQuery);