commit 83fce364225367bcf4f5dfc3fd92eedd6253a628
parent d8d285f05791f97d75fbbc965a60dd2cc78c9860
Author: Alexander Burger <abu@software-lab.de>
Date: Thu, 9 May 2013 20:30:46 +0200
JavaScript 'lisp' mechanisms: Support GET and POST requests
Diffstat:
5 files changed, 89 insertions(+), 77 deletions(-)
diff --git a/ersatz/picolisp.jar b/ersatz/picolisp.jar
Binary files differ.
diff --git a/lib/form.js b/lib/form.js
@@ -1,19 +1,14 @@
-/* 08may13abu
+/* 09may13abu
* (c) Software Lab. Alexander Burger
*/
-var SesId;
var FormReq = new XMLHttpRequest();
-var HintReq = new XMLHttpRequest();
-var LispReq = new XMLHttpRequest();
-
FormReq.upload.addEventListener("progress", dropProgress, false);
FormReq.upload.addEventListener("load", dropLoad, false);
-var Queue = new Array();
-var Btn = new Array();
-var Key, InBtn, Auto, Drop;
-var Hint, Hints, Item, Beg, End;
+var Btn = [];
+var Queue = [];
+var SesId, Key, InBtn, Auto, Drop, Hint, Hints, Item, Beg, End;
function inBtn(flg) {InBtn = flg;}
@@ -32,7 +27,7 @@ function formKey(event) {
function fldChg(field) {
if (!InBtn && Key != 13)
- post(field.form, null);
+ post(field.form, null, null);
return true;
}
@@ -51,7 +46,7 @@ function doDrop(btn, event) {
if (event.dataTransfer.files.length != 0) {
Btn.push(Drop = btn);
btn.value = "0 %";
- post(btn.form, event.dataTransfer.files[0]);
+ post(btn.form, null, event.dataTransfer.files[0]);
}
}
@@ -90,16 +85,16 @@ function doPost(form) {
if (Btn[i].form == form)
break;
}
- return post(form, null);
+ return post(form, null, null);
}
-function post(form, file) {
- var i;
+function post(form, exe, file) {
+ var i, data;
if (!FormReq || !hasElement(form,"*Get") || (i = form.action.indexOf("~")) <= 0)
return true;
if (FormReq.readyState > 0 && FormReq.readyState < 4) {
- Queue.push(form);
+ Queue.push([form, exe, file]);
return false;
}
form.style.cursor = "wait";
@@ -119,9 +114,9 @@ function post(form, file) {
var r = txt[0].split(":");
if (Auto)
- window.clearTimeout(Auto);
+ clearTimeout(Auto);
if (r[1])
- Auto = window.setTimeout("document.getElementById(\"" + r[0] + "\").click()", r[1]);
+ Auto = setTimeout("document.getElementById(\"" + r[0] + "\").click()", r[1]);
}
for (i = 1; i < txt.length;) {
var fld = txt[i++];
@@ -263,10 +258,18 @@ function post(form, file) {
}
}
form.style.cursor = "";
- if (Queue.length > 0)
- post(Queue.shift(), null);
+ if (Queue.length > 0) {
+ var a = Queue.shift();
+ post(a[0], a[1], a[2]);
+ }
+ }
+ if (!exe)
+ data = "";
+ else {
+ data = "*Gui:0:=" + exe[0];
+ for (var i = 1; i < exe.length; ++i)
+ data += "&*JsArgs:" + i + ":=" + exe[i];
}
- var data = "";
for (i = 0; i < Btn.length;)
if (Btn[i].form != form)
++i;
@@ -314,41 +317,37 @@ function post(form, file) {
/*** Hints ***/
function doHint(field) {
- if (HintReq) {
- if (!Hint) {
- Hint = document.createElement("div");
- Hint.setAttribute("class", "hint");
- Hint.style.visibility = "hidden";
- Hint.style.position = "absolute";
- Hint.style.zIndex = 9999;
- Hint.style.textAlign = "left";
- Hints = document.createElement("div");
- Hints.setAttribute("class", "hints");
- Hints.style.position = "relative";
- Hints.style.top = "-2px";
- Hints.style.left = "-3px";
- Hint.appendChild(Hints);
- }
- field.parentNode.appendChild(Hint);
- field.onblur = function() {
- Hint.style.visibility = "hidden";
- }
- var top = field.offsetHeight + 2;
- var left = 3;
- for (var obj = field; obj.id != "main" && obj.id != "menu"; obj = obj.offsetParent) {
- top += obj.offsetTop;
- left += obj.offsetLeft;
- }
- Hint.style.top = top + "px";
- Hint.style.left = left + "px";
+ if (!Hint) {
+ Hint = document.createElement("div");
+ Hint.setAttribute("class", "hint");
+ Hint.style.visibility = "hidden";
+ Hint.style.position = "absolute";
+ Hint.style.zIndex = 9999;
+ Hint.style.textAlign = "left";
+ Hints = document.createElement("div");
+ Hints.setAttribute("class", "hints");
+ Hints.style.position = "relative";
+ Hints.style.top = "-2px";
+ Hints.style.left = "-3px";
+ Hint.appendChild(Hints);
+ }
+ field.parentNode.appendChild(Hint);
+ field.onblur = function() {
+ Hint.style.visibility = "hidden";
}
+ var top = field.offsetHeight + 2;
+ var left = 3;
+ for (var obj = field; obj.id != "main" && obj.id != "menu"; obj = obj.offsetParent) {
+ top += obj.offsetTop;
+ left += obj.offsetLeft;
+ }
+ Hint.style.top = top + "px";
+ Hint.style.left = left + "px";
}
function hintKey(field, event, tok, coy) {
var i, data;
- if (!HintReq)
- return true;
if (event.keyCode == 9 || event.keyCode == 27)
return false;
if (Hint.style.visibility == "visible") {
@@ -374,8 +373,7 @@ function hintKey(field, event, tok, coy) {
}
if (event.keyCode == 13)
return true;
- if (HintReq.readyState > 0 && HintReq.readyState < 4)
- return false;
+ var req = new XMLHttpRequest();
if (tok) {
for (Beg = field.selectionStart; Beg > 0 && !field.value.charAt(Beg-1).match(/\s/); --Beg);
End = field.selectionEnd;
@@ -393,15 +391,15 @@ function hintKey(field, event, tok, coy) {
return false;
}
try {
- HintReq.open("POST", (SesId? SesId : "") +
+ req.open("POST", (SesId? SesId : "") +
((i = field.id.lastIndexOf("-")) < 0?
"!jsHint?$" + field.id : "!jsHint?+" + field.id.substr(i+1) ) );
}
catch (e) {return true;}
- HintReq.onload = function() {
+ req.onload = function() {
var i, n, lst, str;
- if ((str = HintReq.responseText).length == 0)
+ if ((str = req.responseText).length == 0)
Hint.style.visibility = "hidden";
else {
lst = str.split("&");
@@ -427,9 +425,9 @@ function hintKey(field, event, tok, coy) {
else if (fld.name == "*Form")
data += "&*Form=" + fld.value;
}
- try {HintReq.send(data);}
+ try {req.send(data);}
catch (e) {
- HintReq.abort();
+ req.abort();
return true;
}
return (event.keyCode != 45); // INS
@@ -465,7 +463,7 @@ function addHint(i, field, str) {
function setHint(field, item) {
Hint.style.visibility = "hidden";
field.value = field.value.substr(0,Beg) + item.firstChild.nodeValue + field.value.substr(End);
- post(field.form, null);
+ post(field.form, null, null);
field.setSelectionRange(Beg + item.firstChild.nodeValue.length, field.value.length);
field.focus();
field.onchange = function() {
@@ -486,22 +484,27 @@ function hintOff(i) {
}
/*** Lisp calls ***/
-function lisp(form, url) {
- if (!LispReq)
- return true;
+function lisp(form, fun) {
+ if (form) {
+ var exe = [fun];
+
+ for (var i = 2; i < arguments.length; ++i)
+ exe[i-1] = encodeURIComponent(arguments[i]);
+ return post(form, exe, null);
+ }
if (arguments.length > 2) {
- url += "?" + arguments[2]; // arguments should be properly encoded
+ fun += "?" + encodeURIComponent(arguments[2]).replace(/-/,"%2D");
for (var i = 3; i < arguments.length; ++i)
- url += "&" + arguments[i];
+ fun += "&" + encodeURIComponent(arguments[i]).replace(/-/,"%2D");
}
- try {LispReq.open("GET", SesId + "!" + url);}
+ var req = new XMLHttpRequest();
+ try {req.open("GET", SesId + "!" + fun);}
catch (e) {return true;}
- if (form)
- LispReq.onload = function() {post(form, null);}
- try {LispReq.send(null);}
+ try {req.send(null);}
catch (e) {
- LispReq.abort();
+ req.abort();
return true;
}
+ setTimeout(function(){req.abort();}, 40); // No response expected
return false;
}
diff --git a/lib/form.l b/lib/form.l
@@ -1,4 +1,4 @@
-# 07may13abu
+# 09may13abu
# (c) Software Lab. Alexander Burger
# *PRG *Top *Gui *Btn *Get *Got *Form *Evt *Lock *Spans
@@ -7,7 +7,9 @@
(allow "@img/" T)
(push1 '*JS (allow "@lib/form.js"))
(mapc allow
- '(*Gui *Get *Got *Form *Evt *Drop "!jsForm" *JsHint "!jsHint" "!tzOffs") )
+ (quote
+ *Gui *Get *Got *Form "!jsForm" *Evt *Drop
+ *JsHint "!jsHint" *JsArgs "!tzOffs" ) )
(one "*Cnt")
(off "*Lst" "*Post2" "*Cho" "*TZO")
@@ -16,7 +18,7 @@
("+---" "-+--" "--+-" "---+" "--+-" "-+--" .) )
(de tzOffs (Min)
- (setq "*TZO" (* Min 60)) )
+ (setq "*TZO" (* 60 (format Min))) )
# Define GUI form
(de form ("Attr" . "Prg")
@@ -208,10 +210,12 @@
(de postGui ()
(if "*Post2"
(off *Gui "*Post2")
- (let *Btn NIL
+ (let (Fun NIL *Btn NIL)
(for G *Gui
- (and (lt0 (car G)) (setq *Btn (cdr G)))
- (con (assoc (car G) (val *PRG)) (cdr G)) )
+ (if (=0 (car G))
+ (setq Fun (cdr G))
+ (and (lt0 (car G)) (setq *Btn (cdr G)))
+ (con (assoc (car G) (val *PRG)) (cdr G)) ) )
(off *Gui)
(job (: env)
(for This (: gui)
@@ -234,6 +238,11 @@
(=: able (off *Lock))
(sync)
(tell) ) )
+ (when Fun
+ (when (and *Allow (not (idx *Allow Fun)))
+ (notAllowed Fun)
+ (throw "stop") )
+ (apply (intern Fun) (mapcar cdr *JsArgs)) )
(for This (: gui)
(nond
((: id) (setq "*Chart" This))
@@ -285,7 +294,7 @@
(if "*TZO"
(tim$ (% (- Tim -86400 @) 86400))
(javascript NIL
- "lisp(null, 'tzOffs', '+' + (new Date()).getTimezoneOffset());" )
+ "lisp(null, 'tzOffs', (new Date()).getTimezoneOffset());" )
(pack (tim$ (% Tim 86400)) " UTC") ) ) ) )
# Return chart property
diff --git a/src/vers.h b/src/vers.h
@@ -1 +1 @@
-static byte Version[4] = {3,1,2,6};
+static byte Version[4] = {3,1,2,7};
diff --git a/src64/version.l b/src64/version.l
@@ -1,6 +1,6 @@
-# 07may13abu
+# 09may13abu
# (c) Software Lab. Alexander Burger
-(de *Version 3 1 2 6)
+(de *Version 3 1 2 7)
# vi:et:ts=3:sw=3