// Set up namespace and some state.
var mkws = {
+ sort: 'relevance',
+ authenticated: false,
filters: [],
};
}
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 submitted = false;
var SourceMax = 16;
var SubjectMax = 10;
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)
}
var termlist = document.getElementById("mkwsTermlists");
- replaceHtml(termlist, acc.join(''));
+ if (termlist)
+ replaceHtml(termlist, acc.join(''));
}
function add_single_facet(acc, caption, data, max, pzIndex) {
// 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)
{
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) {
}
}
- debug("triggerSearch: filters = " + JSON.stringify(mkws.filters) + ", pp2filter = " + pp2filter + ", pp2limit = " + pp2limit);
- my_paz.search(document.mkwsSearchForm.mkwsQuery.value, recPerPage, curSort, pp2filter, undefined, { limit: pp2limit });
+ 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;
}
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 '<div class="details" id="mkwsDet_' + data.recid + '">' + details + '</div>';
}
-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"]);
- 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 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);
}
-function renderField(caption, data, data2, data3) {
- if (data === undefined) {
- return "";
- }
-
- 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) {
- res = res + " (" + data2 + ")";
- }
-
- 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;
}
}
}
+
+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>';
}
debug("Service proxy auth successfully done");
- mkws.service_proxy_auth = true;
+ mkws.authenticated = true;
+ run_auto_searches();
});
}