"use strict"; // HTML5: disable for debug_level >= 2
// Set up namespace and some state.
-var mkws = {};
+var mkws = {
+ sort: 'relevance',
+ authenticated: false,
+ filters: [],
+};
/*
* global config object: mkws_config
}
var debug = mkws.debug_function; // local alias
+
+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 "";
+});
+
+
+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 = {
}
}
+mkws.sort = mkws_config.sort_default;
+debug("copied mkws_config.sort_default '" + mkws_config.sort_default + "' to mkws.sort");
+
if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
debug("Reset query width: " + mkws_config.query_width);
mkws_config.query_width = 50;
var totalRec = 0;
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");
var html = [];
for (var i = 0; i < data.hits.length; i++) {
var hit = data.hits[i];
- html.push('<div class="record" id="mkwsRecdiv_'+hit.recid+'" >'
- +'<a href="#" id="mkwsRec_'+hit.recid
- +'" onclick="mkws.showDetails(this.id);return false;"><b>'
- + hit["md-title"] +' </b></a>');
- if (hit["md-title-remainder"] !== undefined) {
- html.push('<span>' + hit["md-title-remainder"] + ' </span>');
- }
- if (hit["md-title-responsibility"] !== undefined) {
- html.push('<span><i>'+hit["md-title-responsibility"]+'</i></span>');
- }
- if (hit.recid == curDetRecId) {
+ html.push('<div class="record" id="mkwsRecdiv_' + hit.recid + '" >',
+ renderSummary(hit),
+ '</div>');
+ if (hit.recid == curDetRecId) {
html.push(renderDetails(curDetRecData));
- }
- html.push('</div>');
+ }
}
replaceHtml(results, html.join(''));
}
+
+function renderSummary(hit)
+{
+ if (mkws.templateSummary === undefined) {
+ loadTemplate("Summary");
+ }
+
+ hit._id = "mkwsRec_" + hit.recid;
+ hit._onclick = "mkws.showDetails(this.id);return false;"
+ return mkws.templateSummary(hit);
+}
+
+
function my_onstat(data) {
var stat = document.getElementById("mkwsStat");
if (stat == null)
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] + "'");
}
}
var termlist = document.getElementById("mkwsTermlists");
- replaceHtml(termlist, acc.join(''));
+ if (termlist)
+ 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>');
// when search button pressed
function onFormSubmitEventHandler()
{
+ newSearch(document.mkwsSearchForm.mkwsQuery.value);
+ return false;
+}
+
+function newSearch(query, sort)
+{
+ if (!mkws.authenticated) {
+ alert("searching before authentication");
+ return;
+ }
+
+ mkws.filters = []
+ redraw_navi();
resetPage();
loadSelect();
- triggerSearch();
+ triggerSearch(query, sort);
mkws.switchView('records'); // In case it's configured to start off as hidden
submitted = true;
- return false;
}
function onSelectDdChange()
if (!submitted) return false;
resetPage();
loadSelect();
- my_paz.show(0, recPerPage, curSort);
+ my_paz.show(0, recPerPage, mkws.sort);
return false;
}
totalRec = 0;
}
-function triggerSearch ()
+function triggerSearch (query, sort)
{
- my_paz.search(document.mkwsSearchForm.mkwsQuery.value, recPerPage, curSort, curFilter);
+ var pp2filter = "";
+ var pp2limit = "";
+
+ // Re-use previous query/sort if new ones are not specified
+ if (query) {
+ mkws.query = query;
+ }
+ if (sort) {
+ mkws.sort = sort;
+ }
+
+ 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(" + mkws.query + "): filters = " + JSON.stringify(mkws.filters) + ", pp2filter = " + pp2filter + ", pp2limit = " + pp2limit);
+ my_paz.search(mkws.query, recPerPage, mkws.sort, pp2filter, undefined, { limit: pp2limit });
}
function loadSelect ()
{
if (document.mkwsSelect) {
if (document.mkwsSelect.mkwsSort)
- curSort = document.mkwsSelect.mkwsSort.value;
+ mkws.sort = document.mkwsSelect.mkwsSort.value;
if (document.mkwsSelect.mkwsPerpage)
recPerPage = document.mkwsSelect.mkwsPerpage.value;
}
// 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 colspan="2">'+ marker + '</td></tr>';
-
- var sources = [];
- for (var i in data.location) {
- sources.push(data.location[i]['@name']);
- }
- if (sources.length == 0) {
- details += '<tr><td colspan="2">No sources for record!</td></tr>';
- } else if (sources.length == 1) {
- details += renderField("Source", sources[0]);
- } else {
- details += renderField("Sources", sources);
+ if (mkws.templateRecord === undefined) {
+ loadTemplate("Record");
}
- 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>';
-
- return details;
+ var template = mkws.templateRecord;
+ var details = template(data);
+ return '<div class="details" id="mkwsDet_' + data.recid + '">' + details + '</div>';
}
-function renderField(caption, data, data2, data3) {
- if (data === undefined) {
- return "";
- }
-
- var res = data;
- if (caption == "URL") {
- var tmp = "";
- for (var i = 0; i < data.length; i++) {
- var s = data[i];
- if (i > 0)
- tmp += ", ";
- tmp += '<a href="' + s + '" target="_blank">' + s + '</a>';
- }
- res = tmp;
+function loadTemplate(name)
+{
+ var source = $("#mkwsTemplate" + name).html();
+ if (!source) {
+ source = defaultTemplate(name);
}
- if (data2 != undefined) {
- res = res + " (" + data2 + ")";
- }
+ var template = Handlebars.compile(source);
+ debug("compiled template '" + name + "'");
+ mkws['template' + name] = template;
+}
- if (data3 != undefined) {
- res = res + " <i>" + data3 + "</i>";
+
+function defaultTemplate(name)
+{
+ if (name === 'Record') {
+ return '\
+ <table>\
+ <tr>\
+ <th>Title</th>\
+ <td>\
+ {{md-title}}\
+ {{#if md-title-remainder}}\
+ ({{md-title-remainder}})\
+ {{/if}}\
+ {{#if md-title-responsibility}}\
+ <i>{{md-title-responsibility}}</i>\
+ {{/if}}\
+ </td>\
+ </tr>\
+ {{#if md-date}}\
+ <tr>\
+ <th>Date</th>\
+ <td>{{md-date}}</td>\
+ </tr>\
+ {{/if}}\
+ {{#if md-author}}\
+ <tr>\
+ <th>Author</th>\
+ <td>{{md-author}}</td>\
+ </tr>\
+ {{/if}}\
+ {{#if md-electronic-url}}\
+ <tr>\
+ <th>URL</th>\
+ <td>\
+ {{#each md-electronic-url}}\
+ <a href="{{this}}">{{this}}</a><br/>\
+ {{/each}}\
+ </td>\
+ </tr>\
+ {{/if}}\
+ {{#if-any location having="md-subject"}}\
+ <tr>\
+ <th>Subject</th>\
+ <td>\
+ {{#first location having="md-subject"}}\
+ {{#if md-subject}}\
+ {{md-subject}}\
+ {{/if}}\
+ {{/first}}\
+ </td>\
+ </tr>\
+ {{/if-any}}\
+ <tr>\
+ <th>Locations</th>\
+ <td>\
+ {{#commaList location}}\
+ {{attr "@name"}}{{/commaList}}\
+ </td>\
+ </tr>\
+ </table>\
+';
+ } else if (name === "Summary") {
+ return '\
+ <a href="#" id="{{_id}}" onclick="{{_onclick}}">\
+ <b>{{md-title}}</b>\
+ </a>\
+ {{#if md-title-remainder}}\
+ <span>{{md-title-remainder}}</span>\
+ {{/if}}\
+ {{#if md-title-responsibility}}\
+ <span><i>{{md-title-responsibility}}</i></span>\
+ {{/if}}\
+';
}
- return '<tr><th>' + M(caption) + '</th><td>' + res + '</td></tr>';
+ var s = "There is no default '" + name +"' template!";
+ alert(s);
+ return s;
}
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 run_auto_searches() {
+ var node = $('#mkwsRecords');
+ if (node.attr('autosearch')) {
+ var query = node.attr('autosearch');
+ var sort = node.attr('sort');
+ var targets = node.attr('targets');
+ var s = "running auto search: '" + query + "'";
+ if (sort) s += " sorted by '" + sort + "'";
+ if (targets) s += " in targets '" + targets + "'";
+ debug(s);
+ newSearch(query, sort, targets);
+ }
+}
+
+
function mkws_set_lang() {
var lang = $.parseQuerystring().lang || mkws_config.lang;
if (!lang || !mkws.locale_lang[lang]) {
}
function mkws_html_sort() {
- debug("HTML sort");
+ debug("HTML sort, mkws.sort = '" + mkws.sort + "'");
var sort_html = '<select name="mkwsSort" id="mkwsSort">';
for(var i = 0; i < mkws_config.sort_options.length; i++) {
var val = opt.length == 1 ? opt[0] : opt[1];
sort_html += '<option value="' + key + '"';
- if (key == mkws_config.sort_default) {
+ if (mkws.sort == key || mkws.sort == val) {
sort_html += ' selected="selected"';
}
sort_html += '>' + M(val) + '</option>';
* 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)) {
}
debug("Service proxy auth successfully done");
- mkws.service_proxy_auth = true;
+ mkws.authenticated = true;
+ run_auto_searches();
});
}