]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/jslib.js
iframes should not have borders -- breaks page layout
[irc/quakenet/qwebirc.git] / js / jslib.js
index b07fba134c6e3b0b0ab000249975dba4486b7cd7..1094ff2ec440438f8797582869af20c68dc7c507 100644 (file)
@@ -17,6 +17,7 @@ qwebirc.util.dictCopy = function(d) {
 
 /* 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;
  
@@ -40,7 +41,7 @@ String.prototype.splitMax = function(by, max) {
 
 /* returns the arguments */
 qwebirc.util.parseURI = function(uri) {
-  var result = {}
+  var result = new QHash();
 
   var start = uri.indexOf('?');
   if(start == -1)
@@ -55,11 +56,11 @@ qwebirc.util.parseURI = function(uri) {
     if(r.length < 2)
       continue;
       
-    result[unescape(r[0])] = unescape(r[1]);
+    result.put(unescape(r[0]), unescape(r[1]));
   }
   
   return result;
-}
+};
 
 qwebirc.util.DaysOfWeek = {
   0: "Sun",
@@ -102,9 +103,9 @@ qwebirc.util.NBSPCreate = function(text, element) {
 
 qwebirc.util.longtoduration = function(l) {
   var seconds = l % 60;
-  var minutes = Math.round((l % 3600) / 60);
-  var hours = Math.round((l % (3600 * 24)) / 3600);
-  var days = Math.round(l / (24*3600));
+  var minutes = Math.floor((l % 3600) / 60);
+  var hours = Math.floor((l % (3600 * 24)) / 3600);
+  var days = Math.floor(l / (24*3600));
   
   return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
 }
@@ -117,15 +118,33 @@ qwebirc.util.pad = function(x) {
 }
 
 RegExp.escape = function(text) {
-  if(!arguments.callee.sRE) {
-    var specials = [
-      '/', '.', '*', '+', '?', '|',
-      '(', ')', '[', ']', '{', '}', '\\'
-    ];
-    arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
+  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
+};
+
+RegExp.fromIRCPattern = function(t) {
+  /* escape everything but ? . and * */
+  var t = t.replace(/[-[\]{}()+,\\^$|#\s]/g, "\\$&");
+  t = t.split("");
+  var out = [];
+
+  /* now process the rest */
+  for(var i=0;i<t.length;i++) {
+    var c = t[i];
+    switch(c) {
+      case '.':
+        out.push("\\.");
+        break;
+      case '?':
+        out.push(".");
+        break;
+      case '*':
+        out.push(".*");
+        break;
+      default:
+        out.push(c);
+    }
   }
-  
-  return text.replace(arguments.callee.sRE, '\\$1');
+  return out.join("");
 }
 
 qwebirc.ui.insertAt = function(position, parent, element) {
@@ -204,6 +223,10 @@ 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() {
@@ -238,25 +261,49 @@ qwebirc.util.importJS = function(name, watchFor, onload) {
   document.getElementsByTagName("head")[0].appendChild(script);
 }
 
-qwebirc.util.createInput = function(type, parent, name, selected) {
+qwebirc.util.createInput = function(type, parent, name, selected, id) {
+  var created = false;
   var r;
-  if(Browser.Engine.trident) {
-    if(name) {
-      name = " name=\"" + escape(name) + "\"";
+  if (name)
+    name = "__input" + name;
+
+  if (Browser.Engine.trident) {
+    var name2;
+    if (name) {
+      name2 = " name=\"" + escape(name) + "\"";
     } else {
-      name = "";
+      name2 = "";
     }
-    r = $(document.createElement("<input type=\"" + type + "\"" + name + " " + (selected?" checked":"") + "/>"));
-  } else {    
+    try {
+      var h = "<input type=\"" + type + "\"" + name2 + "/>";
+      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.type = type;
-    if(name)
-      r.name = name;
-      
-    if(selected)
-      r.checked = true;
+    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;
 }
@@ -293,6 +340,11 @@ qwebirc.util.b64Decode = function(data) {
 
   var output = [];
   var table = qwebirc.util.b64Table;
+  
+  /* grossly inefficient... so sue me */
+  while(data.length % 4 != 0)
+    data = data + "=";
+    
   for(var i=0;i<data.length;) {
     var enc1 = table.indexOf(data.charAt(i++));
     var enc2 = table.indexOf(data.charAt(i++));
@@ -330,3 +382,76 @@ qwebirc.util.invertFn = function(fn) {
     return !fn.apply(this, arguments);
   }
 }
+
+qwebirc.util.deviceHasKeyboard = function() {
+  var determine = function() {
+    if(Browser.Engine.ipod)
+      return true;
+
+    var MOBILE_UAs = ["Nintendo Wii", " PIE", "BlackBerry", "IEMobile", "Windows CE", "Nokia", "Opera Mini", "Mobile", "mobile", "Pocket", "pocket", "Android"];
+    /* safari not included because iphones/ipods send that, and we checked for iphone/ipod specifically above */
+    var DESKTOP_UAs = ["Chrome", "Firefox", "Camino", "Iceweasel", "K-Meleon", "Konqueror", "SeaMonkey", "Windows NT", "Windows 9"];
+
+    var ua = navigator.userAgent;
+
+    var contains = function(v) {
+      return ua.indexOf(v) > -1;
+    }
+
+    for(var i=0;i<MOBILE_UAs.length;i++)
+      if(contains(MOBILE_UAs[i]))
+        return false;
+      
+    for(var i=0;i<DESKTOP_UAs.length;i++)
+      if(contains(DESKTOP_UAs[i]))
+        return true;
+      
+    return false;
+  };
+  var v = determine();
+  
+  qwebirc.util.deviceHasKeyboard = function() {
+    return v;
+  }
+  
+  return v;
+}
+
+qwebirc.util.generateID_ID = 0;
+qwebirc.util.generateID = function() {
+  return "qqa-" + qwebirc.util.generateID_ID++;
+};
+
+qwebirc.util.arrayCmp = function(a, b) {
+  for(var p=0;p<a.length;p++) {
+    var ap = a[p];
+    var bp = b[p];
+    if(ap == bp)
+      continue;
+
+    if(ap < bp)
+      return -1;
+
+    return 1;
+  }
+  return 0;
+};
+
+qwebirc.util.__log = function(x) {
+  if(QWEBIRC_DEBUG) {
+    if(typeof console == "undefined") {
+      alert("log: " + x);
+    } else {
+      console.log(x);
+    }
+  }
+};
+
+qwebirc.util.logger = {
+  log: function(x) { qwebirc.util.__log("L " + x) },
+  info: function(x) { qwebirc.util.__log("I " + x) },
+  error: function(x) { qwebirc.util.__log("E " + x) },
+  warn: function(x) { qwebirc.util.__log("W " + x) }
+};
+
+qwebirc.util.log = qwebirc.util.logger.log;