X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=cf7b6c6998e783f1b5e5176e1db8687e68eb8181;hb=659e818488c11679cc7ed8ee57ebcd2368e0c58b;hp=43b22040150526be3892ab9950891f36f3a32a55;hpb=5351e6e2408da4b142249cbe356fe972546bff51;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 43b2204..cf7b6c6 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -101,35 +101,39 @@ mkws.debug_function = function (string) { var debug = mkws.debug_function; // local alias -Handlebars.registerHelper('link', function(a) { - var result = ""; - for (var i in a) { - if (i > 0) result += "
"; - var text = Handlebars.Utils.escapeExpression(a[i]); - result += '' + text + ''; - } - - return new Handlebars.SafeString(result); -}); - - Handlebars.registerHelper('json', function(obj) { return JSON.stringify(obj); }); -// We need {{attr name}} because Handlebars can't parse {{@@name}} +// We need {{attr '@name'}} because Handlebars can't parse {{@name}} Handlebars.registerHelper('attr', function(attrName) { - return this['@' + 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; - debug("#first checking for first item having '" + having + "'"); for (var i in items) { var item = items[i] - debug("considering item " + (+i+1) + " of " + items.length + " = '" + item[having] + "'"); if (!having || item[having]) { return options.fn(item); } @@ -138,6 +142,18 @@ Handlebars.registerHelper('first', function(items, options) { }); +Handlebars.registerHelper('commaList', function(items, options) { + var out = ""; + + for (var i in items) { + if (i > 0) out += ", "; + out += options.fn(items[i]) + } + + return out; +}); + + { /* default mkws config */ var config_default = { @@ -679,29 +695,20 @@ function replaceHtml(el, html) { function renderDetails(data, marker) { if (mkws.templateRecord === undefined) { - maybeLoadTemplate("Record"); - } - - var details; - if (mkws.templateRecord) { - var template = mkws.templateRecord; - details = template(data); - } else { - details = defaultRenderDetails(data, marker); + loadTemplate("Record"); } + var template = mkws.templateRecord; + var details = template(data); return '
' + details + '
'; } -function maybeLoadTemplate(name) +function loadTemplate(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; + source = defaultTemplate(name); } var template = Handlebars.compile(source); @@ -710,6 +717,74 @@ function maybeLoadTemplate(name) } +function defaultTemplate(name) +{ + if (name === 'Record') { + return '\ + \ + \ + \ + \ + \ + {{#if md-date}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if md-author}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if md-electronic-url}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if-any location having="md-subject"}}\ + \ + \ + \ + \ + {{/if-any}}\ + \ + \ + \ + \ +
Title\ + {{md-title}}\ + {{#if md-title-remainder}}\ + ({{md-title-remainder}})\ + {{/if}}\ + {{#if md-title-responsibility}}\ + {{md-title-responsibility}}\ + {{/if}}\ +
Date{{md-date}}
Author{{md-author}}
URL\ + {{#each md-electronic-url}}\ + {{this}}
\ + {{/each}}\ +
Subject\ + {{#first location having="md-subject"}}\ + {{#if md-subject}}\ + {{md-subject}}\ + {{/if}}\ + {{/first}}\ +
Locations\ + {{#commaList location}}\ + {{attr "@name"}}{{/commaList}}\ +
\ +'; + } + + var s = "There is no default '" + name +"' template!"; + alert(s); + return s; +} + + function defaultRenderDetails(data, marker) { var details = '';