add raw error dialog support, only add model to collection once it has been saved
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-util.js
1 // misc utility functions
2
3 // http://stackoverflow.com/questions/2117320/set-maximum-displayed-rows-count-for-html-table
4 function limitTableRows(tableSelector, maxRows) {
5     var table = $(tableSelector)[0] //document.getElementById(tableId);
6     var wrapper = table.parentNode;
7     var rowsInTable = table.rows.length;
8     try {
9         var border = getComputedStyle(table.rows[0].cells[0], '').getPropertyValue('border-top-width');
10         border = border.replace('px', '') * 1;
11     } catch (e) {
12         var border = table.rows[0].cells[0].currentStyle.borderWidth;
13         border = (border.replace('px', '') * 1) / 2;
14     }
15     var height = 0;
16     if (rowsInTable > maxRows) {
17         for (var i = 0; i < maxRows; i++) {
18             height += table.rows[i].clientHeight + border;
19             //console.log("XXX " + height + " " + table.rows[i].clientHeight + " " + border);
20         }
21         wrapper.style.height = height + "px";
22     }
23 }
24
25 function validateField(validatorName, value, obj) {
26     if (validatorName=="notBlank") {
27         if ((value==undefined) || (value=="")) {
28             return "can not be blank";
29         }
30     }
31
32     // if notBlank wasn't set, and the field is blank, then we can return
33     if ((value==undefined) || (value=="")) {
34         return true;
35     }
36
37     switch (validatorName) {
38         case "url":
39             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)) {
40                 return "must be a valid url";
41             }
42             break;
43     }
44
45     return true;
46 }