WebOrbiton
v3.1.0.0

FlatlyPage

272 lines · 10.4 KB
  1. (function () {
  2. 'use strict';
  3. ​
  4. var isPreview = document.documentElement.hasAttribute('data-flatly-preview');
  5. var reducedMotion = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : { matches: false };
  6. ​
  7. function storageGet(key) {
  8. try { return window.localStorage.getItem(key); } catch (e) { return null; }
  9. }
  10. ​
  11. function storageSet(key, value) {
  12. try { window.localStorage.setItem(key, value); } catch (e) {}
  13. }
  14. ​
  15. function once(el, name) {
  16. var flag = 'flatly' + name + 'Ready';
  17. if (el.dataset[flag]) return false;
  18. el.dataset[flag] = '1';
  19. return true;
  20. }
  21. ​
  22. function initTabs(root) {
  23. if (!once(root, 'Tabs')) return;
  24. ​
  25. var tabs = Array.prototype.slice.call(root.querySelectorAll('[role="tab"]'));
  26. var panels = tabs.map(function (tab) {
  27. return document.getElementById(tab.getAttribute('aria-controls'));
  28. });
  29. if (!tabs.length) return;
  30. ​
  31. function activate(index, focus) {
  32. tabs.forEach(function (tab, i) {
  33. var active = i === index;
  34. tab.classList.toggle('is-active', active);
  35. tab.setAttribute('aria-selected', active ? 'true' : 'false');
  36. tab.tabIndex = active ? 0 : -1;
  37. if (panels[i]) {
  38. panels[i].classList.toggle('is-active', active);
  39. panels[i].hidden = !active;
  40. }
  41. });
  42. if (focus) tabs[index].focus();
  43. }
  44. ​
  45. tabs.forEach(function (tab, index) {
  46. tab.addEventListener('click', function () {
  47. activate(index, false);
  48. });
  49. tab.addEventListener('keydown', function (event) {
  50. var target = null;
  51. if (event.key === 'ArrowRight' || event.key === 'ArrowDown') target = (index + 1) % tabs.length;
  52. if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') target = (index - 1 + tabs.length) % tabs.length;
  53. if (event.key === 'Home') target = 0;
  54. if (event.key === 'End') target = tabs.length - 1;
  55. if (target === null) return;
  56. event.preventDefault();
  57. activate(target, true);
  58. });
  59. });
  60. ​
  61. var initial = tabs.findIndex(function (tab) { return tab.getAttribute('aria-selected') === 'true'; });
  62. activate(initial < 0 ? 0 : initial, false);
  63. root.classList.add('is-ready');
  64. }
  65. ​
  66. function initCarousel(root) {
  67. if (!once(root, 'Carousel')) return;
  68. ​
  69. var track = root.querySelector('.carousel-track');
  70. if (!track) return;
  71. var slides = Array.prototype.slice.call(track.children);
  72. var dots = Array.prototype.slice.call(root.querySelectorAll('[data-carousel-dot]'));
  73. var prev = root.querySelector('[data-carousel-prev]');
  74. var next = root.querySelector('[data-carousel-next]');
  75. var autoplay = root.getAttribute('data-autoplay') === '1' && slides.length > 1;
  76. var interval = Math.max(2000, parseInt(root.getAttribute('data-interval'), 10) || 5000);
  77. var current = 0;
  78. var timer = 0;
  79. var paused = false;
  80. var visible = true;
  81. var scrollFrame = 0;
  82. ​
  83. function indexFromScroll() {
  84. var width = track.clientWidth || 1;
  85. return Math.max(0, Math.min(slides.length - 1, Math.round(track.scrollLeft / width)));
  86. }
  87. ​
  88. function sync(index) {
  89. current = index;
  90. dots.forEach(function (dot, i) {
  91. var active = i === index;
  92. dot.classList.toggle('is-active', active);
  93. if (active) {
  94. dot.setAttribute('aria-current', 'true');
  95. } else {
  96. dot.removeAttribute('aria-current');
  97. }
  98. });
  99. slides.forEach(function (slide, i) {
  100. slide.setAttribute('aria-hidden', i === index ? 'false' : 'true');
  101. Array.prototype.forEach.call(slide.querySelectorAll('a, button'), function (el) {
  102. if (i === index) {
  103. el.removeAttribute('tabindex');
  104. } else {
  105. el.setAttribute('tabindex', '-1');
  106. }
  107. });
  108. });
  109. }
  110. ​
  111. function goTo(index) {
  112. var total = slides.length;
  113. var target = ((index % total) + total) % total;
  114. track.scrollTo({ left: target * track.clientWidth, behavior: reducedMotion.matches ? 'auto' : 'smooth' });
  115. sync(target);
  116. }
  117. ​
  118. function stop() {
  119. window.clearTimeout(timer);
  120. timer = 0;
  121. }
  122. ​
  123. function schedule() {
  124. stop();
  125. if (!autoplay || paused || !visible || document.hidden || reducedMotion.matches) return;
  126. timer = window.setTimeout(function () {
  127. goTo(current + 1);
  128. schedule();
  129. }, interval);
  130. }
  131. ​
  132. if (prev) prev.addEventListener('click', function () { goTo(current - 1); schedule(); });
  133. if (next) next.addEventListener('click', function () { goTo(current + 1); schedule(); });
  134. dots.forEach(function (dot) {
  135. dot.addEventListener('click', function () {
  136. goTo(parseInt(dot.getAttribute('data-carousel-dot'), 10) || 0);
  137. schedule();
  138. });
  139. });
  140. ​
  141. root.addEventListener('keydown', function (event) {
  142. if (event.target.closest && event.target.closest('input, textarea, select')) return;
  143. if (event.key === 'ArrowLeft') { event.preventDefault(); goTo(current - 1); schedule(); }
  144. if (event.key === 'ArrowRight') { event.preventDefault(); goTo(current + 1); schedule(); }
  145. });
  146. ​
  147. track.addEventListener('scroll', function () {
  148. if (scrollFrame) return;
  149. scrollFrame = window.requestAnimationFrame(function () {
  150. scrollFrame = 0;
  151. var index = indexFromScroll();
  152. if (index !== current) sync(index);
  153. });
  154. }, { passive: true });
  155. ​
  156. root.addEventListener('mouseenter', function () { paused = true; stop(); });
  157. root.addEventListener('mouseleave', function () { paused = false; schedule(); });
  158. root.addEventListener('focusin', function () { paused = true; stop(); });
  159. root.addEventListener('focusout', function (event) {
  160. if (!root.contains(event.relatedTarget)) {
  161. paused = false;
  162. schedule();
  163. }
  164. });
  165. track.addEventListener('pointerdown', function () { stop(); });
  166. track.addEventListener('pointerup', function () { schedule(); });
  167. document.addEventListener('visibilitychange', schedule);
  168. if (reducedMotion.addEventListener) reducedMotion.addEventListener('change', schedule);
  169. ​
  170. window.addEventListener('resize', function () {
  171. track.scrollTo({ left: current * track.clientWidth, behavior: 'auto' });
  172. });
  173. ​
  174. if ('IntersectionObserver' in window) {
  175. new IntersectionObserver(function (entries) {
  176. visible = entries[0] ? entries[0].isIntersecting : true;
  177. schedule();
  178. }, { threshold: 0.25 }).observe(root);
  179. }
  180. ​
  181. sync(0);
  182. root.classList.add('is-ready');
  183. schedule();
  184. }
  185. ​
  186. function initAnnouncement(bar) {
  187. if (!once(bar, 'Announcement')) return;
  188. ​
  189. var key = 'flatly-announcement:' + bar.getAttribute('data-announcement');
  190. if (!isPreview && storageGet(key)) {
  191. bar.hidden = true;
  192. return;
  193. }
  194. ​
  195. var close = bar.querySelector('[data-announcement-close]');
  196. if (!close) return;
  197. close.addEventListener('click', function () {
  198. bar.hidden = true;
  199. if (!isPreview) storageSet(key, String(Date.now()));
  200. });
  201. }
  202. ​
  203. function pad(value) {
  204. return value < 10 ? '0' + value : String(value);
  205. }
  206. ​
  207. function initCountdown(section) {
  208. if (!once(section, 'Countdown')) return;
  209. ​
  210. var target = parseInt(section.getAttribute('data-target'), 10);
  211. if (!isFinite(target)) return;
  212. ​
  213. var action = section.getAttribute('data-expired') || 'zeros';
  214. var timer = section.querySelector('.countdown-timer');
  215. var message = section.querySelector('.countdown-expired');
  216. var fields = {
  217. days: section.querySelector('[data-days]'),
  218. hours: section.querySelector('[data-hours]'),
  219. minutes: section.querySelector('[data-minutes]'),
  220. seconds: section.querySelector('[data-seconds]')
  221. };
  222. var handle = 0;
  223. ​
  224. function write(days, hours, minutes, seconds) {
  225. if (fields.days) fields.days.textContent = pad(days);
  226. if (fields.hours) fields.hours.textContent = pad(hours);
  227. if (fields.minutes) fields.minutes.textContent = pad(minutes);
  228. if (fields.seconds) fields.seconds.textContent = pad(seconds);
  229. }
  230. ​
  231. function finish() {
  232. window.clearInterval(handle);
  233. write(0, 0, 0, 0);
  234. if (action === 'hide' && !isPreview) {
  235. section.hidden = true;
  236. } else if (action === 'message' && message) {
  237. if (timer) timer.hidden = true;
  238. message.hidden = false;
  239. }
  240. }
  241. ​
  242. function tick() {
  243. var distance = target - Date.now();
  244. if (distance <= 0) {
  245. finish();
  246. return;
  247. }
  248. var total = Math.floor(distance / 1000);
  249. write(Math.floor(total / 86400), Math.floor((total % 86400) / 3600), Math.floor((total % 3600) / 60), total % 60);
  250. }
  251. ​
  252. tick();
  253. if (target > Date.now()) handle = window.setInterval(tick, 1000);
  254. }
  255. ​
  256. function init(scope) {
  257. var root = scope || document;
  258. Array.prototype.forEach.call(root.querySelectorAll('[data-tabs]'), initTabs);
  259. Array.prototype.forEach.call(root.querySelectorAll('[data-carousel]'), initCarousel);
  260. Array.prototype.forEach.call(root.querySelectorAll('[data-announcement]'), initAnnouncement);
  261. Array.prototype.forEach.call(root.querySelectorAll('[data-countdown]'), initCountdown);
  262. }
  263. ​
  264. window.FlatlyBlocks = { init: init };
  265. ​
  266. if (document.readyState === 'loading') {
  267. document.addEventListener('DOMContentLoaded', function () { init(document); });
  268. } else {
  269. init(document);
  270. }
  271. })();
  272. ​