make field names in the generic detail view look nice
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-util.js
1 // misc utility functions
2
3 function assert(outcome, description) {
4     if (!outcome) {
5         console.log(description);
6     }
7 }
8
9 function templateFromId(id) {
10     return _.template($(id).html());
11 }
12
13 function firstCharUpper(s) {
14     return s.charAt(0).toUpperCase() + s.slice(1);
15 }
16
17 function toTitleCase(str)
18 {\r
19     return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});\r
20 }
21
22 function fieldNameToHumanReadable(str)
23 {
24     str = str.replace("_", " ");
25     return toTitleCase(str);
26 }
27
28 // http://stackoverflow.com/questions/2117320/set-maximum-displayed-rows-count-for-html-table
29 function limitTableRows(tableSelector, maxRows) {
30     var table = $(tableSelector)[0] //document.getElementById(tableId);
31     var wrapper = table.parentNode;
32     var rowsInTable = table.rows.length;
33     try {
34         var border = getComputedStyle(table.rows[0].cells[0], '').getPropertyValue('border-top-width');
35         border = border.replace('px', '') * 1;
36     } catch (e) {
37         var border = table.rows[0].cells[0].currentStyle.borderWidth;
38         border = (border.replace('px', '') * 1) / 2;
39     }
40     var height = 0;
41     if (rowsInTable > maxRows) {
42         for (var i = 0; i < maxRows; i++) {
43             height += table.rows[i].clientHeight + border;
44             //console.log("XXX " + height + " " + table.rows[i].clientHeight + " " + border);
45         }
46         wrapper.style.height = height + "px";
47     }
48 }
49
50 function validateField(validatorName, value, obj) {
51     if (validatorName=="notBlank") {
52         if ((value==undefined) || (value=="")) {
53             return "can not be blank";
54         }
55     }
56
57     // if notBlank wasn't set, and the field is blank, then we can return
58     if ((value==undefined) || (value=="")) {
59         return true;
60     }
61
62     switch (validatorName) {
63         case "url":
64             if (! /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value)) {
65                 return "must be a valid url";
66             }
67             break;
68     }
69
70     return true;
71 }