X-Git-Url: https://jfr.im/git/irc/quakenet/qwebirc.git/blobdiff_plain/8408e8345f9d8e22f894b0b3f93be88e61cb2888..0fc80ad2b9b14a97062e2a7eca6117f09f3234d2:/js/jslib.js diff --git a/js/jslib.js b/js/jslib.js index dd828df..89f64ec 100644 --- a/js/jslib.js +++ b/js/jslib.js @@ -7,8 +7,17 @@ Array.prototype.indexFromEnd = function(d) { return p[d]; } +qwebirc.util.dictCopy = function(d) { + var n = {}; + for(var k in d) + n[k] = d[k]; + + return n; +} + /* how horribly inefficient */ String.prototype.replaceAll = function(f, t) { + //return new RegExp("/" + RegExp.escape(f) + "/g").replace(f, RegExp.escape(t)); var i = this.indexOf(f); var c = this; @@ -30,22 +39,9 @@ String.prototype.splitMax = function(by, max) { return newitems; } -qwebirc.util.setAtEnd = function(obj) { - pos = obj.value.length; - - if(obj.createTextRange) { - var range = obj.createTextRange(); - range.move("character", pos); - range.select(); - } else if(obj.selectionStart) { - obj.focus(); - obj.setSelectionRange(pos, pos); - } -} - /* returns the arguments */ qwebirc.util.parseURI = function(uri) { - var result = {} + var result = new QHash(); var start = uri.indexOf('?'); if(start == -1) @@ -55,16 +51,16 @@ qwebirc.util.parseURI = function(uri) { var args = querystring.split("&"); - for(i=0;i= parent.childNodes.length)) { + parent.appendChild(element); + } else { + parent.insertBefore(element, parent.childNodes[position]); + } +} + +qwebirc.util.setCaretPos = function(obj, pos) { + if($defined(obj.selectionStart)) { + obj.focus(); + obj.setSelectionRange(pos, pos); + } else if(obj.createTextRange) { + var range = obj.createTextRange(); + range.move("character", pos); + range.select(); + } +} + +qwebirc.util.setAtEnd = function(obj) { + qwebirc.util.setCaretPos(obj.value.length); +} + +qwebirc.util.getCaretPos = function(element) { + if($defined(element.selectionStart)) + return element.selectionStart; + + if(document.selection) { + element.focus(); + var sel = document.selection.createRange(); + sel.moveStart("character", -element.value.length); + return sel.text.length; + } +} + +qwebirc.util.browserVersion = function() { + //return "engine: " + Browser.Engine.name + " platform: " + Browser.Platform.name + " user agent: " + navigator.userAgent; + return navigator.userAgent; +} + +qwebirc.util.getEnclosedWord = function(text, position) { + var l = text.split(""); + var buf = []; + + if(text == "") + return; + + var start = position - 1; + if(start < 0) { + /* special case: starting with space */ + start = 0; + } else { + /* work back until we find the first space */ + for(;start>=0;start--) { + if(l[start] == ' ') { + start = start + 1; + break; + } + } + } + + if(start < 0) + start = 0; + + var s = text.substring(start); + var pos = s.indexOf(" "); + if(pos != -1) + s = s.substring(0, pos); + + return [start, s]; +} + +String.prototype.startsWith = function(what) { + return this.substring(0, what.length) == what; +} + +String.prototype.endsWith = function(what) { + return this.substring(this.length - what.length, this.length) == what; +}; + +/* NOT cryptographically secure! */ +qwebirc.util.randHexString = function(numBytes) { + var getByte = function() { + return (((1+Math.random())*0x100)|0).toString(16).substring(1); + }; + + var l = []; + for(var i=0;i"; + r = $(document.createElement(h)); + if (type == "radio") { + r.addEvent("click", function () { + $(document.body).getElements("input[name=" + name + "]").forEach(function (x) { + x.setAttribute("defaultChecked", x.checked ? "defaultChecked" : ""); + }); + }); + } + created = true; + } catch (e) { + /* fallthough, trying it the proper way... */ + } + } + + if(!created) { + r = new Element("input"); + r.setAttribute("type", type); + } + if(name) + r.setAttribute("name", name); + if(id) + r.setAttribute("id", id); + if(selected) { + r.setAttribute("checked", "checked"); + if(type == "radio" && Browser.Engine.trident) + r.setAttribute("defaultChecked", "defaultChecked"); + } + + parent.appendChild(r); + return r; +} + +qwebirc.util.composeAnd = function() { + var xargs = arguments; + + return function() { + for(var i=0;i -1; + } + + for(var i=0;i