libs/web: remove debug code from cbi.js
[project/luci.git] / libs / web / htdocs / luci-static / resources / cbi.js
index b4137d4..3959b69 100644 (file)
@@ -75,7 +75,7 @@ var cbi_validators = {
                                        var colons = 0;
                                        var fill = '0';
 
-                                       for( var i = 0; i < addr.length; i++ )
+                                       for( var i = 1; i < (addr.length-1); i++ )
                                                if( addr.charAt(i) == ':' )
                                                        colons++;
 
@@ -85,7 +85,9 @@ var cbi_validators = {
                                        for( var i = 0; i < (7 - colons); i++ )
                                                fill += ':0';
 
-                                       addr = addr.replace(/::/, ':' + fill + ':');
+                                       if (addr.match(/^(.*?)::(.*?)$/))
+                                               addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
+                                                      (RegExp.$2 ? ':' + RegExp.$2 : '');
                                }
 
                                return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
@@ -152,6 +154,22 @@ var cbi_validators = {
                        return (v.length == 5) || (v.length == 13);
        },
 
+       'uciname': function(v)
+       {
+               return (v.match(/^[a-zA-Z0-9_]+$/) != null);
+       },
+
+       'range': function(v, args)
+       {
+               var min = parseInt(args[0]);
+               var max = parseInt(args[1]);
+               var val = parseInt(v);
+
+               if (!isNaN(min) && !isNaN(max) && !isNaN(val))
+                       return ((val >= min) && (val <= max));
+
+               return false;
+       }
 };
 
 
@@ -630,6 +648,14 @@ function cbi_validate_reset(form)
 function cbi_validate_field(cbid, optional, type)
 {
        var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
+       var vargs;
+
+       if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
+       {
+               type  = RegExp.$1;
+               vargs = RegExp.$2.split(/\s*,\s*/);
+       }
+
        var vldcb = cbi_validators[type];
 
        if( field && vldcb )
@@ -645,7 +671,7 @@ function cbi_validate_field(cbid, optional, type)
                                var value = (field.options && field.options.selectedIndex > -1)
                                        ? field.options[field.options.selectedIndex].value : field.value;
 
-                               if( !(((value.length == 0) && optional) || vldcb(value)) )
+                               if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
                                {
                                        // invalid
                                        field.className += ' cbi-input-invalid';
@@ -752,7 +778,7 @@ if( ! String.format )
 
                var str = arguments[0];
                var out = '';
-               var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
+               var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t))/;
                var a = b = [], numSubstitutions = 0, numMatches = 0;
 
                while( a = re.exec(str) )
@@ -813,7 +839,7 @@ if( ! String.format )
 
                                                case 'f':
                                                        subst = (precision > -1)
-                                                               ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
+                                                               ? ((parseFloat(param) || 0.0)).toFixed(precision)
                                                                : (parseFloat(param) || 0.0);
                                                        break;
 
@@ -844,6 +870,33 @@ if( ! String.format )
                                                case 'j':
                                                        subst = String.serialize(param);
                                                        break;
+
+                                               case 't':
+                                                       var td = 0;
+                                                       var th = 0;
+                                                       var tm = 0;
+                                                       var ts = (param || 0);
+
+                                                       if (ts > 60) {
+                                                               tm = Math.floor(ts / 60);
+                                                               ts = (ts % 60);
+                                                       }
+
+                                                       if (tm > 60) {
+                                                               th = Math.floor(tm / 60);
+                                                               tm = (tm % 60);
+                                                       }
+
+                                                       if (th > 24) {
+                                                               td = Math.floor(th / 24);
+                                                               th = (th % 24);
+                                                       }
+
+                                                       subst = (td > 0)
+                                                               ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
+                                                               : String.format('%dh %dm %ds', th, tm, ts);
+
+                                                       break;
                                        }
                                }
                        }