/
flatify.min.js
1 lines (1 loc) · 3.71 KB
/
flatify.min.js
1
"v1.1.0";(function(){"use strict";var e=function(e,t){var n=this;if(!e){if(typeof console==="object"&&typeof console.log==="function"){console.log("You must pass the 'this' object to the flatify constructor for all features to be available. Ex: new flatify(this);")}e={}}n.level=0;n.parentInstance=null;n._scope={flatify:{version:"v1.1.0",masterInstance:n,currentInstance:n}};if(e.flatify&&e.flatify.version===n._scope.flatify.version){n._scope=e;n.parentInstance=n._scope.flatify.currentInstance;n._scope.flatify.currentInstance=n;n.level=n.parentInstance.level+1}t=t?t:{};n.defaultOptions={cont:t.cont?t.cont:false,wait:t.wait||t.wait===false?t.wait:true};n.cont=n.defaultOptions.cont;n.wait=n.defaultOptions.wait;n._index=0;n._nextIndex=null;n._steps=[];n._runCallbackCalled=false;n._callback=null;n._paused=false;n._resume=null;return n};e.prototype.getIndex=function(){return this._index};e.prototype.setNextIndex=function(e){this._nextIndex=e;return this};e.prototype.getNumberJobs=function(){return this._steps.length};e.prototype.deleteJob=function(e){this._steps.splice(e,1);return this};e.prototype.getContext=function(){return this._scope};e.prototype.pause=function(){if(this._callback!==null){this._paused=true}return this};e.prototype.resume=function(){if(this._callback!==null&&this._paused){this._paused=false;this._resume()}return this};e.prototype.isStarted=function(){return!!this._callback};e.prototype.isPaused=function(){return this._paused};e.prototype.isFinished=function(){return this._runCallbackCalled};e.prototype.seq=function(e,t,n){var r=this;if(e){t=t?t:{};t={cont:t.cont||t.cont===false?t.cont:r.defaultOptions.cont};var i={job:e,par:false,options:t};if(n||n===0){r._steps.splice(n,0,i)}else{r._steps.push(i)}}return r};e.prototype.par=function(e,t){var n=this;if(e){t=t?t:{};t={cont:t.cont||t.cont===false?t.cont:n.defaultOptions.cont,wait:t.wait||t.wait===false?t.wait:n.defaultOptions.wait,nb:t.nb?t.nb:1};if(!(e instanceof Array)){if(!(t.nb>0)){t.nb=1}var r=[];for(var i=0;i<t.nb;i++){r.push(e)}e=r}n._steps.push({job:e,par:true,options:t})}return n};e.prototype.run=function(e){var t=this;if(t._callback===null&&typeof e==="function"){t._callback=e;if(t._steps.length===0){e(null)}else{t._exec(null,[])}}return t};e.prototype._exec=function(e,t){var n=this;if(n._scope.flatify.currentInstance._runCallbackCalled){n._scope.flatify.currentInstance=n._scope.flatify.currentInstance.parentInstance}if(n._paused){n._resume=function(e,t){return function(){n._exec(e,t)}}(e,t)}else{if(e&&!n.cont||n._index===n._steps.length){n._execRun(e,t)}else{var r=function(e,t){if(n._nextIndex!==null&&n._steps[n._nextIndex]!==undefined){n._index=n._nextIndex;n._nextIndex=null}else{n._index++}n._exec(e,t)};n.cont=n._steps[n._index].options.cont;if(n._steps[n._index].par){n.wait=n._steps[n._index].options.wait;n._execPar(e,t,r)}else{n._execSeq(e,t,r)}}}};e.prototype._execRun=function(e,t){var n=this;n._runCallbackCalled=true;t.unshift(e);n._callback.apply(n._scope,t)};e.prototype._execPar=function(e,t,n){var r=this;t.unshift(e);var i=t.length;var s=[];var o=[];var u=false;var a=0;var f=false;var l=r._steps[r._index].job.length;for(var c=0;c<l;c++){s[c]=[];o[c]=null;t[i]=function(e,t){return function(i){s[e]=Array.prototype.slice.call(arguments,1);o[e]=i;if(i){u=true}a++;if((a===r._steps[t].job.length||i&&!r.wait)&&!f){f=true;if(!u){o=null}n(o,[s])}}}(c,r._index);r._steps[r._index].job[c].apply(r._scope,t)}};e.prototype._execSeq=function(e,t,n){var r=this;t.unshift(e);t.push(function(e){n(e,Array.prototype.slice.call(arguments,1))});r._steps[r._index].job.apply(r._scope,t)};if(typeof module==="object"&&typeof exports==="object"){exports.flatify=e}else if(typeof window==="object"&&window.window===window&&typeof window.navigator==="object"){window.flatify=e}})()