2016-07-01 05:10:08 +08:00
|
|
|
// This is a mechanism for quickly rendering templates which is Ember aware
|
|
|
|
// templates are highly compatible with Ember so you don't need to worry about calling "get"
|
|
|
|
// and computed properties function, additionally it uses stringParams like Ember does
|
|
|
|
|
|
|
|
// compat with ie8 in case this gets picked up elsewhere
|
2018-06-15 23:03:24 +08:00
|
|
|
const objectCreate =
|
|
|
|
Object.create ||
|
|
|
|
function(parent) {
|
|
|
|
function F() {}
|
|
|
|
F.prototype = parent;
|
|
|
|
return new F();
|
|
|
|
};
|
2016-07-01 05:10:08 +08:00
|
|
|
|
|
|
|
const RawHandlebars = Handlebars.create();
|
|
|
|
|
|
|
|
RawHandlebars.helper = function() {};
|
|
|
|
RawHandlebars.helpers = objectCreate(Handlebars.helpers);
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.helpers["get"] = function(context, options) {
|
|
|
|
var firstContext = options.contexts[0];
|
2016-07-01 05:10:08 +08:00
|
|
|
var val = firstContext[context];
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (context.indexOf("controller.") === 0) {
|
|
|
|
context = context.slice(context.indexOf(".") + 1);
|
2016-12-22 05:38:57 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 00:54:23 +08:00
|
|
|
return val === undefined ? Ember.get(firstContext, context) : val;
|
2016-07-01 05:10:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// adds compatability so this works with stringParams
|
|
|
|
function stringCompatHelper(fn) {
|
|
|
|
const old = RawHandlebars.helpers[fn];
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.helpers[fn] = function(context, options) {
|
2016-07-01 05:10:08 +08:00
|
|
|
return old.apply(this, [
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.helpers.get(context, options),
|
|
|
|
options
|
2016-07-01 05:10:08 +08:00
|
|
|
]);
|
|
|
|
};
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2016-07-01 05:10:08 +08:00
|
|
|
|
|
|
|
// #each .. in support (as format is transformed to this)
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.registerHelper("each", function(
|
|
|
|
localName,
|
|
|
|
inKeyword,
|
|
|
|
contextName,
|
|
|
|
options
|
|
|
|
) {
|
2019-01-12 00:54:23 +08:00
|
|
|
var list = Ember.get(this, contextName);
|
2016-07-01 05:10:08 +08:00
|
|
|
var output = [];
|
2016-07-14 03:36:34 +08:00
|
|
|
var innerContext = objectCreate(this);
|
2018-06-15 23:03:24 +08:00
|
|
|
for (var i = 0; i < list.length; i++) {
|
2016-07-01 05:10:08 +08:00
|
|
|
innerContext[localName] = list[i];
|
|
|
|
output.push(options.fn(innerContext));
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
return output.join("");
|
2016-07-01 05:10:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
stringCompatHelper("if");
|
|
|
|
stringCompatHelper("unless");
|
|
|
|
stringCompatHelper("with");
|
|
|
|
|
2016-07-14 03:36:34 +08:00
|
|
|
function buildPath(blk, args) {
|
2018-06-15 23:03:24 +08:00
|
|
|
var result = {
|
|
|
|
type: "PathExpression",
|
2016-07-14 03:36:34 +08:00
|
|
|
data: false,
|
|
|
|
depth: blk.path.depth,
|
2018-06-15 23:03:24 +08:00
|
|
|
loc: blk.path.loc
|
|
|
|
};
|
2016-07-14 03:36:34 +08:00
|
|
|
|
|
|
|
// Server side precompile doesn't have jquery.extend
|
2018-06-15 23:03:24 +08:00
|
|
|
Object.keys(args).forEach(function(a) {
|
2016-07-14 03:36:34 +08:00
|
|
|
result[a] = args[a];
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceGet(ast) {
|
|
|
|
var visitor = new Handlebars.Visitor();
|
|
|
|
visitor.mutating = true;
|
|
|
|
|
|
|
|
visitor.MustacheStatement = function(mustache) {
|
|
|
|
if (!(mustache.params.length || mustache.hash)) {
|
|
|
|
mustache.params[0] = mustache.path;
|
2018-06-15 23:03:24 +08:00
|
|
|
mustache.path = buildPath(mustache, {
|
|
|
|
parts: ["get"],
|
|
|
|
original: "get",
|
|
|
|
strict: true,
|
|
|
|
falsy: true
|
|
|
|
});
|
2016-07-14 03:36:34 +08:00
|
|
|
}
|
|
|
|
return Handlebars.Visitor.prototype.MustacheStatement.call(this, mustache);
|
|
|
|
};
|
|
|
|
|
|
|
|
// rewrite `each x as |y|` as each y in x`
|
|
|
|
// This allows us to use the same syntax in all templates
|
|
|
|
visitor.BlockStatement = function(block) {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (block.path.original === "each" && block.params.length === 1) {
|
2016-07-14 03:36:34 +08:00
|
|
|
var paramName = block.program.blockParams[0];
|
2018-06-15 23:03:24 +08:00
|
|
|
block.params = [
|
|
|
|
buildPath(block, { original: paramName }),
|
|
|
|
{ type: "CommentStatement", value: "in" },
|
|
|
|
block.params[0]
|
|
|
|
];
|
2016-07-14 03:36:34 +08:00
|
|
|
delete block.program.blockParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Handlebars.Visitor.prototype.BlockStatement.call(this, block);
|
|
|
|
};
|
|
|
|
|
|
|
|
visitor.accept(ast);
|
|
|
|
}
|
|
|
|
|
2016-07-01 05:10:08 +08:00
|
|
|
if (Handlebars.Compiler) {
|
|
|
|
RawHandlebars.Compiler = function() {};
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.Compiler.prototype = objectCreate(
|
|
|
|
Handlebars.Compiler.prototype
|
|
|
|
);
|
2016-07-01 05:10:08 +08:00
|
|
|
RawHandlebars.Compiler.prototype.compiler = RawHandlebars.Compiler;
|
|
|
|
|
|
|
|
RawHandlebars.JavaScriptCompiler = function() {};
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
RawHandlebars.JavaScriptCompiler.prototype = objectCreate(
|
|
|
|
Handlebars.JavaScriptCompiler.prototype
|
|
|
|
);
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.compiler =
|
|
|
|
RawHandlebars.JavaScriptCompiler;
|
2016-07-01 05:10:08 +08:00
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.namespace = "RawHandlebars";
|
|
|
|
|
|
|
|
RawHandlebars.precompile = function(value, asObject) {
|
|
|
|
var ast = Handlebars.parse(value);
|
|
|
|
replaceGet(ast);
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
knownHelpers: {
|
|
|
|
get: true
|
|
|
|
},
|
|
|
|
data: true,
|
|
|
|
stringParams: true
|
|
|
|
};
|
|
|
|
|
|
|
|
asObject = asObject === undefined ? true : asObject;
|
|
|
|
|
|
|
|
var environment = new RawHandlebars.Compiler().compile(ast, options);
|
2018-06-15 23:03:24 +08:00
|
|
|
return new RawHandlebars.JavaScriptCompiler().compile(
|
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
asObject
|
|
|
|
);
|
2016-07-01 05:10:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
RawHandlebars.compile = function(string) {
|
|
|
|
var ast = Handlebars.parse(string);
|
|
|
|
replaceGet(ast);
|
|
|
|
|
|
|
|
// this forces us to rewrite helpers
|
2018-06-15 23:03:24 +08:00
|
|
|
var options = { data: true, stringParams: true };
|
2016-07-01 05:10:08 +08:00
|
|
|
var environment = new RawHandlebars.Compiler().compile(ast, options);
|
2018-06-15 23:03:24 +08:00
|
|
|
var templateSpec = new RawHandlebars.JavaScriptCompiler().compile(
|
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
true
|
|
|
|
);
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
var t = RawHandlebars.template(templateSpec);
|
|
|
|
t.isMethod = false;
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
return t;
|
2016-07-01 05:10:08 +08:00
|
|
|
};
|
2016-07-05 02:15:51 +08:00
|
|
|
}
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
RawHandlebars.get = function(ctx, property, options) {
|
|
|
|
if (options.types && options.data.view) {
|
|
|
|
var view = options.data.view;
|
2018-06-15 23:03:24 +08:00
|
|
|
return view.getStream
|
|
|
|
? view.getStream(property).value()
|
|
|
|
: view.getAttr(property);
|
2016-07-05 02:15:51 +08:00
|
|
|
} else {
|
|
|
|
return Ember.get(ctx, property);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function template() {
|
|
|
|
return RawHandlebars.template.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function precompile() {
|
|
|
|
return RawHandlebars.precompile.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function compile() {
|
|
|
|
return RawHandlebars.compile.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function get() {
|
|
|
|
return RawHandlebars.get.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default RawHandlebars;
|