UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

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