bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

liftAjax.js

(10294B)


      1 (function() {
      2 
      3   window.liftAjax = {
      4     lift_ajaxQueue: [],
      5     lift_ajaxInProcess: null,
      6     lift_doCycleQueueCnt: 0,
      7     lift_ajaxShowing: false,
      8     lift_ajaxRetryCount: 3,
      9 
     10     lift_ajaxHandler: function(theData, theSuccess, theFailure, responseType){
     11 	  var toSend = {retryCnt: 0};
     12 	  toSend.when = (new Date()).getTime();
     13 	  toSend.theData = theData;
     14 	  toSend.onSuccess = theSuccess;
     15 	  toSend.onFailure = theFailure;
     16 	  toSend.responseType = responseType;
     17 	  toSend.version = liftAjax.lift_ajaxVersion++;
     18 
     19       // Make sure we wrap when we hit JS max int.
     20       var version = liftAjax.lift_ajaxVersion
     21       if ((version - (version + 1) != -1) || (version - (version - 1) != 1))
     22         liftAjax.lift_ajaxVersion = 0;
     23 
     24 	  if (liftAjax.lift_uriSuffix) {
     25 	    theData += '&' + liftAjax.lift_uriSuffix;
     26 	    toSend.theData = theData;
     27 	    liftAjax.lift_uriSuffix = undefined;
     28 	  }
     29 
     30 	  liftAjax.lift_ajaxQueue.push(toSend);
     31 	  liftAjax.lift_ajaxQueueSort();
     32 	  liftAjax.lift_doCycleQueueCnt++;
     33 	  liftAjax.lift_doAjaxCycle();
     34 	  return false; // buttons in forms don't trigger the form
     35 
     36     },
     37 
     38     knownPromises: {},
     39 
     40     randStr: function() {
     41            return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);},
     42 
     43      makeGuid: function() {return this.randStr() + this.randStr() + '-' + this.randStr() + '-' + this.randStr() + '-' +
     44                        this.randStr() + '-' + this.randStr() + this.randStr() + this.randStr();},
     45 
     46     Promise: function() {
     47       return {
     48         guid: liftAjax.makeGuid(),
     49         "_values": [],
     50         '_events': [],
     51         '_failMsg': "",
     52         '_valueFuncs': [],
     53         '_doneFuncs': [],
     54         '_failureFuncs': [],
     55         '_eventFuncs': [],
     56         '_done': false,
     57         '_failed': false,
     58         processMsg: function(evt) {if (this._done || this._failed) return;
     59                                 this._events.push(evt);
     60                                 for (var v in this._eventFuncs) {try {this._eventFuncs[v](evt);} catch (e) {liftAjax.lift_defaultLogError(e);}};
     61                                 if (evt.done != null) {this.doneMsg();} else if (evt.success != null) {this.successMsg(evt.success);} else if (evt.failure != null) {this.failMsg(evt.failure);}},
     62         successMsg: function(value) {if (this._done || this._failed) return; this._values.push(value); for (var f in this._valueFuncs) {this._valueFuncs[f](value);}},
     63         failMsg: function(msg) {if (this._done || this._failed) return; liftAjax._removeIt(this.guid); this._failed = true; this._failMsg = msg; for (var f in this._failureFuncs) {this._failureFuncs[f](msg);}},
     64         doneMsg: function() {if (this._done || this._failed) return; liftAjax._removeIt(this.guid); this._done = true; for (var f in this._doneFuncs) {this._doneFuncs[f]();}},
     65         then: function(f) {this._valueFuncs.push(f); for (var v in this._values) {try {f(this._values[v]);} catch (e) {liftAjax.lift_defaultLogError(e);;}} return this;},
     66         fail: function(f) {this._failureFuncs.push(f); if (this._failed) {try {f(this._failMsg);} catch (e) {liftAjax.lift_defaultLogError(e);;}}; return this;},
     67         done: function(f) {this._doneFuncs.push(f); if (this._done) {try {f();} catch (e) {liftAjax.lift_defaultLogError(e);;}} return this;},
     68         onEvent: function(f) {this._eventFuncs.push(f); for (var v in this._events) {try {f(this._events[v]);} catch (e) {liftAjax.lift_defaultLogError(e);;}}; return this;},
     69         map: function(f) {var ret = new liftAjax.Promise(); this.done(function() {ret.doneMsg();}); this.fail(function (m) {ret.failMsg(m);}); this.then(function (v) {ret.successMsg(f(v));}); return ret;}
     70       };
     71     },
     72 
     73     _removeIt: function(g) {this.knownPromises[g] = undefined;},
     74 
     75     sendEvent: function(g, evt) {
     76       var p = this.knownPromises[g];
     77       if (p) {
     78         p.processMsg(evt);
     79       }
     80     },
     81 
     82     associate: function(promise) {this.knownPromises[promise.guid] = promise;},
     83 
     84     lift_uriSuffix: undefined,
     85 
     86     lift_logError: function(msg) {
     87                                                                      liftAjax.lift_defaultLogError(msg);
     88     },
     89 
     90     lift_defaultLogError: function(msg) {
     91       if (console && typeof console.error == 'function')
     92         console.error(msg);
     93       else
     94         alert(msg);
     95     },
     96     
     97     lift_ajaxQueueSort: function() {
     98       liftAjax.lift_ajaxQueue.sort(function (a, b) {return a.when - b.when;});
     99     },
    100 
    101     lift_defaultFailure: function() {
    102       alert("The server cannot be contacted at this time");
    103     },
    104 
    105     lift_startAjax: function() {
    106       liftAjax.lift_ajaxShowing = true;
    107       jQuery('#'+"ajax-spinner").show();
    108     },
    109 
    110     lift_endAjax: function() {
    111       liftAjax.lift_ajaxShowing = false;
    112       jQuery('#'+"ajax-spinner").hide();
    113     },
    114 
    115     lift_testAndShowAjax: function() {
    116       if (liftAjax.lift_ajaxShowing && liftAjax.lift_ajaxQueue.length == 0 && liftAjax.lift_ajaxInProcess == null) {
    117         liftAjax.lift_endAjax();
    118       } else if (!liftAjax.lift_ajaxShowing && (liftAjax.lift_ajaxQueue.length > 0 || liftAjax.lift_ajaxInProcess != null)) {
    119         liftAjax.lift_startAjax();
    120       }
    121     },
    122 
    123     lift_traverseAndCall: function(node, func) {
    124       if (node.nodeType == 1) func(node);
    125       var i = 0;
    126       var cn = node.childNodes;
    127 
    128       for (i = 0; i < cn.length; i++) {
    129         liftAjax.lift_traverseAndCall(cn.item(i), func);
    130       }
    131     },
    132 
    133     lift_successRegisterGC: function() {
    134       setTimeout("liftAjax.lift_registerGC()", 75000);
    135     },
    136 
    137     lift_failRegisterGC: function() {
    138       setTimeout("liftAjax.lift_registerGC()", 15000);
    139     },
    140 
    141     lift_registerGC: function() {
    142       var data = "__lift__GC=_",
    143           version = null;
    144       jQuery.ajax({ url : liftAjax.addPageNameAndVersion("/ajax_request/", version), data : data, type : "POST", dataType : "script", timeout : 5000, cache : false, success : liftAjax.lift_successRegisterGC, error : liftAjax.lift_failRegisterGC });
    145        },
    146 
    147 
    148       lift_sessionLost: function() {
    149         location.reload();
    150       },
    151 
    152        lift_doAjaxCycle: function() {
    153          if (liftAjax.lift_doCycleQueueCnt > 0) liftAjax.lift_doCycleQueueCnt--;
    154          var queue = liftAjax.lift_ajaxQueue;
    155          if (queue.length > 0) {
    156            var now = (new Date()).getTime();
    157            if (liftAjax.lift_ajaxInProcess == null && queue[0].when <= now) {
    158              var aboutToSend = queue.shift();
    159 
    160              liftAjax.lift_ajaxInProcess = aboutToSend;
    161 
    162              var successFunc = function(data) {
    163                liftAjax.lift_ajaxInProcess = null;
    164                if (aboutToSend.onSuccess) {
    165                  aboutToSend.onSuccess(data);
    166                }
    167                liftAjax.lift_doCycleQueueCnt++;
    168                liftAjax.lift_doAjaxCycle();
    169              };
    170 
    171              var failureFunc = function() {
    172                liftAjax.lift_ajaxInProcess = null;
    173                var cnt = aboutToSend.retryCnt;
    174                if (arguments.length == 3 && arguments[1] == 'parsererror') {
    175                  liftAjax.lift_logError('The server call succeeded, but the returned Javascript contains an error: '+arguments[2])
    176                } else
    177   
    178                if (cnt < liftAjax.lift_ajaxRetryCount) {
    179                  aboutToSend.retryCnt = cnt + 1;
    180                  var now = (new Date()).getTime();
    181                  aboutToSend.when = now + (1000 * Math.pow(2, cnt));
    182                  queue.push(aboutToSend);
    183                  liftAjax.lift_ajaxQueueSort();
    184                } else {
    185                  if (aboutToSend.onFailure) {
    186                    aboutToSend.onFailure();
    187                  } else {
    188                    liftAjax.lift_defaultFailure();
    189                  }
    190                }
    191                liftAjax.lift_doCycleQueueCnt++;
    192                liftAjax.lift_doAjaxCycle();
    193              };
    194 
    195              if (aboutToSend.responseType != undefined &&
    196                  aboutToSend.responseType != null &&
    197                  aboutToSend.responseType.toLowerCase() === "json") {
    198                liftAjax.lift_actualJSONCall(aboutToSend.theData, successFunc, failureFunc);
    199              } else {
    200                var theData = aboutToSend.theData,
    201                    version = aboutToSend.version;
    202 
    203                liftAjax.lift_actualAjaxCall(theData, version, successFunc, failureFunc);
    204              }
    205             }
    206          }
    207 
    208          liftAjax.lift_testAndShowAjax();
    209          if (liftAjax.lift_doCycleQueueCnt <= 0) liftAjax.lift_doCycleIn200()
    210        },
    211 
    212        lift_doCycleIn200: function() {
    213          liftAjax.lift_doCycleQueueCnt++;
    214          setTimeout("liftAjax.lift_doAjaxCycle();", 200);
    215        },
    216 
    217        lift_ajaxVersion: 0,
    218 
    219        addPageNameAndVersion: function(url, version) {
    220          
    221       var replacement = 'ajax_request/'+lift_page;
    222       if (version!=null)
    223         replacement += ('-'+version.toString(36)) + (liftAjax.lift_ajaxQueue.length > 35 ? 35 : liftAjax.lift_ajaxQueue.length).toString(36);
    224       return url.replace('ajax_request', replacement);
    225     },
    226 
    227     lift_actualAjaxCall: function(data, version, onSuccess, onFailure) {
    228       jQuery.ajax({ url : liftAjax.addPageNameAndVersion("/ajax_request/", version), data : data, type : "POST", dataType : "script", timeout : 5000, cache : false, success : onSuccess, error : onFailure });
    229         },
    230 
    231         lift_actualJSONCall: function(data, onSuccess, onFailure) {
    232           var version = null;
    233           jQuery.ajax({ url : liftAjax.addPageNameAndVersion("/ajax_request/", version), data : data, type : "POST", dataType : "json", timeout : 5000, cache : false, success : onSuccess, error : onFailure });
    234               }
    235             };
    236 
    237             window.liftUtils = {
    238               lift_blurIfReturn: function(e) {
    239                 var code;
    240                 if (!e) var e = window.event;
    241                 if (e.keyCode) code = e.keyCode;
    242                 else if (e.which) code = e.which;
    243 
    244                 var targ;
    245 
    246                 if (e.target) targ = e.target;
    247                 else if (e.srcElement) targ = e.srcElement;
    248                 if (targ.nodeType == 3) // defeat Safari bug
    249                   targ = targ.parentNode;
    250                 if (code == 13) {targ.blur(); return false;} else {return true;};
    251               }
    252             };
    253 
    254 
    255           })();
    256           jQuery(document).ready(function() {liftAjax.lift_doCycleIn200();});