v3.1.0.0
FlatlyPage
- (function () {
- 'use strict';
-
- var isPreview = document.documentElement.hasAttribute('data-flatly-preview');
- var reducedMotion = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : { matches: false };
-
- function storageGet(key) {
- try { return window.localStorage.getItem(key); } catch (e) { return null; }
- }
-
- function storageSet(key, value) {
- try { window.localStorage.setItem(key, value); } catch (e) {}
- }
-
- function once(el, name) {
- var flag = 'flatly' + name + 'Ready';
- if (el.dataset[flag]) return false;
- el.dataset[flag] = '1';
- return true;
- }
-
- function initTabs(root) {
- if (!once(root, 'Tabs')) return;
-
- var tabs = Array.prototype.slice.call(root.querySelectorAll('[role="tab"]'));
- var panels = tabs.map(function (tab) {
- return document.getElementById(tab.getAttribute('aria-controls'));
- });
- if (!tabs.length) return;
-
- function activate(index, focus) {
- tabs.forEach(function (tab, i) {
- var active = i === index;
- tab.classList.toggle('is-active', active);
- tab.setAttribute('aria-selected', active ? 'true' : 'false');
- tab.tabIndex = active ? 0 : -1;
- if (panels[i]) {
- panels[i].classList.toggle('is-active', active);
- panels[i].hidden = !active;
- }
- });
- if (focus) tabs[index].focus();
- }
-
- tabs.forEach(function (tab, index) {
- tab.addEventListener('click', function () {
- activate(index, false);
- });
- tab.addEventListener('keydown', function (event) {
- var target = null;
- if (event.key === 'ArrowRight' || event.key === 'ArrowDown') target = (index + 1) % tabs.length;
- if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') target = (index - 1 + tabs.length) % tabs.length;
- if (event.key === 'Home') target = 0;
- if (event.key === 'End') target = tabs.length - 1;
- if (target === null) return;
- event.preventDefault();
- activate(target, true);
- });
- });
-
- var initial = tabs.findIndex(function (tab) { return tab.getAttribute('aria-selected') === 'true'; });
- activate(initial < 0 ? 0 : initial, false);
- root.classList.add('is-ready');
- }
-
- function initCarousel(root) {
- if (!once(root, 'Carousel')) return;
-
- var track = root.querySelector('.carousel-track');
- if (!track) return;
- var slides = Array.prototype.slice.call(track.children);
- var dots = Array.prototype.slice.call(root.querySelectorAll('[data-carousel-dot]'));
- var prev = root.querySelector('[data-carousel-prev]');
- var next = root.querySelector('[data-carousel-next]');
- var autoplay = root.getAttribute('data-autoplay') === '1' && slides.length > 1;
- var interval = Math.max(2000, parseInt(root.getAttribute('data-interval'), 10) || 5000);
- var current = 0;
- var timer = 0;
- var paused = false;
- var visible = true;
- var scrollFrame = 0;
-
- function indexFromScroll() {
- var width = track.clientWidth || 1;
- return Math.max(0, Math.min(slides.length - 1, Math.round(track.scrollLeft / width)));
- }
-
- function sync(index) {
- current = index;
- dots.forEach(function (dot, i) {
- var active = i === index;
- dot.classList.toggle('is-active', active);
- if (active) {
- dot.setAttribute('aria-current', 'true');
- } else {
- dot.removeAttribute('aria-current');
- }
- });
- slides.forEach(function (slide, i) {
- slide.setAttribute('aria-hidden', i === index ? 'false' : 'true');
- Array.prototype.forEach.call(slide.querySelectorAll('a, button'), function (el) {
- if (i === index) {
- el.removeAttribute('tabindex');
- } else {
- el.setAttribute('tabindex', '-1');
- }
- });
- });
- }
-
- function goTo(index) {
- var total = slides.length;
- var target = ((index % total) + total) % total;
- track.scrollTo({ left: target * track.clientWidth, behavior: reducedMotion.matches ? 'auto' : 'smooth' });
- sync(target);
- }
-
- function stop() {
- window.clearTimeout(timer);
- timer = 0;
- }
-
- function schedule() {
- stop();
- if (!autoplay || paused || !visible || document.hidden || reducedMotion.matches) return;
- timer = window.setTimeout(function () {
- goTo(current + 1);
- schedule();
- }, interval);
- }
-
- if (prev) prev.addEventListener('click', function () { goTo(current - 1); schedule(); });
- if (next) next.addEventListener('click', function () { goTo(current + 1); schedule(); });
- dots.forEach(function (dot) {
- dot.addEventListener('click', function () {
- goTo(parseInt(dot.getAttribute('data-carousel-dot'), 10) || 0);
- schedule();
- });
- });
-
- root.addEventListener('keydown', function (event) {
- if (event.target.closest && event.target.closest('input, textarea, select')) return;
- if (event.key === 'ArrowLeft') { event.preventDefault(); goTo(current - 1); schedule(); }
- if (event.key === 'ArrowRight') { event.preventDefault(); goTo(current + 1); schedule(); }
- });
-
- track.addEventListener('scroll', function () {
- if (scrollFrame) return;
- scrollFrame = window.requestAnimationFrame(function () {
- scrollFrame = 0;
- var index = indexFromScroll();
- if (index !== current) sync(index);
- });
- }, { passive: true });
-
- root.addEventListener('mouseenter', function () { paused = true; stop(); });
- root.addEventListener('mouseleave', function () { paused = false; schedule(); });
- root.addEventListener('focusin', function () { paused = true; stop(); });
- root.addEventListener('focusout', function (event) {
- if (!root.contains(event.relatedTarget)) {
- paused = false;
- schedule();
- }
- });
- track.addEventListener('pointerdown', function () { stop(); });
- track.addEventListener('pointerup', function () { schedule(); });
- document.addEventListener('visibilitychange', schedule);
- if (reducedMotion.addEventListener) reducedMotion.addEventListener('change', schedule);
-
- window.addEventListener('resize', function () {
- track.scrollTo({ left: current * track.clientWidth, behavior: 'auto' });
- });
-
- if ('IntersectionObserver' in window) {
- new IntersectionObserver(function (entries) {
- visible = entries[0] ? entries[0].isIntersecting : true;
- schedule();
- }, { threshold: 0.25 }).observe(root);
- }
-
- sync(0);
- root.classList.add('is-ready');
- schedule();
- }
-
- function initAnnouncement(bar) {
- if (!once(bar, 'Announcement')) return;
-
- var key = 'flatly-announcement:' + bar.getAttribute('data-announcement');
- if (!isPreview && storageGet(key)) {
- bar.hidden = true;
- return;
- }
-
- var close = bar.querySelector('[data-announcement-close]');
- if (!close) return;
- close.addEventListener('click', function () {
- bar.hidden = true;
- if (!isPreview) storageSet(key, String(Date.now()));
- });
- }
-
- function pad(value) {
- return value < 10 ? '0' + value : String(value);
- }
-
- function initCountdown(section) {
- if (!once(section, 'Countdown')) return;
-
- var target = parseInt(section.getAttribute('data-target'), 10);
- if (!isFinite(target)) return;
-
- var action = section.getAttribute('data-expired') || 'zeros';
- var timer = section.querySelector('.countdown-timer');
- var message = section.querySelector('.countdown-expired');
- var fields = {
- days: section.querySelector('[data-days]'),
- hours: section.querySelector('[data-hours]'),
- minutes: section.querySelector('[data-minutes]'),
- seconds: section.querySelector('[data-seconds]')
- };
- var handle = 0;
-
- function write(days, hours, minutes, seconds) {
- if (fields.days) fields.days.textContent = pad(days);
- if (fields.hours) fields.hours.textContent = pad(hours);
- if (fields.minutes) fields.minutes.textContent = pad(minutes);
- if (fields.seconds) fields.seconds.textContent = pad(seconds);
- }
-
- function finish() {
- window.clearInterval(handle);
- write(0, 0, 0, 0);
- if (action === 'hide' && !isPreview) {
- section.hidden = true;
- } else if (action === 'message' && message) {
- if (timer) timer.hidden = true;
- message.hidden = false;
- }
- }
-
- function tick() {
- var distance = target - Date.now();
- if (distance <= 0) {
- finish();
- return;
- }
- var total = Math.floor(distance / 1000);
- write(Math.floor(total / 86400), Math.floor((total % 86400) / 3600), Math.floor((total % 3600) / 60), total % 60);
- }
-
- tick();
- if (target > Date.now()) handle = window.setInterval(tick, 1000);
- }
-
- function init(scope) {
- var root = scope || document;
- Array.prototype.forEach.call(root.querySelectorAll('[data-tabs]'), initTabs);
- Array.prototype.forEach.call(root.querySelectorAll('[data-carousel]'), initCarousel);
- Array.prototype.forEach.call(root.querySelectorAll('[data-announcement]'), initAnnouncement);
- Array.prototype.forEach.call(root.querySelectorAll('[data-countdown]'), initCountdown);
- }
-
- window.FlatlyBlocks = { init: init };
-
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', function () { init(document); });
- } else {
- init(document);
- }
- })();
-