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.
This commit is contained in:
hallgren
2010-10-26 11:52:04 +00:00
parent 161dabcba1
commit 41edd0a84f

View File

@@ -54,20 +54,23 @@ function GetXmlHttpObject(handler)
} }
function ajax_http_get(url,callback) { function ajax_http_get(url,callback) {
var http=GetXmlHttpObject() var http=GetXmlHttpObject()
if (http==null) { if (http==null) {
alert ("Browser does not support HTTP Request") alert ("Browser does not support HTTP Request")
return return
} }
var statechange=function() { var statechange=function() {
if (http.readyState==4 || http.readyState=="complete") if (http.readyState==4 || http.readyState=="complete") {
callback(http.responseText) if(http.status==200) callback(http.responseText);
} else alert("Request for "+url+" failed: "
http.onreadystatechange=statechange +http.status+" "+http.statusText);
http.open("GET",url,true) }
http.send(null) }
//dump("http get "+url+"\n") http.onreadystatechange=statechange;
return http http.open("GET",url,true)
http.send(null)
//dump("http get "+url+"\n")
return http
} }
// JSON via AJAX // JSON via AJAX
@@ -89,10 +92,17 @@ function http_get_json(url,cont) {
/* --- HTML construction ---------------------------------------------------- */ /* --- HTML construction ---------------------------------------------------- */
function text(s) { return document.createTextNode(s); } 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) { function empty(tag,name,value) {
var el=document.createElement(tag); var el=node(tag,{},[])
if(name && value) el.setAttribute(name,value); if(name && value) el.setAttribute(name,value);
return el; return el;
} }
function empty_id(tag,id) { return empty(tag,"id",id); } 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 div_id(id) { return empty_id("div",id); }
function span_id(id) { return empty_id("span",id); } function span_id(id) { return empty_id("span",id); }
function wrap(tag,contents) { function wrap(tag,contents) { return node(tag,{},[contents]); }
var el=empty(tag);
el.appendChild(contents);
return el;
}
function wrap_class(tag,cls,contents) { function wrap_class(tag,cls,contents) {
var el=empty_class(tag,cls); 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 th(contents) { return wrap("th",contents); }
function td(contents) { return wrap("td",contents); } function td(contents) { return wrap("td",contents); }
function tr(cells) { function tr(cells) { return node("tr",{},cells); }
var tr=empty("tr");
for(var i=0;i<cells.length;i++)
tr.appendChild(cells[i]);
return tr;
}
function button(label,action,key) { function button(label,action,key) {
var el=empty("input","type","button"); var el=node("input",{"type":"button","value":label},[]);
el.setAttribute("value",label); if(typeof action=="string") el.setAttribute("onclick",action);
el.setAttribute("onclick",action); else el.onclick=action;
if(key) el.setAttribute("accesskey",key); if(key) el.setAttribute("accesskey",key);
return el;
}
function option(label,value) {
var el=empty("option","value",value);
el.innerHTML=label;
return el; return el;
} }
function appendChildren(el,cs) { function option(label,value) {
for(var i=0;i<cs.length;i++) return node("option",{"value":value},[text(label)]);
el.appendChild(cs[i]);
return el;
} }
function tda(cs) { return appendChildren(empty("td"),cs); } function appendChildren(el,ds) {
for(var i in ds) el.appendChild(ds[i]);
return el;
}
function tda(cs) { return node("td",{},cs); }
function img(src) { return empty("img","src",src); } function img(src) { return empty("img","src",src); }
/* --- Debug ---------------------------------------------------------------- */ /* --- Debug ---------------------------------------------------------------- */
function debug(s) {
var d=element("debug");
if(d) d.appendChild(text(s+"\n"))
}
function show_props(obj, objName) { function show_props(obj, objName) {
var result = ""; var result = "";
for (var i in obj) { for (var i in obj) {