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