1 // misc utility functions
3 function idInArray(id, arr) {
4 // because sometimes ids are strings and sometimes they're integers
6 if (id.toString() == arr[index].toString()) {
13 function assert(outcome, description) {
15 console.log(description);
19 function templateFromId(id) {
20 return _.template($(id).html());
23 function firstCharUpper(s) {
24 return s.charAt(0).toUpperCase() + s.slice(1);
27 function toTitleCase(str)
29 return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
\r
32 function fieldNameToHumanReadable(str)
34 str = str.replace("_", " ");
35 return toTitleCase(str);
38 // http://stackoverflow.com/questions/2117320/set-maximum-displayed-rows-count-for-html-table
39 function limitTableRows(tableSelector, maxRows) {
40 var table = $(tableSelector)[0] //document.getElementById(tableId);
41 var wrapper = table.parentNode;
42 var rowsInTable = table.rows.length;
44 var border = getComputedStyle(table.rows[0].cells[0], '').getPropertyValue('border-top-width');
45 border = border.replace('px', '') * 1;
47 var border = table.rows[0].cells[0].currentStyle.borderWidth;
48 border = (border.replace('px', '') * 1) / 2;
51 if (rowsInTable > maxRows) {
52 for (var i = 0; i < maxRows; i++) {
53 height += table.rows[i].clientHeight + border;
54 //console.log("XXX " + height + " " + table.rows[i].clientHeight + " " + border);
56 wrapper.style.height = height + "px";
60 function validateField(validatorName, value, obj) {
61 if (validatorName=="notBlank") {
62 if ((value==undefined) || (value=="")) {
63 return "can not be blank";
67 // if notBlank wasn't set, and the field is blank, then we can return
68 if ((value==undefined) || (value=="")) {
72 switch (validatorName) {
74 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)) {
75 return "must be a valid url";
80 if (! $.trim(value).match(portlist_regexp())) {
81 return "must be a valid portspec (example: 'tcp 123, udp 456-789')"
89 function array_diff(a1, a2)
92 for(var i=0;i<a1.length;i++)
\r
94 for(var i=0;i<a2.length;i++)
\r
95 if(a[a2[i]]) delete a[a2[i]];
\r
102 function array_subtract(a1, a2)
107 if (!$.inArray(value, a2) >= 0) {
114 function array_same_elements(arr1, arr2)
116 // return true if arrays have same elements, even if order is different
117 return ($(arr1).not(arr2).length == 0 && $(arr2).not(arr1).length == 0);
120 function array_pair_lookup(x, names, values)
122 for (index in values) {
123 if (values[index] == x) {
127 return "object #" + x;
130 function all_options(selector) {
133 _.each(el.find("option"), function(option) {
134 result.push($(option).val());
139 function make_same_width(containerSelector, itemSelector) {
141 $(containerSelector).find(itemSelector).each( function(index) { maxWidth = Math.max(maxWidth, $(this).width()); });
\r
142 console.log(maxWidth);
\r
143 $(containerSelector).find(itemSelector).each( function(index) { $(this).width(maxWidth); });
146 function parse_portlist(ports) {
147 /* Support a list of ports in the format "protocol:port, protocol:port, ..."
151 tcp 123, tcp 124, tcp 125, udp 201, udp 202
153 User can put either a "/" or a " " between protocol and ports
154 Port ranges can be specified with "-" or ":"
156 This is a straightforward port of the code in core/models/network.py
161 parts = ports.split(",")
162 $.each(parts, function(index, part) {
164 if (part.indexOf("/")>=0) {
165 parts2 = part.split("/",2);
168 } else if (part.indexOf(" ")>=0) {
169 parts2 = part.split(" +",2);
173 throw 'malformed port specifier ' + part + ', format example: "tcp 123, tcp 201:206, udp 333"';
176 protocol = $.trim(protocol);
177 ports = $.trim(ports);
181 if (protocol!="tcp" && protocol!="udp") {
182 throw 'unknown protocol ' + protocol;
185 if (ports.indexOf("-")>=0) {
186 parts2 = ports.split("-");
187 first = parseInt($.trim(parts2[0]));
188 last = parseInt($.trim(parts2[1]));
189 portStr = first + ":" + last;
190 } else if (ports.indexOf(":")>=0) {
191 parts2 = ports.split(":");
192 first = parseInt($.trim(parts2[0]));
193 last = parseInt($.trim(parts2[1]));
194 portStr = first + ":" + last;
196 portStr = parseInt(ports).toString();
199 nats.push( {l4_protocol: protocol, l4_port: portStr} );
200 }); /* end $.each(ports) */
205 function portlist_regexp() {
206 /* this constructs the big complicated regexp that validates port
207 specifiers. Saved here in long form, in case we need to change it
211 paren = function(x) { return "(?:" + x + ")"; }
213 protocol = paren("tcp|udp");
214 protocolSlash = protocol + paren(whitespace + "|\/");
215 numbers = paren("[0-9]+");
216 range = paren(numbers + paren("-|:") + numbers);
217 numbersOrRange = paren(numbers + "|" + range);
218 protoPorts = paren(protocolSlash + numbersOrRange);
219 protoPortsCommas = paren(paren(protoPorts + "," + whitespace)+"+");
220 multiProtoPorts = paren(protoPortsCommas + protoPorts);
221 portSpec = "^" + paren(protoPorts + "|" + multiProtoPorts) + "$";
222 return RegExp(portSpec);
225 function portlist_selftest() {
226 r = portlist_regexp();
227 assert(! "tcp".match(r), 'should not have matched: "tcp"');
228 assert("tcp 1".match(r), 'should have matched: "tcp 1"');
229 assert("tcp 123".match(r), 'should have matched: "tcp 123"');
230 assert("tcp 123".match(r), 'should have matched: "tcp 123"');
231 assert("tcp 123-456".match(r), 'should have matched: "tcp 123-456"');
232 assert("tcp 123:456".match(r), 'should have matched: "tcp 123:456"');
233 assert(! "tcp 123-".match(r), 'should have matched: "tcp 123-"');
234 assert(! "tcp 123:".match(r), 'should have matched: "tcp 123:"');
235 assert(! "foo 123".match(r), 'should not have matched "foo 123"');
236 assert("udp 123".match(r), 'should have matched: "udp 123"');
237 assert("tcp 123,udp 456".match(r), 'should have matched: "tcp 123,udp 456"');
238 assert("tcp 123, udp 456".match(r), 'should have matched: "tcp 123, udp 456"');
239 assert("tcp 123, udp 456".match(r), 'should have matched: "tcp 123, udp 456"');
240 assert("tcp 123-45, udp 456".match(r), 'should have matched: "tcp 123-45, udp 456"');
241 assert("tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61".match(r), 'should have matched: "tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61"');
245 //portlist_selftest();