"use strict"; // HTML5: disable for debug_level >= 2
// Set up namespace and some state.
-var mkws = {};
-
-if (!mkws_config)
- var mkws_config = {}; // for the guys who forgot to define mkws_config...
-
-// Wrapper for jQuery
-(function ($) {
+var mkws = {
+ filters: [],
+};
/*
* global config object: mkws_config
*
- * needs to be defined in the HTML header before
- * including this JS file
+ * Needs to be defined in the HTML header before including this JS file.
+ * Define empty mkws_config for simple applications that don't define it.
*/
+if (!mkws_config)
+ var mkws_config = {};
-if (typeof mkws_config.use_service_proxy === 'undefined')
- mkws_config.use_service_proxy = true;
-
-var pazpar2_url = mkws_config.pazpar2_url ? mkws_config.pazpar2_url : "http://mkws.indexdata.com/service-proxy/";
-
-mkws.pazpar2path = pazpar2_url;
-mkws.usesessions = mkws_config.use_service_proxy ? false : true;
+// Wrapper for jQuery
+(function ($) {
mkws.locale_lang = {
"de": {
"start": $.now(),
"last": $.now()
};
+
mkws.debug_function = function (string) {
if (!mkws.debug_level)
return;
}
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 = {
+ use_service_proxy: true,
+ service_proxy_auth: "http://mkws.indexdata.com/service-proxy-auth",
+ lang: "",
+ sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
+ perpage_options: [10, 20, 30, 50],
+ sort_default: "relevance",
+ perpage_default: 20,
+ query_width: 50,
+ show_lang: true, /* show/hide language menu */
+ show_sort: true, /* show/hide sort menu */
+ show_perpage: true, /* show/hide perpage menu */
+ lang_options: [], /* display languages links for given languages, [] for all */
+ facets: ["sources", "subjects", "authors"], /* display facets, in this order, [] for none */
+ responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
+ debug_level: 1, /* debug level for development: 0..2 */
+
+ dummy: "dummy"
+ };
+
+ /* set global debug_level flag early */
+ if (typeof mkws_config.debug_level !== 'undefined') {
+ mkws.debug_level = mkws_config.debug_level;
+ } else if (typeof config_default.debug_level !== 'undefined') {
+ mkws.debug_level = config_default.debug_level;
+ }
+
+ /* override standard config values by function parameters */
+ for (var k in config_default) {
+ if (typeof mkws_config[k] === 'undefined')
+ mkws_config[k] = config_default[k];
+ debug("Set config: " + k + ' => ' + mkws_config[k]);
+ }
+}
+
+if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
+ debug("Reset query width: " + mkws_config.query_width);
+ mkws_config.query_width = 50;
+}
+
for (var key in mkws_config) {
if (mkws_config.hasOwnProperty(key)) {
if (key.match(/^language_/)) {
var lang = key.replace(/^language_/, "");
// Copy custom languages into list
mkws.locale_lang[lang] = mkws_config[key];
+ debug("Added locally configured language '" + lang + "'");
}
}
}
// autoInit is set to true on default
var my_paz = new pz2( { "onshow": my_onshow,
"showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
- "pazpar2path": mkws.pazpar2path,
+ "pazpar2path": mkws_config.pazpar2_url || "http://mkws.indexdata.com/service-proxy/",
"oninit": my_oninit,
"onstat": my_onstat,
"onterm": my_onterm,
"termlist": "xtargets,subject,author",
"onbytarget": my_onbytarget,
- "usesessions" : mkws.usesessions,
+ "usesessions" : mkws_config.use_service_proxy ? false : true,
"showResponseType": '', // or "json" (for debugging?)
"onrecord": my_onrecord } );
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");
for (var i = 0; i < data.hits.length; i++) {
var hit = data.hits[i];
html.push('<div class="record" id="mkwsRecdiv_'+hit.recid+'" >'
- +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>'
+'<a href="#" id="mkwsRec_'+hit.recid
+'" onclick="mkws.showDetails(this.id);return false;"><b>'
+ hit["md-title"] +' </b></a>');
if (stat == null)
return;
- stat.innerHTML = '<span class="head">Status info</span>' +
+ stat.innerHTML = '<span class="head">' + M('Status info') + '</span>' +
' -- ' +
- '<span class="clients">' + data.activeclients + '/' + data.clients + '</span>' +
+ '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
' -- ' +
- '<span class="records">' + data.records + '/' + data.hits + '</span>';
+ '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>';
}
function my_onterm(data) {
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>';
}
* result pages.
*/
function mkws_html_all() {
-
- /* default mkws config */
- /* ### No defaults given for:
- * lang, service_proxy_auth, use_service_proxy
- * and of course the optional language_* entries.
- */
- var config_default = {
- sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
- perpage_options: [10, 20, 30, 50],
- sort_default: "relevance",
- perpage_default: 20,
- query_width: 50,
- show_lang: true, /* show/hide language menu */
- show_sort: true, /* show/hide sort menu */
- show_perpage: true, /* show/hide perpage menu */
- lang_options: [], /* display languages links for given languages, [] for all */
- facets: ["sources", "subjects", "authors"], /* display facets, in this order, [] for none */
- responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
- debug_level: 1, /* debug level for development: 0..2 */
-
- dummy: "dummy"
- };
-
- /* set global debug_level flag early */
- if (typeof mkws_config.debug_level !== 'undefined') {
- mkws.debug_level = mkws_config.debug_level;
- } else if (typeof config_default.debug_level !== 'undefined') {
- mkws.debug_level = config_default.debug_level;
- }
-
- /* override standard config values by function parameters */
- for (var k in config_default) {
- if (typeof mkws_config[k] === 'undefined')
- mkws_config[k] = config_default[k];
- debug("Set config: " + k + ' => ' + mkws_config[k]);
- }
-
- if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
- debug("Reset query width: " + mkws_config.query_width);
- mkws_config.query_width = 50;
- }
-
mkws_set_lang();
if (mkws_config.show_lang)
mkws_html_lang();
$("#mkwsRanking").html(ranking_data);
}
- mkws_html_switch(mkws_config);
+ 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
}
function mkws_set_lang() {
- var lang = $.parseQuerystring().lang || mkws_config.lang || "";
+ var lang = $.parseQuerystring().lang || mkws_config.lang;
if (!lang || !mkws.locale_lang[lang]) {
mkws_config.lang = ""
} else {
return mkws_config.lang;
}
-function mkws_html_switch(config) {
+function mkws_html_switch() {
debug("HTML switch");
$("#mkwsSwitch").append($('<a href="#" id="mkwsSwitch_records" onclick="mkws.switchView(\'records\')">' + M('Records') + '</a>'));
if (key == mkws_config.sort_default) {
sort_html += ' selected="selected"';
}
- sort_html += '>' + val + '</option>';
+ sort_html += '>' + M(val) + '</option>';
}
sort_html += '</select>';
* The username/password is configured in the apache config file
* for the site.
*/
-function mkws_service_proxy_auth(auth_url) {
- if (!auth_url)
- auth_url = "http://mkws.indexdata.com/service-proxy-auth";
-
+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)) {
if (!lang || !mkws.locale_lang[lang])
return word;
- return mkws.locale_lang[lang][word] ? mkws.locale_lang[lang][word] : word;
+ return mkws.locale_lang[lang][word] || word;
}
/*