Changeset 1939 for trunk


Ignore:
Timestamp:
2011-09-01 07:09:34 (9 months ago)
Author:
mpo
Message:

various code-enhancements suggested by jslint

Location:
trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util/core.js

    r1920 r1939  
    1515// - no empty args in fn.apply call + 
    1616// - no "hello"[indexing] in string variables: use "hello".charAt(index) 
     17 
    1718; 
    1819( function( $) { 
    1920 
    20     if ($ == undefined) 
     21    if ($ == undefined) { 
    2122        throw "[core.js] requires jQuery object"; 
     23    } 
    2224 
    2325    $.org = $.org || {}; 
    2426    $.org.kauriproject = $.org.kauriproject || {}; 
    25     var kf = $.org.kauriproject.forms = {}; 
    26  
     27    $.org.kauriproject.forms = {}; 
     28    var kf = $.org.kauriproject.forms; 
    2729 
    2830    /* ---------------------- additions to the jQeury object ------------------------- */ 
     
    4042    $.format = function( source, params) { 
    4143 
    42         if (arguments.length == 1) 
     44        if (arguments.length == 1) { 
    4345            return function() { 
    4446 
     
    4749                return jQuery.format.apply(this, args); 
    4850            }; 
     51        } 
    4952        if (arguments.length > 2 && params.constructor != Array) { 
    5053            params = jQuery.makeArray(arguments).slice(1); 
     
    7073 
    7174        return (val >= 0 || val < 0); 
    72     } 
     75    }; 
    7376 
    7477 
     
    8285    $.hasProperties = function( obj) { 
    8386 
    84         if (obj == undefined) 
     87        if (obj == undefined) { 
    8588            return false; 
    86         if ($.isFunction(obj)) 
     89        } 
     90        if ($.isFunction(obj)) { 
    8791            return undefined; // meaningless in this context 
    88         for (i in obj) 
     92        } 
     93        for (i in obj) { 
    8994            return true; // first property returns 
     95        } 
    9096        return false; // no props were found! 
    91     } 
     97    }; 
    9298     
    9399    $.isEmpty = function (val) { 
    94         return (val == undefined || (val.length != undefined && val.length == 0) || (val.constructor == Object && !$ 
    95                 .hasProperties(val))); 
    96     } 
     100        return (val == undefined || (val.length != undefined && val.length == 0) || (val.constructor == Object && !$.hasProperties(val))); 
     101    }; 
    97102 
    98103    /** 
     
    116121        delim = delim || "/"; 
    117122         
    118         if (relativ.charAt(0) == delim) 
     123        if (relativ.charAt(0) == delim) { 
    119124            return relativ; 
    120         if (base.length == 0 || base.charAt(base.length - 1) != delim) 
     125        } 
     126        if (base.length == 0 || base.charAt(base.length - 1) != delim) { 
    121127            base += delim; 
     128        } 
    122129        return base + relativ; 
    123     } 
     130    }; 
    124131     
    125132   /** 
     
    130137       n = n.toLocaleString().substring(1, 2); 
    131138       return n; 
    132    } 
     139   }; 
    133140 
    134141   /** 
     
    142149       } 
    143150       return nlocal.substring(1, 2); 
    144    } 
     151   }; 
    145152 
    146153 
    147154    function elementsCompare( a, b) { 
    148155 
    149         if (a.length != b.length) 
     156        if (a.length != b.length) { 
    150157            return false; 
     158        } 
    151159        var last = a.length; 
    152         for ( var i = 0; i < last; i++) 
    153             if (!$.valueCompare(a[i], b[i])) 
     160        for ( var i = 0; i < last; i++) { 
     161            if (!$.valueCompare(a[i], b[i])) { 
    154162                return false; 
     163            } 
     164        } 
    155165        return true; // all elements of the array match 
    156166    } 
     
    163173        } 
    164174        for ( var bprop in b) { 
    165             if ($.inArray(bprop, aprops) < 0 || !$.valueCompare(a[bprop], b[bprop])) 
     175            if ($.inArray(bprop, aprops) < 0 || !$.valueCompare(a[bprop], b[bprop])) { 
    166176                return false; 
    167         } 
    168         return true // all elements of the object match 
     177            } 
     178        } 
     179        return true; // all elements of the object match 
    169180    } 
    170181 
    171182    $.valueCompare = function( a, b) { 
    172183 
    173         if (a == undefined || b == undefined) 
     184        if (a == undefined || b == undefined) { 
    174185            return (a == b); 
    175  
    176         if (a.constructor != b.constructor) 
     186        } 
     187        if (a.constructor != b.constructor) { 
    177188            return false; 
    178  
     189        } 
     190         
    179191        if (a.constructor == Array) { 
    180192            return elementsCompare(a, b); 
     
    182194            return propertiesCompare(a, b); 
    183195        } else if (a.constructor == Number) { 
    184             return (a.valueOf() === b.valueOf()) 
     196            return (a.valueOf() === b.valueOf()); 
    185197        } else if (a.constructor == Boolean) { 
    186             return (a.valueOf() === b.valueOf()) 
     198            return (a.valueOf() === b.valueOf()); 
    187199        } else if (a.constructor == Date) { 
    188200            return a.getTime() == b.getTime(); 
     
    190202 
    191203        return (a === b); 
    192     } 
     204    }; 
    193205     
    194206    $.valueInArray = function (elem, array){ 
    195207        var last = array.length; 
    196         for (var i = 0; i<last; i++) 
    197             if ($.valueCompare(array[i], elem)) 
     208        for (var i = 0; i<last; i++) { 
     209            if ($.valueCompare(array[i], elem)) { 
    198210                return i; 
     211            } 
     212        } 
    199213             
    200214        return -1; 
    201     } 
     215    }; 
    202216 
    203217    /** 
     
    209223     *            baseConstr the function defining the superclass 
    210224     */ 
    211     $.inherit = function( subConstr, baseConstr) { 
    212  
    213         if (!($.isFunction(subConstr) && $.isFunction(baseConstr))) 
     225    $.inherit = function( Sub, Base) { 
     226 
     227        if (!($.isFunction(Sub) && $.isFunction(Base))) { 
    214228            return; 
    215              
    216         subConstr.prototype = new baseConstr();  
    217         subConstr.prototype.constructor = subConstr; 
    218     } 
     229        } 
     230         
     231        Sub.prototype = new Base();  
     232        Sub.prototype.constructor = Sub; 
     233    }; 
    219234 
    220235 
     
    245260        debug : function( /* string */message) { 
    246261 
    247             if (debug) 
    248                 $("<div>" + message + "</div>").appendTo($("div#errors")); // TODO should change to #debug in stead of #errors? 
     262            if (debug) { 
     263                $("<div>" + message + "</div>").appendTo($("div#errors"));  
     264                // TODO should change to #debug in stead of #errors? 
     265            } 
    249266        }, 
    250267         
     
    261278        debugStyle : function( /* jQuery obj */container, /* string */cssName, /* string */cssValue) { 
    262279         
    263             if (debug) 
     280            if (debug) { 
    264281                container.css(cssName, cssValue); 
     282            } 
    265283        } 
    266284    }); 
     
    269287     * Extra jQuery functions 
    270288     */ 
    271     $.extend( $.expr[':'], { 
    272         attached: attached, 
    273         detached: function( elem ){ return !attached( elem ); } 
    274     }); 
    275        
     289 
    276290    function attached( elem ) { 
    277291      var contains = $.contains, 
     
    290304      } 
    291305      return false; 
    292     }; 
    293  
     306    } 
     307 
     308    $.extend( $.expr[':'], { 
     309        attached: attached, 
     310        detached: function( elem ){ return !attached( elem ); } 
     311    }); 
     312       
    294313    function extor(name, fn) { 
    295         var extor = {}; 
    296         extor[name] = function(pass) { 
     314        var ext = {}; 
     315        ext[name] = function(pass) { 
    297316            return $(this).map(function(){  
    298317                return fn($(this), pass); 
    299318            }); 
    300         } 
    301         $.fn.extend(extor);         
    302     }; 
     319        }; 
     320        $.fn.extend(ext); 
     321    } 
    303322     
    304323    $.extend( { 
    305         "define": function(name, cstr) { 
    306             extor(name, function($e,p){return new cstr($e,p)}); 
     324        "define": function(name, Cstr) { 
     325            extor(name, function($e,p){return new Cstr($e,p);}); 
    307326        }, 
    308327        "build": function(name, fn) { 
  • trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util/locale.js

    r1414 r1939  
    55( function( $) { 
    66 
    7     if (!$) 
     7    if (!$) { 
    88        throw "Kauri locale support requires jQuery"; 
    9  
    10     if (!$.org.kauriproject.forms) 
     9    } 
     10 
     11    if (!$.org.kauriproject.forms) { 
    1112        throw "Kauri locale support requires the kauri-form namespace"; 
     13    } 
    1214 
    1315    var kf = $.org.kauriproject.forms; 
     
    3335 
    3436        return this.text; 
    35     } 
     37    }; 
    3638 
    3739    var I18N_PREFIX_REGEX = /^i18n:/; 
     
    5254        text = $.format(text, args); 
    5355        return new Message(text, type, source); 
    54     } 
     56    }; 
    5557 
    5658    Message.prototype.getText = function() { 
    5759 
    5860        return this.text; 
    59     } 
     61    }; 
    6062 
    6163    Message.prototype.getType = function() { 
    6264 
    6365        return this.type; 
    64     } 
     66    }; 
    6567 
    6668    Message.prototype.getSource = function() { 
    6769 
    6870        return this.source; 
    69     } 
     71    }; 
    7072 
    7173 
     
    7577 
    7678        this.separator = Locale.numericSeparators(); 
    77         this.separator['date'] = '/'; 
    78         this.separator['time'] = ':'; 
     79        this.separator.date = '/'; 
     80        this.separator.time = ':'; 
    7981 
    8082        this.timezone = Locale.timezone(); 
     
    101103            '1000' :t 
    102104        }; 
    103     } 
     105    }; 
    104106 
    105107    /** 
     
    112114        var n = new Date(); 
    113115        return n.getHours() - n.getUTCHours(); 
    114     } 
     116    }; 
    115117     
    116118     
     
    123125 
    124126        return 'en-US'; // default if not found 
    125     } 
     127    }; 
    126128     
    127129 
     
    200202        // registry of Locales - support classes? 
    201203        return new Locale(id); 
    202     } 
    203  
    204     var LOCALE_ID_REGEX = /^([a-z]+)-?([a-z]*)$/i 
     204    }; 
     205 
     206    var LOCALE_ID_REGEX = /^([a-z]+)-?([a-z]*)$/i; 
    205207    Locale.prototype.setId = function( newId) { 
    206208 
    207209        var parts = LOCALE_ID_REGEX.exec(newId); 
    208         if (!parts) 
     210        if (!parts) { 
    209211            throw Message.build("Not a proper locale identification: {0}", [ newId ], "Locale.setId"); 
     212        } 
    210213        this.id = parts[0]; 
    211214        this.language = parts[1]; 
    212215        this.country = parts[2]; 
    213     } 
    214  
     216    }; 
     217     
    215218    Locale.prototype.toString = function() { 
    216219 
    217220        return this.id; 
    218     } 
     221    }; 
    219222 
    220223    Locale.prototype.getMessage = function( key) { 
     
    224227        } 
    225228        return key; 
    226     } 
     229    }; 
    227230     
    228231    Locale.prototype.loadMessages = function( path) { 
    229232 
    230233        throw "TODO implement Locale.addMessages"; 
    231     } 
     234    }; 
    232235     
    233236    Locale.prototype.setMessages = function(newMessages) { 
    234237        this.messages = newMessages; 
    235     } 
     238    }; 
    236239 
    237240    var locale = Locale.build(); 
  • trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util/uritemplate.js

    r1872 r1939  
    4545 *   - skipEscape: boolean to control if %HH replacements should be made or not: without the replacing this templates get broather use outside URI scopes. 
    4646 */ 
    47  
    4847; 
    4948( function( $) { 
    5049 
    51     if (!$) 
     50    if (!$) { 
    5251        throw "[uritemplate.js] requires jQuery"; 
    53     if (!$.org.kauriproject) 
     52    }          
     53    if (!$.org.kauriproject) { 
    5454        throw "[uritemplate.js] requires the kauriproject namespace"; 
     55    } 
    5556 
    5657    var kp = $.org.kauriproject; 
     
    7475     */ 
    7576    $.getProperty = function (context, expr) { 
    76         if ($.isEmpty(expr) || context == undefined) return context; 
     77        if ($.isEmpty(expr) || context == undefined) { return context; }  
    7778         
    7879        // fast turn around for the simplest case. 
    79         if (Object.hasOwnProperty.call(context,expr)) return context[expr]; 
     80        if (Object.hasOwnProperty.call(context,expr)) { return context[expr]; }; 
    8081        // no need to further evaluate if there is no expression inside: 
    81         if (!EXPR_REGEXP.test(expr)) 
     82        if (!EXPR_REGEXP.test(expr)) { 
    8283            return undefined; 
    83          
     84        } 
    8485         
    8586        /* unsafe eval implementation  
     
    121122        } 
    122123        var subcontext = $.getProperty(context, subexpr); 
    123         if (remainder.charAt(0) == '.') 
     124        if (remainder.charAt(0) == '.') { 
    124125            remainder = remainder.substring(1); 
     126        } 
    125127        return $.getProperty(subcontext, remainder); 
    126     } 
     128    }; 
    127129     
    128130     
     
    139141 
    140142        return ($.inArray(item, array) != -1); 
    141     } 
     143    }; 
    142144 
    143145    var BIDIREGEXP = /[\u202A\u202B\u202D\u202E\u200E\u200F\u202C]/g; 
     
    146148        var str = arguments[0]; 
    147149        return str.replace(BIDIREGEXP, ""); 
    148     } 
    149  
    150     Template.opregex = /^\{-([^\|]+)\|([^\|]+)\|([^\|\}]+)\}$/ 
    151     Template.tregex = /^\{([^\}]+)\}$/ 
     150    }; 
     151 
     152    Template.opregex = /^\{-([^\|]+)\|([^\|]+)\|([^\|\}]+)\}$/; 
     153    Template.tregex = /^\{([^\}]+)\}$/; 
    152154    Template.initTokens = function() { 
    153155 
    154156        var template = arguments[0]; 
    155         var vars = new Array(); 
     157        var vars = []; 
    156158        var tokens = template.match(/\{[^{}]+\}/g); 
    157159        if (tokens) { 
    158160            for ( var i = 0; i < tokens.length; i++) { 
    159                 if (!Template.contains(vars, tokens[i])) 
     161                if ( !Template.contains(vars, tokens[i]) ) { 
    160162                    vars.push(tokens[i]); 
     163                } 
    161164            } 
    162165        } 
    163166        return vars; 
    164     } 
     167    }; 
    165168    Template.initVariables = function() { 
    166169 
    167170        var tokens = arguments[0]; 
    168         var vars = new Array(); 
     171        var vars = []; 
    169172        for ( var i = 0; i < tokens.length; i++) { 
    170173            var token = tokens[i]; 
     
    174177                for ( var n = 0; n < matches.length; n++) { 
    175178                    var name = matches[n].split(/\s*=\s*/)[0]; 
    176                     if (!Template.contains(vars, name)) 
     179                    if ( !Template.contains(vars, name) ) { 
    177180                        vars.push(name); 
     181                    } 
    178182                } 
    179183            } else { 
    180184                var matches = Template.tregex.exec(token); 
    181185                var name = matches[1].split(/\s*=\s*/)[0]; 
    182                 if (!Template.contains(vars, name)) 
     186                if (!Template.contains(vars, name)) { 
    183187                    vars.push(name); 
     188                } 
    184189            } 
    185190        } 
    186191        return vars; 
    187     } 
     192    }; 
    188193    Template.evaluate = function() { 
    189194 
     
    199204                // TODO fix to work with arrays! 
    200205                var value = Template.getVarValue(matches[3], context, null); 
    201                 return value ? matches[2] + Template.escape(value, template.opts) : ""; 
    202                 break; 
     206                return value ? matches[2] + Template.escape(value, template.opts) : "";  
    203207            case "append": 
    204208                var value = Template.getVarValue(matches[3], context, null); 
    205209                return value ? Template.escape(value, template.opts) + matches[2] : ""; 
    206                 break; 
    207210            case "opt": 
    208211                var vars = matches[3].split(/\s*,\s*/); 
    209212                for ( var n = 0; n < vars.length; n++) { 
    210213                    var value = Template.getVarValue(vars[n], context, null); 
    211                     if (value) 
     214                    if (value) { 
    212215                        return matches[2]; 
     216                    } 
    213217                } 
    214218                return ""; 
     
    217221                for ( var n = 0; n < vars.length; n++) { 
    218222                    var value = Template.getVarValue(vars[n], context, null); 
    219                     if (value) 
     223                    if (value) { 
    220224                        return ""; 
     225                    } 
    221226                } 
    222227                return matches[2]; 
     
    227232                if(value){ 
    228233                    for ( var n = 0; n < value.length; n++) { 
    229                         if (n > 0) 
     234                        if (n > 0) { 
    230235                            rep += sep; 
     236                        } 
    231237                        rep += Template.escape(value[n], template.opts); 
    232238                    } 
    233239                } 
    234240                return rep; 
    235                 break; 
    236241            case "join": 
    237242                var rep = ""; 
     
    242247                    var value = Template.getVarValue(name, context, null); 
    243248                    if (value) { 
    244                         if (rep.length > 0) 
     249                        if (rep.length > 0) { 
    245250                            rep += sep; 
     251                        } 
    246252                        rep += name.split(/\s*=\s*/)[0] + "=" + Template.escape(value, template.opts); 
    247253                    } 
    248254                } 
    249255                return rep; 
    250                 break; 
    251256            } 
    252257        } else { 
     
    254259            return Template.getVarOrDefaultValue(matches[1], context, "", template.opts); 
    255260        } 
    256     } 
     261    }; 
    257262 
    258263    Template.getVarOrDefaultValue = function() { 
     
    270275        } 
    271276 
    272         if (value != undefined || value != null) 
     277        if (value != undefined || value != null) { 
    273278            return Template.escape(value, opts); 
    274         else if (def) 
     279        } else if (def) { 
    275280            return def; 
    276         else 
     281        } else { 
    277282            return defval; 
    278     } 
     283        } 
     284    }; 
    279285 
    280286    Template.getVarValue = function() { 
     
    290296        } 
    291297        return value ? value : def ? def : defval; 
    292     } 
     298    }; 
    293299    Template.escape = function() { 
    294300        var str = arguments[0]; 
    295301        var opts = arguments[1]; 
    296         if (opts && opts.skipEscape) 
     302        if (opts && opts.skipEscape) { 
    297303            return str; 
     304        } 
    298305         
    299306        // TODO the URI template spec forces a specific unicode normalization to happen on the characters 
     
    315322        } 
    316323        return ret; 
    317     } 
     324    }; 
    318325    Template.isunreserved = function() { 
    319326 
    320327        var c = arguments[0]; 
    321328        return (c >= 48 && c <= 57) || (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 45 || c == 46 || c == 95 || c == 126; 
    322     } 
     329    }; 
    323330    Template.isiunreserved = function() { 
    324331 
    325332        var c = arguments[0]; 
    326         return Template.isunreserved(c) || (c >= 0x00a0 && c <= 0xd7ff) || (c >= 0xF900 && c <= 0xfdcf) 
     333        return (Template.isunreserved(c) || (c >= 0x00a0 && c <= 0xd7ff) || (c >= 0xF900 && c <= 0xfdcf) 
    327334                || (c >= 0xFDF0 && c <= 0xfeff) || (c >= 0x10000 && c <= 0x1FFFD) || (c >= 0x20000 && c <= 0x2FFFD) 
    328335                || (c >= 0x30000 && c <= 0x3FFFD) || (c >= 0x40000 && c <= 0x4FFFD) || (c >= 0x50000 && c <= 0x5FFFD) 
    329336                || (c >= 0x60000 && c <= 0x6FFFD) || (c >= 0x70000 && c <= 0x7FFFD) || (c >= 0x80000 && c <= 0x8FFFD) 
    330337                || (c >= 0x90000 && c <= 0x9FFFD) || (c >= 0xA0000 && c <= 0xAFFFD) || (c >= 0xB0000 && c <= 0xBFFFD) 
    331                 || (c >= 0xC0000 && c <= 0xCFFFD) || (c >= 0xD0000 && c <= 0xDFFFD) || (c >= 0xE0000 && c <= 0xEFFFD); 
    332     } 
     338                || (c >= 0xC0000 && c <= 0xCFFFD) || (c >= 0xD0000 && c <= 0xDFFFD) || (c >= 0xE0000 && c <= 0xEFFFD)); 
     339    }; 
    333340    Template.utf8escape = function() { 
    334341 
     
    346353        } 
    347354        return ret; 
    348     } 
     355    }; 
    349356    Template.expand = function() { 
    350357 
     
    352359        var context = arguments[1]; 
    353360        return new Template(template).expand(context); 
    354     } 
     361    }; 
    355362 
    356363    Template.makeTokenReplaceRegex = function( text) { 
     
    358365        text = text.replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\|/g, "\\|").replace(/\?/, "\\?"); 
    359366        return new RegExp(text, "g"); 
    360     } 
     367    }; 
    361368 
    362369    Template.prototype = { 
     
    374381            return template; 
    375382        } 
    376     } 
     383    }; 
    377384 
    378385    // add to kauriproject ns 
Note: See TracChangeset for help on using the changeset viewer.