]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/jslib.js
Merge.
[irc/quakenet/qwebirc.git] / js / jslib.js
index 7e75d0355d68f4ff5b3a9439f7f164141e986787..b07fba134c6e3b0b0ab000249975dba4486b7cd7 100644 (file)
@@ -50,7 +50,7 @@ qwebirc.util.parseURI = function(uri) {
   
   var args = querystring.split("&");
   
-  for(i=0;i<args.length;i++) {
+  for(var i=0;i<args.length;i++) {
     var r = args[i].splitMax("=", 2);
     if(r.length < 2)
       continue;
@@ -136,7 +136,7 @@ qwebirc.ui.insertAt = function(position, parent, element) {
   }
 }
 
-qwebirc.util.setAt = function(obj, pos) {
+qwebirc.util.setCaretPos = function(obj, pos) {
   if($defined(obj.selectionStart)) { 
     obj.focus(); 
     obj.setSelectionRange(pos, pos); 
@@ -148,7 +148,7 @@ qwebirc.util.setAt = function(obj, pos) {
 }
 
 qwebirc.util.setAtEnd = function(obj) {
-  qwebirc.util.setAt(obj.value.length);
+  qwebirc.util.setCaretPos(obj.value.length);
 }
 
 qwebirc.util.getCaretPos = function(element) {
@@ -167,3 +167,166 @@ 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;
+}
+
+/* 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<numBytes;i++)
+    l.push(getByte());
+  
+  return l.join("");
+}
+
+qwebirc.util.importJS = function(name, watchFor, onload) {
+  var script = document.createElement("script");
+  script.type = "text/javascript";
+  script.src = name;
+  
+  if(Browser.Engine.trident) {
+    /* HORRID */
+    var checkFn = function() {
+      if(eval("typeof " + watchFor) != "undefined") {
+        onload();
+      } else {
+        checkFn.delay(100);
+      }
+    }
+    checkFn();
+  } else {
+    script.onload = onload;
+  }
+  document.getElementsByTagName("head")[0].appendChild(script);
+}
+
+qwebirc.util.createInput = function(type, parent, name, selected) {
+  var r;
+  if(Browser.Engine.trident) {
+    if(name) {
+      name = " name=\"" + escape(name) + "\"";
+    } else {
+      name = "";
+    }
+    r = $(document.createElement("<input type=\"" + type + "\"" + name + " " + (selected?" checked":"") + "/>"));
+  } else {    
+    r = new Element("input");
+    r.type = type;
+    if(name)
+      r.name = name;
+      
+    if(selected)
+      r.checked = true;
+  }
+    
+  parent.appendChild(r);
+  return r;
+}
+
+/* From: www.webtoolkit.info */
+qwebirc.util.b64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+qwebirc.util.b64Encode = function(data) {
+  var output = [];
+  var table = qwebirc.util.b64Table;
+  for(var i=0;i<data.length;) {
+    var chr1 = data.charCodeAt(i++);
+    var chr2 = data.charCodeAt(i++);
+    var chr3 = data.charCodeAt(i++);
+
+    var enc1 = chr1 >> 2;
+    var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+    var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+    var enc4 = chr3 & 63;
+
+    if(isNaN(chr2)) {
+      enc3 = enc4 = 64;
+    } else if(isNaN(chr3)) {
+      enc4 = 64;
+    }
+
+    output.push(table.charAt(enc1) + table.charAt(enc2) + table.charAt(enc3) + table.charAt(enc4));
+  }
+  return output.join("");
+}
+
+/* From: www.webtoolkit.info */
+qwebirc.util.b64Decode = function(data) {
+  data = data.replace(/[^A-Za-z0-9\+\/\=]/g, "");
+
+  var output = [];
+  var table = qwebirc.util.b64Table;
+  for(var i=0;i<data.length;) {
+    var enc1 = table.indexOf(data.charAt(i++));
+    var enc2 = table.indexOf(data.charAt(i++));
+    var enc3 = table.indexOf(data.charAt(i++));
+    var enc4 = table.indexOf(data.charAt(i++));
+
+    var chr1 = (enc1 << 2) | (enc2 >> 4);
+    var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+    var chr3 = ((enc3 & 3) << 6) | enc4;
+
+    output.push(String.fromCharCode(chr1));
+    if (enc3 != 64)
+      output.push(String.fromCharCode(chr2));
+    if (enc4 != 64)
+      output.push(String.fromCharCode(chr3));
+  }
+
+  return output.join("");
+}
+
+qwebirc.util.composeAnd = function() {
+ var xargs = arguments;
+
+  return function() {
+    for(var i=0;i<xargs.length;i++)
+      if(!xargs[i].apply(this, arguments))
+        return false;
+        
+    return true;
+  }
+}
+
+qwebirc.util.invertFn = function(fn) {
+  return function() {
+    return !fn.apply(this, arguments);
+  }
+}