X-Git-Url: https://jfr.im/git/irc/quakenet/qwebirc.git/blobdiff_plain/b481a99a735825756a5ee72910b7d8f7abb2cf6d..0fc80ad2b9b14a97062e2a7eca6117f09f3234d2:/js/jslib.js diff --git a/js/jslib.js b/js/jslib.js index e2f1e98..89f64ec 100644 --- a/js/jslib.js +++ b/js/jslib.js @@ -1,31 +1,400 @@ -Array.prototype.indexFromEnd = function(d) { - var p = this; - - if(d < 0) - return p[p.length + d]; - - return p[d]; -} - -/* how horribly inefficient */ -String.prototype.replaceAll = function(f, t) { - var i = this.indexOf(f); - var c = this; - - while(i > -1) { - c = c.replace(f, t); - i = c.indexOf(f); - } - return c; -} - -/* how horribly inefficient (again) */ -String.prototype.splitMax = function(by, max) { - var items = this.split(by); - var newitems = items.slice(0, max-1); - - if(items.length >= max) - newitems.push(items.slice(max-1).join(by)); - - return newitems; -} +Array.prototype.indexFromEnd = function(d) { + var p = this; + + if(d < 0) + return p[p.length + 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; + + while(i > -1) { + c = c.replace(f, t); + i = c.indexOf(f); + } + return c; +} + +/* how horribly inefficient (again) */ +String.prototype.splitMax = function(by, max) { + var items = this.split(by); + var newitems = items.slice(0, max-1); + + if(items.length >= max) + newitems.push(items.slice(max-1).join(by)); + + return newitems; +} + +/* returns the arguments */ +qwebirc.util.parseURI = function(uri) { + var result = new QHash(); + + var start = uri.indexOf('?'); + if(start == -1) + return result; + + var querystring = uri.substring(start + 1); + + var args = querystring.split("&"); + + for(var 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