"use strict"; // HTML5: disable for debug_level >= 2
// Set up namespace and some state.
-var mkws = {};
+var mkws = {
+ filters: [],
+};
/*
* global config object: mkws_config
}
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 = {
var curDetRecId = '';
var curDetRecData = null;
var curSort = 'relevance';
-var curFilter = null;
var submitted = false;
var SourceMax = 16;
var SubjectMax = 10;
totalRec = data.merged;
// move it out
var pager = document.getElementById("mkwsPager");
- pager.innerHTML = "";
- pager.innerHTML +='<div style="float: right">' + M('Displaying') + ': '
- + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
- ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
- + data.total + ')</div>';
- drawPager(pager);
+ if (pager) {
+ pager.innerHTML = "";
+ pager.innerHTML +='<div style="float: right">' + M('Displaying') + ': '
+ + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
+ ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
+ + data.total + ')</div>';
+ drawPager(pager);
+ }
+
// navi
var results = document.getElementById("mkwsRecords");
if (facets[i] == "sources") {
add_single_facet(acc, "Sources", data.xtargets, SourceMax, null);
} else if (facets[i] == "subjects") {
- add_single_facet(acc, "Subjects", data.subject, SubjectMax, "su");
+ add_single_facet(acc, "Subjects", data.subject, SubjectMax, "subject");
} else if (facets[i] == "authors") {
- add_single_facet(acc, "Authors", data.author, AuthorMax, "au");
+ add_single_facet(acc, "Authors", data.author, AuthorMax, "author");
} else {
alert("bad facet configuration: '" + facets[i] + "'");
}
replaceHtml(termlist, acc.join(''));
}
-function add_single_facet(acc, caption, data, max, cclIndex) {
+function add_single_facet(acc, caption, data, max, pzIndex) {
acc.push('<div class="facet" id="mkwsFacet' + caption + '">');
acc.push('<div class="termtitle">' + M(caption) + '</div>');
for (var i = 0; i < data.length && i < max; i++ ) {
acc.push('<div class="term">');
acc.push('<a href="#" ');
var action;
- if (!cclIndex) {
+ if (!pzIndex) {
// Special case: target selection
acc.push('target_id='+data[i].id+' ');
action = 'mkws.limitTarget(this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
} else {
- action = 'mkws.limitQuery(\'' + cclIndex + '\', this.firstChild.nodeValue)';
+ action = 'mkws.limitQuery(\'' + pzIndex + '\', this.firstChild.nodeValue)';
}
acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
+ ' <span>' + data[i].freq + '</span>');
return;
}
- var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
- +'<td>Records</td><td>State</td></tr></thead><tbody>';
+ var table ='<table><thead><tr>' +
+ '<td>' + M('Target ID') + '</td>' +
+ '<td>' + M('Hits') + '</td>' +
+ '<td>' + M('Diags') + '</td>' +
+ '<td>' + M('Records') + '</td>' +
+ '<td>' + M('State') + '</td>' +
+ '</tr></thead><tbody>';
for (var i = 0; i < data.length; i++ ) {
table += "<tr><td>" + data[i].id +
function triggerSearch ()
{
- my_paz.search(document.mkwsSearchForm.mkwsQuery.value, recPerPage, curSort, curFilter);
+ var pp2filter = "";
+ var pp2limit = "";
+
+ for (var i in mkws.filters) {
+ var filter = mkws.filters[i];
+ if (filter.id) {
+ if (pp2filter)
+ pp2filter += ",";
+ pp2filter += 'pz:id=' + filter.id;
+ } else {
+ if (pp2limit)
+ pp2limit += ",";
+ pp2limit += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
+ }
+ }
+
+ debug("triggerSearch: filters = " + JSON.stringify(mkws.filters) + ", pp2filter = " + pp2filter + ", pp2limit = " + pp2limit);
+ my_paz.search(document.mkwsSearchForm.mkwsQuery.value, recPerPage, curSort, pp2filter, undefined, { limit: pp2limit });
}
function loadSelect ()
// limit the query after clicking the facet
mkws.limitQuery = function (field, value)
{
- document.mkwsSearchForm.mkwsQuery.value += ' and ' + field + '="' + value + '"';
- onFormSubmitEventHandler();
+ debug("limitQuery(field=" + field + ", value=" + value + ")");
+ mkws.filters.push({ field: field, value: value });
+ redraw_navi();
+ resetPage();
+ loadSelect();
+ triggerSearch();
+ return false;
}
// limit by target functions
mkws.limitTarget = function (id, name)
{
- var navi = document.getElementById('mkwsNavi');
- navi.innerHTML =
- 'Source: <a class="crossout" href="#" onclick="mkws.delimitTarget();return false;">'
- + name + '</a>';
- curFilter = 'pz:id=' + id;
+ debug("limitTarget(id=" + id + ", name=" + name + ")");
+ mkws.filters.push({ id: id, name: name });
+ redraw_navi();
resetPage();
loadSelect();
triggerSearch();
return false;
}
-mkws.delimitTarget = function ()
+mkws.delimitQuery = function (field, value)
{
- var navi = document.getElementById('mkwsNavi');
- navi.innerHTML = '';
- curFilter = null;
+ debug("delimitQuery(field=" + field + ", value=" + value + ")");
+ var newFilters = [];
+ for (var i in mkws.filters) {
+ var filter = mkws.filters[i];
+ if (filter.field &&
+ field == filter.field &&
+ value == filter.value) {
+ debug("delimitTarget() removing filter " + JSON.stringify(filter));
+ } else {
+ debug("delimitTarget() keeping filter " + JSON.stringify(filter));
+ newFilters.push(filter);
+ }
+ }
+ mkws.filters = newFilters;
+
+ redraw_navi();
+ resetPage();
+ loadSelect();
+ triggerSearch();
+ return false;
+}
+
+
+mkws.delimitTarget = function (id)
+{
+ debug("delimitTarget(id=" + id + ")");
+ var newFilters = [];
+ for (var i in mkws.filters) {
+ var filter = mkws.filters[i];
+ if (filter.id) {
+ debug("delimitTarget() removing filter " + JSON.stringify(filter));
+ } else {
+ debug("delimitTarget() keeping filter " + JSON.stringify(filter));
+ newFilters.push(filter);
+ }
+ }
+ mkws.filters = newFilters;
+
+ redraw_navi();
resetPage();
loadSelect();
triggerSearch();
return false;
}
+
+function redraw_navi ()
+{
+ var navi = document.getElementById('mkwsNavi');
+ if (!navi) return;
+
+ var text = "";
+ for (var i in mkws.filters) {
+ if (text) {
+ text += " | ";
+ }
+ var filter = mkws.filters[i];
+ if (filter.id) {
+ text += 'Source: <a class="crossout" href="#" onclick="mkws.delimitTarget(' +
+ "'" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
+ } else {
+ text += filter.field + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(' +
+ "'" + filter.field + "', '" + filter.value + "'" +
+ ');return false;">' + filter.value + '</a>';
+ }
+ }
+
+ navi.innerHTML = text;
+}
+
+
function drawPager (pagerDiv)
{
//client indexes pages from 1 but pz2 from 0
function renderDetails(data, marker)
{
- var details = '<div class="details" id="mkwsDet_'+data.recid+'"><table>';
- if (marker) details += '<tr><td>'+ marker + '</td></tr>';
+ 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 = [];
+ for (var i in data.location) {
+ locations.push(data.location[i]['@name']);
+ }
details += renderField("Title", data["md-title"], data["md-title-remainder"], data["md-title-responsibility"]);
details += renderField("Date", data["md-date"]);
details += renderField("Author", data["md-author"]);
details += renderField("URL", data["md-electronic-url"]);
details += renderField("Subject", data["location"][0]["md-subject"]);
- details += renderField("Location", data["location"][0]["@name"], data["location"][0]["@id"]);
- details += '</table></div>';
-
+ if (locations.length == 0) {
+ details += '<tr><td colspan="2">No locations for record!</td></tr>';
+ } else {
+ details += renderField("Location" + (locations.length == 1 ? "" : "s"), locations);
+ }
+ details += '</table>';
return details;
}
+
function renderField(caption, data, data2, data3) {
if (data === undefined) {
return "";
}
- if (caption == "URL") {
- data = '<a href="' + data + '" target="_blank">' + data + '</a>';
+ var res = "";
+ for (var i = 0; i < data.length; i++) {
+ var s = data[i];
+ if (i > 0)
+ res += ", ";
+
+ if (caption == "URL")
+ s = '<a href="' + s + '" target="_blank">' + s + '</a>';
+
+ res += s
}
if (data2 != undefined) {
- data = data + " (" + data2 + ")";
+ res = res + " (" + data2 + ")";
}
if (data3 != undefined) {
- data = data + " <i>" + data3 + "</i>";
+ res = res + " <i>" + data3 + "</i>";
}
- return '<tr><th>' + M(caption) + '</th><td>' + data + '</td></tr>';
+ return '<tr><th>' + M(caption) + '</th><td>' + res + '</td></tr>';
}
mkws_html_switch();
if (mkws_config.use_service_proxy)
- mkws_service_proxy_auth(mkws_config.service_proxy_auth);
+ mkws_service_proxy_auth(mkws_config.service_proxy_auth,
+ mkws_config.service_proxy_auth_domain);
if (mkws_config.responsive_design_width) {
// Responsive web design - change layout on the fly based on
* The username/password is configured in the apache config file
* for the site.
*/
-function mkws_service_proxy_auth(auth_url) {
+function mkws_service_proxy_auth(auth_url, auth_domain) {
debug("Run service proxy auth URL: " + auth_url);
var request = new pzHttpRequest(auth_url, function(err) {
- alert("HTTP call for authentication failed: " + err)
- return;
- });
+ alert("HTTP call for authentication failed: " + err)
+ return;
+ }, auth_domain);
request.get(null, function(data) {
if (!$.isXMLDoc(data)) {