@fancyapps/ui
Version:
Robust JavaScript UI Component Library
3 lines (2 loc) • 2.73 kB
JavaScript
/*! License details at fancyapps.com/license */
import{isPlainObject as t}from"../utils/isPlainObject.js";import{addClass as e}from"../utils/addClass.js";const n={autoStart:!0,pauseOnHover:!0,showProgressbar:!0,timeout:2e3},o=()=>{let o,i,s=!1,a=!1,r=!1,u=null;function f(e){const i=o?.getOptions().Autoplay;let s=(t(i)?{...n,...i}:n)[e];return s&&"function"==typeof s&&o?s(o):s}function l(){clearTimeout(i),i=void 0}function g(){if(!o||!s||r||a||i||!o.isSettled()||function(){const t=o?.getPage()?.slides||[];for(const e of t)if(0===e.state)return!0;return!1}())return;!function(){if(!o)return;if(m(),!f("showProgressbar"))return;let t=f("progressbarParentEl");!t&&o.getPlugins().Toolbar?.isEnabled()&&(t=o.getContainer());if(!t&&!0!==o.getPlugins().Toolbar?.isEnabled()){const e=o.getPages()[0]?.slides||[],n=o.getPage()?.slides||[];1===e.length&&1===n.length&&(t=n[0].el)}t||(t=o.getViewport());if(!t)return;u=document.createElement("div"),e(u,"f-progressbar"),t.prepend(u);const n=f("timeout")||1e3;u.style.animationDuration=`${n}ms`}();const t=f("timeout");i=setTimeout(()=>{o&&s&&!a&&(o.isInfinite()||o.getPageIndex()!==o.getPages().length-1?o.next():o.goTo(0))},t)}function c(){if(!o||o.getPages().length<2||!1===o.getOptions().Autoplay)return;if(s)return;s=!0,o.emit("autoplay:start",f("timeout")),e(o.getContainer(),"has-autoplay"),o.getTween()?.on("start",b);const t=o?.getContainer();t&&f("pauseOnHover")&&matchMedia("(hover: hover)").matches&&(t.addEventListener("mouseenter",E,!1),t.addEventListener("mouseleave",w,!1)),o.on("change",P),o.on("settle",y),o.on("contentReady",h),o.on("panzoom:touchStart",d),o.on("panzoom:wheel",d),o.isSettled()&&g()}function d(){if(l(),m(),o){if(s){o.emit("autoplay:end"),o.getTween()?.off("start",b);const t=o.getContainer();t&&(t.classList.remove("has-autoplay"),t.removeEventListener("mouseenter",E,!1),t.removeEventListener("mouseleave",w,!1))}o.off("change",P),o.off("settle",y),o.off("contentReady",h),o.off("panzoom:touchStart",d),o.off("panzoom:wheel",d)}s=!1,a=!1}function m(){u&&(u.remove(),u=null)}function p(){o&&o.getPages().length>1&&f("autoStart")&&c()}function h(){g()}function v(t,e){const n=e.target;n&&!e.defaultPrevented&&"toggle"===n.dataset.autoplayAction&&S.toggle()}function P(){!o||!o?.isInfinite()&&o.getPageIndex()===o.getPages().length-1?d():(l(),m())}function y(){g()}function b(){l(),m()}function E(){r=!0,s&&(l(),m())}function w(){r=!1,s&&!a&&o?.isSettled()&&g()}const S={init:function(t){o=t,o.on("ready",p),o.on("click",v)},destroy:function(){d(),o?.off("ready",p),o?.off("click",v),o=void 0},isEnabled:()=>s,pause:function(){a=!0,l(),m()},resume:function(){a=!1,s&&!r&&g()},start(){c()},stop(){d()},toggle(){s?d():c()}};return S};export{o as Autoplay};