From 74314537d81775581f122362ceff8d71193da4ff Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Mon, 19 Sep 2016 08:15:32 +0200 Subject: [PATCH] aligned 'commons.js' with fieldify repo --- web/commons.js | 142 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/web/commons.js b/web/commons.js index c0d6d31..28e6aae 100644 --- a/web/commons.js +++ b/web/commons.js @@ -306,6 +306,7 @@ function send_mesg(mesg) function server_request(page, sess) { var xhr_wr = createXMLHttpRequest(); + var tout = -1; var i, collect = "", post_collect = null, is_post = false; if (arguments.length > 0) { @@ -322,8 +323,6 @@ function server_request(page, sess) collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]); } } - // alert("Args: "+arguments.length); - var uri = page + '?' + (sess != null ? 'sess=' + sess + '&' : '') + collect; if (is_post) { xhr_wr.open('POST', uri, false); @@ -332,7 +331,13 @@ function server_request(page, sess) else { xhr_wr.open('GET', uri, false); } - xhr_wr.onreadystatechange = function() { return; }; + if (tout > 0) { + xhr_wr.timeout = tout; + } + + xhr_wr.onreadystatechange = function() { + console.log('we are here'); + return; }; xhr_wr.send(post_collect); if (xhr_wr.responseText != null) { @@ -343,6 +348,50 @@ function server_request(page, sess) return (null); } + +function server_asyncreq(async_req, page, sess) +{ + var xhr_wr = createXMLHttpRequest(); + var tout = -1; + var i, collect = "", post_collect = null, is_post = false; + + if (arguments.length > 0) { + for (i = 3 ; i < arguments.length ; i+= 2) { + if (arguments[i] == "__POST__") { + is_post = true; + post_collect = ""; + i -= 1; + continue; + } + else if (arguments[i] == "__TOUT__") { + tout = arguments[i+1]; + continue; + } + if (is_post) + post_collect += (post_collect == "" ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]); + else + collect += (i == 0 ? "" : "&") + arguments[i] + "=" + encodeURIComponent(arguments[i+1]); + } + } + var already_quest = (page.indexOf('?') != -1); + var uri = page + (already_quest ? '&' : '?') + (sess != null ? 'sess=' + sess + '&' : '') + collect; + if (is_post) { + xhr_wr.open('POST', uri, true); + xhr_wr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); + } + else { + xhr_wr.open('GET', uri, true); + } + if (tout > 0) { + xhr_wr.timeout = tout; + } + + xhr_wr.onreadystatechange = async_req; + xhr_wr.send(post_collect); + + return; +} + /* Stat: CHAT and TABLE */ function chatt_checksend(obj,e) @@ -1474,3 +1523,90 @@ function class_subst(item, cls_out, cls_in) class_add(item, cls_in); } +function nexst(after) +{ + if (after != undefined) { + this._after = after; + } +} + +nexst.next = function(st) +{ + if (st === null || st === undefined) + st = new nexst(); + st._step++; + + return st; +}; + +nexst.prev = function(st) +{ + st._step--; +} + +nexst.reprise = function () +{ + var thiz, args_in, args = []; + + if (arguments.length > 1) { + thiz = arguments[0]; + args_in = arguments[1]; + } + else { + thiz = null; + args_in = arguments[0]; + } + + for (var i = 0 ; i < args_in.length ; i++) { + args[i] = args_in[i]; + } + return args_in.callee.apply(thiz, args); +}; + +nexst.prototype = { + _step: 0, // current step for the async multistep command + _data: null, // data returned at the end of the async command + _subst: null, // substatus for nested async command + _after: null, // callback (as 'arguments' object) to run at the end + // of last step + + step_set: function(step) { + this._step = step; + }, + + step_get: function() { + return this._step; + }, + + data_set: function(data) { + this._data = data; + }, + + data_get: function() { + return this._data; + }, + + subst_set: function(subst) { + this._subst = subst; + }, + + subst_get: function() { + return this._subst; + }, + + after_set: function(after) { + this._after = after; + }, + + after_get: function() { + return this._after; + }, + + done_set: function() { + this.done = true; + }, + + done_get: function() { + return (this.done); + } +}; -- 2.17.1