UNPKG

@scandit/web-datacapture-core

Version:

Scandit Data Capture SDK for the Web

1 lines 7.02 kB
var u=class extends EventTarget{constructor(e,i){var t;super();this.__isMockAnimation=true;this._playState="idle";this.resolver=()=>{};this.readyResolver=()=>{};this.timeoutId=null;this.finished=Promise.resolve();this.timeline=null;this.effect=null;this.id="";this.pending=false;this.playbackRate=1;this._replaceState="active";this.startTime=null;this.currentTime=null;this.onfinish=null;this.oncancel=null;this.onremove=null;this.effect=e||null,this.timeline=i!==void 0?i:((t=globalThis.document)==null?void 0:t.timeline)||null,e&&"target"in e&&(this.element=e.target,this.keyFrameEffect=e),this.createReadyPromise();}get playState(){return this._playState}get replaceState(){return this._replaceState}play(){var i,t,r;if(this.timeoutId&&(clearTimeout(this.timeoutId),this._playState==="running"||this._playState==="paused"))try{this.resolver();}catch(a){}(this._playState==="finished"||this._playState==="running"&&this.timeoutId)&&this.createReadyPromise();let e=(t=(i=this.keyFrameEffect)==null?void 0:i.getTiming().duration)!=null?t:0;if(this._playState==="paused"){this._playState="running",this.pending=false;try{this.readyResolver(this);}catch(s){}let a=Math.max(0,e-((r=this.currentTime)!=null?r:0));this.timeoutId=setTimeout(()=>{var s;this._playState="finished",this.pending=false,this.currentTime=e,this.applyFinalStyles(),this.dispatchEvent(new Event("finish")),(s=this.onfinish)==null||s.call(this),this.resolver();},a);}else this._playState="running",this.pending=true,this.startTime=performance.now(),this.currentTime=0,this.finished=new Promise(a=>{this.resolver=a;}),Promise.resolve().then(()=>{if(this._playState==="running"){this.pending=false;try{this.readyResolver(this);}catch(a){}}}),this.timeoutId=setTimeout(()=>{var a;this._playState="finished",this.pending=false,this.currentTime=e,this.applyFinalStyles(),this.dispatchEvent(new Event("finish")),(a=this.onfinish)==null||a.call(this),this.resolver();},e);}pause(){this.timeoutId&&clearTimeout(this.timeoutId),this._playState="paused",this.pending=false,this.startTime!==null&&(this.currentTime=performance.now()-this.startTime);}finish(){var r,a,s,l,o;if(this.playbackRate===0){let m=new Error("Cannot finish animation with playbackRate of 0");throw m.name="InvalidStateError",m}let e=(a=(r=this.keyFrameEffect)==null?void 0:r.getTiming().duration)!=null?a:0,i=(l=(s=this.keyFrameEffect)==null?void 0:s.getTiming().iterations)!=null?l:1,t=e*i;if(this.playbackRate>0&&(t===Number.POSITIVE_INFINITY||i===Number.POSITIVE_INFINITY)){let m=new Error("Cannot finish animation with infinite end time and positive playbackRate");throw m.name="InvalidStateError",m}this.timeoutId&&clearTimeout(this.timeoutId),this._playState="finished",this.pending=false,this.currentTime=this.playbackRate<0?0:e,this.applyFinalStyles(),this.dispatchEvent(new Event("finish")),(o=this.onfinish)==null||o.call(this),this.resolver();}cancel(){var e;this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=null),this._playState="idle",this.pending=false,this.currentTime=null,this.startTime=null,this.cleanupAppliedStyles(),this.dispatchEvent(new Event("cancel")),(e=this.oncancel)==null||e.call(this);try{this.resolver();}catch(i){}this.createReadyPromise();}reverse(){this.playbackRate*=-1,this.dispatchEvent(new Event("reverse"));}createReadyPromise(){this.ready=new Promise(e=>{this.readyResolver=e;});}applyFinalStyles(){if(!this.keyFrameEffect||!this.element)return;let e=this.keyFrameEffect.getKeyframes(),i=e[e.length-1];if(i&&this.element instanceof HTMLElement){for(let[t,r]of Object.entries(i))if(t!=="offset"&&t!=="easing"&&t!=="composite")try{this.element.style.setProperty(t,String(r));}catch(a){}}}cleanupAppliedStyles(){if(!this.keyFrameEffect||!this.element)return;let e=this.keyFrameEffect.getKeyframes();if(this.element instanceof HTMLElement){for(let i of e)for(let[t]of Object.entries(i))if(t!=="offset"&&t!=="easing"&&t!=="composite")try{let r=t.replace(/[A-Z]/g,a=>`-${a.toLowerCase()}`);this.element.style.removeProperty(r);}catch(r){}}}commitStyles(){this.applyFinalStyles();}persist(){this._replaceState="persisted";}updatePlaybackRate(e){this.playbackRate=e;}get overallProgress(){var i,t,r;if(this._playState==="idle")return 0;if(this._playState==="finished")return 1;let e=(t=(i=this.keyFrameEffect)==null?void 0:i.getTiming().duration)!=null?t:0;return e===0?0:Math.min(1,Math.max(0,((r=this.currentTime)!=null?r:0)/e))}},f=class{constructor(n,e,i={}){this.composite="replace";this.pseudoElement=null;this.iterationComposite="replace";if(arguments.length===1&&n&&typeof n=="object"&&"getKeyframes"in n&&"target"in n){let t=n;this.target=t.target;let r=t.getKeyframes?t.getKeyframes():[];this.keyframes=r.map(a=>{let{easing:s,computedOffset:l,...o}=a;return s&&s!=="linear"&&(o.easing=s),o}),this.options=t.getTiming?t.getTiming():{},this.composite=t.composite||"replace",this.pseudoElement=t.pseudoElement||null,this.iterationComposite=t.iterationComposite||"replace";}else this.target=n,this.keyframes=this.normalizeKeyframes(e!=null?e:[]),this.options=typeof i=="number"?{duration:i}:i;this.activeDuration=this.calculateActiveDuration();}normalizeKeyframes(n){if(!n)return [];if(Array.isArray(n))return n;let e=[],i=Object.keys(n);if(i.length===0)return [];let t=0;for(let r of i){let a=n[r];Array.isArray(a)?t=Math.max(t,a.length):a!==void 0&&(t=Math.max(t,1));}for(let r=0;r<t;r++){let a={};for(let s of i){let l=n[s];Array.isArray(l)?a[s]=l[r]||l[l.length-1]:l!==void 0&&(a[s]=l);}e.push(a);}return e}calculateActiveDuration(){var i;let n=typeof this.options.duration=="number"?this.options.duration:0,e=(i=this.options.iterations)!=null?i:1;return n*e}getTiming(){return {...this.options}}updateTiming(n){this.options={...this.options,...n},this.activeDuration=this.calculateActiveDuration();}setKeyframes(n){this.keyframes=n;}getKeyframes(){return this.keyframes.map(n=>{var e,i;return {...n,computedOffset:(e=n.offset)!=null?e:null,easing:(i=n.easing)!=null?i:"linear"}})}getTarget(){return this.target}getComputedTiming(){let n=this.getTiming();return {...n,activeDuration:this.activeDuration,endTime:this.activeDuration+(n.delay||0)+(n.endDelay||0),localTime:null,progress:null,currentIteration:null}}};function c(){let h=globalThis.Animation,n=globalThis.KeyframeEffect,e=globalThis.Element.prototype.animate;return Object.defineProperty(globalThis,"Animation",{writable:true,configurable:true,value:u}),Object.defineProperty(globalThis,"KeyframeEffect",{writable:true,configurable:true,value:f}),Object.defineProperty(globalThis.Element.prototype,"animate",{writable:true,configurable:true,value:function(i,t){let r=new f(this,i,t),a=new u(r);return a.play(),a}}),()=>{Object.defineProperty(globalThis,"Animation",{writable:true,configurable:true,value:h}),Object.defineProperty(globalThis,"KeyframeEffect",{writable:true,configurable:true,value:n}),Object.defineProperty(globalThis.Element.prototype,"animate",{writable:true,configurable:true,value:e});}}export{u as MockAnimation,f as MockKeyframeEffect,c as polyfill};