| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
this.jsviews || this.jQuery && jQuery.views || (function( window, undefined ) { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
var versionNumber = "v1.0pre", |
| 16 |
|
| 17 |
$, rTag, rTmplString, extend, |
| 18 |
sub = {}, |
| 19 |
FALSE = false, TRUE = true, |
| 20 |
jQuery = window.jQuery, |
| 21 |
|
| 22 |
rPath = /^(?:null|true|false|\d[\d.]*|([\w$]+|~([\w$]+)|#(view|([\w$]+))?)([\w$.]*?)(?:[.[]([\w$]+)\]?)?|(['"]).*\8)$/g, |
| 23 |
|
| 24 |
|
| 25 |
rParams = /(\()(?=|\s*\()|(?:([([])\s*)?(?:([#~]?[\w$.]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*!:?\/]|(=))\s*|([#~]?[\w$.]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*([)\]])([([]?))|(\s+)/g, |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
rNewLine = /\r?\n/g, |
| 30 |
rUnescapeQuotes = /\\(['"])/g, |
| 31 |
rEscapeQuotes = /\\?(['"])/g, |
| 32 |
rBuildHash = /\x08(~)?([^\x08]+)\x08/g, |
| 33 |
|
| 34 |
autoViewKey = 0, |
| 35 |
autoTmplName = 0, |
| 36 |
escapeMapForHtml = { |
| 37 |
"&": "&", |
| 38 |
"<": "<", |
| 39 |
">": ">" |
| 40 |
}, |
| 41 |
tmplAttr = "data-jsv-tmpl", |
| 42 |
fnDeclStr = "var j=j||" + (jQuery ? "jQuery." : "js") + "views,", |
| 43 |
htmlSpecialChar = /[\x00"&'<>]/g, |
| 44 |
slice = Array.prototype.slice, |
| 45 |
|
| 46 |
render = {}, |
| 47 |
|
| 48 |
|
| 49 |
jsv = { |
| 50 |
jsviews: versionNumber, |
| 51 |
sub: sub, |
| 52 |
debugMode: TRUE, |
| 53 |
err: function( e ) { |
| 54 |
return jsv.debugMode ? ("<br/><b>Error:</b> <em> " + (e.message || e) + ". </em>") : '""'; |
| 55 |
}, |
| 56 |
tmplFn: tmplFn, |
| 57 |
render: render, |
| 58 |
templates: templates, |
| 59 |
tags: tags, |
| 60 |
helpers: helpers, |
| 61 |
converters: converters, |
| 62 |
View: View, |
| 63 |
convert: convert, |
| 64 |
delimiters: setDelimiters, |
| 65 |
tag: renderTag |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
function setDelimiters( openChars, closeChars ) { |
| 75 |
|
| 76 |
|
| 77 |
var firstOpenChar = "\\" + openChars.charAt( 0 ), |
| 78 |
secondOpenChar = "\\" + openChars.charAt( 1 ), |
| 79 |
firstCloseChar = "\\" + closeChars.charAt( 0 ), |
| 80 |
secondCloseChar = "\\" + closeChars.charAt( 1 ); |
| 81 |
|
| 82 |
jsv.rTag = rTag |
| 83 |
= secondOpenChar |
| 84 |
|
| 85 |
+ "(?:(?:(\\w+(?=[\\/\\s" + firstCloseChar + "]))|(?:(\\w+)?(:)|(>)|(\\*)))" |
| 86 |
|
| 87 |
+ "\\s*((?:[^" + firstCloseChar + "]|" + firstCloseChar + "(?!" + secondCloseChar + "))*?)" |
| 88 |
|
| 89 |
+ "(\\/)?|(?:\\/(\\w+)))" |
| 90 |
|
| 91 |
+ firstCloseChar; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
rTag = new RegExp( firstOpenChar + rTag + secondCloseChar, "g" ); |
| 98 |
rTmplString = new RegExp( "<.*>|" + openChars + ".*" + closeChars ); |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function getHelper( helper ) { |
| 107 |
|
| 108 |
var view = this, |
| 109 |
tmplHelpers = view.tmpl.helpers || {}; |
| 110 |
helper = (view.ctx[ helper ] !== undefined ? view.ctx : tmplHelpers[ helper ] !== undefined ? tmplHelpers : helpers[ helper ] !== undefined ? helpers : {})[ helper ]; |
| 111 |
return typeof helper !== "function" ? helper : function() { |
| 112 |
return helper.apply(view, arguments); |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
function convert( converter, view, text ) { |
| 121 |
var tmplConverters = view.tmpl.converters; |
| 122 |
converter = tmplConverters && tmplConverters[ converter ] || converters[ converter ]; |
| 123 |
return converter ? converter.call( view, text ) : text; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
function renderTag( tag, parentView, converter, content, tagObject ) { |
| 131 |
|
| 132 |
|
| 133 |
tagObject.props = tagObject.props || {}; |
| 134 |
var ret, |
| 135 |
tmpl = tagObject.props.tmpl, |
| 136 |
tmplTags = parentView.tmpl.tags, |
| 137 |
nestedTemplates = parentView.tmpl.templates, |
| 138 |
args = arguments, |
| 139 |
tagFn = tmplTags && tmplTags[ tag ] || tags[ tag ]; |
| 140 |
|
| 141 |
if ( !tagFn ) { |
| 142 |
return ""; |
| 143 |
} |
| 144 |
|
| 145 |
content = content && parentView.tmpl.tmpls[ content - 1 ]; |
| 146 |
tmpl = tmpl || content || undefined; |
| 147 |
tagObject.tmpl = |
| 148 |
"" + tmpl === tmpl |
| 149 |
? nestedTemplates && nestedTemplates[ tmpl ] || templates[ tmpl ] || templates( tmpl ) |
| 150 |
: tmpl; |
| 151 |
|
| 152 |
tagObject.isTag = TRUE; |
| 153 |
tagObject.converter = converter; |
| 154 |
tagObject.view = parentView; |
| 155 |
tagObject.renderContent = renderContent; |
| 156 |
if ( parentView.ctx ) { |
| 157 |
extend( tagObject.ctx, parentView.ctx); |
| 158 |
} |
| 159 |
|
| 160 |
ret = tagFn.apply( tagObject, args.length > 5 ? slice.call( args, 5 ) : [] ); |
| 161 |
return ret || ( ret == undefined ? "" : ret.toString()); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
function View( context, path, parentView, data, template, index ) { |
| 169 |
|
| 170 |
var views = parentView.views, |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
self = { |
| 175 |
tmpl: template, |
| 176 |
path: path, |
| 177 |
parent: parentView, |
| 178 |
data: data, |
| 179 |
ctx: context, |
| 180 |
views: $.isArray( data ) ? [] : {}, |
| 181 |
hlp: getHelper |
| 182 |
}; |
| 183 |
|
| 184 |
if ( $.isArray( views )) { |
| 185 |
views.splice( |
| 186 |
self.index = index !== undefined |
| 187 |
? index |
| 188 |
: views.length, 0, self |
| 189 |
); |
| 190 |
} else { |
| 191 |
views[ self.index = "_" + autoViewKey++ ] = self; |
| 192 |
} |
| 193 |
return self; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
function addToStore( self, store, name, item, process ) { |
| 201 |
|
| 202 |
var key, onStore; |
| 203 |
if ( name && typeof name === "object" && !name.nodeType ) { |
| 204 |
|
| 205 |
for ( key in name ) { |
| 206 |
store( key, name[ key ]); |
| 207 |
} |
| 208 |
return self; |
| 209 |
} |
| 210 |
if ( !name || item === undefined ) { |
| 211 |
if ( process ) { |
| 212 |
item = process( undefined, item || name ); |
| 213 |
} |
| 214 |
} else if ( "" + name === name ) { |
| 215 |
if ( item === null ) { |
| 216 |
|
| 217 |
delete store[name]; |
| 218 |
} else if ( item = process ? process( name, item ) : item ) { |
| 219 |
store[ name ] = item; |
| 220 |
} |
| 221 |
} |
| 222 |
if ( onStore = sub.onStoreItem ) { |
| 223 |
|
| 224 |
onStore( store, name, item, process ); |
| 225 |
} |
| 226 |
return item; |
| 227 |
} |
| 228 |
|
| 229 |
function templates( name, tmpl ) { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
return addToStore( this, templates, name, tmpl, compile ); |
| 238 |
} |
| 239 |
|
| 240 |
function tags( name, tagFn ) { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
return addToStore( this, tags, name, tagFn ); |
| 249 |
} |
| 250 |
|
| 251 |
function helpers( name, helperFn ) { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
return addToStore( this, helpers, name, helperFn ); |
| 258 |
} |
| 259 |
|
| 260 |
function converters( name, converterFn ) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
return addToStore( this, converters, name, converterFn ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
function renderContent( data, context, parentView, path, index ) { |
| 274 |
|
| 275 |
|
| 276 |
var i, l, dataItem, newView, itemWrap, itemsWrap, itemResult, parentContext, tmpl, layout, |
| 277 |
props = {}, |
| 278 |
swapContent = index === TRUE, |
| 279 |
self = this, |
| 280 |
result = ""; |
| 281 |
|
| 282 |
if ( self.isTag ) { |
| 283 |
|
| 284 |
tmpl = self.tmpl; |
| 285 |
context = context || self.ctx; |
| 286 |
parentView = parentView || self.view; |
| 287 |
path = path || self.path; |
| 288 |
index = index || self.index; |
| 289 |
props = self.props; |
| 290 |
} else { |
| 291 |
tmpl = self.jquery && self[0] |
| 292 |
|| self; |
| 293 |
} |
| 294 |
parentView = parentView || jsv.topView; |
| 295 |
parentContext = parentView.ctx; |
| 296 |
layout = tmpl.layout; |
| 297 |
if ( data === parentView ) { |
| 298 |
|
| 299 |
|
| 300 |
data = parentView.data; |
| 301 |
layout = TRUE; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
context = (context && context === parentContext) |
| 307 |
? parentContext |
| 308 |
: (parentContext |
| 309 |
|
| 310 |
? ((parentContext = extend( {}, parentContext ), context) |
| 311 |
|
| 312 |
? extend( parentContext, context ) |
| 313 |
: parentContext) |
| 314 |
|
| 315 |
: context || {}); |
| 316 |
|
| 317 |
if ( props.link === FALSE ) { |
| 318 |
|
| 319 |
|
| 320 |
context.link = FALSE; |
| 321 |
} |
| 322 |
if ( !tmpl.fn ) { |
| 323 |
tmpl = templates[ tmpl ] || templates( tmpl ); |
| 324 |
} |
| 325 |
itemWrap = context.link && sub.onRenderItem; |
| 326 |
itemsWrap = context.link && sub.onRenderItems; |
| 327 |
|
| 328 |
if ( tmpl ) { |
| 329 |
if ( $.isArray( data ) && !layout ) { |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
newView = swapContent ? parentView : (index !== undefined && parentView) || View( context, path, parentView, data, tmpl, index ); |
| 334 |
|
| 335 |
for ( i = 0, l = data.length; i < l; i++ ) { |
| 336 |
|
| 337 |
dataItem = data[ i ]; |
| 338 |
itemResult = tmpl.fn( dataItem, View( context, path, newView, dataItem, tmpl, (index||0) + i ), jsv ); |
| 339 |
result += itemWrap ? itemWrap( itemResult, props ) : itemResult; |
| 340 |
} |
| 341 |
} else { |
| 342 |
|
| 343 |
newView = swapContent ? parentView : View( context, path, parentView, data, tmpl, index ); |
| 344 |
result += (data || layout) ? tmpl.fn( data, newView, jsv ) : ""; |
| 345 |
} |
| 346 |
parentView.topKey = newView.index; |
| 347 |
return itemsWrap ? itemsWrap( result, path, newView.index, tmpl, props ) : result; |
| 348 |
} |
| 349 |
return ""; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
function syntaxError() { |
| 360 |
throw "Syntax error"; |
| 361 |
} |
| 362 |
|
| 363 |
function tmplFn( markup, tmpl, bind ) { |
| 364 |
|
| 365 |
|
| 366 |
var newNode, node, i, l, code, hasTag, hasEncoder, getsValue, hasConverter, hasViewPath, tag, converter, params, hash, nestedTmpl, allowCode, |
| 367 |
tmplOptions = tmpl ? { |
| 368 |
allowCode: allowCode = tmpl.allowCode, |
| 369 |
debug: tmpl.debug |
| 370 |
} : {}, |
| 371 |
nested = tmpl && tmpl.tmpls, |
| 372 |
astTop = [], |
| 373 |
loc = 0, |
| 374 |
stack = [], |
| 375 |
content = astTop, |
| 376 |
current = [,,,astTop], |
| 377 |
nestedIndex = 0; |
| 378 |
|
| 379 |
|
| 380 |
function pushPreceedingContent( shift ) { |
| 381 |
shift -= loc; |
| 382 |
if ( shift ) { |
| 383 |
content.push( markup.substr( loc, shift ).replace( rNewLine, "\\n" )); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
function parseTag( all, tagName, converter, colon, html, code, params, slash, closeBlock, index ) { |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if ( html ) { |
| 392 |
colon = ":"; |
| 393 |
converter = "html"; |
| 394 |
} |
| 395 |
var hash = "", |
| 396 |
passedCtx = "", |
| 397 |
block = !slash && !colon; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
tagName = tagName || colon; |
| 402 |
pushPreceedingContent( index ); |
| 403 |
loc = index + all.length; |
| 404 |
if ( code ) { |
| 405 |
if ( allowCode ) { |
| 406 |
content.push([ "*", params.replace( rUnescapeQuotes, "$1" ) ]); |
| 407 |
} |
| 408 |
} else if ( tagName ) { |
| 409 |
if ( tagName === "else" ) { |
| 410 |
current[ 5 ] = markup.substring( current[ 5 ], index ); |
| 411 |
current = stack.pop(); |
| 412 |
content = current[ 3 ]; |
| 413 |
block = TRUE; |
| 414 |
} |
| 415 |
params = (params |
| 416 |
? parseParams( params, bind ) |
| 417 |
.replace( rBuildHash, function( all, isCtx, keyValue ) { |
| 418 |
if ( isCtx ) { |
| 419 |
passedCtx += keyValue + ","; |
| 420 |
} else { |
| 421 |
hash += keyValue + ","; |
| 422 |
} |
| 423 |
return ""; |
| 424 |
}) |
| 425 |
: ""); |
| 426 |
hash = hash.slice( 0, -1 ); |
| 427 |
params = params.slice( 0, -1 ); |
| 428 |
newNode = [ |
| 429 |
tagName, |
| 430 |
converter || "", |
| 431 |
params, |
| 432 |
block && [], |
| 433 |
"{" + (hash ? ("props:{" + hash + "},"): "") + "path:'" + params + "'" + (passedCtx ? ",ctx:{" + passedCtx.slice( 0, -1 ) + "}" : "") + "}" |
| 434 |
]; |
| 435 |
if ( block ) { |
| 436 |
stack.push( current ); |
| 437 |
current = newNode; |
| 438 |
current[ 5 ] = loc; |
| 439 |
} |
| 440 |
content.push( newNode ); |
| 441 |
} else if ( closeBlock ) { |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
current[ 5 ] = markup.substring( current[ 5 ], index ); |
| 446 |
current = stack.pop(); |
| 447 |
} |
| 448 |
if ( !current ) { |
| 449 |
throw "Expected block tag"; |
| 450 |
} |
| 451 |
content = current[ 3 ]; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
markup = markup.replace( rEscapeQuotes, "\\$1" ); |
| 456 |
|
| 457 |
|
| 458 |
markup.replace( rTag, parseTag ); |
| 459 |
|
| 460 |
pushPreceedingContent( markup.length ); |
| 461 |
|
| 462 |
|
| 463 |
l = astTop.length; |
| 464 |
code = (l ? "" : '"";'); |
| 465 |
|
| 466 |
for ( i = 0; i < l; i++ ) { |
| 467 |
|
| 468 |
node = astTop[ i ]; |
| 469 |
if ( node[ 0 ] === "*" ) { |
| 470 |
code = code.slice( 0, i ? -1 : -3 ) + ";" + node[ 1 ] + (i + 1 < l ? "ret+=" : ""); |
| 471 |
} else if ( "" + node === node ) { |
| 472 |
code += '"' + node + '"+'; |
| 473 |
} else { |
| 474 |
tag = node[ 0 ]; |
| 475 |
converter = node[ 1 ]; |
| 476 |
params = node[ 2 ]; |
| 477 |
content = node[ 3 ]; |
| 478 |
hash = node[ 4 ]; |
| 479 |
markup = node[ 5 ]; |
| 480 |
if ( content ) { |
| 481 |
|
| 482 |
nestedTmpl = TmplObject( markup, tmplOptions, tmpl, nestedIndex++ ); |
| 483 |
|
| 484 |
tmplFn( markup, nestedTmpl); |
| 485 |
nested.push( nestedTmpl ); |
| 486 |
} |
| 487 |
hasViewPath = hasViewPath || hash.indexOf( "view" ) > -1; |
| 488 |
code += (tag === ":" |
| 489 |
? (converter === "html" |
| 490 |
? (hasEncoder = TRUE, "e(" + params) |
| 491 |
: converter |
| 492 |
? (hasConverter = TRUE, 'c("' + converter + '",view,' + params) |
| 493 |
: (getsValue = TRUE, "((v=" + params + ')!=u?v:""') |
| 494 |
) |
| 495 |
: (hasTag = TRUE, 't("' + tag + '",view,"' + (converter || "") + '",' |
| 496 |
+ (content ? nested.length : '""') |
| 497 |
+ "," + hash + (params ? "," : "") + params)) |
| 498 |
+ ")+"; |
| 499 |
} |
| 500 |
} |
| 501 |
code = new Function( "data, view, j, b, u", fnDeclStr |
| 502 |
+ (getsValue ? "v," : "") |
| 503 |
+ (hasTag ? "t=j.tag," : "") |
| 504 |
+ (hasConverter ? "c=j.convert," : "") |
| 505 |
+ (hasEncoder ? "e=j.converters.html," : "") |
| 506 |
+ "ret; try{\n\n" |
| 507 |
+ (tmplOptions.debug ? "debugger;" : "") |
| 508 |
+ (allowCode ? 'ret=' : 'return ') |
| 509 |
+ code.slice( 0, -1 ) + ";\n\n" |
| 510 |
+ (allowCode ? "return ret;" : "") |
| 511 |
+ "}catch(e){return j.err(e);}" |
| 512 |
); |
| 513 |
|
| 514 |
|
| 515 |
if ( tmpl ) { |
| 516 |
tmpl.fn = code; |
| 517 |
tmpl.useVw = hasConverter || hasViewPath || hasTag; |
| 518 |
} |
| 519 |
return code; |
| 520 |
} |
| 521 |
|
| 522 |
function parseParams( params, bind ) { |
| 523 |
var named, |
| 524 |
fnCall = {}, |
| 525 |
parenDepth = 0, |
| 526 |
quoted = FALSE, |
| 527 |
aposed = FALSE; |
| 528 |
|
| 529 |
function parseTokens( all, lftPrn0, lftPrn, path, operator, err, eq, path2, prn, comma, lftPrn2, apos, quot, rtPrn, prn2, space ) { |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
operator = operator || ""; |
| 534 |
lftPrn = lftPrn || lftPrn0 || lftPrn2; |
| 535 |
path = path || path2; |
| 536 |
prn = prn || prn2 || ""; |
| 537 |
operator = operator || ""; |
| 538 |
|
| 539 |
function parsePath( all, object, helper, view, viewProperty, pathTokens, leafToken ) { |
| 540 |
|
| 541 |
|
| 542 |
if ( object ) { |
| 543 |
var ret = (helper |
| 544 |
? 'view.hlp("' + helper + '")' |
| 545 |
: view |
| 546 |
? "view" |
| 547 |
: "data") |
| 548 |
+ (leafToken |
| 549 |
? (viewProperty |
| 550 |
? "." + viewProperty |
| 551 |
: helper |
| 552 |
? "" |
| 553 |
: (view ? "" : "." + object) |
| 554 |
) + (pathTokens || "") |
| 555 |
: (leafToken = helper ? "" : view ? viewProperty || "" : object, "")); |
| 556 |
|
| 557 |
if ( bind && prn !== "(" ) { |
| 558 |
ret = "b(" + ret + ',"' + leafToken + '")'; |
| 559 |
} |
| 560 |
return ret + (leafToken ? "." + leafToken : ""); |
| 561 |
} |
| 562 |
return all; |
| 563 |
} |
| 564 |
|
| 565 |
if ( err ) { |
| 566 |
syntaxError(); |
| 567 |
} else { |
| 568 |
return (aposed |
| 569 |
|
| 570 |
? (aposed = !apos, (aposed ? all : '"')) |
| 571 |
: quoted |
| 572 |
|
| 573 |
? (quoted = !quot, (quoted ? all : '"')) |
| 574 |
: |
| 575 |
( |
| 576 |
(lftPrn |
| 577 |
? (parenDepth++, lftPrn) |
| 578 |
: "") |
| 579 |
+ (space |
| 580 |
? (parenDepth |
| 581 |
? "" |
| 582 |
: named |
| 583 |
? (named = FALSE, "\b") |
| 584 |
: "," |
| 585 |
) |
| 586 |
: eq |
| 587 |
|
| 588 |
? (parenDepth && syntaxError(), named = TRUE, '\b' + path + ':') |
| 589 |
: path |
| 590 |
|
| 591 |
? (path.replace( rPath, parsePath ) |
| 592 |
+ (prn |
| 593 |
? (fnCall[ ++parenDepth ] = TRUE, prn) |
| 594 |
: operator) |
| 595 |
) |
| 596 |
: operator |
| 597 |
? all |
| 598 |
: rtPrn |
| 599 |
|
| 600 |
? ((fnCall[ parenDepth-- ] = FALSE, rtPrn) |
| 601 |
+ (prn |
| 602 |
? (fnCall[ ++parenDepth ] = TRUE, prn) |
| 603 |
: "") |
| 604 |
) |
| 605 |
: comma |
| 606 |
? (fnCall[ parenDepth ] || syntaxError(), ",") |
| 607 |
: lftPrn0 |
| 608 |
? "" |
| 609 |
: (aposed = apos, quoted = quot, '"') |
| 610 |
)) |
| 611 |
); |
| 612 |
} |
| 613 |
} |
| 614 |
params = (params + " " ).replace( rParams, parseTokens ); |
| 615 |
return params; |
| 616 |
} |
| 617 |
|
| 618 |
function compile( name, tmpl, parent, options ) { |
| 619 |
|
| 620 |
|
| 621 |
var tmplOrMarkup, elem, key, nested, nestedItem; |
| 622 |
|
| 623 |
|
| 624 |
function tmplOrMarkupFromStr( value ) { |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if ( ("" + value === value) || value.nodeType > 0 ) { |
| 629 |
|
| 630 |
elem = value.nodeType > 0 ? value : !rTmplString.test( value ) && jQuery && jQuery( value )[0]; |
| 631 |
if ( elem && elem.type ) { |
| 632 |
|
| 633 |
|
| 634 |
value = templates[ elem.getAttribute( tmplAttr )]; |
| 635 |
if ( !value ) { |
| 636 |
|
| 637 |
name = name || "_" + autoTmplName++; |
| 638 |
elem.setAttribute( tmplAttr, name ); |
| 639 |
value = compile( name, elem.innerHTML, parent, options ); |
| 640 |
templates[ name ] = value; |
| 641 |
} |
| 642 |
} |
| 643 |
return value; |
| 644 |
} |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
tmplOrMarkup = tmplOrMarkupFromStr( tmpl ); |
| 650 |
|
| 651 |
|
| 652 |
options = options || (tmpl.markup ? tmpl : {}); |
| 653 |
options.name = name; |
| 654 |
nested = options.templates; |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
if ( !tmplOrMarkup && tmpl.markup && (tmplOrMarkup = tmplOrMarkupFromStr( tmpl.markup ))) { |
| 659 |
if ( tmplOrMarkup.fn && (tmplOrMarkup.debug !== tmpl.debug || tmplOrMarkup.allowCode !== tmpl.allowCode )) { |
| 660 |
|
| 661 |
tmplOrMarkup = tmplOrMarkup.markup; |
| 662 |
} |
| 663 |
} |
| 664 |
if ( tmplOrMarkup !== undefined ) { |
| 665 |
if ( name && !parent ) { |
| 666 |
render[ name ] = function() { |
| 667 |
return tmpl.render.apply( tmpl, arguments ); |
| 668 |
}; |
| 669 |
} |
| 670 |
if ( tmplOrMarkup.fn || tmpl.fn ) { |
| 671 |
|
| 672 |
if ( tmplOrMarkup.fn ) { |
| 673 |
if ( name && name !== tmplOrMarkup.name ) { |
| 674 |
tmpl = extend( extend( {}, tmplOrMarkup ), options); |
| 675 |
} else { |
| 676 |
tmpl = tmplOrMarkup; |
| 677 |
} |
| 678 |
} |
| 679 |
} else { |
| 680 |
|
| 681 |
|
| 682 |
tmpl = TmplObject( tmplOrMarkup, options, parent, 0 ); |
| 683 |
|
| 684 |
tmplFn( tmplOrMarkup, tmpl ); |
| 685 |
} |
| 686 |
for ( key in nested ) { |
| 687 |
|
| 688 |
nestedItem = nested[ key ]; |
| 689 |
if ( nestedItem.name !== key ) { |
| 690 |
nested[ key ] = compile( key, nestedItem, tmpl ); |
| 691 |
} |
| 692 |
} |
| 693 |
return tmpl; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
function TmplObject( markup, options, parent, index ) { |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
function extendStore( storeName ) { |
| 703 |
if ( parent[ storeName ]) { |
| 704 |
|
| 705 |
tmpl[ storeName ] = extend( extend( {}, parent[ storeName ] ), options[ storeName ] ); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
options = options || {}; |
| 710 |
var tmpl = { |
| 711 |
markup: markup, |
| 712 |
tmpls: [], |
| 713 |
links: [], |
| 714 |
render: renderContent |
| 715 |
}; |
| 716 |
if ( parent ) { |
| 717 |
if ( parent.templates ) { |
| 718 |
tmpl.templates = extend( extend( {}, parent.templates ), options.templates ); |
| 719 |
} |
| 720 |
tmpl.parent = parent; |
| 721 |
tmpl.name = parent.name + "[" + index + "]"; |
| 722 |
tmpl.index = index; |
| 723 |
} |
| 724 |
|
| 725 |
extend( tmpl, options ); |
| 726 |
if ( parent ) { |
| 727 |
extendStore( "templates" ); |
| 728 |
extendStore( "tags" ); |
| 729 |
extendStore( "helpers" ); |
| 730 |
extendStore( "converters" ); |
| 731 |
} |
| 732 |
return tmpl; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
if ( jQuery ) { |
| 738 |
|
| 739 |
|
| 740 |
$ = jQuery; |
| 741 |
$.templates = templates; |
| 742 |
$.render = render; |
| 743 |
$.views = jsv; |
| 744 |
$.fn.render = renderContent; |
| 745 |
|
| 746 |
} else { |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
$ = window.jsviews = jsv; |
| 751 |
$.extend = function( target, source ) { |
| 752 |
var name; |
| 753 |
target = target || {}; |
| 754 |
for ( name in source ) { |
| 755 |
target[ name ] = source[ name ]; |
| 756 |
} |
| 757 |
return target; |
| 758 |
}; |
| 759 |
|
| 760 |
$.isArray = Array && Array.isArray || function( obj ) { |
| 761 |
return Object.prototype.toString.call( obj ) === "[object Array]"; |
| 762 |
}; |
| 763 |
} |
| 764 |
|
| 765 |
extend = $.extend; |
| 766 |
|
| 767 |
jsv.topView = { views: {}, tmpl: {}, hlp: getHelper, ctx: jsv.helpers }; |
| 768 |
|
| 769 |
function replacerForHtml( ch ) { |
| 770 |
|
| 771 |
return escapeMapForHtml[ ch ] |
| 772 |
|
| 773 |
|| (escapeMapForHtml[ ch ] = "&#" + ch.charCodeAt( 0 ) + ";"); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
tags({ |
| 779 |
"if": function() { |
| 780 |
var ifTag = this, |
| 781 |
view = ifTag.view; |
| 782 |
|
| 783 |
view.onElse = function( tagObject, args ) { |
| 784 |
var i = 0, |
| 785 |
l = args.length; |
| 786 |
|
| 787 |
while ( l && !args[ i++ ]) { |
| 788 |
|
| 789 |
if ( i === l ) { |
| 790 |
return ""; |
| 791 |
} |
| 792 |
} |
| 793 |
view.onElse = undefined; |
| 794 |
tagObject.path = ""; |
| 795 |
return tagObject.renderContent( view ); |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
}; |
| 800 |
return view.onElse( this, arguments ); |
| 801 |
}, |
| 802 |
"else": function() { |
| 803 |
var view = this.view; |
| 804 |
return view.onElse ? view.onElse( this, arguments ) : ""; |
| 805 |
}, |
| 806 |
"for": function() { |
| 807 |
var i, |
| 808 |
self = this, |
| 809 |
result = "", |
| 810 |
args = arguments, |
| 811 |
l = args.length; |
| 812 |
if ( self.props.layout ) { |
| 813 |
self.tmpl.layout = TRUE; |
| 814 |
} |
| 815 |
for ( i = 0; i < l; i++ ) { |
| 816 |
result += self.renderContent( args[ i ]); |
| 817 |
} |
| 818 |
return result; |
| 819 |
}, |
| 820 |
"=": function( value ) { |
| 821 |
return value; |
| 822 |
}, |
| 823 |
"*": function( value ) { |
| 824 |
return value; |
| 825 |
} |
| 826 |
}); |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
converters({ |
| 837 |
html: function( text ) { |
| 838 |
|
| 839 |
|
| 840 |
return text != undefined ? String( text ).replace( htmlSpecialChar, replacerForHtml ) : ""; |
| 841 |
} |
| 842 |
}); |
| 843 |
|
| 844 |
|
| 845 |
setDelimiters( "{{", "}}" ); |
| 846 |
|
| 847 |
})( this ); |