}
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 = {
function renderDetails(data, marker)
{
- var details = '<div class="details" id="mkwsDet_'+data.recid+'"><table>';
+ if (mkws.templateRecord === undefined) {
+ maybeLoadTemplate("Record");
+ }
+
+ var details;
+ if (mkws.templateRecord) {
+ var template = mkws.templateRecord;
+ details = template(data);
+ } else {
+ details = defaultRenderDetails(data, marker);
+ }
+
+ return '<div class="details" id="mkwsDet_' + data.recid + '">' + details + '</div>';
+}
+
+
+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>';
if (marker) details += '<tr><td colspan="2">'+ marker + '</td></tr>';
var locations = [];
} else {
details += renderField("Location" + (locations.length == 1 ? "" : "s"), locations);
}
- details += '</table></div>';
-
+ details += '</table>';
return details;
}
+
function renderField(caption, data, data2, data3) {
if (data === undefined) {
return "";