edit users button in tenant view
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-util.js
index d03ab14..1d135f4 100644 (file)
@@ -1,5 +1,40 @@
 // misc utility functions
 
+function idInArray(id, arr) {
+    // because sometimes ids are strings and sometimes they're integers
+    for (index in arr) {
+        if (id.toString() == arr[index].toString()) {
+            return true;
+        }
+    }
+    return false;
+}
+
+function assert(outcome, description) {
+    if (!outcome) {
+        console.log(description);
+    }
+}
+
+function templateFromId(id) {
+    return _.template($(id).html());
+}
+
+function firstCharUpper(s) {
+    return s.charAt(0).toUpperCase() + s.slice(1);
+}
+
+function toTitleCase(str)
+{\r
+    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});\r
+}
+
+function fieldNameToHumanReadable(str)
+{
+    str = str.replace("_", " ");
+    return toTitleCase(str);
+}
+
 // http://stackoverflow.com/questions/2117320/set-maximum-displayed-rows-count-for-html-table
 function limitTableRows(tableSelector, maxRows) {
     var table = $(tableSelector)[0] //document.getElementById(tableId);
@@ -22,7 +57,7 @@ function limitTableRows(tableSelector, maxRows) {
     }
 }
 
-function validateField(validatorName, value) {
+function validateField(validatorName, value, obj) {
     if (validatorName=="notBlank") {
         if ((value==undefined) || (value=="")) {
             return "can not be blank";
@@ -41,5 +76,38 @@ function validateField(validatorName, value) {
             }
             break;
     }
+
     return true;
 }
+
+function array_diff(a1, a2)
+{\r
+  var a=[], diff=[];\r
+  for(var i=0;i<a1.length;i++)\r
+    a[a1[i]]=true;\r
+  for(var i=0;i<a2.length;i++)\r
+    if(a[a2[i]]) delete a[a2[i]];\r
+    else a[a2[i]]=true;\r
+  for(var k in a)\r
+    diff.push(k);\r
+  return diff;\r
+}
+
+function array_pair_lookup(x, names, values)
+{
+    for (index in values) {
+        if (values[index] == x) {
+            return names[index];
+        }
+    }
+    return undefined;
+}
+
+function all_options(selector) {
+    el = $(selector);
+    result = [];
+    _.each(el.find("option"), function(option) {
+        result.push($(option).val());
+    });
+    return result;
+}