]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/jslib.js
Merge pull request #373 from retropc/kill_flash
[irc/quakenet/qwebirc.git] / js / jslib.js
index 52e3550023af49aa37f6ea23b1fae053de633c6b..89f64ec0b1bb408beb7ed2b7a78cc33c8c13abcf 100644 (file)
@@ -223,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() {
@@ -258,94 +262,50 @@ qwebirc.util.importJS = function(name, watchFor, onload) {
 }
 
 qwebirc.util.createInput = function(type, parent, name, selected, id) {
+  var created = false;
   var r;
-  if(Browser.Engine.trident) {
-    if(name) {
-      name = " name=\"" + escape(name) + "\"";
-    } else {
-      name = "";
-    }
-    if(id) {
-      id = " id=\"" + escape(id) + "\"";
+  if (name)
+    name = "__input" + name;
+
+  if (Browser.Engine.trident) {
+    var name2;
+    if (name) {
+      name2 = " name=\"" + escape(name) + "\"";
     } else {
-      id = "";
+      name2 = "";
     }
     try {
-      return $(document.createElement("<input type=\"" + type + "\"" + name + id + " " + (selected?" checked":"") + "/>"));
-    } catch(e) {
+      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... */
     }
   }
-  
-  r = new Element("input");
-  r.type = type;
-  if(name)
-    r.name = name;
-  if(id)
-    r.id = id;
-      
-  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));
+  if(!created) {
+    r = new Element("input");
+    r.setAttribute("type", type);
   }
-  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;
-  
-  /* 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++));
-    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));
+  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");
   }
 
-  return output.join("");
+  parent.appendChild(r);
+  return r;
 }
 
 qwebirc.util.composeAnd = function() {
@@ -403,7 +363,22 @@ qwebirc.util.deviceHasKeyboard = function() {
 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) {