1959 lines
69 KiB
JavaScript
1959 lines
69 KiB
JavaScript
|
/*! JsRender v1.0.0-beta: http://www.jsviews.com/#jsrender
|
||
|
informal pre V1.0 commit counter: 64pre*/
|
||
|
/*
|
||
|
* Optimized version of jQuery Templates, for rendering to string.
|
||
|
* Does not require jQuery, or HTML DOM
|
||
|
* Integrates with JsViews (http://www.jsviews.com/#jsviews)
|
||
|
*
|
||
|
* Copyright 2015, Boris Moore
|
||
|
* Released under the MIT License.
|
||
|
*/
|
||
|
|
||
|
//jshint -W018, -W041
|
||
|
|
||
|
(function (factory) {
|
||
|
if (typeof define === 'function' && define.amd) {
|
||
|
// Loading from AMD script loader. Register as an anonymous module.
|
||
|
define(factory);
|
||
|
} else if (typeof exports === 'object') {
|
||
|
// CommonJS
|
||
|
var jsrender = module.exports = factory(true, require("fs")); // jsrender = jsviews.views
|
||
|
|
||
|
jsrender.renderFile = jsrender.__express = function(filepath, data, callback) { // Support for rendering templates from file
|
||
|
// system in Node.js Node, and for Express template engine integration, using app.engine('html', jsrender.__express);
|
||
|
var html = jsrender.templates("@" + filepath).render(data);
|
||
|
if (callback) {
|
||
|
callback(null, html);
|
||
|
}
|
||
|
return html;
|
||
|
};
|
||
|
} else {
|
||
|
// Browser using plain <script> tag
|
||
|
factory(false);
|
||
|
}
|
||
|
} (function (isCommonJS, fs) {
|
||
|
"use strict";
|
||
|
|
||
|
isCommonJS = isCommonJS === true;
|
||
|
|
||
|
//========================== Top-level vars ==========================
|
||
|
|
||
|
var versionNumber = "v1.0.0-beta",
|
||
|
|
||
|
// global is the this object, which is window when running in the usual browser environment.
|
||
|
global = (0, eval)('this'), // jshint ignore:line
|
||
|
|
||
|
$ = global.jQuery,
|
||
|
|
||
|
jsvStoreName, rTag, rTmplString, topView,
|
||
|
|
||
|
//TODO tmplFnsCache = {},
|
||
|
|
||
|
document = global.document,
|
||
|
|
||
|
delimOpenChar0 = "{", delimOpenChar1 = "{", delimCloseChar0 = "}", delimCloseChar1 = "}", linkChar = "^",
|
||
|
|
||
|
rPath = /^(!*?)(?:null|true|false|\d[\d.]*|([\w$]+|\.|~([\w$]+)|#(view|([\w$]+))?)([\w$.^]*?)(?:[.[^]([\w$]+)\]?)?)$/g,
|
||
|
// not object helper view viewProperty pathTokens leafToken
|
||
|
|
||
|
rParams = /(\()(?=\s*\()|(?:([([])\s*)?(?:(\^?)(!*?[#~]?[\w$.^]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*:?\/]|(=))\s*|(!*?[#~]?[\w$.^]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*(([)\]])(?=\s*[.^]|\s*$|[^\(\[])|[)\]])([([]?))|(\s+)/g,
|
||
|
// lftPrn0 lftPrn bound path operator err eq path2 prn comma lftPrn2 apos quot rtPrn rtPrnDot prn2 space
|
||
|
// (left paren? followed by (path? followed by operator) or (path followed by left paren?)) or comma or apos or quot or right paren or space
|
||
|
|
||
|
isRenderCall,
|
||
|
rNewLine = /[ \t]*(\r\n|\n|\r)/g,
|
||
|
rUnescapeQuotes = /\\(['"])/g,
|
||
|
rEscapeQuotes = /['"\\]/g, // Escape quotes and \ character
|
||
|
rBuildHash = /(?:\x08|^)(onerror:)?(?:(~?)(([\w$_\.]+):)?([^\x08]+))\x08(,)?([^\x08]+)/gi,
|
||
|
rTestElseIf = /^if\s/,
|
||
|
rFirstElem = /<(\w+)[>\s]/,
|
||
|
rAttrEncode = /[\x00`><"'&]/g, // Includes > encoding since rConvertMarkers in JsViews does not skip > characters in attribute strings
|
||
|
rIsHtml = /[\x00`><\"'&]/,
|
||
|
rHasHandlers = /^on[A-Z]|^convert(Back)?$/,
|
||
|
rHtmlEncode = rAttrEncode,
|
||
|
autoTmplName = 0,
|
||
|
viewId = 0,
|
||
|
charEntities = {
|
||
|
"&": "&",
|
||
|
"<": "<",
|
||
|
">": ">",
|
||
|
"\x00": "�",
|
||
|
"'": "'",
|
||
|
'"': """,
|
||
|
"`": "`"
|
||
|
},
|
||
|
HTML = "html",
|
||
|
OBJECT = "object",
|
||
|
tmplAttr = "data-jsv-tmpl",
|
||
|
indexStr = "For #index in nested block use #getIndex().",
|
||
|
$render = {},
|
||
|
jsvStores = {
|
||
|
template: {
|
||
|
compile: compileTmpl
|
||
|
},
|
||
|
tag: {
|
||
|
compile: compileTag
|
||
|
},
|
||
|
helper: {},
|
||
|
converter: {}
|
||
|
},
|
||
|
|
||
|
// jsviews object ($.views if jQuery is loaded)
|
||
|
$views = {
|
||
|
jsviews: versionNumber,
|
||
|
settings: function(settings) {
|
||
|
$extend($viewsSettings, settings);
|
||
|
dbgMode($viewsSettings._dbgMode);
|
||
|
if ($viewsSettings.jsv) {
|
||
|
$viewsSettings.jsv();
|
||
|
}
|
||
|
},
|
||
|
sub: {
|
||
|
// subscription, e.g. JsViews integration
|
||
|
View: View,
|
||
|
Err: JsViewsError,
|
||
|
tmplFn: tmplFn,
|
||
|
parse: parseParams,
|
||
|
extend: $extend,
|
||
|
syntaxErr: syntaxError,
|
||
|
onStore: {},
|
||
|
_ths: tagHandlersFromProps,
|
||
|
_tg: function() {} // Constructor for tagDef
|
||
|
},
|
||
|
map: dataMap, // If jsObservable loaded first, use that definition of dataMap
|
||
|
_cnvt: convertVal,
|
||
|
_tag: renderTag,
|
||
|
_err: error
|
||
|
},
|
||
|
|
||
|
jsViewsPrev = global.jsviews;
|
||
|
|
||
|
function getDerivedMethod(baseMethod, method) {
|
||
|
return function () {
|
||
|
var ret,
|
||
|
tag = this,
|
||
|
prevBase = tag.base;
|
||
|
|
||
|
tag.base = baseMethod; // Within method call, calling this.base will call the base method
|
||
|
ret = method.apply(tag, arguments); // Call the method
|
||
|
tag.base = prevBase; // Replace this.base to be the base method of the previous call, for chained calls
|
||
|
return ret;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function getMethod(baseMethod, method) {
|
||
|
// For derived methods (or handlers declared declaratively as in {{:foo onChange=~fooChanged}} replace by a derived method, to allow using this.base(...)
|
||
|
// or this.baseApply(arguments) to call the base implementation. (Equivalent to this._super(...) and this._superApply(arguments) in jQuery UI)
|
||
|
if ($isFunction(method)) {
|
||
|
method = getDerivedMethod(
|
||
|
!baseMethod
|
||
|
? noop // no base method implementation, so use noop as base method
|
||
|
: baseMethod._d
|
||
|
? baseMethod // baseMethod is a derived method, so us it
|
||
|
: getDerivedMethod(noop, baseMethod), // baseMethod is not derived so make its base method be the noop method
|
||
|
method
|
||
|
);
|
||
|
method._d = 1; // Add flag that this is a derived method
|
||
|
}
|
||
|
return method;
|
||
|
}
|
||
|
|
||
|
function tagHandlersFromProps(tag, tagCtx) {
|
||
|
for (var prop in tagCtx.props) {
|
||
|
if (rHasHandlers.test(prop)) {
|
||
|
tag[prop] = getMethod(tag[prop], tagCtx.props[prop]);
|
||
|
// Copy over the onFoo props, convert and convertBack from tagCtx.props to tag (overrides values in tagDef).
|
||
|
// Note: unsupported scenario: if handlers are dynamically added ^onFoo=expression this will work, but dynamically removing will not work.
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function retVal(val) {
|
||
|
return val;
|
||
|
}
|
||
|
|
||
|
function noop() {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
function dbgBreak(val) {
|
||
|
// Usage examples: {{dbg:...}}, {{:~dbg(...)}}, {{for ... onAfterLink=~dbg}}, {{dbg .../}} etc.
|
||
|
// To break here, stop on caught exceptions.
|
||
|
try {
|
||
|
debugger;
|
||
|
throw "dbg breakpoint";
|
||
|
}
|
||
|
catch (e) {}
|
||
|
return this.base ? this.baseApply(arguments) : val;
|
||
|
}
|
||
|
|
||
|
function dbgMode(debugMode) {
|
||
|
$viewsSettings._dbgMode = debugMode !== false; // Pass in false to unset. Otherwise sets to true.
|
||
|
}
|
||
|
|
||
|
function JsViewsError(message) {
|
||
|
// Error exception type for JsViews/JsRender
|
||
|
// Override of $.views.sub.Error is possible
|
||
|
this.name = ($.link ? "JsViews" : "JsRender") + " Error";
|
||
|
this.message = message || this.name;
|
||
|
}
|
||
|
|
||
|
function $extend(target, source) {
|
||
|
var name;
|
||
|
for (name in source) {
|
||
|
target[name] = source[name];
|
||
|
}
|
||
|
return target;
|
||
|
}
|
||
|
|
||
|
(JsViewsError.prototype = new Error()).constructor = JsViewsError;
|
||
|
|
||
|
//========================== Top-level functions ==========================
|
||
|
|
||
|
//===================
|
||
|
// jsviews.delimiters
|
||
|
//===================
|
||
|
function $viewsDelimiters(openChars, closeChars, link) {
|
||
|
// Set the tag opening and closing delimiters and 'link' character. Default is "{{", "}}" and "^"
|
||
|
// openChars, closeChars: opening and closing strings, each with two characters
|
||
|
|
||
|
if (this !== 0 || openChars) {
|
||
|
delimOpenChar0 = openChars ? openChars.charAt(0) : delimOpenChar0; // Escape the characters - since they could be regex special characters
|
||
|
delimOpenChar1 = openChars ? openChars.charAt(1) : delimOpenChar1;
|
||
|
delimCloseChar0 = closeChars ? closeChars.charAt(0) : delimCloseChar0;
|
||
|
delimCloseChar1 = closeChars ? closeChars.charAt(1) : delimCloseChar1;
|
||
|
linkChar = link || linkChar;
|
||
|
openChars = "\\" + delimOpenChar0 + "(\\" + linkChar + ")?\\" + delimOpenChar1; // Default is "{^{"
|
||
|
closeChars = "\\" + delimCloseChar0 + "\\" + delimCloseChar1; // Default is "}}"
|
||
|
// Build regex with new delimiters
|
||
|
// tag (followed by / space or }) or cvtr+colon or html or code
|
||
|
rTag = "(?:(?:(\\w+(?=[\\/\\s\\" + delimCloseChar0 + "]))|(?:(\\w+)?(:)|(>)|!--((?:[^-]|-(?!-))*)--|(\\*)))"
|
||
|
+ "\\s*((?:[^\\" + delimCloseChar0 + "]|\\" + delimCloseChar0 + "(?!\\" + delimCloseChar1 + "))*?)";
|
||
|
|
||
|
// make rTag available to JsViews (or other components) for parsing binding expressions
|
||
|
$sub.rTag = rTag + ")";
|
||
|
|
||
|
rTag = new RegExp(openChars + rTag + "(\\/)?|(?:\\/(\\w+)))" + closeChars, "g");
|
||
|
|
||
|
// Default: bind tag converter colon html comment code params slash closeBlock
|
||
|
// /{(\^)?{(?:(?:(\w+(?=[\/\s}]))|(?:(\w+)?(:)|(>)|!--((?:[^-]|-(?!-))*)--|(\*)))\s*((?:[^}]|}(?!}))*?)(\/)?|(?:\/(\w+)))}}/g
|
||
|
|
||
|
rTmplString = new RegExp("<.*>|([^\\\\]|^)[{}]|" + openChars + ".*" + closeChars);
|
||
|
// rTmplString looks for html tags or { or } char not preceded by \\, or JsRender tags {{xxx}}. Each of these strings are considered
|
||
|
// NOT to be jQuery selectors
|
||
|
}
|
||
|
return [delimOpenChar0, delimOpenChar1, delimCloseChar0, delimCloseChar1, linkChar];
|
||
|
}
|
||
|
|
||
|
//=========
|
||
|
// View.get
|
||
|
//=========
|
||
|
|
||
|
function getView(inner, type) { //view.get(inner, type)
|
||
|
if (!type) {
|
||
|
// view.get(type)
|
||
|
type = inner;
|
||
|
inner = undefined;
|
||
|
}
|
||
|
|
||
|
var views, i, l, found,
|
||
|
view = this,
|
||
|
root = !type || type === "root";
|
||
|
// If type is undefined, returns root view (view under top view).
|
||
|
|
||
|
if (inner) {
|
||
|
// Go through views - this one, and all nested ones, depth-first - and return first one with given type.
|
||
|
found = view.type === type ? view : undefined;
|
||
|
if (!found) {
|
||
|
views = view.views;
|
||
|
if (view._.useKey) {
|
||
|
for (i in views) {
|
||
|
if (found = views[i].get(inner, type)) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
for (i = 0, l = views.length; !found && i < l; i++) {
|
||
|
found = views[i].get(inner, type);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} else if (root) {
|
||
|
// Find root view. (view whose parent is top view)
|
||
|
while (view.parent.parent) {
|
||
|
found = view = view.parent;
|
||
|
}
|
||
|
} else {
|
||
|
while (view && !found) {
|
||
|
// Go through views - this one, and all parent ones - and return first one with given type.
|
||
|
found = view.type === type ? view : undefined;
|
||
|
view = view.parent;
|
||
|
}
|
||
|
}
|
||
|
return found;
|
||
|
}
|
||
|
|
||
|
function getNestedIndex() {
|
||
|
var view = this.get("item");
|
||
|
return view ? view.index : undefined;
|
||
|
}
|
||
|
|
||
|
getNestedIndex.depends = function() {
|
||
|
return [this.get("item"), "index"];
|
||
|
};
|
||
|
|
||
|
function getIndex() {
|
||
|
return this.index;
|
||
|
}
|
||
|
|
||
|
getIndex.depends = "index";
|
||
|
|
||
|
//==========
|
||
|
// View.hlp
|
||
|
//==========
|
||
|
|
||
|
function getHelper(helper) {
|
||
|
// Helper method called as view.hlp(key) from compiled template, for helper functions or template parameters ~foo
|
||
|
var wrapped,
|
||
|
view = this,
|
||
|
ctx = view.linkCtx,
|
||
|
res = (view.ctx || {})[helper];
|
||
|
|
||
|
if (res === undefined && ctx && ctx.ctx) {
|
||
|
res = ctx.ctx[helper];
|
||
|
}
|
||
|
if (res === undefined) {
|
||
|
res = $helpers[helper];
|
||
|
}
|
||
|
|
||
|
if (res) {
|
||
|
if ($isFunction(res) && !res._wrp) {
|
||
|
// If it is of type function, and not already wrapped, we will wrap it, so if called with no this pointer it will be called with the
|
||
|
// view as 'this' context. If the helper ~foo() was in a data-link expression, the view will have a 'temporary' linkCtx property too.
|
||
|
// Note that helper functions on deeper paths will have specific this pointers, from the preceding path.
|
||
|
// For example, ~util.foo() will have the ~util object as 'this' pointer
|
||
|
wrapped = function() {
|
||
|
return res.apply((!this || this === global) ? view : this, arguments);
|
||
|
};
|
||
|
wrapped._wrp = true;
|
||
|
$extend(wrapped, res); // Attach same expandos (if any) to the wrapped function
|
||
|
}
|
||
|
}
|
||
|
return wrapped || res;
|
||
|
}
|
||
|
|
||
|
//==============
|
||
|
// jsviews._cnvt
|
||
|
//==============
|
||
|
|
||
|
function convertVal(converter, view, tagCtx, onError) {
|
||
|
// self is template object or linkCtx object
|
||
|
var tag, value,
|
||
|
// if tagCtx is an integer, then it is the key for the compiled function to return the boundTag tagCtx
|
||
|
boundTag = typeof tagCtx === "number" && view.tmpl.bnds[tagCtx-1],
|
||
|
linkCtx = view.linkCtx; // For data-link="{cvt:...}"...
|
||
|
|
||
|
if (onError !== undefined) {
|
||
|
tagCtx = onError = {props: {}, args: [onError]};
|
||
|
} else if (boundTag) {
|
||
|
tagCtx = boundTag(view.data, view, $views);
|
||
|
}
|
||
|
|
||
|
value = tagCtx.args[0];
|
||
|
if (converter || boundTag) {
|
||
|
tag = linkCtx && linkCtx.tag;
|
||
|
if (!tag) {
|
||
|
tag = $extend(new $sub._tg(), {
|
||
|
_: {
|
||
|
inline: !linkCtx,
|
||
|
bnd: boundTag,
|
||
|
unlinked: true
|
||
|
},
|
||
|
tagName: ":",
|
||
|
cvt: converter,
|
||
|
flow: true,
|
||
|
tagCtx: tagCtx
|
||
|
});
|
||
|
if (linkCtx) {
|
||
|
linkCtx.tag = tag;
|
||
|
tag.linkCtx = linkCtx;
|
||
|
}
|
||
|
tagCtx.ctx = extendCtx(tagCtx.ctx, (linkCtx ? linkCtx.view : view).ctx);
|
||
|
}
|
||
|
tag._er = onError && value;
|
||
|
tagHandlersFromProps(tag, tagCtx);
|
||
|
|
||
|
tagCtx.view = view;
|
||
|
|
||
|
tag.ctx = tagCtx.ctx || {};
|
||
|
tagCtx.ctx = undefined;
|
||
|
// Provide this tag on view, for addBindingMarkers on bound tags to add the tag to view._.bnds, associated with the tag id,
|
||
|
view._.tag = tag;
|
||
|
|
||
|
value = tag.cvtArgs(tag.convert || converter !== "true" && converter)[0]; // If there is a convertBack but no convert, converter will be "true"
|
||
|
|
||
|
// Call onRender (used by JsViews if present, to add binding annotations around rendered content)
|
||
|
value = boundTag && view._.onRender
|
||
|
? view._.onRender(value, view, boundTag)
|
||
|
: value;
|
||
|
view._.tag = undefined;
|
||
|
}
|
||
|
return value != undefined ? value : "";
|
||
|
}
|
||
|
|
||
|
function convertArgs(converter) {
|
||
|
var tag = this,
|
||
|
tagCtx = tag.tagCtx,
|
||
|
view = tagCtx.view,
|
||
|
args = tagCtx.args;
|
||
|
|
||
|
converter = tag.convert || converter;
|
||
|
converter = converter && ("" + converter === converter
|
||
|
? (view.getRsc("converters", converter) || error("Unknown converter: '" + converter + "'"))
|
||
|
: converter);
|
||
|
|
||
|
args = !args.length && !tagCtx.index // On the opening tag with no args, bind to the current data context
|
||
|
? [view.data]
|
||
|
: converter
|
||
|
? args.slice() // If there is a converter, use a copy of the tagCtx.args array for rendering, and replace the args[0] in
|
||
|
// the copied array with the converted value. But we do not modify the value of tag.tagCtx.args[0] (the original args array)
|
||
|
: args; // If no converter, get the original tagCtx.args
|
||
|
|
||
|
if (converter) {
|
||
|
if (converter.depends) {
|
||
|
tag.depends = $sub.getDeps(tag.depends, tag, converter.depends, converter);
|
||
|
}
|
||
|
args[0] = converter.apply(tag, args);
|
||
|
}
|
||
|
return args;
|
||
|
}
|
||
|
|
||
|
//=============
|
||
|
// jsviews._tag
|
||
|
//=============
|
||
|
|
||
|
function getResource(resourceType, itemName) {
|
||
|
var res, store,
|
||
|
view = this;
|
||
|
while ((res === undefined) && view) {
|
||
|
store = view.tmpl && view.tmpl[resourceType];
|
||
|
res = store && store[itemName];
|
||
|
view = view.parent;
|
||
|
}
|
||
|
return res || $views[resourceType][itemName];
|
||
|
}
|
||
|
|
||
|
function renderTag(tagName, parentView, tmpl, tagCtxs, isUpdate, onError) {
|
||
|
parentView = parentView || topView;
|
||
|
var tag, tag_, tagDef, template, tags, attr, parentTag, i, l, itemRet, tagCtx, tagCtxCtx, content, callInit, mapDef, thisMap, args, props, initialTmpl,
|
||
|
ret = "",
|
||
|
linkCtx = parentView.linkCtx || 0,
|
||
|
ctx = parentView.ctx,
|
||
|
parentTmpl = tmpl || parentView.tmpl,
|
||
|
// if tagCtx is an integer, then it is the key for the compiled function to return the boundTag tagCtxs
|
||
|
boundTag = typeof tagCtxs === "number" && parentView.tmpl.bnds[tagCtxs-1];
|
||
|
|
||
|
if (tagName._is === "tag") {
|
||
|
tag = tagName;
|
||
|
tagName = tag.tagName;
|
||
|
tagCtxs = tag.tagCtxs;
|
||
|
template = tag.template;
|
||
|
} else {
|
||
|
tagDef = parentView.getRsc("tags", tagName) || error("Unknown tag: {{" + tagName + "}} ");
|
||
|
template = tagDef.template;
|
||
|
}
|
||
|
|
||
|
if (onError !== undefined) {
|
||
|
ret += onError;
|
||
|
tagCtxs = onError = [{props: {}, args: []}];
|
||
|
} else if (boundTag) {
|
||
|
tagCtxs = boundTag(parentView.data, parentView, $views);
|
||
|
}
|
||
|
|
||
|
l = tagCtxs.length;
|
||
|
for (i = 0; i < l; i++) {
|
||
|
tagCtx = tagCtxs[i];
|
||
|
if (!linkCtx || !linkCtx.tag || i && !linkCtx.tag._.inline || tag._er) {
|
||
|
// Initialize tagCtx
|
||
|
// For block tags, tagCtx.tmpl is an integer > 0
|
||
|
if (content = tagCtx.tmpl) {
|
||
|
content = tagCtx.content = parentTmpl.tmpls[content - 1];
|
||
|
}
|
||
|
tagCtx.index = i;
|
||
|
tagCtx.tmpl = template || content; // Set the tmpl property to the content of the block tag
|
||
|
tagCtx.render = renderContent;
|
||
|
tagCtx.view = parentView;
|
||
|
tagCtx.ctx = extendCtx(tagCtx.ctx, ctx); // Clone and extend parentView.ctx
|
||
|
}
|
||
|
if (tmpl = tagCtx.props.tmpl) {
|
||
|
// If the tmpl property is overridden, set the value (when initializing, or, in case of binding: ^tmpl=..., when updating)
|
||
|
tmpl = "" + tmpl === tmpl // if a string
|
||
|
? parentView.getRsc("templates", tmpl) || $templates(tmpl)
|
||
|
: tmpl;
|
||
|
|
||
|
tagCtx.tmpl = tmpl;
|
||
|
}
|
||
|
|
||
|
if (!tag) {
|
||
|
// This will only be hit for initial tagCtx (not for {{else}}) - if the tag instance does not exist yet
|
||
|
// Instantiate tag if it does not yet exist
|
||
|
// If the tag has not already been instantiated, we will create a new instance.
|
||
|
// ~tag will access the tag, even within the rendering of the template content of this tag.
|
||
|
// From child/descendant tags, can access using ~tag.parent, or ~parentTags.tagName
|
||
|
tag = new tagDef._ctr();
|
||
|
callInit = !!tag.init;
|
||
|
|
||
|
tag.parent = parentTag = ctx && ctx.tag;
|
||
|
tag.tagCtxs = tagCtxs;
|
||
|
|
||
|
if (linkCtx) {
|
||
|
tag._.inline = false;
|
||
|
linkCtx.tag = tag;
|
||
|
tag.linkCtx = linkCtx;
|
||
|
}
|
||
|
if (tag._.bnd = boundTag || linkCtx.fn) {
|
||
|
// Bound if {^{tag...}} or data-link="{tag...}"
|
||
|
tag._.arrVws = {};
|
||
|
} else if (tag.dataBoundOnly) {
|
||
|
error("{^{" + tagName + "}} tag must be data-bound");
|
||
|
}
|
||
|
|
||
|
//TODO better perf for childTags() - keep child tag.tags array, (and remove child, when disposed)
|
||
|
// tag.tags = [];
|
||
|
}
|
||
|
tagCtx.tag = tag;
|
||
|
if (tag.dataMap && tag.tagCtxs) {
|
||
|
tagCtx.map = tag.tagCtxs[i].map; // Copy over the compiled map instance from the previous tagCtxs to the refreshed ones
|
||
|
}
|
||
|
if (!tag.flow) {
|
||
|
tagCtxCtx = tagCtx.ctx = tagCtx.ctx || {};
|
||
|
|
||
|
// tags hash: tag.ctx.tags, merged with parentView.ctx.tags,
|
||
|
tags = tag.parents = tagCtxCtx.parentTags = ctx && extendCtx(tagCtxCtx.parentTags, ctx.parentTags) || {};
|
||
|
if (parentTag) {
|
||
|
tags[parentTag.tagName] = parentTag;
|
||
|
//TODO better perf for childTags: parentTag.tags.push(tag);
|
||
|
}
|
||
|
tags[tag.tagName] = tagCtxCtx.tag = tag;
|
||
|
}
|
||
|
}
|
||
|
if (boundTag || linkCtx) {
|
||
|
// Provide this tag on view, for addBindingMarkers on bound tags to add the tag to view._.bnds, associated with the tag id
|
||
|
parentView._.tag = tag;
|
||
|
}
|
||
|
if (!(tag._er = onError)) {
|
||
|
tagHandlersFromProps(tag, tagCtxs[0]);
|
||
|
tag.rendering = {}; // Provide object for state during render calls to tag and elses. (Used by {{if}} and {{for}}...)
|
||
|
for (i = 0; i < l; i++) {
|
||
|
tagCtx = tag.tagCtx = tag.tagCtxs[i];
|
||
|
props = tagCtx.props;
|
||
|
args = tag.cvtArgs();
|
||
|
|
||
|
if (mapDef = props.dataMap || tag.dataMap) {
|
||
|
if (args.length || props.dataMap) {
|
||
|
thisMap = tagCtx.map;
|
||
|
if (!thisMap || thisMap.src !== args[0] || isUpdate) {
|
||
|
if (thisMap && thisMap.src) {
|
||
|
thisMap.unmap(); // only called if observable map - not when only used in JsRender, e.g. by {{props}}
|
||
|
}
|
||
|
thisMap = tagCtx.map = mapDef.map(args[0], props);
|
||
|
}
|
||
|
args = [thisMap.tgt];
|
||
|
}
|
||
|
}
|
||
|
tag.ctx = tagCtx.ctx;
|
||
|
|
||
|
if (!i) {
|
||
|
if (callInit) {
|
||
|
initialTmpl = tag.template;
|
||
|
tag.init(tagCtx, linkCtx, tag.ctx);
|
||
|
callInit = undefined;
|
||
|
if (tag.template !== initialTmpl) {
|
||
|
tag._.tmpl = tag.template; // This will override the tag.template and also tagCtx.props.tmpl for all tagCtxs
|
||
|
}
|
||
|
}
|
||
|
if (linkCtx) {
|
||
|
// Set attr on linkCtx to ensure outputting to the correct target attribute.
|
||
|
// Setting either linkCtx.attr or this.attr in the init() allows per-instance choice of target attrib.
|
||
|
linkCtx.attr = tag.attr = linkCtx.attr || tag.attr;
|
||
|
}
|
||
|
attr = tag.attr;
|
||
|
tag._.noVws = attr && attr !== HTML;
|
||
|
}
|
||
|
|
||
|
itemRet = undefined;
|
||
|
if (tag.render) {
|
||
|
itemRet = tag.render.apply(tag, args);
|
||
|
}
|
||
|
if (!args.length) {
|
||
|
args = [parentView]; // no arguments - get data context from view.
|
||
|
}
|
||
|
if (itemRet === undefined) {
|
||
|
itemRet = tagCtx.render(args.length ? args[0] : parentView, true) || (isUpdate ? undefined : "");
|
||
|
}
|
||
|
// No return value from render, and no template/content tagCtx.render(...), so return undefined
|
||
|
ret = ret ? ret + (itemRet || "") : itemRet; // If no rendered content, this will be undefined
|
||
|
}
|
||
|
tag.rendering = undefined;
|
||
|
}
|
||
|
tag.tagCtx = tag.tagCtxs[0];
|
||
|
tag.ctx = tag.tagCtx.ctx;
|
||
|
|
||
|
if (tag._.noVws) {
|
||
|
if (tag._.inline) {
|
||
|
// inline tag with attr set to "text" will insert HTML-encoded content - as if it was element-based innerText
|
||
|
ret = attr === "text"
|
||
|
? $converters.html(ret)
|
||
|
: "";
|
||
|
}
|
||
|
}
|
||
|
return boundTag && parentView._.onRender
|
||
|
// Call onRender (used by JsViews if present, to add binding annotations around rendered content)
|
||
|
? parentView._.onRender(ret, parentView, boundTag)
|
||
|
: ret;
|
||
|
}
|
||
|
|
||
|
//=================
|
||
|
// View constructor
|
||
|
//=================
|
||
|
|
||
|
function View(context, type, parentView, data, template, key, contentTmpl, onRender) {
|
||
|
// Constructor for view object in view hierarchy. (Augmented by JsViews if JsViews is loaded)
|
||
|
var views, parentView_, tag, self_,
|
||
|
self = this,
|
||
|
isArray = type === "array";
|
||
|
|
||
|
self.content = contentTmpl;
|
||
|
self.views = isArray ? [] : {};
|
||
|
self.parent = parentView;
|
||
|
self.type = type || "top";
|
||
|
self.data = data;
|
||
|
self.tmpl = template;
|
||
|
// If the data is an array, this is an 'array view' with a views array for each child 'item view'
|
||
|
// If the data is not an array, this is an 'item view' with a views 'hash' object for any child nested views
|
||
|
// ._.useKey is non zero if is not an 'array view' (owning a data array). Use this as next key for adding to child views hash
|
||
|
self_ = self._ = {
|
||
|
key: 0,
|
||
|
useKey: isArray ? 0 : 1,
|
||
|
id: "" + viewId++,
|
||
|
onRender: onRender,
|
||
|
bnds: {}
|
||
|
};
|
||
|
self.linked = !!onRender;
|
||
|
if (parentView) {
|
||
|
views = parentView.views;
|
||
|
parentView_ = parentView._;
|
||
|
if (parentView_.useKey) {
|
||
|
// Parent is an 'item view'. Add this view to its views object
|
||
|
// self._key = is the key in the parent view hash
|
||
|
views[self_.key = "_" + parentView_.useKey++] = self;
|
||
|
self.index = indexStr;
|
||
|
self.getIndex = getNestedIndex;
|
||
|
tag = parentView_.tag;
|
||
|
self_.bnd = isArray && (!tag || !!tag._.bnd && tag); // For array views that are data bound for collection change events, set the
|
||
|
// view._.bnd property to true for top-level link() or data-link="{for}", or to the tag instance for a data-bound tag, e.g. {^{for ...}}
|
||
|
} else if (views.length === (self_.key = self.index = key)) { // Parent is an 'array view'. Add this view to its views array
|
||
|
views.push(self); // Use push when possible (better perf than splice)
|
||
|
} else {
|
||
|
views.splice(key, 0, self);
|
||
|
}
|
||
|
// If no context was passed in, use parent context
|
||
|
// If context was passed in, it should have been merged already with parent context
|
||
|
self.ctx = context || parentView.ctx;
|
||
|
} else {
|
||
|
self.ctx = context;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
View.prototype = {
|
||
|
get: getView,
|
||
|
getIndex: getIndex,
|
||
|
getRsc: getResource,
|
||
|
hlp: getHelper,
|
||
|
_is: "view"
|
||
|
};
|
||
|
|
||
|
//=============
|
||
|
// Registration
|
||
|
//=============
|
||
|
|
||
|
function compileChildResources(parentTmpl) {
|
||
|
var storeName, resources, resourceName, resource, settings, compile, onStore;
|
||
|
for (storeName in jsvStores) {
|
||
|
settings = jsvStores[storeName];
|
||
|
if ((compile = settings.compile) && (resources = parentTmpl[storeName + "s"])) {
|
||
|
for (resourceName in resources) {
|
||
|
// compile child resource declarations (templates, tags, tags["for"] or helpers)
|
||
|
resource = resources[resourceName] = compile(resourceName, resources[resourceName], parentTmpl, 0);
|
||
|
resource._is = storeName; // Only do this for compiled objects (tags, templates...)
|
||
|
if (resource && (onStore = $sub.onStore[storeName])) {
|
||
|
// e.g. JsViews integration
|
||
|
onStore(resourceName, resource, compile);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function compileTag(name, tagDef, parentTmpl) {
|
||
|
var tmpl, baseTag, prop,
|
||
|
compiledDef = new $sub._tg();
|
||
|
|
||
|
function Tag() {
|
||
|
var tag = this;
|
||
|
tag._ = {
|
||
|
inline: true,
|
||
|
unlinked: true
|
||
|
};
|
||
|
|
||
|
tag.tagName = name;
|
||
|
}
|
||
|
|
||
|
if ($isFunction(tagDef)) {
|
||
|
// Simple tag declared as function. No presenter instantation.
|
||
|
tagDef = {
|
||
|
depends: tagDef.depends,
|
||
|
render: tagDef
|
||
|
};
|
||
|
} else if ("" + tagDef === tagDef) {
|
||
|
tagDef = {template: tagDef};
|
||
|
}
|
||
|
if (baseTag = tagDef.baseTag) {
|
||
|
tagDef.flow = !!tagDef.flow; // Set flow property, so defaults to false even if baseTag has flow=true
|
||
|
tagDef.baseTag = baseTag = "" + baseTag === baseTag
|
||
|
? (parentTmpl && parentTmpl.tags[baseTag] || $tags[baseTag])
|
||
|
: baseTag;
|
||
|
|
||
|
compiledDef = $extend(compiledDef, baseTag);
|
||
|
|
||
|
for (prop in tagDef) {
|
||
|
compiledDef[prop] = getMethod(baseTag[prop], tagDef[prop]);
|
||
|
}
|
||
|
} else {
|
||
|
compiledDef = $extend(compiledDef, tagDef);
|
||
|
}
|
||
|
|
||
|
// Tag declared as object, used as the prototype for tag instantiation (control/presenter)
|
||
|
if ((tmpl = compiledDef.template) !== undefined) {
|
||
|
compiledDef.template = "" + tmpl === tmpl ? ($templates[tmpl] || $templates(tmpl)) : tmpl;
|
||
|
}
|
||
|
if (compiledDef.init !== false) {
|
||
|
// Set init: false on tagDef if you want to provide just a render method, or render and template, but no constuctor or prototype.
|
||
|
// so equivalent to setting tag to render function, except you can also provide a template.
|
||
|
(Tag.prototype = compiledDef).constructor = compiledDef._ctr = Tag;
|
||
|
}
|
||
|
|
||
|
if (parentTmpl) {
|
||
|
compiledDef._parentTmpl = parentTmpl;
|
||
|
}
|
||
|
return compiledDef;
|
||
|
}
|
||
|
|
||
|
function baseApply(args) {
|
||
|
// In derived method (or handler declared declaratively as in {{:foo onChange=~fooChanged}} can call base method,
|
||
|
// using this.baseApply(arguments) (Equivalent to this._superApply(arguments) in jQuery UI)
|
||
|
return this.base.apply(this, args);
|
||
|
}
|
||
|
|
||
|
function compileTmpl(name, tmpl, parentTmpl, options) {
|
||
|
// tmpl is either a template object, a selector for a template script block, the name of a compiled template, or a template object
|
||
|
|
||
|
//==== nested functions ====
|
||
|
function lookupTemplate(value) {
|
||
|
// If value is of type string - treat as selector, or name of compiled template
|
||
|
// Return the template object, if already compiled, or the markup string
|
||
|
var currentName;
|
||
|
if (("" + value === value) || value.nodeType > 0 && (elem = value)) {
|
||
|
if (!elem) {
|
||
|
if (value.charAt(0) === "@") {
|
||
|
// tmpl="@file.path"
|
||
|
if (fs) {
|
||
|
// node.js - load template file from the file system
|
||
|
// Consider supporting/using async version with callback: fs.readFile(path, {encoding: "utf8"}, callback);
|
||
|
value = $templates[name = name || (value = fs.realpathSync(value.slice(1)))] // If the template is not named, use "@file.path" as name.
|
||
|
= $templates[name]
|
||
|
|| compileTmpl(name, fs.readFileSync(value, "utf8"), parentTmpl, options);
|
||
|
} else {
|
||
|
// On the client, so look for server-generated script block with id "@file.path"
|
||
|
elem = document.getElementById(value);
|
||
|
}
|
||
|
} else if ($.fn && !rTmplString.test(value)) {
|
||
|
try {
|
||
|
elem = $(document).find(value)[0]; // if jQuery is loaded, test for selector returning elements, and get first element
|
||
|
} catch (e) {}
|
||
|
}
|
||
|
}
|
||
|
if (elem) {
|
||
|
// Generally this is a script element.
|
||
|
// However we allow it to be any element, so you can for example take the content of a div,
|
||
|
// use it as a template, and replace it by the same content rendered against data.
|
||
|
// e.g. for linking the content of a div to a container, and using the initial content as template:
|
||
|
// $.link("#content", model, {tmpl: "#content"});
|
||
|
if (options) {
|
||
|
// We will compile a new template using the markup in the script element
|
||
|
value = elem.innerHTML;
|
||
|
} else {
|
||
|
// We will cache a single copy of the compiled template, and associate it with the name
|
||
|
// (renaming from a previous name if there was one).
|
||
|
if ((currentName = elem.getAttribute(tmplAttr)) && (value = $templates[currentName]) && name !== currentName) {
|
||
|
delete $templates[currentName];
|
||
|
}
|
||
|
name = name || currentName || "_" + autoTmplName++;
|
||
|
if (!currentName) {
|
||
|
// Not already compiled and cached, so compile and cache the name
|
||
|
// Create a name for compiled template if none provided
|
||
|
value = compileTmpl(name, elem.innerHTML, parentTmpl, options);
|
||
|
}
|
||
|
elem.setAttribute(tmplAttr, name);
|
||
|
$templates[value.tmplName = name] = value;
|
||
|
}
|
||
|
elem = undefined;
|
||
|
}
|
||
|
} else if (!value.fn) {
|
||
|
value = undefined;
|
||
|
// If value is not a string. HTML element, or compiled template, return undefined
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
var elem, compiledTmpl,
|
||
|
tmplOrMarkup = tmpl = tmpl || "";
|
||
|
|
||
|
//==== Compile the template ====
|
||
|
if (options === 0) {
|
||
|
options = undefined;
|
||
|
tmplOrMarkup = lookupTemplate(tmplOrMarkup); // Top-level compile so do a template lookup
|
||
|
}
|
||
|
|
||
|
// If options, then this was already compiled from a (script) element template declaration.
|
||
|
// If not, then if tmpl is a template object, use it for options
|
||
|
options = options || (tmpl.markup ? tmpl : {});
|
||
|
options.tmplName = name;
|
||
|
if (parentTmpl) {
|
||
|
options._parentTmpl = parentTmpl;
|
||
|
}
|
||
|
// If tmpl is not a markup string or a selector string, then it must be a template object
|
||
|
// In that case, get it from the markup property of the object
|
||
|
if (!tmplOrMarkup && tmpl.markup && (tmplOrMarkup = lookupTemplate(tmpl.markup))) {
|
||
|
if (tmplOrMarkup.fn) {
|
||
|
// If the string references a compiled template object, need to recompile to merge any modified options
|
||
|
tmplOrMarkup = tmplOrMarkup.markup;
|
||
|
}
|
||
|
}
|
||
|
if (tmplOrMarkup !== undefined) {
|
||
|
if (tmplOrMarkup.fn || tmpl.fn) {
|
||
|
// tmpl is already compiled, so use it
|
||
|
if (tmplOrMarkup.fn) {
|
||
|
compiledTmpl = tmplOrMarkup;
|
||
|
}
|
||
|
} else {
|
||
|
// tmplOrMarkup is a markup string, not a compiled template
|
||
|
// Create template object
|
||
|
tmpl = tmplObject(tmplOrMarkup, options);
|
||
|
// Compile to AST and then to compiled function
|
||
|
tmplFn(tmplOrMarkup.replace(rEscapeQuotes, "\\$&"), tmpl);
|
||
|
}
|
||
|
if (!compiledTmpl) {
|
||
|
compileChildResources(options);
|
||
|
|
||
|
compiledTmpl = $extend(function() {
|
||
|
return tmpl.render.apply(tmpl, arguments);
|
||
|
}, tmpl);
|
||
|
}
|
||
|
if (name && !parentTmpl) {
|
||
|
$render[name] = compiledTmpl;
|
||
|
}
|
||
|
return compiledTmpl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function dataMap(mapDef) {
|
||
|
function Map(source, options) {
|
||
|
this.tgt = mapDef.getTgt(source, options);
|
||
|
}
|
||
|
|
||
|
if ($isFunction(mapDef)) {
|
||
|
// Simple map declared as function
|
||
|
mapDef = {
|
||
|
getTgt: mapDef
|
||
|
};
|
||
|
}
|
||
|
|
||
|
if (mapDef.baseMap) {
|
||
|
mapDef = $extend($extend({}, mapDef.baseMap), mapDef);
|
||
|
}
|
||
|
|
||
|
mapDef.map = function(source, options) {
|
||
|
return new Map(source, options);
|
||
|
};
|
||
|
return mapDef;
|
||
|
}
|
||
|
|
||
|
//==== /end of function compile ====
|
||
|
|
||
|
function tmplObject(markup, options) {
|
||
|
// Template object constructor
|
||
|
var htmlTag,
|
||
|
wrapMap = $viewsSettings.wrapMap || {}, // Only used in JsViews. Otherwise empty: {}
|
||
|
tmpl = $extend(
|
||
|
{
|
||
|
tmpls: [],
|
||
|
links: {}, // Compiled functions for link expressions
|
||
|
bnds: [],
|
||
|
_is: "template",
|
||
|
render: renderContent
|
||
|
},
|
||
|
options
|
||
|
);
|
||
|
|
||
|
tmpl.markup = markup;
|
||
|
if (!options.htmlTag) {
|
||
|
// Set tmpl.tag to the top-level HTML tag used in the template, if any...
|
||
|
htmlTag = rFirstElem.exec(markup);
|
||
|
tmpl.htmlTag = htmlTag ? htmlTag[1].toLowerCase() : "";
|
||
|
}
|
||
|
htmlTag = wrapMap[tmpl.htmlTag];
|
||
|
if (htmlTag && htmlTag !== wrapMap.div) {
|
||
|
// When using JsViews, we trim templates which are inserted into HTML contexts where text nodes are not rendered (i.e. not 'Phrasing Content').
|
||
|
// Currently not trimmed for <li> tag. (Not worth adding perf cost)
|
||
|
tmpl.markup = $.trim(tmpl.markup);
|
||
|
}
|
||
|
|
||
|
return tmpl;
|
||
|
}
|
||
|
|
||
|
function registerStore(storeName, storeSettings) {
|
||
|
|
||
|
function theStore(name, item, parentTmpl) {
|
||
|
// The store is also the function used to add items to the store. e.g. $.templates, or $.views.tags
|
||
|
|
||
|
// For store of name 'thing', Call as:
|
||
|
// $.views.things(items[, parentTmpl]),
|
||
|
// or $.views.things(name, item[, parentTmpl])
|
||
|
|
||
|
var onStore, compile, itemName, thisStore;
|
||
|
|
||
|
if (name && typeof name === OBJECT && !name.nodeType && !name.markup && !name.getTgt) {
|
||
|
// Call to $.views.things(items[, parentTmpl]),
|
||
|
|
||
|
// Adding items to the store
|
||
|
// If name is a hash, then item is parentTmpl. Iterate over hash and call store for key.
|
||
|
for (itemName in name) {
|
||
|
theStore(itemName, name[itemName], item);
|
||
|
}
|
||
|
return $views;
|
||
|
}
|
||
|
// Adding a single unnamed item to the store
|
||
|
if (item === undefined) {
|
||
|
item = name;
|
||
|
name = undefined;
|
||
|
}
|
||
|
if (name && "" + name !== name) { // name must be a string
|
||
|
parentTmpl = item;
|
||
|
item = name;
|
||
|
name = undefined;
|
||
|
}
|
||
|
thisStore = parentTmpl ? parentTmpl[storeNames] = parentTmpl[storeNames] || {} : theStore;
|
||
|
compile = storeSettings.compile;
|
||
|
if (item === null) {
|
||
|
// If item is null, delete this entry
|
||
|
if (name) {
|
||
|
delete thisStore[name];
|
||
|
}
|
||
|
} else {
|
||
|
item = compile ? compile(name, item, parentTmpl, 0) : item;
|
||
|
if (name) {
|
||
|
thisStore[name] = item;
|
||
|
}
|
||
|
}
|
||
|
if (compile && item) {
|
||
|
item._is = storeName; // Only do this for compiled objects (tags, templates...)
|
||
|
}
|
||
|
if (item && (onStore = $sub.onStore[storeName])) {
|
||
|
// e.g. JsViews integration
|
||
|
onStore(name, item, compile);
|
||
|
}
|
||
|
return item;
|
||
|
}
|
||
|
|
||
|
var storeNames = storeName + "s";
|
||
|
|
||
|
$views[storeNames] = theStore;
|
||
|
}
|
||
|
|
||
|
//==============
|
||
|
// renderContent
|
||
|
//==============
|
||
|
|
||
|
function renderContent(data, context, noIteration, parentView, key, onRender) {
|
||
|
var i, l, tag, tmpl, tagCtx, isTopRenderCall, prevData, prevIndex,
|
||
|
view = parentView,
|
||
|
result = "";
|
||
|
|
||
|
if (context === true) {
|
||
|
noIteration = context; // passing boolean as second param - noIteration
|
||
|
context = undefined;
|
||
|
} else if (typeof context !== OBJECT) {
|
||
|
context = undefined; // context must be a boolean (noIteration) or a plain object
|
||
|
}
|
||
|
|
||
|
if (tag = this.tag) {
|
||
|
// This is a call from renderTag or tagCtx.render(...)
|
||
|
tagCtx = this;
|
||
|
tmpl = tag._.tmpl || tagCtx.tmpl;
|
||
|
view = view || tagCtx.view;
|
||
|
if (!arguments.length) {
|
||
|
data = view;
|
||
|
}
|
||
|
} else {
|
||
|
// This is a template.render(...) call
|
||
|
tmpl = this;
|
||
|
}
|
||
|
|
||
|
if (tmpl) {
|
||
|
if (!view && data && data._is === "view") {
|
||
|
view = data; // When passing in a view to render or link (and not passing in a parent view) use the passed-in view as parentView
|
||
|
}
|
||
|
|
||
|
if (view) {
|
||
|
if (data === view) {
|
||
|
// Inherit the data from the parent view.
|
||
|
// This may be the contents of an {{if}} block
|
||
|
data = view.data;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!tmpl.fn) {
|
||
|
// Template has not been compiled yet, so compile it.
|
||
|
tmpl = tag._.tmpl = $templates[tmpl] || $templates(tmpl);
|
||
|
}
|
||
|
|
||
|
isRenderCall = isRenderCall || (isTopRenderCall = !view);
|
||
|
if (!view) {
|
||
|
(context = context || {}).root = data; // Provide ~root as shortcut to top-level data.
|
||
|
}
|
||
|
if (!isRenderCall || tmpl.useViews) {
|
||
|
result = renderWithViews(tmpl, data, context, noIteration, view, key, onRender, tag);
|
||
|
} else {
|
||
|
if (view) { // In a block
|
||
|
prevData = view.data;
|
||
|
prevIndex = view.index;
|
||
|
view.index = indexStr;
|
||
|
} else {
|
||
|
view = topView;
|
||
|
view.data = data;
|
||
|
view.ctx = context;
|
||
|
}
|
||
|
if ($isArray(data) && !noIteration) {
|
||
|
// Create a view for the array, whose child views correspond to each data item. (Note: if key and parentView are passed in
|
||
|
// along with parent view, treat as insert -e.g. from view.addViews - so parentView is already the view item for array)
|
||
|
for (i = 0, l = data.length; i < l; i++) {
|
||
|
view.index = i;
|
||
|
view.data = data[i];
|
||
|
result += tmpl.fn(data[i], view, $views);
|
||
|
}
|
||
|
} else {
|
||
|
result += tmpl.fn(data, view, $views);
|
||
|
}
|
||
|
view.data = prevData;
|
||
|
view.index = prevIndex;
|
||
|
}
|
||
|
if (isTopRenderCall) {
|
||
|
isRenderCall = undefined;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
function renderWithViews(tmpl, data, context, noIteration, view, key, onRender, tag) {
|
||
|
function setItemVar(item) {
|
||
|
// When itemVar is specified, set modified ctx with user-named ~item
|
||
|
newCtx = $extend({}, context);
|
||
|
newCtx[itemVar] = item;
|
||
|
}
|
||
|
|
||
|
// Render template against data as a tree of subviews (nested rendered template instances), or as a string (top-level template).
|
||
|
// If the data is the parent view, treat as noIteration, re-render with the same data context.
|
||
|
var i, l, newView, childView, itemResult, swapContent, contentTmpl, outerOnRender, tmplName, itemVar, newCtx, tagCtx,
|
||
|
result="" ;
|
||
|
|
||
|
if (tag) {
|
||
|
// This is a call from renderTag or tagCtx.render(...)
|
||
|
tmplName = tag.tagName;
|
||
|
tagCtx = tag.tagCtx;
|
||
|
context = context ? extendCtx(context, tag.ctx) : tag.ctx;
|
||
|
contentTmpl = tagCtx.content;
|
||
|
if (tagCtx.props.link === false) {
|
||
|
// link=false setting on block tag
|
||
|
// We will override inherited value of link by the explicit setting link=false taken from props
|
||
|
// The child views of an unlinked view are also unlinked. So setting child back to true will not have any effect.
|
||
|
context = context || {};
|
||
|
context.link = false;
|
||
|
}
|
||
|
if (itemVar = tagCtx.props.itemVar) {
|
||
|
if (itemVar.charAt(0) !== "~") {
|
||
|
syntaxError("Use itemVar='~myItem'");
|
||
|
}
|
||
|
itemVar = itemVar.slice(1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (view) {
|
||
|
contentTmpl = contentTmpl || view.content; // The wrapped content - to be added as #content property on views, below
|
||
|
onRender = onRender || view._.onRender;
|
||
|
context = context || view.ctx;
|
||
|
}
|
||
|
|
||
|
if (key === true) {
|
||
|
swapContent = true;
|
||
|
key = 0;
|
||
|
}
|
||
|
|
||
|
// If link===false, do not call onRender, so no data-linking marker nodes
|
||
|
if (onRender && (context && context.link === false || tag && tag._.noVws)) {
|
||
|
onRender = undefined;
|
||
|
}
|
||
|
outerOnRender = onRender;
|
||
|
if (onRender === true) {
|
||
|
// Used by view.refresh(). Don't create a new wrapper view.
|
||
|
outerOnRender = undefined;
|
||
|
onRender = view._.onRender;
|
||
|
}
|
||
|
// Set additional context on views created here, (as modified context inherited from the parent, and to be inherited by child views)
|
||
|
context = tmpl.helpers
|
||
|
? extendCtx(tmpl.helpers, context)
|
||
|
: context;
|
||
|
|
||
|
newCtx = context;
|
||
|
if ($isArray(data) && !noIteration) {
|
||
|
// Create a view for the array, whose child views correspond to each data item. (Note: if key and view are passed in
|
||
|
// along with parent view, treat as insert -e.g. from view.addViews - so view is already the view item for array)
|
||
|
newView = swapContent
|
||
|
? view
|
||
|
: (key !== undefined && view)
|
||
|
|| new View(context, "array", view, data, tmpl, key, contentTmpl, onRender);
|
||
|
if (itemVar) {
|
||
|
newView.it = itemVar;
|
||
|
}
|
||
|
itemVar = newView.it;
|
||
|
for (i = 0, l = data.length; i < l; i++) {
|
||
|
// Create a view for each data item.
|
||
|
if (itemVar) {
|
||
|
setItemVar(data[i]); // use modified ctx with user-named ~item
|
||
|
}
|
||
|
childView = new View(newCtx, "item", newView, data[i], tmpl, (key || 0) + i, contentTmpl, onRender);
|
||
|
|
||
|
itemResult = tmpl.fn(data[i], childView, $views);
|
||
|
result += newView._.onRender ? newView._.onRender(itemResult, childView) : itemResult;
|
||
|
}
|
||
|
} else {
|
||
|
// Create a view for singleton data object. The type of the view will be the tag name, e.g. "if" or "myTag" except for
|
||
|
// "item", "array" and "data" views. A "data" view is from programmatic render(object) against a 'singleton'.
|
||
|
if (itemVar) {
|
||
|
setItemVar(data);
|
||
|
}
|
||
|
newView = swapContent ? view : new View(newCtx, tmplName || "data", view, data, tmpl, key, contentTmpl, onRender);
|
||
|
if (tag && !tag.flow) {
|
||
|
newView.tag = tag;
|
||
|
}
|
||
|
result += tmpl.fn(data, newView, $views);
|
||
|
}
|
||
|
return outerOnRender ? outerOnRender(result, newView) : result;
|
||
|
}
|
||
|
|
||
|
//===========================
|
||
|
// Build and compile template
|
||
|
//===========================
|
||
|
|
||
|
// Generate a reusable function that will serve to render a template against data
|
||
|
// (Compile AST then build template function)
|
||
|
|
||
|
function error(e, view, fallback) {
|
||
|
var message = $viewsSettings.onError(e, view, fallback);
|
||
|
if ("" + e === e) { // if e is a string, not an Exception, then throw new Exception
|
||
|
throw new $sub.Err(message);
|
||
|
}
|
||
|
return !view.linkCtx && view.linked ? $converters.html(message) : message;
|
||
|
}
|
||
|
|
||
|
function syntaxError(message) {
|
||
|
error("Syntax error\n" + message);
|
||
|
}
|
||
|
|
||
|
function tmplFn(markup, tmpl, isLinkExpr, convertBack, hasElse) {
|
||
|
// Compile markup to AST (abtract syntax tree) then build the template function code from the AST nodes
|
||
|
// Used for compiling templates, and also by JsViews to build functions for data link expressions
|
||
|
|
||
|
//==== nested functions ====
|
||
|
function pushprecedingContent(shift) {
|
||
|
shift -= loc;
|
||
|
if (shift) {
|
||
|
content.push(markup.substr(loc, shift).replace(rNewLine, "\\n"));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function blockTagCheck(tagName, block) {
|
||
|
if (tagName) {
|
||
|
tagName += '}}';
|
||
|
// '{{include}} block has {{/for}} with no open {{for}}'
|
||
|
syntaxError((
|
||
|
block
|
||
|
? '{{' + block + '}} block has {{/' + tagName + ' without {{' + tagName
|
||
|
: 'Unmatched or missing {{/' + tagName) + ', in template:\n' + markup);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function parseTag(all, bind, tagName, converter, colon, html, comment, codeTag, params, slash, closeBlock, index) {
|
||
|
|
||
|
// bind tag converter colon html comment code params slash closeBlock
|
||
|
// /{(\^)?{(?:(?:(\w+(?=[\/\s}]))|(?:(\w+)?(:)|(>)|!--((?:[^-]|-(?!-))*)--|(\*)))\s*((?:[^}]|}(?!}))*?)(\/)?|(?:\/(\w+)))}}/g
|
||
|
// Build abstract syntax tree (AST): [tagName, converter, params, content, hash, bindings, contentMarkup]
|
||
|
if (html) {
|
||
|
colon = ":";
|
||
|
converter = HTML;
|
||
|
}
|
||
|
slash = slash || isLinkExpr && !hasElse;
|
||
|
|
||
|
var pathBindings = (bind || isLinkExpr) && [[]],
|
||
|
props = "",
|
||
|
args = "",
|
||
|
ctxProps = "",
|
||
|
paramsArgs = "",
|
||
|
paramsProps = "",
|
||
|
paramsCtxProps = "",
|
||
|
onError = "",
|
||
|
useTrigger = "",
|
||
|
// Block tag if not self-closing and not {{:}} or {{>}} (special case) and not a data-link expression
|
||
|
block = !slash && !colon && !comment;
|
||
|
|
||
|
//==== nested helper function ====
|
||
|
tagName = tagName || (params = params || "#data", colon); // {{:}} is equivalent to {{:#data}}
|
||
|
pushprecedingContent(index);
|
||
|
loc = index + all.length; // location marker - parsed up to here
|
||
|
if (codeTag) {
|
||
|
if (allowCode) {
|
||
|
content.push(["*", "\n" + params.replace(/^:/, "ret+= ").replace(rUnescapeQuotes, "$1") + ";\n"]);
|
||
|
}
|
||
|
} else if (tagName) {
|
||
|
if (tagName === "else") {
|
||
|
if (rTestElseIf.test(params)) {
|
||
|
syntaxError('for "{{else if expr}}" use "{{else expr}}"');
|
||
|
}
|
||
|
pathBindings = current[7] && [[]];
|
||
|
current[8] = markup.substring(current[8], index); // contentMarkup for block tag
|
||
|
current = stack.pop();
|
||
|
content = current[2];
|
||
|
block = true;
|
||
|
}
|
||
|
if (params) {
|
||
|
// remove newlines from the params string, to avoid compiled code errors for unterminated strings
|
||
|
parseParams(params.replace(rNewLine, " "), pathBindings, tmpl)
|
||
|
.replace(rBuildHash, function(all, onerror, isCtx, key, keyToken, keyValue, arg, param) {
|
||
|
key = "'" + keyToken + "':";
|
||
|
if (arg) {
|
||
|
args += keyValue + ",";
|
||
|
paramsArgs += "'" + param + "',";
|
||
|
} else if (isCtx) {
|
||
|
ctxProps += key + keyValue + ",";
|
||
|
paramsCtxProps += key + "'" + param + "',";
|
||
|
} else if (onerror) {
|
||
|
onError += keyValue;
|
||
|
} else {
|
||
|
if (keyToken === "trigger") {
|
||
|
useTrigger += keyValue;
|
||
|
}
|
||
|
props += key + keyValue + ",";
|
||
|
paramsProps += key + "'" + param + "',";
|
||
|
hasHandlers = hasHandlers || rHasHandlers.test(keyToken);
|
||
|
}
|
||
|
return "";
|
||
|
}).slice(0, -1);
|
||
|
}
|
||
|
|
||
|
if (pathBindings && pathBindings[0]) {
|
||
|
pathBindings.pop(); // Remove the bindings that was prepared for next arg. (There is always an extra one ready).
|
||
|
}
|
||
|
|
||
|
newNode = [
|
||
|
tagName,
|
||
|
converter || !!convertBack || hasHandlers || "",
|
||
|
block && [],
|
||
|
parsedParam(paramsArgs, paramsProps, paramsCtxProps),
|
||
|
parsedParam(args, props, ctxProps),
|
||
|
onError,
|
||
|
useTrigger,
|
||
|
pathBindings || 0
|
||
|
];
|
||
|
content.push(newNode);
|
||
|
if (block) {
|
||
|
stack.push(current);
|
||
|
current = newNode;
|
||
|
current[8] = loc; // Store current location of open tag, to be able to add contentMarkup when we reach closing tag
|
||
|
}
|
||
|
} else if (closeBlock) {
|
||
|
blockTagCheck(closeBlock !== current[0] && current[0] !== "else" && closeBlock, current[0]);
|
||
|
current[8] = markup.substring(current[8], index); // contentMarkup for block tag
|
||
|
current = stack.pop();
|
||
|
}
|
||
|
blockTagCheck(!current && closeBlock);
|
||
|
content = current[2];
|
||
|
}
|
||
|
//==== /end of nested functions ====
|
||
|
|
||
|
var result, newNode, hasHandlers,
|
||
|
allowCode = $viewsSettings.allowCode || tmpl && tmpl.allowCode,
|
||
|
astTop = [],
|
||
|
loc = 0,
|
||
|
stack = [],
|
||
|
content = astTop,
|
||
|
current = [,,astTop];
|
||
|
|
||
|
if (allowCode) {
|
||
|
tmpl.allowCode = allowCode;
|
||
|
}
|
||
|
|
||
|
//TODO result = tmplFnsCache[markup]; // Only cache if template is not named and markup length < ...,
|
||
|
//and there are no bindings or subtemplates?? Consider standard optimization for data-link="a.b.c"
|
||
|
// if (result) {
|
||
|
// tmpl.fn = result;
|
||
|
// } else {
|
||
|
|
||
|
// result = markup;
|
||
|
if (isLinkExpr) {
|
||
|
markup = delimOpenChar0 + markup + delimCloseChar1;
|
||
|
}
|
||
|
|
||
|
blockTagCheck(stack[0] && stack[0][2].pop()[0]);
|
||
|
// Build the AST (abstract syntax tree) under astTop
|
||
|
markup.replace(rTag, parseTag);
|
||
|
|
||
|
pushprecedingContent(markup.length);
|
||
|
|
||
|
if (loc = astTop[astTop.length - 1]) {
|
||
|
blockTagCheck("" + loc !== loc && (+loc[8] === loc[8]) && loc[0]);
|
||
|
}
|
||
|
// result = tmplFnsCache[markup] = buildCode(astTop, tmpl);
|
||
|
// }
|
||
|
|
||
|
if (isLinkExpr) {
|
||
|
result = buildCode(astTop, markup, isLinkExpr);
|
||
|
setPaths(result, [astTop[0][7]]); // With data-link expressions, pathBindings array is astTop[0][7]
|
||
|
} else {
|
||
|
result = buildCode(astTop, tmpl);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
function setPaths(fn, pathsArr) {
|
||
|
var key, paths,
|
||
|
i = 0,
|
||
|
l = pathsArr.length;
|
||
|
fn.deps = [];
|
||
|
for (; i < l; i++) {
|
||
|
paths = pathsArr[i];
|
||
|
for (key in paths) {
|
||
|
if (key !== "_jsvto" && paths[key].length) {
|
||
|
fn.deps = fn.deps.concat(paths[key]); // deps is the concatenation of the paths arrays for the different bindings
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
fn.paths = paths; // The array of paths arrays for the different bindings
|
||
|
}
|
||
|
|
||
|
function parsedParam(args, props, ctx) {
|
||
|
return [args.slice(0, -1), props.slice(0, -1), ctx.slice(0, -1)];
|
||
|
}
|
||
|
|
||
|
function paramStructure(parts, type) {
|
||
|
return '\n\t'
|
||
|
+ (type
|
||
|
? type + ':{'
|
||
|
: '')
|
||
|
+ 'args:[' + parts[0] + ']'
|
||
|
+ (parts[1] || !type
|
||
|
? ',\n\tprops:{' + parts[1] + '}'
|
||
|
: "")
|
||
|
+ (parts[2] ? ',\n\tctx:{' + parts[2] + '}' : "");
|
||
|
}
|
||
|
|
||
|
function parseParams(params, pathBindings, tmpl) {
|
||
|
|
||
|
function parseTokens(all, lftPrn0, lftPrn, bound, path, operator, err, eq, path2, prn, comma, lftPrn2, apos, quot, rtPrn, rtPrnDot, prn2, space, index, full) {
|
||
|
// /(\()(?=\s*\()|(?:([([])\s*)?(?:(\^?)(!*?[#~]?[\w$.^]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*:?\/]|(=))\s*|(!*?[#~]?[\w$.^]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*(([)\]])(?=\s*[.^]|\s*$|\s)|[)\]])([([]?))|(\s+)/g,
|
||
|
// lftPrn0 lftPrn bound path operator err eq path2 prn comma lftPrn2 apos quot rtPrn rtPrnDot prn2 space
|
||
|
// (left paren? followed by (path? followed by operator) or (path followed by paren?)) or comma or apos or quot or right paren or space
|
||
|
bound = bindings && bound;
|
||
|
if (bound && !eq) {
|
||
|
path = bound + path; // e.g. some.fn(...)^some.path - so here path is "^some.path"
|
||
|
}
|
||
|
operator = operator || "";
|
||
|
lftPrn = lftPrn || lftPrn0 || lftPrn2;
|
||
|
path = path || path2;
|
||
|
// Could do this - but not worth perf cost?? :-
|
||
|
// if (!path.lastIndexOf("#data.", 0)) { path = path.slice(6); } // If path starts with "#data.", remove that.
|
||
|
prn = prn || prn2 || "";
|
||
|
|
||
|
var expr, exprFn, binds, theOb, newOb;
|
||
|
|
||
|
function parsePath(allPath, not, object, helper, view, viewProperty, pathTokens, leafToken) {
|
||
|
// rPath = /^(?:null|true|false|\d[\d.]*|(!*?)([\w$]+|\.|~([\w$]+)|#(view|([\w$]+))?)([\w$.^]*?)(?:[.[^]([\w$]+)\]?)?)$/g,
|
||
|
// none object helper view viewProperty pathTokens leafToken
|
||
|
var subPath = object === ".";
|
||
|
if (object) {
|
||
|
path = path.slice(not.length);
|
||
|
if (!subPath) {
|
||
|
allPath = (helper
|
||
|
? 'view.hlp("' + helper + '")'
|
||
|
: view
|
||
|
? "view"
|
||
|
: "data")
|
||
|
+ (leafToken
|
||
|
? (viewProperty
|
||
|
? "." + viewProperty
|
||
|
: helper
|
||
|
? ""
|
||
|
: (view ? "" : "." + object)
|
||
|
) + (pathTokens || "")
|
||
|
: (leafToken = helper ? "" : view ? viewProperty || "" : object, ""));
|
||
|
|
||
|
allPath = allPath + (leafToken ? "." + leafToken : "");
|
||
|
|
||
|
allPath = not + (allPath.slice(0, 9) === "view.data"
|
||
|
? allPath.slice(5) // convert #view.data... to data...
|
||
|
: allPath);
|
||
|
}
|
||
|
if (bindings) {
|
||
|
binds = named === "linkTo" ? (bindto = pathBindings._jsvto = pathBindings._jsvto || []) : bndCtx.bd;
|
||
|
if (theOb = subPath && binds[binds.length-1]) {
|
||
|
if (theOb._jsv) {
|
||
|
while (theOb.sb) {
|
||
|
theOb = theOb.sb;
|
||
|
}
|
||
|
if (theOb.bnd) {
|
||
|
path = "^" + path.slice(1);
|
||
|
}
|
||
|
theOb.sb = path;
|
||
|
theOb.bnd = theOb.bnd || path.charAt(0) === "^";
|
||
|
}
|
||
|
} else {
|
||
|
binds.push(path);
|
||
|
}
|
||
|
pathStart[parenDepth] = index + (subPath ? 1 : 0);
|
||
|
}
|
||
|
}
|
||
|
return allPath;
|
||
|
}
|
||
|
|
||
|
if (err && !aposed && !quoted) {
|
||
|
syntaxError(params);
|
||
|
} else {
|
||
|
if (bindings && rtPrnDot && !aposed && !quoted) {
|
||
|
// This is a binding to a path in which an object is returned by a helper/data function/expression, e.g. foo()^x.y or (a?b:c)^x.y
|
||
|
// We create a compiled function to get the object instance (which will be called when the dependent data of the subexpression changes, to return the new object, and trigger re-binding of the subsequent path)
|
||
|
if (!named || boundName || bindto) {
|
||
|
expr = pathStart[parenDepth - 1];
|
||
|
if (full.length - 1 > index - (expr || 0)) { // We need to compile a subexpression
|
||
|
expr = full.slice(expr, index + all.length);
|
||
|
if (exprFn !== true) { // If not reentrant call during compilation
|
||
|
binds = bindto || bndStack[parenDepth-1].bd;
|
||
|
// Insert exprOb object, to be used during binding to return the computed object
|
||
|
theOb = binds[binds.length-1];
|
||
|
if (theOb && theOb.prm) {
|
||
|
while (theOb.sb && theOb.sb.prm) {
|
||
|
theOb = theOb.sb;
|
||
|
}
|
||
|
newOb = theOb.sb = {path: theOb.sb, bnd: theOb.bnd};
|
||
|
} else {
|
||
|
binds.push(newOb = {path: binds.pop()}); // Insert exprOb object, to be used during binding to return the computed object
|
||
|
} // (e.g. "some.object()" in "some.object().a.b" - to be used as context for binding the following tokens "a.b")
|
||
|
}
|
||
|
rtPrnDot = delimOpenChar1 + ":" + expr // The parameter or function subexpression
|
||
|
+ " onerror=''" // set onerror='' in order to wrap generated code with a try catch - returning '' as object instance if there is an error/missing parent
|
||
|
+ delimCloseChar0;
|
||
|
exprFn = tmplLinks[rtPrnDot];
|
||
|
if (!exprFn) {
|
||
|
tmplLinks[rtPrnDot] = true; // Flag that this exprFn (for rtPrnDot) is being compiled
|
||
|
tmplLinks[rtPrnDot] = exprFn = tmplFn(rtPrnDot, tmpl, true); // Compile the expression (or use cached copy already in tmpl.links)
|
||
|
}
|
||
|
if (exprFn !== true && newOb) {
|
||
|
// If not reentrant call during compilation
|
||
|
newOb._jsv = exprFn;
|
||
|
newOb.prm = bndCtx.bd;
|
||
|
newOb.bnd = newOb.bnd || newOb.path && newOb.path.indexOf("^") >= 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return (aposed
|
||
|
// within single-quoted string
|
||
|
? (aposed = !apos, (aposed ? all : lftPrn2 + '"'))
|
||
|
: quoted
|
||
|
// within double-quoted string
|
||
|
? (quoted = !quot, (quoted ? all : lftPrn2 + '"'))
|
||
|
:
|
||
|
(
|
||
|
(lftPrn
|
||
|
? (pathStart[parenDepth] = index++, bndCtx = bndStack[++parenDepth] = {bd: []}, lftPrn)
|
||
|
: "")
|
||
|
+ (space
|
||
|
? (parenDepth
|
||
|
? ""
|
||
|
// New arg or prop - so insert backspace \b (\x08) as separator for named params, used subsequently by rBuildHash, and prepare new bindings array
|
||
|
: (paramIndex = full.slice(paramIndex, index), named
|
||
|
? (named = boundName = bindto = false, "\b")
|
||
|
: "\b,") + paramIndex + (paramIndex = index + all.length, bindings && pathBindings.push(bndCtx.bd = []), "\b")
|
||
|
)
|
||
|
: eq
|
||
|
// named param. Remove bindings for arg and create instead bindings array for prop
|
||
|
? (parenDepth && syntaxError(params), bindings && pathBindings.pop(), named = path, boundName = bound, paramIndex = index + all.length, bound && (bindings = bndCtx.bd = pathBindings[named] = []), path + ':')
|
||
|
: path
|
||
|
// path
|
||
|
? (path.split("^").join(".").replace(rPath, parsePath)
|
||
|
+ (prn
|
||
|
// some.fncall(
|
||
|
? (bndCtx = bndStack[++parenDepth] = {bd: []}, fnCall[parenDepth] = true, prn)
|
||
|
: operator)
|
||
|
)
|
||
|
: operator
|
||
|
// operator
|
||
|
? operator
|
||
|
: rtPrn
|
||
|
// function
|
||
|
? ((fnCall[parenDepth] = false, bndCtx = bndStack[--parenDepth], rtPrn)
|
||
|
+ (prn // rtPrn and prn, e.g )( in (a)() or a()(), or )[ in a()[]
|
||
|
? (bndCtx = bndStack[++parenDepth], fnCall[parenDepth] = true, prn)
|
||
|
: "")
|
||
|
)
|
||
|
: comma
|
||
|
? (fnCall[parenDepth] || syntaxError(params), ",") // We don't allow top-level literal arrays or objects
|
||
|
: lftPrn0
|
||
|
? ""
|
||
|
: (aposed = apos, quoted = quot, '"')
|
||
|
))
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var named, bindto, boundName,
|
||
|
quoted, // boolean for string content in double quotes
|
||
|
aposed, // or in single quotes
|
||
|
bindings = pathBindings && pathBindings[0], // bindings array for the first arg
|
||
|
bndCtx = {bd: bindings},
|
||
|
bndStack = {0: bndCtx},
|
||
|
paramIndex = 0, // list,
|
||
|
tmplLinks = tmpl ? tmpl.links : bindings && (bindings.links = bindings.links || {}),
|
||
|
// The following are used for tracking path parsing including nested paths, such as "a.b(c^d + (e))^f", and chained computed paths such as
|
||
|
// "a.b().c^d().e.f().g" - which has four chained paths, "a.b()", "^c.d()", ".e.f()" and ".g"
|
||
|
parenDepth = 0,
|
||
|
fnCall = {}, // We are in a function call
|
||
|
pathStart = {}; // tracks the start of the current path such as c^d() in the above example
|
||
|
|
||
|
return (params + (tmpl ? " " : ""))
|
||
|
.replace(rParams, parseTokens);
|
||
|
}
|
||
|
|
||
|
function buildCode(ast, tmpl, isLinkExpr) {
|
||
|
// Build the template function code from the AST nodes, and set as property on the passed-in template object
|
||
|
// Used for compiling templates, and also by JsViews to build functions for data link expressions
|
||
|
var i, node, tagName, converter, tagCtx, hasTag, hasEncoder, getsVal, hasCnvt, useCnvt, tmplBindings, pathBindings, params, boundOnErrStart, boundOnErrEnd,
|
||
|
tagRender, nestedTmpls, tmplName, nestedTmpl, tagAndElses, content, markup, nextIsElse, oldCode, isElse, isGetVal, tagCtxFn, onError, tagStart, trigger,
|
||
|
tmplBindingKey = 0,
|
||
|
useViews = tmpl.useViews || tmpl.tags || tmpl.templates || tmpl.helpers || tmpl.converters,
|
||
|
code = "",
|
||
|
tmplOptions = {},
|
||
|
l = ast.length;
|
||
|
|
||
|
if ("" + tmpl === tmpl) {
|
||
|
tmplName = isLinkExpr ? 'data-link="' + tmpl.replace(rNewLine, " ").slice(1, -1) + '"' : tmpl;
|
||
|
tmpl = 0;
|
||
|
} else {
|
||
|
tmplName = tmpl.tmplName || "unnamed";
|
||
|
if (tmpl.allowCode) {
|
||
|
tmplOptions.allowCode = true;
|
||
|
}
|
||
|
if (tmpl.debug) {
|
||
|
tmplOptions.debug = true;
|
||
|
}
|
||
|
tmplBindings = tmpl.bnds;
|
||
|
nestedTmpls = tmpl.tmpls;
|
||
|
}
|
||
|
for (i = 0; i < l; i++) {
|
||
|
// AST nodes: [0: tagName, 1: converter, 2: content, 3: params, 4: code, 5: onError, 6: trigger, 7:pathBindings, 8: contentMarkup]
|
||
|
node = ast[i];
|
||
|
|
||
|
// Add newline for each callout to t() c() etc. and each markup string
|
||
|
if ("" + node === node) {
|
||
|
// a markup string to be inserted
|
||
|
code += '\n+"' + node + '"';
|
||
|
} else {
|
||
|
// a compiled tag expression to be inserted
|
||
|
tagName = node[0];
|
||
|
if (tagName === "*") {
|
||
|
// Code tag: {{* }}
|
||
|
code += ";\n" + node[1] + "\nret=ret";
|
||
|
} else {
|
||
|
converter = node[1];
|
||
|
content = !isLinkExpr && node[2];
|
||
|
tagCtx = paramStructure(node[3], 'params') + '},' + paramStructure(params = node[4]);
|
||
|
onError = node[5];
|
||
|
trigger = node[6];
|
||
|
markup = node[8] && node[8].replace(rUnescapeQuotes, "$1");
|
||
|
if (isElse = tagName === "else") {
|
||
|
if (pathBindings) {
|
||
|
pathBindings.push(node[7]);
|
||
|
}
|
||
|
} else {
|
||
|
tmplBindingKey = 0;
|
||
|
if (tmplBindings && (pathBindings = node[7])) { // Array of paths, or false if not data-bound
|
||
|
pathBindings = [pathBindings];
|
||
|
tmplBindingKey = tmplBindings.push(1); // Add placeholder in tmplBindings for compiled function
|
||
|
}
|
||
|
}
|
||
|
useViews = useViews || params[1] || params[2] || pathBindings || /view.(?!index)/.test(params[0]);
|
||
|
// useViews is for perf optimization. For render() we only use views if necessary - for the more advanced scenarios.
|
||
|
// We use views if there are props, contextual properties or args with #... (other than #index) - but you can force
|
||
|
// using the full view infrastructure, (and pay a perf price) by opting in: Set useViews: true on the template, manually...
|
||
|
if (isGetVal = tagName === ":") {
|
||
|
if (converter) {
|
||
|
tagName = converter === HTML ? ">" : converter + tagName;
|
||
|
}
|
||
|
} else {
|
||
|
if (content) { // TODO optimize - if content.length === 0 or if there is a tmpl="..." specified - set content to null / don't run this compilation code - since content won't get used!!
|
||
|
// Create template object for nested template
|
||
|
nestedTmpl = tmplObject(markup, tmplOptions);
|
||
|
nestedTmpl.tmplName = tmplName + "/" + tagName;
|
||
|
// Compile to AST and then to compiled function
|
||
|
nestedTmpl.useViews = nestedTmpl.useViews || useViews;
|
||
|
buildCode(content, nestedTmpl);
|
||
|
useViews = nestedTmpl.useViews;
|
||
|
nestedTmpls.push(nestedTmpl);
|
||
|
}
|
||
|
|
||
|
if (!isElse) {
|
||
|
// This is not an else tag.
|
||
|
tagAndElses = tagName;
|
||
|
useViews = useViews || tagName && (!$tags[tagName] || !$tags[tagName].flow);
|
||
|
// Switch to a new code string for this bound tag (and its elses, if it has any) - for returning the tagCtxs array
|
||
|
oldCode = code;
|
||
|
code = "";
|
||
|
}
|
||
|
nextIsElse = ast[i + 1];
|
||
|
nextIsElse = nextIsElse && nextIsElse[0] === "else";
|
||
|
}
|
||
|
tagStart = onError ? ";\ntry{\nret+=" : "\n+";
|
||
|
boundOnErrStart = "";
|
||
|
boundOnErrEnd = "";
|
||
|
|
||
|
if (isGetVal && (pathBindings || trigger || converter && converter !== HTML)) {
|
||
|
// For convertVal we need a compiled function to return the new tagCtx(s)
|
||
|
tagCtxFn = "return {" + tagCtx + "};";
|
||
|
tagRender = 'c("' + converter + '",view,';
|
||
|
tagCtxFn = new Function("data,view,j,u", " // " + tmplName + " " + tmplBindingKey + " " + tagName
|
||
|
+ "\n" + tagCtxFn);
|
||
|
tagCtxFn._er = onError;
|
||
|
|
||
|
boundOnErrStart = tagRender + tmplBindingKey + ",";
|
||
|
boundOnErrEnd = ")";
|
||
|
|
||
|
tagCtxFn._tag = tagName;
|
||
|
if (isLinkExpr) {
|
||
|
return tagCtxFn;
|
||
|
}
|
||
|
setPaths(tagCtxFn, pathBindings);
|
||
|
useCnvt = true;
|
||
|
}
|
||
|
code += (isGetVal
|
||
|
? (isLinkExpr ? (onError ? "\ntry{\n" : "") + "return " : tagStart) + (useCnvt // Call _cnvt if there is a converter: {{cnvt: ... }} or {^{cnvt: ... }}
|
||
|
? (useCnvt = undefined, useViews = hasCnvt = true, tagRender + (pathBindings
|
||
|
? ((tmplBindings[tmplBindingKey - 1] = tagCtxFn), tmplBindingKey) // Store the compiled tagCtxFn in tmpl.bnds, and pass the key to convertVal()
|
||
|
: "{" + tagCtx + "}") + ")")
|
||
|
: tagName === ">"
|
||
|
? (hasEncoder = true, "h(" + params[0] + ')')
|
||
|
: (getsVal = true, "((v=" + (params[0] || 'data') + ')!=null?v:"")') // Strict equality just for data-link="title{:expr}" so expr=null will remove title attribute
|
||
|
)
|
||
|
: (hasTag = true, "\n{view:view,tmpl:" // Add this tagCtx to the compiled code for the tagCtxs to be passed to renderTag()
|
||
|
+ (content ? nestedTmpls.length : "0") + "," // For block tags, pass in the key (nestedTmpls.length) to the nested content template
|
||
|
+ tagCtx + "},"));
|
||
|
|
||
|
if (tagAndElses && !nextIsElse) {
|
||
|
// This is a data-link expression or an inline tag without any elses, or the last {{else}} of an inline tag
|
||
|
// We complete the code for returning the tagCtxs array
|
||
|
code = "[" + code.slice(0, -1) + "]";
|
||
|
tagRender = 't("' + tagAndElses + '",view,this,';
|
||
|
if (isLinkExpr || pathBindings) {
|
||
|
// This is a bound tag (data-link expression or inline bound tag {^{tag ...}}) so we store a compiled tagCtxs function in tmp.bnds
|
||
|
code = new Function("data,view,j,u", " // " + tmplName + " " + tmplBindingKey + " " + tagAndElses + "\nreturn " + code + ";");
|
||
|
code._er = onError;
|
||
|
code._tag = tagAndElses;
|
||
|
if (pathBindings) {
|
||
|
setPaths(tmplBindings[tmplBindingKey - 1] = code, pathBindings);
|
||
|
}
|
||
|
if (isLinkExpr) {
|
||
|
return code; // For a data-link expression we return the compiled tagCtxs function
|
||
|
}
|
||
|
boundOnErrStart = tagRender + tmplBindingKey + ",undefined,";
|
||
|
boundOnErrEnd = ")";
|
||
|
}
|
||
|
|
||
|
// This is the last {{else}} for an inline tag.
|
||
|
// For a bound tag, pass the tagCtxs fn lookup key to renderTag.
|
||
|
// For an unbound tag, include the code directly for evaluating tagCtxs array
|
||
|
code = oldCode + tagStart + tagRender + (tmplBindingKey || code) + ")";
|
||
|
pathBindings = 0;
|
||
|
tagAndElses = 0;
|
||
|
}
|
||
|
if (onError) {
|
||
|
useViews = true;
|
||
|
code += ';\n}catch(e){ret' + (isLinkExpr ? "urn " : "+=") + boundOnErrStart + 'j._err(e,view,' + onError + ')' + boundOnErrEnd + ';}\n' + (isLinkExpr ? "" : 'ret=ret');
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// Include only the var references that are needed in the code
|
||
|
code = "// " + tmplName
|
||
|
|
||
|
+ "\nvar v"
|
||
|
+ (hasTag ? ",t=j._tag" : "") // has tag
|
||
|
+ (hasCnvt ? ",c=j._cnvt" : "") // converter
|
||
|
+ (hasEncoder ? ",h=j.converters.html" : "") // html converter
|
||
|
+ (isLinkExpr ? ";\n" : ',ret=""\n')
|
||
|
+ (tmplOptions.debug ? "debugger;" : "")
|
||
|
+ code
|
||
|
+ (isLinkExpr ? "\n" : ";\nreturn ret;");
|
||
|
|
||
|
if ($viewsSettings._dbgMode) {
|
||
|
code = "try {\n" + code + "\n}catch(e){\nreturn j._err(e, view);\n}";
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
code = new Function("data,view,j,u", code);
|
||
|
} catch (e) {
|
||
|
syntaxError("Compiled template code:\n\n" + code + '\n: "' + e.message + '"');
|
||
|
}
|
||
|
if (tmpl) {
|
||
|
tmpl.fn = code;
|
||
|
tmpl.useViews = !!useViews;
|
||
|
}
|
||
|
return code;
|
||
|
}
|
||
|
|
||
|
//==========
|
||
|
// Utilities
|
||
|
//==========
|
||
|
|
||
|
// Merge objects, in particular contexts which inherit from parent contexts
|
||
|
function extendCtx(context, parentContext) {
|
||
|
// Return copy of parentContext, unless context is defined and is different, in which case return a new merged context
|
||
|
// If neither context nor parentContext are defined, return undefined
|
||
|
return context && context !== parentContext
|
||
|
? (parentContext
|
||
|
? $extend($extend({}, parentContext), context)
|
||
|
: context)
|
||
|
: parentContext && $extend({}, parentContext);
|
||
|
}
|
||
|
|
||
|
// Get character entity for HTML and Attribute encoding
|
||
|
function getCharEntity(ch) {
|
||
|
return charEntities[ch] || (charEntities[ch] = "&#" + ch.charCodeAt(0) + ";");
|
||
|
}
|
||
|
|
||
|
//========================== Initialize ==========================
|
||
|
|
||
|
for (jsvStoreName in jsvStores) {
|
||
|
registerStore(jsvStoreName, jsvStores[jsvStoreName]);
|
||
|
}
|
||
|
|
||
|
var $isFunction, $isArray,
|
||
|
$templates = $views.templates,
|
||
|
$converters = $views.converters,
|
||
|
$helpers = $views.helpers,
|
||
|
$tags = $views.tags,
|
||
|
$sub = $views.sub,
|
||
|
$viewsSettings = $views.settings;
|
||
|
|
||
|
$sub._tg.prototype = {
|
||
|
baseApply: baseApply,
|
||
|
cvtArgs: convertArgs
|
||
|
};
|
||
|
|
||
|
topView = $sub.topView = new View();
|
||
|
|
||
|
if ($) {
|
||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
// jQuery (= $) is loaded
|
||
|
$.fn.render = function(data, context, noIteration) {
|
||
|
var tmplElem = this.jquery && (this[0] || error('Unknown template: "' + this.selector + '"')),
|
||
|
tmpl = tmplElem.getAttribute(tmplAttr);
|
||
|
|
||
|
return renderContent.call(tmpl ? $templates[tmpl] : $templates(tmplElem), data, context, noIteration);
|
||
|
};
|
||
|
|
||
|
if ($.observable) {
|
||
|
$extend($sub, $.views.sub); // jquery.observable.js was loaded before jsrender.js
|
||
|
$views.map = $.views.map;
|
||
|
}
|
||
|
} else {
|
||
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
// jQuery is not loaded.
|
||
|
|
||
|
$ = {};
|
||
|
|
||
|
if (!isCommonJS) {
|
||
|
// We are loading jsrender.js from a script element, not AMD or CommonJS, so set global
|
||
|
global.jsviews = $;
|
||
|
}
|
||
|
|
||
|
$.isFunction = function(ob) {
|
||
|
return typeof ob === "function";
|
||
|
};
|
||
|
|
||
|
$.isArray = Array.isArray || function(obj) {
|
||
|
return $.toString.call(obj) === "[object Array]";
|
||
|
};
|
||
|
|
||
|
$.noConflict = function() {
|
||
|
if (global.jsviews === $) {
|
||
|
global.jsviews = jsViewsPrev;
|
||
|
}
|
||
|
return $;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
$isFunction = $.isFunction;
|
||
|
$isArray = $.isArray;
|
||
|
$.render = $render;
|
||
|
$.views = $views;
|
||
|
$.templates = $templates = $views.templates;
|
||
|
|
||
|
$views.compile = function(markup, options) {
|
||
|
options = options || {};
|
||
|
options.markup = markup;
|
||
|
return $templates(options);
|
||
|
};
|
||
|
|
||
|
$viewsSettings({
|
||
|
debugMode: dbgMode,
|
||
|
delimiters: $viewsDelimiters,
|
||
|
onError: function(e, view, fallback) {
|
||
|
// Can override using $.views.settings({onError: function(...) {...}});
|
||
|
if (view) {
|
||
|
// For render errors, e is an exception thrown in compiled template, and view is the current view. For other errors, e is an error string.
|
||
|
e = fallback === undefined
|
||
|
? "{Error: " + (e.message || e) + "}"
|
||
|
: $isFunction(fallback)
|
||
|
? fallback(e, view) : fallback;
|
||
|
}
|
||
|
return e == undefined ? "" : e;
|
||
|
},
|
||
|
_dbgMode: false
|
||
|
});
|
||
|
|
||
|
//========================== Register tags ==========================
|
||
|
|
||
|
$tags({
|
||
|
"if": {
|
||
|
render: function(val) {
|
||
|
// This function is called once for {{if}} and once for each {{else}}.
|
||
|
// We will use the tag.rendering object for carrying rendering state across the calls.
|
||
|
// If not done (a previous block has not been rendered), look at expression for this block and render the block if expression is truthy
|
||
|
// Otherwise return ""
|
||
|
var self = this,
|
||
|
tagCtx = self.tagCtx,
|
||
|
ret = (self.rendering.done || !val && (arguments.length || !tagCtx.index))
|
||
|
? ""
|
||
|
: (self.rendering.done = true, self.selected = tagCtx.index,
|
||
|
// Test is satisfied, so render content on current context. We call tagCtx.render() rather than return undefined
|
||
|
// (which would also render the tmpl/content on the current context but would iterate if it is an array)
|
||
|
tagCtx.render(tagCtx.view, true)); // no arg, so renders against parentView.data
|
||
|
return ret;
|
||
|
},
|
||
|
flow: true
|
||
|
},
|
||
|
"for": {
|
||
|
render: function (val) {
|
||
|
// This function is called once for {{for}} and once for each {{else}}.
|
||
|
// We will use the tag.rendering object for carrying rendering state across the calls.
|
||
|
var finalElse = !arguments.length,
|
||
|
value,
|
||
|
self = this,
|
||
|
tagCtx = self.tagCtx,
|
||
|
result = "",
|
||
|
done = 0;
|
||
|
|
||
|
if (!self.rendering.done) {
|
||
|
value = finalElse ? tagCtx.view.data : val; // For the final else, defaults to current data without iteration.
|
||
|
if (value !== undefined ) {
|
||
|
result += tagCtx.render(value, finalElse); // Iterates except on final else, if data is an array. (Use {{include}} to compose templates without array iteration)
|
||
|
done += $isArray(value) ? value.length : 1;
|
||
|
}
|
||
|
if (self.rendering.done = done) {
|
||
|
self.selected = tagCtx.index;
|
||
|
}
|
||
|
// If nothing was rendered we will look at the next {{else}}. Otherwise, we are done.
|
||
|
}
|
||
|
return result;
|
||
|
},
|
||
|
flow: true
|
||
|
},
|
||
|
props: {
|
||
|
baseTag: "for",
|
||
|
dataMap: dataMap(getTargetProps),
|
||
|
flow: true
|
||
|
},
|
||
|
include: {
|
||
|
flow: true
|
||
|
},
|
||
|
"*": {
|
||
|
// {{* code... }} - Ignored if template.allowCode and $.views.settings.allowCode are false. Otherwise include code in compiled template
|
||
|
render: retVal,
|
||
|
flow: true
|
||
|
},
|
||
|
":*": {
|
||
|
// {{:* returnedExpression }} - Ignored if template.allowCode and $.views.settings.allowCode are false. Otherwise include code in compiled template
|
||
|
render: retVal,
|
||
|
flow: true
|
||
|
},
|
||
|
dbg: $helpers.dbg = $converters.dbg = dbgBreak // Register {{dbg/}}, {{dbg:...}} and ~dbg() to throw and catch, as breakpoints for debugging.
|
||
|
});
|
||
|
|
||
|
function getTargetProps(source) {
|
||
|
// this pointer is theMap - which has tagCtx.props too
|
||
|
// arguments: tagCtx.args.
|
||
|
var key, prop,
|
||
|
props = [];
|
||
|
|
||
|
if (typeof source === OBJECT) {
|
||
|
for (key in source) {
|
||
|
prop = source[key];
|
||
|
if (!prop || !prop.toJSON || prop.toJSON()) {
|
||
|
if (!$isFunction(prop)) {
|
||
|
props.push({ key: key, prop: prop });
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return props;
|
||
|
}
|
||
|
|
||
|
//========================== Register converters ==========================
|
||
|
|
||
|
function htmlEncode(text) {
|
||
|
// HTML encode: Replace < > & ' and " by corresponding entities.
|
||
|
return text != undefined ? rIsHtml.test(text) && ("" + text).replace(rHtmlEncode, getCharEntity) || text : "";
|
||
|
}
|
||
|
|
||
|
$converters({
|
||
|
html: htmlEncode,
|
||
|
attr: htmlEncode, // Includes > encoding since rConvertMarkers in JsViews does not skip > characters in attribute strings
|
||
|
url: function(text) {
|
||
|
// URL encoding helper.
|
||
|
return text != undefined ? encodeURI("" + text) : text === null ? text : ""; // null returns null, e.g. to remove attribute. undefined returns ""
|
||
|
}
|
||
|
});
|
||
|
|
||
|
//========================== Define default delimiters ==========================
|
||
|
$viewsDelimiters();
|
||
|
|
||
|
return $views;
|
||
|
}));
|