Initial dev repository
[kcls-web.git] / opac / extras / mobile / mobile / js / lib / jsdeferred.js
diff --git a/opac/extras/mobile/mobile/js/lib/jsdeferred.js b/opac/extras/mobile/mobile/js/lib/jsdeferred.js
new file mode 100644 (file)
index 0000000..27050f0
--- /dev/null
@@ -0,0 +1,15 @@
+function Deferred(){return this instanceof Deferred?this.init():new Deferred}Deferred.ok=function(a){return a};Deferred.ng=function(a){throw a;};
+Deferred.prototype={init:function(){this._next=null;this.callback={ok:Deferred.ok,ng:Deferred.ng};return this},next:function(a){return this._post("ok",a)},error:function(a){return this._post("ng",a)},call:function(a){return this._fire("ok",a)},fail:function(a){return this._fire("ng",a)},cancel:function(){(this.canceller||function(){})();return this.init()},_post:function(a,b){this._next=new Deferred;this._next.callback[a]=b;return this._next},_fire:function(a,b){var c="ok";try{b=this.callback[a].call(this,
+b)}catch(d){c="ng";b=d;Deferred.onerror&&Deferred.onerror(d)}if(b instanceof Deferred)b._next=this._next;else this._next&&this._next._fire(c,b);return this}};Deferred.next_default=function(a){var b=new Deferred,c=setTimeout(function(){b.call()},0);b.canceller=function(){clearTimeout(c)};if(a)b.callback.ok=a;return b};
+Deferred.next_faster_way_readystatechange=typeof window==="object"&&location.protocol=="http:"&&!window.opera&&/\bMSIE\b/.test(navigator.userAgent)&&function(a){var b=new Deferred,c=(new Date).getTime();if(c-arguments.callee._prev_timeout_called<150){var d=false,e=document.createElement("script");e.type="text/javascript";e.src="javascript:";e.onreadystatechange=function(){if(!d){b.canceller();b.call()}};b.canceller=function(){if(!d){d=true;e.onreadystatechange=null;document.body.removeChild(e)}};
+document.body.appendChild(e)}else{arguments.callee._prev_timeout_called=c;var f=setTimeout(function(){b.call()},0);b.canceller=function(){clearTimeout(f)}}if(a)b.callback.ok=a;return b};
+Deferred.next_faster_way_Image=typeof window==="object"&&typeof Image!="undefined"&&document.addEventListener&&function(a){var b=new Deferred,c=new Image,d=function(){b.canceller();b.call()};c.addEventListener("load",d,false);c.addEventListener("error",d,false);b.canceller=function(){c.removeEventListener("load",d,false);c.removeEventListener("error",d,false)};c.src="data:,/ _ / X";if(a)b.callback.ok=a;return b};
+Deferred.next=Deferred.next_faster_way_readystatechange||Deferred.next_faster_way_Image||Deferred.next_default;Deferred.chain=function(){for(var a=next(),b=0,c=arguments.length;b<c;b++)(function(d){switch(typeof d){case "function":var e=null;try{e=d.toString().match(/^\s*function\s+([^\s()]+)/)[1]}catch(f){}a=e!="error"?a.next(d):a.error(d);break;case "object":a=a.next(function(){return parallel(d)});break;default:throw"unknown type in process chains";}})(arguments[b]);return a};
+Deferred.wait=function(a){var b=new Deferred,c=new Date,d=setTimeout(function(){b.call((new Date).getTime()-c.getTime())},a*1E3);b.canceller=function(){clearTimeout(d)};return b};Deferred.call=function(a){var b=Array.prototype.slice.call(arguments,1);return Deferred.next(function(){return a.apply(this,b)})};
+Deferred.parallel=function(a){if(arguments.length>1)a=Array.prototype.slice.call(arguments);var b=new Deferred,c={},d=0;for(var e in a)a.hasOwnProperty(e)&&function(f,g){if(typeof f=="function")f=next(f);f.next(function(h){c[g]=h;if(--d<=0){if(a instanceof Array){c.length=a.length;c=Array.prototype.slice.call(c,0)}b.call(c)}}).error(function(h){b.fail(h)});d++}(a[e],e);d||Deferred.next(function(){b.call()});b.canceller=function(){for(var f in a)a.hasOwnProperty(f)&&a[f].cancel()};return b};
+Deferred.earlier=function(a){if(arguments.length>1)a=Array.prototype.slice.call(arguments);var b=new Deferred,c={},d=0;for(var e in a)a.hasOwnProperty(e)&&function(f,g){f.next(function(h){c[g]=h;if(a instanceof Array){c.length=a.length;c=Array.prototype.slice.call(c,0)}b.canceller();b.call(c)}).error(function(h){b.fail(h)});d++}(a[e],e);d||Deferred.next(function(){b.call()});b.canceller=function(){for(var f in a)a.hasOwnProperty(f)&&a[f].cancel()};return b};
+Deferred.loop=function(a,b){var c={begin:a.begin||0,end:typeof a.end=="number"?a.end:a-1,step:a.step||1,last:false,prev:null},d,e=c.step;return Deferred.next(function(){function f(g){if(g<=c.end){if(g+e>c.end){c.last=true;c.step=c.end-g+1}c.prev=d;d=b.call(this,g,c);return d instanceof Deferred?d.next(function(h){d=h;return Deferred.call(f,g+e)}):Deferred.call(f,g+e)}else return d}return c.begin<=c.end?Deferred.call(f,c.begin):null})};
+Deferred.repeat=function(a,b){var c=0,d=null;return Deferred.next(function(){var e=(new Date).getTime();a:{do{if(c>=a)break a;d=b(c++)}while((new Date).getTime()-e<20);return Deferred.call(arguments.callee)}})};Deferred.register=function(a,b){this.prototype[a]=function(){var c=arguments;return this.next(function(){return b.apply(this,c)})}};Deferred.register("loop",Deferred.loop);Deferred.register("wait",Deferred.wait);Deferred.Arguments=function(a){this.args=Array.prototype.slice.call(a,0)};
+Deferred.connect=function(a,b,c){var d,e;if(typeof b=="string"){d=a;e=d[b];a=c||{}}else{e=a;a=b||{};d=a.target}var f=a.args?Array.prototype.slice.call(a.args,0):[],g=isFinite(a.ok)?a.ok:a.args?a.args.length:undefined,h=a.ng;return function(){var i=new Deferred;i.next=function(k){return this._post("ok",function(){k.apply(this,arguments[0]instanceof Deferred.Arguments?arguments[0].args:arguments)})};var j=f.concat(Array.prototype.slice.call(arguments,0));if(!(isFinite(g)&&g!==null))g=j.length;j.splice(g,
+0,function(){i.call(new Deferred.Arguments(arguments))});isFinite(h)&&h!==null&&j.splice(h,0,function(){i.fail(arguments)});Deferred.next(function(){e.apply(d,j)});return i}};Deferred.retry=function(a,b,c){c||(c={});var d=c.wait||0,e=new Deferred,f=function(){b(a).next(function(g){e.call(g)}).error(function(g){--a<=0?e.fail(["retry failed",g]):setTimeout(f,d*1E3)})};setTimeout(f,0);return e};Deferred.methods=["parallel","wait","next","call","loop","repeat","chain"];
+Deferred.define=function(a,b){if(!b)b=Deferred.methods;a||(a=function(){return this}());for(var c=0;c<b.length;c++){var d=b[c];a[d]=Deferred[d]}return Deferred};