Changeset 1939
- Timestamp:
- 2011-09-01 07:09:34 (9 months ago)
- 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 15 15 // - no empty args in fn.apply call + 16 16 // - no "hello"[indexing] in string variables: use "hello".charAt(index) 17 17 18 ; 18 19 ( function( $) { 19 20 20 if ($ == undefined) 21 if ($ == undefined) { 21 22 throw "[core.js] requires jQuery object"; 23 } 22 24 23 25 $.org = $.org || {}; 24 26 $.org.kauriproject = $.org.kauriproject || {}; 25 var kf =$.org.kauriproject.forms = {};26 27 $.org.kauriproject.forms = {}; 28 var kf = $.org.kauriproject.forms; 27 29 28 30 /* ---------------------- additions to the jQeury object ------------------------- */ … … 40 42 $.format = function( source, params) { 41 43 42 if (arguments.length == 1) 44 if (arguments.length == 1) { 43 45 return function() { 44 46 … … 47 49 return jQuery.format.apply(this, args); 48 50 }; 51 } 49 52 if (arguments.length > 2 && params.constructor != Array) { 50 53 params = jQuery.makeArray(arguments).slice(1); … … 70 73 71 74 return (val >= 0 || val < 0); 72 } 75 }; 73 76 74 77 … … 82 85 $.hasProperties = function( obj) { 83 86 84 if (obj == undefined) 87 if (obj == undefined) { 85 88 return false; 86 if ($.isFunction(obj)) 89 } 90 if ($.isFunction(obj)) { 87 91 return undefined; // meaningless in this context 88 for (i in obj) 92 } 93 for (i in obj) { 89 94 return true; // first property returns 95 } 90 96 return false; // no props were found! 91 } 97 }; 92 98 93 99 $.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 }; 97 102 98 103 /** … … 116 121 delim = delim || "/"; 117 122 118 if (relativ.charAt(0) == delim) 123 if (relativ.charAt(0) == delim) { 119 124 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) { 121 127 base += delim; 128 } 122 129 return base + relativ; 123 } 130 }; 124 131 125 132 /** … … 130 137 n = n.toLocaleString().substring(1, 2); 131 138 return n; 132 } 139 }; 133 140 134 141 /** … … 142 149 } 143 150 return nlocal.substring(1, 2); 144 } 151 }; 145 152 146 153 147 154 function elementsCompare( a, b) { 148 155 149 if (a.length != b.length) 156 if (a.length != b.length) { 150 157 return false; 158 } 151 159 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])) { 154 162 return false; 163 } 164 } 155 165 return true; // all elements of the array match 156 166 } … … 163 173 } 164 174 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])) { 166 176 return false; 167 } 168 return true // all elements of the object match 177 } 178 } 179 return true; // all elements of the object match 169 180 } 170 181 171 182 $.valueCompare = function( a, b) { 172 183 173 if (a == undefined || b == undefined) 184 if (a == undefined || b == undefined) { 174 185 return (a == b); 175 176 if (a.constructor != b.constructor) 186 } 187 if (a.constructor != b.constructor) { 177 188 return false; 178 189 } 190 179 191 if (a.constructor == Array) { 180 192 return elementsCompare(a, b); … … 182 194 return propertiesCompare(a, b); 183 195 } else if (a.constructor == Number) { 184 return (a.valueOf() === b.valueOf()) 196 return (a.valueOf() === b.valueOf()); 185 197 } else if (a.constructor == Boolean) { 186 return (a.valueOf() === b.valueOf()) 198 return (a.valueOf() === b.valueOf()); 187 199 } else if (a.constructor == Date) { 188 200 return a.getTime() == b.getTime(); … … 190 202 191 203 return (a === b); 192 } 204 }; 193 205 194 206 $.valueInArray = function (elem, array){ 195 207 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)) { 198 210 return i; 211 } 212 } 199 213 200 214 return -1; 201 } 215 }; 202 216 203 217 /** … … 209 223 * baseConstr the function defining the superclass 210 224 */ 211 $.inherit = function( subConstr, baseConstr) {212 213 if (!($.isFunction( subConstr) && $.isFunction(baseConstr)))225 $.inherit = function( Sub, Base) { 226 227 if (!($.isFunction(Sub) && $.isFunction(Base))) { 214 228 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 }; 219 234 220 235 … … 245 260 debug : function( /* string */message) { 246 261 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 } 249 266 }, 250 267 … … 261 278 debugStyle : function( /* jQuery obj */container, /* string */cssName, /* string */cssValue) { 262 279 263 if (debug) 280 if (debug) { 264 281 container.css(cssName, cssValue); 282 } 265 283 } 266 284 }); … … 269 287 * Extra jQuery functions 270 288 */ 271 $.extend( $.expr[':'], { 272 attached: attached, 273 detached: function( elem ){ return !attached( elem ); } 274 }); 275 289 276 290 function attached( elem ) { 277 291 var contains = $.contains, … … 290 304 } 291 305 return false; 292 }; 293 306 } 307 308 $.extend( $.expr[':'], { 309 attached: attached, 310 detached: function( elem ){ return !attached( elem ); } 311 }); 312 294 313 function extor(name, fn) { 295 var ext or= {};296 ext or[name] = function(pass) {314 var ext = {}; 315 ext[name] = function(pass) { 297 316 return $(this).map(function(){ 298 317 return fn($(this), pass); 299 318 }); 300 } 301 $.fn.extend(ext or);302 } ;319 }; 320 $.fn.extend(ext); 321 } 303 322 304 323 $.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);}); 307 326 }, 308 327 "build": function(name, fn) { -
trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util/locale.js
r1414 r1939 5 5 ( function( $) { 6 6 7 if (!$) 7 if (!$) { 8 8 throw "Kauri locale support requires jQuery"; 9 10 if (!$.org.kauriproject.forms) 9 } 10 11 if (!$.org.kauriproject.forms) { 11 12 throw "Kauri locale support requires the kauri-form namespace"; 13 } 12 14 13 15 var kf = $.org.kauriproject.forms; … … 33 35 34 36 return this.text; 35 } 37 }; 36 38 37 39 var I18N_PREFIX_REGEX = /^i18n:/; … … 52 54 text = $.format(text, args); 53 55 return new Message(text, type, source); 54 } 56 }; 55 57 56 58 Message.prototype.getText = function() { 57 59 58 60 return this.text; 59 } 61 }; 60 62 61 63 Message.prototype.getType = function() { 62 64 63 65 return this.type; 64 } 66 }; 65 67 66 68 Message.prototype.getSource = function() { 67 69 68 70 return this.source; 69 } 71 }; 70 72 71 73 … … 75 77 76 78 this.separator = Locale.numericSeparators(); 77 this.separator ['date']= '/';78 this.separator ['time']= ':';79 this.separator.date = '/'; 80 this.separator.time = ':'; 79 81 80 82 this.timezone = Locale.timezone(); … … 101 103 '1000' :t 102 104 }; 103 } 105 }; 104 106 105 107 /** … … 112 114 var n = new Date(); 113 115 return n.getHours() - n.getUTCHours(); 114 } 116 }; 115 117 116 118 … … 123 125 124 126 return 'en-US'; // default if not found 125 } 127 }; 126 128 127 129 … … 200 202 // registry of Locales - support classes? 201 203 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; 205 207 Locale.prototype.setId = function( newId) { 206 208 207 209 var parts = LOCALE_ID_REGEX.exec(newId); 208 if (!parts) 210 if (!parts) { 209 211 throw Message.build("Not a proper locale identification: {0}", [ newId ], "Locale.setId"); 212 } 210 213 this.id = parts[0]; 211 214 this.language = parts[1]; 212 215 this.country = parts[2]; 213 } 214 216 }; 217 215 218 Locale.prototype.toString = function() { 216 219 217 220 return this.id; 218 } 221 }; 219 222 220 223 Locale.prototype.getMessage = function( key) { … … 224 227 } 225 228 return key; 226 } 229 }; 227 230 228 231 Locale.prototype.loadMessages = function( path) { 229 232 230 233 throw "TODO implement Locale.addMessages"; 231 } 234 }; 232 235 233 236 Locale.prototype.setMessages = function(newMessages) { 234 237 this.messages = newMessages; 235 } 238 }; 236 239 237 240 var locale = Locale.build(); -
trunk/modules/kauri-jquery/src/main/kauri/static-{build}.key/kauri.util/uritemplate.js
r1872 r1939 45 45 * - skipEscape: boolean to control if %HH replacements should be made or not: without the replacing this templates get broather use outside URI scopes. 46 46 */ 47 48 47 ; 49 48 ( function( $) { 50 49 51 if (!$) 50 if (!$) { 52 51 throw "[uritemplate.js] requires jQuery"; 53 if (!$.org.kauriproject) 52 } 53 if (!$.org.kauriproject) { 54 54 throw "[uritemplate.js] requires the kauriproject namespace"; 55 } 55 56 56 57 var kp = $.org.kauriproject; … … 74 75 */ 75 76 $.getProperty = function (context, expr) { 76 if ($.isEmpty(expr) || context == undefined) return context;77 if ($.isEmpty(expr) || context == undefined) { return context; } 77 78 78 79 // 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]; }; 80 81 // no need to further evaluate if there is no expression inside: 81 if (!EXPR_REGEXP.test(expr)) 82 if (!EXPR_REGEXP.test(expr)) { 82 83 return undefined; 83 84 } 84 85 85 86 /* unsafe eval implementation … … 121 122 } 122 123 var subcontext = $.getProperty(context, subexpr); 123 if (remainder.charAt(0) == '.') 124 if (remainder.charAt(0) == '.') { 124 125 remainder = remainder.substring(1); 126 } 125 127 return $.getProperty(subcontext, remainder); 126 } 128 }; 127 129 128 130 … … 139 141 140 142 return ($.inArray(item, array) != -1); 141 } 143 }; 142 144 143 145 var BIDIREGEXP = /[\u202A\u202B\u202D\u202E\u200E\u200F\u202C]/g; … … 146 148 var str = arguments[0]; 147 149 return str.replace(BIDIREGEXP, ""); 148 } 149 150 Template.opregex = /^\{-([^\|]+)\|([^\|]+)\|([^\|\}]+)\}$/ 151 Template.tregex = /^\{([^\}]+)\}$/ 150 }; 151 152 Template.opregex = /^\{-([^\|]+)\|([^\|]+)\|([^\|\}]+)\}$/; 153 Template.tregex = /^\{([^\}]+)\}$/; 152 154 Template.initTokens = function() { 153 155 154 156 var template = arguments[0]; 155 var vars = new Array();157 var vars = []; 156 158 var tokens = template.match(/\{[^{}]+\}/g); 157 159 if (tokens) { 158 160 for ( var i = 0; i < tokens.length; i++) { 159 if ( !Template.contains(vars, tokens[i]))161 if ( !Template.contains(vars, tokens[i]) ) { 160 162 vars.push(tokens[i]); 163 } 161 164 } 162 165 } 163 166 return vars; 164 } 167 }; 165 168 Template.initVariables = function() { 166 169 167 170 var tokens = arguments[0]; 168 var vars = new Array();171 var vars = []; 169 172 for ( var i = 0; i < tokens.length; i++) { 170 173 var token = tokens[i]; … … 174 177 for ( var n = 0; n < matches.length; n++) { 175 178 var name = matches[n].split(/\s*=\s*/)[0]; 176 if ( !Template.contains(vars, name))179 if ( !Template.contains(vars, name) ) { 177 180 vars.push(name); 181 } 178 182 } 179 183 } else { 180 184 var matches = Template.tregex.exec(token); 181 185 var name = matches[1].split(/\s*=\s*/)[0]; 182 if (!Template.contains(vars, name)) 186 if (!Template.contains(vars, name)) { 183 187 vars.push(name); 188 } 184 189 } 185 190 } 186 191 return vars; 187 } 192 }; 188 193 Template.evaluate = function() { 189 194 … … 199 204 // TODO fix to work with arrays! 200 205 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) : ""; 203 207 case "append": 204 208 var value = Template.getVarValue(matches[3], context, null); 205 209 return value ? Template.escape(value, template.opts) + matches[2] : ""; 206 break;207 210 case "opt": 208 211 var vars = matches[3].split(/\s*,\s*/); 209 212 for ( var n = 0; n < vars.length; n++) { 210 213 var value = Template.getVarValue(vars[n], context, null); 211 if (value) 214 if (value) { 212 215 return matches[2]; 216 } 213 217 } 214 218 return ""; … … 217 221 for ( var n = 0; n < vars.length; n++) { 218 222 var value = Template.getVarValue(vars[n], context, null); 219 if (value) 223 if (value) { 220 224 return ""; 225 } 221 226 } 222 227 return matches[2]; … … 227 232 if(value){ 228 233 for ( var n = 0; n < value.length; n++) { 229 if (n > 0) 234 if (n > 0) { 230 235 rep += sep; 236 } 231 237 rep += Template.escape(value[n], template.opts); 232 238 } 233 239 } 234 240 return rep; 235 break;236 241 case "join": 237 242 var rep = ""; … … 242 247 var value = Template.getVarValue(name, context, null); 243 248 if (value) { 244 if (rep.length > 0) 249 if (rep.length > 0) { 245 250 rep += sep; 251 } 246 252 rep += name.split(/\s*=\s*/)[0] + "=" + Template.escape(value, template.opts); 247 253 } 248 254 } 249 255 return rep; 250 break;251 256 } 252 257 } else { … … 254 259 return Template.getVarOrDefaultValue(matches[1], context, "", template.opts); 255 260 } 256 } 261 }; 257 262 258 263 Template.getVarOrDefaultValue = function() { … … 270 275 } 271 276 272 if (value != undefined || value != null) 277 if (value != undefined || value != null) { 273 278 return Template.escape(value, opts); 274 else if (def)279 } else if (def) { 275 280 return def; 276 else281 } else { 277 282 return defval; 278 } 283 } 284 }; 279 285 280 286 Template.getVarValue = function() { … … 290 296 } 291 297 return value ? value : def ? def : defval; 292 } 298 }; 293 299 Template.escape = function() { 294 300 var str = arguments[0]; 295 301 var opts = arguments[1]; 296 if (opts && opts.skipEscape) 302 if (opts && opts.skipEscape) { 297 303 return str; 304 } 298 305 299 306 // TODO the URI template spec forces a specific unicode normalization to happen on the characters … … 315 322 } 316 323 return ret; 317 } 324 }; 318 325 Template.isunreserved = function() { 319 326 320 327 var c = arguments[0]; 321 328 return (c >= 48 && c <= 57) || (c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c == 45 || c == 46 || c == 95 || c == 126; 322 } 329 }; 323 330 Template.isiunreserved = function() { 324 331 325 332 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) 327 334 || (c >= 0xFDF0 && c <= 0xfeff) || (c >= 0x10000 && c <= 0x1FFFD) || (c >= 0x20000 && c <= 0x2FFFD) 328 335 || (c >= 0x30000 && c <= 0x3FFFD) || (c >= 0x40000 && c <= 0x4FFFD) || (c >= 0x50000 && c <= 0x5FFFD) 329 336 || (c >= 0x60000 && c <= 0x6FFFD) || (c >= 0x70000 && c <= 0x7FFFD) || (c >= 0x80000 && c <= 0x8FFFD) 330 337 || (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 }; 333 340 Template.utf8escape = function() { 334 341 … … 346 353 } 347 354 return ret; 348 } 355 }; 349 356 Template.expand = function() { 350 357 … … 352 359 var context = arguments[1]; 353 360 return new Template(template).expand(context); 354 } 361 }; 355 362 356 363 Template.makeTokenReplaceRegex = function( text) { … … 358 365 text = text.replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\|/g, "\\|").replace(/\?/, "\\?"); 359 366 return new RegExp(text, "g"); 360 } 367 }; 361 368 362 369 Template.prototype = { … … 374 381 return template; 375 382 } 376 } 383 }; 377 384 378 385 // add to kauriproject ns
Note: See TracChangeset
for help on using the changeset viewer.