UNPKG

16.9 kBJavaScriptView Raw
1var uiloosActiveList=function(t){"use strict";const i="uiloos > ActiveList >";class s extends Error{constructor(){super(`${i} autoPlay > duration cannot be negative or zero`),this.name="ActiveListAutoPlayDurationError"}}class h{constructor(t,i){this.t=null,this.i=new Date,this.h=null,this.o=0,this.u=null,this.l=t,this.u=i}v(t){this.u=t}I(t){if(this.m(),!this.u||null===this.l.lastActivatedContent)return;const i=this.p(this.u,this.l.lastActivatedContent);if(i<=0)throw new s;if(this.o=i,null===this.h?this.l.autoPlay.duration=i:this.h=null,this.i=new Date,this.t=window.setTimeout((()=>{this.t=null,this.l.activateNext({isUserInteraction:!1})}),i),this.l.autoPlay.isPlaying=!0,t){const t={type:"AUTO_PLAY_PLAYING",time:new Date};this.l._(t)}}D(){if(!this.l.autoPlay.isPlaying)return;this.h=new Date,this.m(),this.l.autoPlay.isPlaying=!1;const t={type:"AUTO_PLAY_PAUSED",time:new Date};this.l._(t)}A(){if(!this.l.autoPlay.isPlaying&&!this.h)return;this.m(),this.h=null,this.l.autoPlay.isPlaying=!1,this.l.autoPlay.duration=0,this.l.autoPlay.hasBeenStoppedBefore=!0;const t={type:"AUTO_PLAY_STOPPED",time:new Date};this.l._(t)}m(){null!==this.t&&(window.clearTimeout(this.t),this.t=null)}L(t){null!==this.l.lastActivatedContent?this.u&&this.g(t,this.u)&&this.A():this.A()}onActiveIndexChanged(t,i){this.u&&(this.g(i,this.u)||!1===this.l.isCircular&&t===this.l.getLastIndex()?this.A():this.I(!1))}g(t,i){return!(!t||!1===t.isUserInteraction||!i.stopsOnUserInteraction)}p(t,i){return this.h?this.o-(this.h.getTime()-this.i.getTime()):"number"==typeof t.duration?t.duration:t.duration({index:i.index,content:i,value:i.value,activeList:this.l})}}class e{constructor(t,i,s){this.isActive=!1,this.hasBeenActiveBefore=!1,this.isFirst=!1,this.isLast=!1,this.hasNext=!1,this.hasPrevious=!1,this.isNext=!1,this.isPrevious=!1,this.activeList=t,this.index=i,this.value=s}activate(t){this.activeList.activateByIndex(this.index,t)}deactivate(t){this.activeList.deactivateByIndex(this.index,t)}toggle(t){this.activeList.toggleByIndex(this.index,t)}remove(){return this.activeList.removeByIndex(this.index)}swapWith(t){const i=this.activeList.getIndex(t);this.swapWithByIndex(i)}swapWithByIndex(t){this.activeList.swapByIndex(this.index,t)}swapWithNext(){const t=this.activeList.B(this.index);this.swapWithByIndex(t)}swapWithPrevious(){const t=this.activeList.P(this.index);this.swapWithByIndex(t)}moveToIndex(t){this.activeList.moveByIndex(this.index,t)}moveToPredicate(t,i){this.activeList.moveByIndexByPredicate(this.index,t,i)}moveToFirst(){this.activeList.moveByIndex(this.index,0)}moveToLast(){this.activeList.moveByIndex(this.index,this.activeList.getLastIndex())}}class n extends Error{constructor(){super(`${i} cooldown > duration cannot be negative or zero`),this.name="ActiveListCooldownDurationError"}}class r{constructor(t,i){this.T=null,this.U=void 0,"number"==typeof i&&this.C(i),this.l=t,this.U=i}O(t){return!1!==t.isUserInteraction&&this.l.cooldown.isActive}M(t,i){if(!1===t.isUserInteraction)return;const s=this.p(t,i);if(-1===s)return void this.N();this.l.cooldown.isActive=!0,this.l.cooldown.duration=s,this.T=window.setTimeout((()=>{this.N()}),s);const h={type:"COOLDOWN_STARTED",time:new Date};this.l._(h)}p(t,i){let s=-1;if(void 0!==t.cooldown)s=this.$(t.cooldown,i);else{if(void 0===this.U)return-1;s=this.$(this.U,i)}return this.C(s),s}$(t,i){return"number"==typeof t?t:t({index:i.index,content:i,value:i.value,activeList:this.l})}C(t){if(t<=0)throw new n}N(){if(this.T&&window.clearTimeout(this.T),!this.l.cooldown.isActive)return;this.l.cooldown.isActive=!1,this.l.cooldown.duration=0;const t={type:"COOLDOWN_ENDED",time:new Date};this.l._(t)}}class o extends Error{constructor(){super(`${i} activateByIndex > activation limit reached`),this.name="ActiveListActivationLimitReachedError"}}class c extends Error{constructor(t){super(t),this.name="ActiveListIndexOutOfBoundsError"}}function u(t,s){throw new c(`${i} ${t} > "${s}" is out of bounds`)}class a extends Error{constructor(){super(`${i} getIndex > index cannot be found, item is not in contents array`),this.name="ActiveListItemNotFoundError"}}class d{constructor(){this.V=[],this.S=0}W(t){this.S>0&&(this.V.push(t),this.V.length-1===this.S&&this.V.shift())}R(t=0){this.S=t}}class l{constructor(){this.F=[]}Y(t){return this.F.push(t),()=>{this.k(t)}}k(t){this.F=this.F.filter((i=>t!==i))}G(){this.F.length=0}_(t,i){this.F.forEach((s=>s(t,i)))}}return t.ActiveList=class{constructor(t={},i){this.H=!1,this.contents=[],this.maxActivationLimit=1,this.maxActivationLimitBehavior="circular",this.active=[],this.activeContents=[],this.activeIndexes=[],this.lastActivated=null,this.lastActivatedContent=null,this.lastActivatedIndex=-1,this.lastDeactivated=null,this.lastDeactivatedContent=null,this.lastDeactivatedIndex=-1,this.isCircular=!1,this.direction="right",this.oppositeDirection="left",this.K=new d,this.history=this.K.V,this.Z=new l,this.hasActiveChangedAtLeastOnce=!1,this.cooldown={isActive:!1,duration:0},this.autoPlay={isPlaying:!1,duration:0,hasBeenStoppedBefore:!1},uiloosLicenseChecker.licenseChecker._checkLicense(),i&&this.subscribe(i),this.initialize(t)}subscribe(t){return this.Z.Y(t)}unsubscribe(t){this.Z.k(t)}unsubscribeAll(){this.Z.G()}initialize(t){this.H=!0,this.maxActivationLimit=void 0!==t.maxActivationLimit?t.maxActivationLimit:1,this.maxActivationLimitBehavior=t.maxActivationLimitBehavior?t.maxActivationLimitBehavior:"circular",this.isCircular=!!t.isCircular;const i=t.contents?t.contents:[];this.contents.length=0,i.forEach(((t,s)=>{this.contents[s]=this.j(t,s,i)})),this.q=t.directions?t.directions:{next:"right",previous:"left"},this.K.V.length=0,this.K.R(t.keepHistoryFor),this.J(),this.X=new r(this,t.cooldown),this.cooldown.isActive=!1,this.cooldown.duration=0,void 0!==t.active?Array.isArray(t.active)?t.active.forEach((t=>this.activate(t,{isUserInteraction:!1}))):this.activate(t.active,{isUserInteraction:!1}):void 0!==t.activeIndexes&&(Array.isArray(t.activeIndexes)?t.activeIndexes.forEach((t=>this.activateByIndex(t,{isUserInteraction:!1}))):this.activateByIndex(t.activeIndexes,{isUserInteraction:!1})),this.tt(),this.hasActiveChangedAtLeastOnce=!1,this.direction=this.q.next,this.oppositeDirection=this.q.previous,this.autoPlay.isPlaying=!1,this.autoPlay.duration=0,this.autoPlay.hasBeenStoppedBefore=!1,this.it=new h(this,t.autoPlay?t.autoPlay:null),this.it.I(!1),this.H=!1;const s={type:"INITIALIZED",values:[...this.active],indexes:[...this.activeIndexes],time:new Date};this._(s)}j(t,i,s){const h=new e(this,i,t);return this.st(h,i,s),h}activateByIndex(t,i={isUserInteraction:!0,cooldown:void 0}){const s=this.lastDeactivatedIndex,h=this.ht(t,i);if(!h)return;let e=-1,n=null;s!==this.lastDeactivatedIndex&&(e=this.lastDeactivatedIndex,n=this.lastDeactivated);const r={type:"ACTIVATED",value:h.value,index:t,deactivatedIndex:e,deactivatedValue:n,time:new Date};this._(r),this.X.M(i,this.lastActivatedContent)}ht(t,i){if(this.et(t)&&u("activateByIndex","index"),this.activeIndexes.includes(t))return null;if(this.X.O(i))return null;const s=!1!==this.maxActivationLimit&&this.maxActivationLimit===this.activeIndexes.length;if(s){if("error"===this.maxActivationLimitBehavior)throw new o;if("ignore"===this.maxActivationLimitBehavior)return null}const h=this.nt(t),e=this.rt(t);return this.contents.forEach(((i,n)=>{if(i.isActive=i.isActive?i.isActive:t===n,i.isNext=h===n,i.isPrevious=e===n,t===n){if(this.activeIndexes.push(n),this.activeContents.push(i),this.active.push(i.value),s){this.activeIndexes.shift(),this.active.shift();const t=this.activeContents.shift();t&&this.ot(t)}this.direction=this.ct(n),this.oppositeDirection=this.direction===this.q.next?this.q.previous:this.q.next,this.lastActivated=i.value,this.lastActivatedContent=i,this.lastActivatedIndex=n,i.hasBeenActiveBefore=!0}})),this.it&&this.it.onActiveIndexChanged(t,i),this.hasActiveChangedAtLeastOnce=!0,this.lastActivatedContent}activate(t,i){const s=this.getIndex(t);this.activateByIndex(s,i)}activateByPredicate(t,i={isUserInteraction:!0,cooldown:void 0}){if(this.X.O(i))return;const s=[...this.activeIndexes];let h=null,e=null;if(this.ut(t,(t=>{try{const s=this.ht(t,i);s&&(h=s)}catch(t){if(t instanceof o)return e=t,!0}})),h){const t=[],e=[];this.activeIndexes.forEach((i=>{s.every((t=>t!==i))&&(e.push(i),t.push(this.contents[i].value))}));const n=[],r=[];!1!==this.maxActivationLimit&&"circular"===this.maxActivationLimitBehavior&&s.forEach((t=>{this.activeIndexes.every((i=>t!==i))&&(r.push(t),n.push(this.contents[t].value))}));const o={type:"ACTIVATED_MULTIPLE",values:t,indexes:e,deactivatedIndexes:r,deactivatedValues:n,time:new Date};this._(o),h&&this.X.M(i,h)}if(e)throw e}activateNext(t){if(this.isEmpty())return;const i=this.B(this.lastActivatedIndex);this.activateByIndex(i,t)}activatePrevious(t){if(this.isEmpty())return;const i=this.P(this.lastActivatedIndex);this.activateByIndex(i,t)}activateFirst(t){this.isEmpty()||this.activateByIndex(0,t)}activateLast(t){this.isEmpty()||this.activateByIndex(this.getLastIndex(),t)}deactivateByIndex(t,i={isUserInteraction:!0,cooldown:void 0}){const s=this.dt(t,i);if(!s)return;const h={type:"DEACTIVATED",value:this.contents[t].value,index:t,time:new Date};this._(h),this.X.M(i,s)}dt(t,i){this.et(t)&&u("deactivateByIndex","index");const s=this.activeIndexes.indexOf(t);if(-1===s)return null;if(this.X.O(i))return null;const h=this.activeContents[s];return this.ot(h),this.activeIndexes.splice(s,1),this.active.splice(s,1),this.activeContents.splice(s,1),0===this.activeIndexes.length?(this.lt(),this.direction=this.q.next):(this.vt(),this.direction=this.ct(h.index),this.oppositeDirection=this.direction,this.direction=this.direction===this.q.next?this.q.previous:this.q.next),this.ft(),this.hasActiveChangedAtLeastOnce=!0,this.it&&this.it.L(i),h}deactivate(t,i){const s=this.getIndex(t);this.deactivateByIndex(s,i)}deactivateByPredicate(t,i={isUserInteraction:!0,cooldown:void 0}){if(this.X.O(i))return;const s=[],h=[];let e=null;if(this.ut(t,(t=>{const n=this.dt(t,i);n&&(s.push(n.index),h.push(n.value),e=n)})),0===s.length)return;const n={type:"DEACTIVATED_MULTIPLE",values:h,indexes:s,time:new Date};this._(n),e&&this.X.M(i,e)}toggleByIndex(t,i){this.et(t)&&u("toggleByIndex","index"),this.contents[t].isActive?this.deactivateByIndex(t,i):this.activateByIndex(t,i)}toggle(t,i){const s=this.getIndex(t);this.toggleByIndex(s,i)}play(){this.it.I(!0)}pause(){this.it.D()}stop(){this.it.A()}configureAutoPlay(t){this.it.v(t),t?this.it.I(!0):this.it.A()}insertAtIndex(t,i){(i<0||i>this.contents.length)&&u("insertAtIndex","index");const s=this.j(t,i,this.contents);this.activeIndexes.forEach(((t,s)=>{this.activeIndexes[s]=t>=i?t+1:t})),this.contents.splice(i,0,s),i<=this.lastActivatedIndex&&(this.lastActivatedIndex+=1),this.ft();const h={type:"INSERTED",value:t,index:i,time:new Date};return this._(h),s}push(t){return this.insertAtIndex(t,this.contents.length)}unshift(t){return this.insertAtIndex(t,0)}insertByPredicate(t,i,s={mode:"at"}){const h=this.xt(s.mode);return this.ut(i,(i=>{const s=Math.max(0,i+h);return this.insertAtIndex(t,s)}))}removeByIndex(t){const i=this.It(t),s=this.activeIndexes.indexOf(t);-1!==s&&(this.activeIndexes.splice(s,1),this.active.splice(s,1),this.activeContents.splice(s,1),this.hasActiveChangedAtLeastOnce=!0),this.activeIndexes.map(((i,s)=>{this.activeIndexes[s]=i>=t?i-1:i})),this.isEmpty()?(this.J(),this.it.A()):this.vt(),this.ft();const h={type:"REMOVED",value:i,index:t,time:new Date};return this._(h),i}It(t){this.et(t)&&u("removeByIndex","index"),this.lastDeactivated&&this.lastDeactivatedIndex===t&&this.tt();const i=this.contents[t].value;return this.contents.splice(t,1),i}remove(t){const i=this.getIndex(t);return this.removeByIndex(i)}pop(){if(!this.isEmpty())return this.removeByIndex(this.getLastIndex())}shift(){if(!this.isEmpty())return this.removeByIndex(0)}removeByPredicate(t){if(this.isEmpty())return[];const i=[];this.ut(t,(t=>{const s=this.contents[t];i.push(s)}));const s=[];i.forEach(((t,i)=>{const h=t.index-i;this.It(h),s.push(t.index)})),this.isEmpty()?(this.J(),this.it.A()):(s.forEach((t=>{const i=this.activeIndexes.indexOf(t);-1!==i&&(this.activeIndexes.splice(i,1),this.active.splice(i,1),this.activeContents.splice(i,1),this.hasActiveChangedAtLeastOnce=!0)})),s.forEach((t=>{this.activeIndexes.forEach(((i,s)=>{this.activeIndexes[s]=i>=t?i-1:i}))})),this.vt());const h=i.map((t=>t.value));if(s.length>0){this.ft();const t={type:"REMOVED_MULTIPLE",indexes:[...s],values:[...h],time:new Date};this._(t)}return h}swapByIndex(t,i){if(this.et(t)&&u("swapByIndex","a"),this.et(i)&&u("swapByIndex","b"),t===i)return;const s=this.contents[t],h=this.contents[i];this.lastActivatedIndex===s.index?this.lastActivatedIndex=h.index:this.lastActivatedIndex===h.index&&(this.lastActivatedIndex=s.index);const e=this.activeIndexes.indexOf(s.index),n=this.activeIndexes.indexOf(h.index);-1!==e&&(this.activeIndexes[e]=h.index),-1!==n&&(this.activeIndexes[n]=s.index),s.index=i,h.index=t,this.contents[t]=h,this.contents[i]=s,this.ft();const r={type:"SWAPPED",value:{a:s.value,b:h.value},index:{a:t,b:i},time:new Date};this._(r)}swap(t,i){const s=this.getIndex(t),h=this.getIndex(i);this.swapByIndex(s,h)}moveByIndex(t,i){if(this.et(t)&&u("moveByIndex","from"),this.et(i)&&u("moveByIndex","to"),t===i)return;const s=this.lastActivatedIndex;s===t?this.lastActivatedIndex=i:i===s&&t>s?this.lastActivatedIndex+=1:i===s&&t<s||i>s&&t<s?this.lastActivatedIndex-=1:i<s&&t>s&&(this.lastActivatedIndex+=1),this.activeIndexes.forEach(((s,h)=>{this.activeIndexes[h]=s!==t?s>t&&s>i||s<t&&s<i?s:t>i?s+1:s-1:i}));const h=this.contents[t];this.contents.splice(t,1),this.contents.splice(i,0,h),this.ft();const e={type:"MOVED",value:h.value,index:{from:t,to:i},time:new Date};this._(e)}move(t,i){const s=this.getIndex(t);this.moveByIndex(s,i)}moveByIndexByPredicate(t,i,s={mode:"at"}){const h=this.xt(s.mode);this.ut(i,((i,s)=>{const e=Math.min(Math.max(0,i+h),s-1);return this.moveByIndex(t,e),!0}))}moveByPredicate(t,i,s){const h=this.getIndex(t);this.moveByIndexByPredicate(h,i,s)}getIndex(t){const i=this.contents,s=i.length;for(let h=0;h<s;h++)if(i[h].value===t)return h;throw new a}getLastIndex(){return this.contents.length-1}B(t){let i=t+1;return i>=this.contents.length&&(i=this.isCircular?0:this.getLastIndex()),i}P(t){let i=t-1;return i<0&&(i=this.isCircular?this.getLastIndex():0),i}nt(t){const i=t+1;return this.isCircular&&i===this.contents.length?0:i}rt(t){const i=t-1;return this.isCircular&&i<0?this.getLastIndex():i}isEmpty(){return 0===this.contents.length}ct(t){const i=this.lastActivatedIndex;if(this.isCircular){if(-1===this.lastActivatedIndex)return this.q.next;const i=this.lastActivatedIndex>t,s=this.getLastIndex();return(i?this.lastActivatedIndex-t:s-t+this.lastActivatedIndex+1)>=(i?1+t+(s-this.lastActivatedIndex):t-this.lastActivatedIndex)?this.q.next:this.q.previous}return t>=i?this.q.next:this.q.previous}ft(){let t=null,i=null;-1!==this.lastActivatedIndex&&(t=this.nt(this.lastActivatedIndex),i=this.rt(this.lastActivatedIndex)),this.contents.forEach(((s,h)=>{s.index=h,s.isNext=t===h,s.isPrevious=i===h,this.st(s,h,this.contents)}))}st(t,i,s){t.isFirst=0===i,t.isLast=i===s.length-1,this.isCircular?(t.hasNext=!0,t.hasPrevious=!0):(t.hasNext=i+1<s.length,t.hasPrevious=i-1>=0)}lt(){this.lastActivatedIndex=-1,this.lastActivated=null,this.lastActivatedContent=null}tt(){this.lastDeactivatedIndex=-1,this.lastDeactivated=null,this.lastDeactivatedContent=null}J(){this.tt(),this.lt(),this.activeContents.length=0,this.activeIndexes.length=0,this.active.length=0,this.hasActiveChangedAtLeastOnce=!0}vt(){if(0===this.activeIndexes.length)return void this.lt();const t=this.activeIndexes[this.activeIndexes.length-1],i=this.contents[t];this.lastActivated=i.value,this.lastActivatedContent=i,this.lastActivatedIndex=t}ot(t){t.isActive=!1,this.lastDeactivated=t.value,this.lastDeactivatedContent=t,this.lastDeactivatedIndex=t.index}ut(t,i){const s=this.contents.length;for(let h=0;h<s;h++){const e=this.contents[h];if(t({index:h,content:e,value:e.value,activeList:this})){const t=i(h,s);if(void 0!==t)return t}}return null}_(t){this.H||(this.K.W(t),this.Z._(this,t))}et(t){return t<0||t>=this.contents.length}xt(t){return"at"===t?0:"after"===t?1:-1}},t.ActiveListActivationLimitReachedError=o,t.ActiveListAutoPlayDurationError=s,t.ActiveListContent=e,t.ActiveListCooldownDurationError=n,t.ActiveListIndexOutOfBoundsError=c,t.ActiveListItemNotFoundError=a,t.createActiveListSubscriber=function(t){return(i,s)=>{!function(t,i,s,h){const e="on"+i.type.toLowerCase().split("_").reduce(((t,i)=>{const s=i.split("");return s[0]=s[0].toUpperCase(),t+s.join("")}),""),n=h[e];n?n(i,s):h.debug&&console.warn(`uiloos > ${t} event '${i.type}' was fired but '${e}' method is not implemented, this might not be correct.`)}("createActiveListSubscriber",s,i,t)}},t}({});