diff options
author | Christian Schneider | 2007-10-11 00:39:30 +0000 |
---|---|---|
committer | Christian Schneider | 2007-10-11 00:39:30 +0000 |
commit | 35fe33f7364329dacf415c950bff01b6de9ef88e (patch) | |
tree | b0e6b018b50038ca20266723c53750268f508df5 /itjs/it.js | |
parent | 1f95711ff3e9697cd85a54545ab42e5fd3611317 (diff) | |
download | itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.gz itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.bz2 itools-35fe33f7364329dacf415c950bff01b6de9ef88e.zip |
Populated release branch
Diffstat (limited to 'itjs/it.js')
-rw-r--r-- | itjs/it.js | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/itjs/it.js b/itjs/it.js new file mode 100644 index 0000000..a43ba3a --- /dev/null +++ b/itjs/it.js @@ -0,0 +1,177 @@ +// $Id$ + +/** + * Clear contents of element 'jsdebug' + */ +function CED(txt) +{ + var element = document.getElementById('jsdebug'); + if (element) + element.innerHTML = txt ? txt : ""; +} + +/** + * Add debugging output to element 'jsdebug' + */ +function ED() +{ + var element = document.getElementById('jsdebug'); + if (element) + { + var text = ""; + + for (var i = 0; i < arguments.length; i++) + { + var variable = arguments[i]; + + if (typeof variable == "string") + variable = variable.replace(/&/g, '&').replace(/</g, '<'); + + text += (typeof variable) + " " + variable; + + if (typeof variable == "object") + { + text += ":"; + + for (field in variable) + { + text += field + "="; + + try { text += typeof variable[field] == 'function' ? 'function' : variable[field]; } + catch (e) { text += "*" + e + "*"; } + + text += "\n"; + } + text += "\n"; + } + + text += "\n"; + } + + element.innerHTML += '<pre style="background-color:#FEE; margin:0">' + text + '</pre>'; + } +} + +/** + * Quote HTML special chars + * @return Text string with & " < > htmlentities-encoded + */ +function Q(value) +{ + return typeof value == "undefined" ? "" : value.toString().replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); +} + +/** + * String class: Replaces variables of the form {var} with values from given array + * @param values Associative array containing values to fill in (optional) + * @return Text string with variables replaced by their values + */ +String.prototype.T = function(values) +{ + var result = this; + + for (key in values) + result = result.replace(new RegExp("{" + key + "}", "g"), values[key]); + + return result; +} + +/** + * Insert an event handler on top of chain + * @param p.element Element to handle event for + * @param p.event Name of event:'focus', 'click', ... (without 'on') + * @param p.object Object that contains handler method + * @param p.method Method of p.object to call on p.event + */ +function it_event(p) +{ + var oldhandler = p.element["on" + p.event]; + + p.element["on" + p.event] = function(ev) + { + var pp = arguments.callee.p ? arguments.callee.p : p; + var oo = arguments.callee.oldhandler ? arguments.callee.oldhandler : oldhandler; + + var result = pp.object[pp.method](ev ? ev : window.event, pp); + + if (result && oo) + result = oo(ev); + + return result; + } + p.element["on" + p.event].p = p; + p.element["on" + p.event].oldhandler = oldhandler; +} + +/* Get object pixel position. Based on quirksmode.org's code */ +function it_get_obj_x(obj) +{ + var curleft = 0; + if (obj.offsetParent) + while (obj) + { + curleft += obj.offsetLeft; + obj = obj.offsetParent; + } + else if (obj.x) + curleft += obj.x; + return curleft; +} + +function it_get_obj_y(obj) +{ + var curtop = 0; + if (obj.offsetParent) + while (obj) + { + curtop += obj.offsetTop; + obj = obj.offsetParent; + } + else if (obj.y) + curtop += obj.y; + return curtop; +} + +/* Get an iframe's content document in a compatible way */ +function it_get_iframe_document(iframe) +{ + return iframe.contentWindow ? iframe.contentWindow.document : iframe.contentDocument; +} + +/** + * Copy attributes from src to dst in a recursive manner. + * @param dst Destination object which gets attributes + * @param src Source object containing attributes + */ +function it_set(dst, src) +{ + if (dst) + { + for (var i in src) + { + if (typeof src[i] == 'object') + it_set(dst[i], src[i]); + else + dst[i] = src[i]; + } + } +} + +/** + * Return the current timestamp + */ +function it_now() +{ + return new Date().getTime(); +} + +/** + * Encodes arbitrary string for use in an url + * @param str string to be encoded + */ +function it_url_encode(str) +{ + var result = window.encodeURIComponent ? encodeURIComponent(str) : escape(str).replace(/\+/g, "%2B"); + + return result.replace(/%20/gi, "+").replace(/%2C/gi, ",").replace(/%28/gi, "(").replace(/%29/gi, ")"); +} |