From 41edd0a84f173eade46897e373c91906fb382ac2 Mon Sep 17 00:00:00 2001 From: hallgren Date: Tue, 26 Oct 2010 11:52:04 +0000 Subject: [PATCH] support.js: some enhancements and new functions + ajax_http_get(url,callback) now checks the HTTP response status and shows an error message if the status is not OK (is not 200) + New function node(tag,attrs,children) to create document nodes with arbitraty attributes and children. The functions node() and text() should now be enough to construct arbitrary document trees in an easy, functional way, and many of the more specialized functions could be removed. + When calling the function button(label,action,key), the action can be a function instead of a string, so you can use local/anonymous functions. + New function debug(str) adds text to the element with id=debug, if it exists. --- src/runtime/javascript/minibar/support.js | 89 ++++++++++++----------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/src/runtime/javascript/minibar/support.js b/src/runtime/javascript/minibar/support.js index 364a7fb88..0857526d3 100644 --- a/src/runtime/javascript/minibar/support.js +++ b/src/runtime/javascript/minibar/support.js @@ -54,20 +54,23 @@ function GetXmlHttpObject(handler) } function ajax_http_get(url,callback) { - var http=GetXmlHttpObject() - if (http==null) { - alert ("Browser does not support HTTP Request") - return - } - var statechange=function() { - if (http.readyState==4 || http.readyState=="complete") - callback(http.responseText) - } - http.onreadystatechange=statechange - http.open("GET",url,true) - http.send(null) - //dump("http get "+url+"\n") - return http + var http=GetXmlHttpObject() + if (http==null) { + alert ("Browser does not support HTTP Request") + return + } + var statechange=function() { + if (http.readyState==4 || http.readyState=="complete") { + if(http.status==200) callback(http.responseText); + else alert("Request for "+url+" failed: " + +http.status+" "+http.statusText); + } + } + http.onreadystatechange=statechange; + http.open("GET",url,true) + http.send(null) + //dump("http get "+url+"\n") + return http } // JSON via AJAX @@ -89,10 +92,17 @@ function http_get_json(url,cont) { /* --- HTML construction ---------------------------------------------------- */ function text(s) { return document.createTextNode(s); } +function node(tag,as,ds) { + var n=document.createElement(tag); + for(var a in as) n.setAttribute(a,as[a]); + for(var i in ds) n.appendChild(ds[i]); + return n; +} + function empty(tag,name,value) { - var el=document.createElement(tag); - if(name && value) el.setAttribute(name,value); - return el; + var el=node(tag,{},[]) + if(name && value) el.setAttribute(name,value); + return el; } function empty_id(tag,id) { return empty(tag,"id",id); } @@ -101,11 +111,7 @@ function empty_class(tag,cls) { return empty(tag,"class",cls); } function div_id(id) { return empty_id("div",id); } function span_id(id) { return empty_id("span",id); } -function wrap(tag,contents) { - var el=empty(tag); - el.appendChild(contents); - return el; -} +function wrap(tag,contents) { return node(tag,{},[contents]); } function wrap_class(tag,cls,contents) { var el=empty_class(tag,cls); @@ -123,39 +129,36 @@ function li(contents) { return wrap("li",contents); } function th(contents) { return wrap("th",contents); } function td(contents) { return wrap("td",contents); } -function tr(cells) { - var tr=empty("tr"); - for(var i=0;i