}
var debug = mkws.debug_function; // local alias
+
+Handlebars.registerHelper('link', function(a) {
+ var result = "";
+ for (var i in a) {
+ if (i > 0) result += "<br/>";
+ var text = Handlebars.Utils.escapeExpression(a[i]);
+ result += '<a href="' + text + '">' + text + '</a>';
+ }
+
+ return new Handlebars.SafeString(result);
+});
+
+
+Handlebars.registerHelper('json', function(obj) {
+ return JSON.stringify(obj);
+});
+
+
+// We need {{attr '@name'}} because Handlebars can't parse {{@name}}
+Handlebars.registerHelper('attr', function(attrName) {
+ return this[attrName];
+});
+
+
+/*
+ * Use as follows: {{#if-any NAME1 having="NAME2"}}
+ * Applicable when NAME1 is the name of an array
+ * The guarded code runs only if at least one element of the NAME1
+ * array has a subelement called NAME2.
+ */
+Handlebars.registerHelper('if-any', function(items, options) {
+ var having = options.hash.having;
+ for (var i in items) {
+ var item = items[i]
+ if (!having || item[having]) {
+ return options.fn(this);
+ }
+ }
+ return "";
+});
+
+
+Handlebars.registerHelper('first', function(items, options) {
+ var having = options.hash.having;
+ for (var i in items) {
+ var item = items[i]
+ if (!having || item[having]) {
+ return options.fn(item);
+ }
+ }
+ return "";
+});
+
+
{
/* default mkws config */
var config_default = {
details = defaultRenderDetails(data, marker);
}
- details = '<div class="details" id="mkwsDet_'+data.recid+'">' + details;
- details += '</div>';
+ return '<div class="details" id="mkwsDet_' + data.recid + '">' + details + '</div>';
+}
- return details;
+
+function maybeLoadTemplate(name)
+{
+ var source = $("#mkwsTemplate" + name).html();
+ if (!source) {
+ debug("no template '" + name + "': falling back to default behaviour");
+ // Mark template as not provided
+ mkws['template' + name] = 0;
+ return;
+ }
+
+ var template = Handlebars.compile(source);
+ debug("compiled template '" + name + "'");
+ mkws['template' + name] = template;
}
+
function defaultRenderDetails(data, marker)
{
var details = '<table>';
}
-function maybeLoadTemplate(name)
-{
- var source = $("#mkwsTemplate" + name).html();
- if (!source) {
- debug("no template '" + name + "': falling back to default behaviour");
- // Mark template as not provided
- mkws['template' + name] = 0;
- return;
- }
-
- var template = Handlebars.compile(source);
- debug("compiled template '" + name + "'");
- mkws['template' + name] = template;
-}
-
-
function renderField(caption, data, data2, data3) {
if (data === undefined) {
return "";