@fancyapps/ui
Version:
Robust JavaScript UI Component Library
2 lines • 3.06 kB
JavaScript
/*! License details at fancyapps.com/license */
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.window=e.window||{}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=e=>typeof e==`object`&&!!e&&e.constructor===Object&&Object.prototype.toString.call(e)===`[object Object]`,n=(e,t=``)=>{e&&e.classList&&t.split(` `).forEach(t=>{t&&e.classList.add(t)})},r={autoStart:!0,pauseOnHover:!0,showProgressbar:!0,timeout:2e3};e.Autoplay=()=>{let e,i,a=!1,o=!1,s=!1,c=null;function l(n){let i=e?.getOptions().Autoplay,a=(t(i)?{...r,...i}:r)[n];return a&&typeof a==`function`&&e?a(e):a}function u(){let t=e?.getPage()?.slides||[];for(let e of t)if(e.state===0)return!0;return!1}function d(){clearTimeout(i),i=void 0}function f(){if(!e||!a||s||o||i||!e.isSettled()||u())return;h();let t=l(`timeout`);i=setTimeout(()=>{if(!(!e||!a||o)){if(!e.isInfinite()&&e.getPageIndex()===e.getPages().length-1){e.goTo(0);return}e.next()}},t)}function p(){if(!e||e.getPages().length<2||e.getOptions().Autoplay===!1||a)return;a=!0,e.emit(`autoplay:start`,l(`timeout`)),n(e.getContainer(),`has-autoplay`),e.getTween()?.on(`start`,S);let t=e?.getContainer();t&&l(`pauseOnHover`)&&matchMedia(`(hover: hover)`).matches&&(t.addEventListener(`mouseenter`,C,!1),t.addEventListener(`mouseleave`,w,!1)),e.on(`change`,b),e.on(`settle`,x),e.on(`contentReady`,v),e.on(`panzoom:touchStart`,m),e.on(`panzoom:wheel`,m),e.isSettled()&&f()}function m(){if(d(),g(),e){if(a){e.emit(`autoplay:end`),e.getTween()?.off(`start`,S);let t=e.getContainer();t&&(t.classList.remove(`has-autoplay`),t.removeEventListener(`mouseenter`,C,!1),t.removeEventListener(`mouseleave`,w,!1))}e.off(`change`,b),e.off(`settle`,x),e.off(`contentReady`,v),e.off(`panzoom:touchStart`,m),e.off(`panzoom:wheel`,m)}a=!1,o=!1}function h(){if(!e||(g(),!l(`showProgressbar`)))return;let t=l(`progressbarParentEl`);if(!t&&e.getPlugins().Toolbar?.isEnabled()&&(t=e.getContainer()),!t&&e.getPlugins().Toolbar?.isEnabled()!==!0){let n=e.getPages()[0]?.slides||[],r=e.getPage()?.slides||[];n.length===1&&r.length===1&&(t=r[0].el)}if(t||=e.getViewport(),!t)return;c=document.createElement(`div`),n(c,`f-progressbar`),t.prepend(c);let r=l(`timeout`)||1e3;c.style.animationDuration=`${r}ms`}function g(){c&&=(c.remove(),null)}function _(){e&&e.getPages().length>1&&l(`autoStart`)&&p()}function v(){f()}function y(e,t){let n=t.target;!n||t.defaultPrevented||n.dataset.autoplayAction===`toggle`&&T.toggle()}function b(){!e||!e?.isInfinite()&&e.getPageIndex()===e.getPages().length-1?m():(d(),g())}function x(){f()}function S(){d(),g()}function C(){s=!0,a&&(d(),g())}function w(){s=!1,a&&!o&&e?.isSettled()&&f()}let T={init:function(t){e=t,e.on(`ready`,_),e.on(`click`,y)},destroy:function(){m(),e?.off(`ready`,_),e?.off(`click`,y),e=void 0},isEnabled(){return a},pause:function(){o=!0,d(),g()},resume:function(){o=!1,a&&!s&&f()},start(){p()},stop(){m()},toggle(){a?m():p()}};return T}});