/* A very simple client that shows a basic usage of the pz2.js
*/
+"use strict"; // HTML5: disable for debug >= 2
+
+/*
+ * global config object: mkws_config
+ *
+ * needs to be defined in the HTML header before
+ * including this JS file
+ */
+
+if (!mkws_config)
+ var mkws_config = {}; // for the guys who forgot to define mkws_config...
+
+if (typeof mkws_config.use_service_proxy === 'undefined')
+ mkws_config.use_service_proxy = true;
+
+var mkws_debug = 1;
+
+var pazpar2_url = mkws_config.pazpar2_url ? mkws_config.pazpar2_url : "/pazpar2/search.pz2";
+var service_proxy_url = mkws_config.service_proxy_url ? mkws_config.service_proxy_url : "http://mkws.indexdata.com/service-proxy/";
+
+var pazpar2path = mkws_config.use_service_proxy ? service_proxy_url : pazpar2_url;
+var usesessions = mkws_config.use_service_proxy ? false : true;
+
+
+var mkws_locale_lang = {
+ "de": {
+ "Authors": "Autoren",
+ "Subjects": "Schlagwörter",
+ "Sources": "Daten und Quellen",
+ "Termlists": "Termlisten",
+ "Next": "Weiter",
+ "Prev": "Zurück",
+ "Search": "Suche",
+ "Sort by": "Sortieren nach",
+ "and show": "und zeige",
+ "per page": "pro Seite",
+ "Displaying": "Zeige",
+ "to": "von",
+ "of": "aus",
+ "found": "gefunden",
+ "Title": "Titel",
+ "Author": "Autor",
+ "Date": "Datum",
+ "Subject": "Schlagwort",
+ "Location": "Ort",
+
+ "dummy": "dummy"
+ },
+
+ "da": {
+ "Authors": "Forfattere",
+ "Subjects": "Emner",
+ "Sources": "Kilder",
+ "Termlists": "Termlists",
+ "Next": "Næste",
+ "Prev": "Forrige",
+ "Search": "Søg",
+ "Sort by": "Sorter efter",
+ "and show": "og vis",
+ "per page": "per side",
+ "Displaying": "Viser",
+ "to": "til",
+ "of": "ud af",
+ "found": "fandt",
+ "Title": "Title",
+ "Author": "Forfatter",
+ "Date": "Dato",
+ "Subject": "Emneord",
+ "Location": "Lokation",
+
+ "dummy": "dummy"
+ }
+};
+
// create a parameters array and pass it to the pz2's constructor
// then register the form submit event with the pz2.search function
// 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": '/service-proxy/',
+ "pazpar2path": pazpar2path,
"oninit": my_oninit,
"onstat": my_onstat,
"onterm": my_onterm,
"termlist": "xtargets,subject,author",
"onbytarget": my_onbytarget,
- "usesessions" : false,
+ "usesessions" : usesessions,
"showResponseType": '', // or "json" (for debugging?)
"onrecord": my_onrecord } );
// some state vars
// move it out
var pager = document.getElementById("pager");
pager.innerHTML = "";
- pager.innerHTML +='<hr/><div style="float: right">Displaying: '
- + (data.start + 1) + ' to ' + (data.start + data.num) +
- ' of ' + data.merged + ' (found: '
+ pager.innerHTML +='<hr/><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
if (stat == null)
return;
- stat.innerHTML = '<b>STATUS INFO</b> -- Active clients: '
- + data.activeclients
- + '/' + data.clients + ' -- </span>'
- + '<span>Retrieved records: ' + data.records
- + '/' + data.hits + ' :.</span>';
+ stat.innerHTML = '<span class="head">Status info</span>' +
+ ' -- ' +
+ '<span class="clients">' + data.activeclients + '/' + data.clients + '</span>' +
+ ' -- ' +
+ '<span class="records">' + data.records + '/' + data.hits + '</span>';
}
function my_onterm(data) {
+ if (!mkws_config.termlist_menu)
+ return;
+
var termlists = [];
- termlists.push('<hr/><b>TERMLISTS:</b><hr/><div class="termtitle">Sources</div>');
+ termlists.push('<div class="title">' + M('Termlists') + '</div>');
+
+ termlists.push('<div class="facet">');
+ termlists.push('<div class="termtitle">' + M('Sources') + '</div>');
for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
- termlists.push('<a href="#" target_id='+data.xtargets[i].id
- + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
- + ' </a><span> (' + data.xtargets[i].freq + ')</span><br/>');
+ termlists.push('<a href="#" target_id='+data.xtargets[i].id + ' onclick="limitTarget(this.getAttribute(\'target_id\'),this.firstChild.nodeValue);return false;">'
+ + data.xtargets[i].name
+ + '</a><span> ('
+ + data.xtargets[i].freq
+ + ')</span><br/>');
}
+ termlists.push('</div>');
- termlists.push('<hr/><div class="termtitle">Subjects</div>');
+ termlists.push('<div class="facet">');
+ termlists.push('<div class="termtitle">' + M('Subjects') + '</div>');
for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
- termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '</a><span> ('
- + data.subject[i].freq + ')</span><br/>');
+ termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">'
+ + data.subject[i].name
+ + '</a><span> ('
+ + data.subject[i].freq
+ + ')</span><br/>');
}
+ termlists.push('</div>');
- termlists.push('<hr/><div class="termtitle">Authors</div>');
+ termlists.push('<div class="facet">');
+ termlists.push('<div class="termtitle">' + M('Authors') + '</div>');
for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
- + data.author[i].name
- + ' </a><span> ('
- + data.author[i].freq
- + ')</span><br/>');
+ + data.author[i].name
+ + '</a><span> ('
+ + data.author[i].freq
+ + ')</span><br/>');
}
+ termlists.push('</div>');
+
var termlist = document.getElementById("termlist");
replaceHtml(termlist, termlists.join(''));
}
resetPage();
loadSelect();
triggerSearch();
+ switchView('records'); // In case it's configured to start off as hidden
submitted = true;
return false;
}
? firstClkbl + 2*onsides
: pages;
- var prev = '<span id="prev"><< Prev</span><b> | </b>';
+ var prev = '<span id="prev"><< ' + M('Prev') + '</span><b> | </b>';
if (curPage > 1)
prev = '<a href="#" id="prev" onclick="pagerPrev();">'
- +'<< Prev</a><b> | </b>';
+ +'<< ' + M('Prev') + '</a><b> | </b>';
var middle = '';
for(var i = firstClkbl; i <= lastClkbl; i++) {
+ numLabel + ' </a>';
}
- var next = '<b> | </b><span id="next">Next >></span>';
+ var next = '<b> | </b><span id="next">' + M('Next') + ' >></span>';
if (pages - curPage > 0)
next = '<b> | </b><a href="#" id="next" onclick="pagerNext()">'
- +'Next >></a>';
+ + M('Next') + ' >></a>';
var predots = '';
if (firstClkbl > 1)
var targets = document.getElementById('mkwsTargets');
var records = document.getElementById('mkwsRecords');
+ var blanket = document.getElementById('mkwsBlanket');
switch(view) {
case 'targets':
targets.style.display = "block";
records.style.display = "none";
+ if (blanket) { blanket.style.display = "none"; }
break;
case 'records':
targets.style.display = "none";
records.style.display = "block";
+ if (blanket) { blanket.style.display = "block"; }
+ break;
+ case 'none':
+ targets.style.display = "none";
+ records.style.display = "none";
+ if (blanket) { blanket.style.display = "none"; }
break;
default:
alert('Unknown view.');
{
var details = '<div class="details" id="det_'+data.recid+'"><table>';
if (marker) details += '<tr><td>'+ marker + '</td></tr>';
- if (data["md-title"] != undefined) {
- details += '<tr><td><b>Title</b></td><td><b>:</b> '+data["md-title"];
- if (data["md-title-remainder"] !== undefined) {
- details += ' : <span>' + data["md-title-remainder"] + ' </span>';
- }
- if (data["md-title-responsibility"] !== undefined) {
- details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
- }
- details += '</td></tr>';
- }
- if (data["md-date"] != undefined)
- details += '<tr><td><b>Date</b></td><td><b>:</b> ' + data["md-date"] + '</td></tr>';
- if (data["md-author"] != undefined)
- details += '<tr><td><b>Author</b></td><td><b>:</b> ' + data["md-author"] + '</td></tr>';
- if (data["md-electronic-url"] != undefined)
- details += '<tr><td><b>URL</b></td><td><b>:</b> <a href="' + data["md-electronic-url"] + '" target="_blank">' + data["md-electronic-url"] + '</a>' + '</td></tr>';
- if (data["location"][0]["md-subject"] != undefined)
- details += '<tr><td><b>Subject</b></td><td><b>:</b> ' + data["location"][0]["md-subject"] + '</td></tr>';
- if (data["location"][0]["@name"] != undefined)
- details += '<tr><td><b>Location</b></td><td><b>:</b> ' + data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")" + '</td></tr>';
+
+ 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;
}
+function renderField(caption, data, data2, data3) {
+ if (data === undefined) {
+ return "";
+ }
+
+ if (caption == "URL") {
+ data = '<a href="' + data + '" target="_blank">' + data + '</a>';
+ }
+
+ if (data2 != undefined) {
+ data = data + " (" + data2 + ")";
+ }
+
+ if (data3 != undefined) {
+ data = data + " <i>" + data3 + "</i>";
+ }
+
+ return '<tr><th>' + M(caption) + '</th><td>' + data + '</td></tr>';
+}
+
+
/*
* All the HTML stuff to render the search forms and
* result pages.
*/
-function mkws_html_all(data) {
+function mkws_html_all(config) {
- var config = {
- sort: [ ["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
+ /* default mkws config */
+ var mkws_config_default = {
+ sort: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
perpage: [10, 20, 30, 50],
sort_default: "relevance",
perpage_default: 20,
query_width: 50,
- mkws_switch: true, /* show/hide Records|Targets menu */
+ switch_menu: true, /* show/hide Records|Targets menu */
+ lang_menu: true, /* show/hide language menu */
+ lang_display: [], /* display languages links for given languages, [] for all */
+ termlist_menu: true, /* show/hide termlist */
+ debug: 0, /* debug level for development: 0..2 */
dummy: "dummy"
};
+ /* set global debug flag early */
+ if (config.debug !== 'undefined') {
+ mkws_debug = config.debug;
+ } else if (mkws_config_default.debug !== 'undefined') {
+ mkws_debug = mkws_config_default.debug;
+ }
+
/* override standard config values by function parameters */
- for (var k in data) {
- config[k] = data[k];
+ for (var k in mkws_config_default) {
+ if (typeof config[k] === 'undefined')
+ mkws_config[k] = mkws_config_default[k];
+ debug("Set config: " + k + ' => ' + mkws_config[k]);
}
-
- $("#mkwsSwitch").html($("<a/>", {
- href: '#',
- onclick: "switchView(\'records\')",
- text: "Records",
- }));
- $("#mkwsSwitch").append($("<span/>", { text: " | " }));
- $("#mkwsSwitch").append($("<a/>", {
- href: '#',
- onclick: "switchView(\'targets\')",
- text: "Targets",
- }));
-
- if (!config.mkws_switch) {
- $("#mkwsSwitch").css("display", "none");
+ 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(mkws_config);
+ if (mkws_config.lang_menu)
+ mkws_html_lang(mkws_config);
// For some reason, doing this programmatically results in
// document.search.query being undefined, hence the raw HTML.
+ debug("HTML search form");
$("#mkwsSearch").html('\
- <form id="searchForm" name="search">\
- <input id="query" type="text" size="50" />\
- <input id="button" type="submit" value="Search" />\
+ <form id="searchForm" name="search" action="" >\
+ <input id="query" type="text" size="' + mkws_config.query_width + '" />\
+ <input id="button" type="submit" value="' + M('Search') + '" />\
</form>');
+ debug("HTML records");
$("#mkwsRecords").html('\
<table width="100%" border="0" cellpadding="6" cellspacing="0">\
<tr>\
</td>\
<td valign="top">\
<div id="ranking">\
- <form name="select" id="select">\
- Sort by\
- <select name="sort" id="sort"><option value="relevance" selected="selected">relevance</option><option value="title:1">title</option><option value="date:0">newest</option><option value="date:1">oldest</option></select>\
- and show \
- <select name="perpage" id="perpage"><option value="10">10</option><option value="20" selected="selected">20</option><option value="30">30</option><option value="50">50</option></select>\
- per page.\
+ <form name="select" id="select" action="" >\
+ ' + M('Sort by') + ' ' + mkws_html_sort(config) + '\
+ ' + M('and show') + ' ' + mkws_html_perpage(config) + '\
+ ' + M('per page') + '.\
</form>\
</div>\
<div id="pager"></div>\
<div id="results"></div>\
</td>\
</tr>\
- </table>\
- </div>');
+ </table>');
+
+ mkws_html_switch(config);
+ if (mkws_config.use_service_proxy)
+ mkws_service_proxy_auth(config.service_proxy_auth);
+
+ domReady();
+}
+
+function mkws_set_lang(mkws_config) {
+ var lang = jQuery.parseQuerystring().lang || mkws_config.lang || "";
+ if (!lang || !mkws_locale_lang[lang]) {
+ mkws_config.lang = ""
+ } else {
+ mkws_config.lang = lang;
+ }
+
+ debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
+ return mkws_config.lang;
+}
+function mkws_html_switch(config) {
+ debug("HTML switch");
+
+ $("#mkwsSwitch").html($("<a/>", {
+ href: '#',
+ onclick: "switchView(\'records\')",
+ text: "Records"
+ }));
+ $("#mkwsSwitch").append($("<span/>", { text: " | " }));
+ $("#mkwsSwitch").append($("<a/>", {
+ href: '#',
+ onclick: "switchView(\'targets\')",
+ text: "Targets"
+ }));
+
+ debug("HTML targets");
$("#mkwsTargets").html('\
<div id="bytarget">\
No information available yet.\
</div>');
$("#mkwsTargets").css("display", "none");
- domReady();
+ if (!config.switch_menu) {
+ debug("disable switch menu");
+ $("#mkwsSwitch").css("display", "none");
+ }
}
-/*
+function mkws_html_sort(config) {
+ debug("HTML sort");
+ var sort_html = '<select name="sort" id="sort">';
+
+ for(var i = 0; i < config.sort.length; i++) {
+ var key = config.sort[i][0];
+ var val = config.sort[i].length == 1 ? config.sort[i][0] : config.sort[i][1];
+
+ sort_html += '<option value="' + key + '"';
+ if (key == config.sort_default) {
+ sort_html += ' selected="selected"';
+ }
+ sort_html += '>' + val + '</option>';
+ }
+ sort_html += '</select>';
+
+ return sort_html;
+}
+
+function mkws_html_perpage(config) {
+ debug("HTML perpage");
+ var perpage_html = '<select name="perpage" id="perpage">';
+
+ for(var i = 0; i < config.perpage.length; i++) {
+ var key = config.perpage[i];
+
+ perpage_html += '<option value="' + key + '"';
+ if (key == config.perpage_default) {
+ perpage_html += ' selected="selected"';
+ }
+ perpage_html += '>' + key + '</option>';
+ }
+ perpage_html += '</select>';
+
+ return perpage_html;
+}
+
+/*
* Run service-proxy authentication in background (after page load).
* The username/password is configured in the apache config file
* for the site.
*/
-function mkws_service_proxy_auth() {
- var jqxhr = jQuery.get("/service-proxy-auth")
- .fail(function() {
- alert("service proxy authentication failed, give up!");
- })
- .success(function(data) {
- if (!jQuery.isXMLDoc(data)) {
- alert("service proxy auth response document is not valid XML document, give up!");
- return;
- }
- var status = $(data).find("status");
- if (status.text() != "OK") {
- alert("service proxy auth repsonse status: " + status.text() + ", give up!");
- return;
- }
- });
-}
-
-/* moved to index.html: $(document).ready(function() { mkws_html_all( { mkws_switch: false }); }); */
-$(document).ready(function() { mkws_service_proxy_auth(); });
+function mkws_service_proxy_auth(auth_url) {
+ if (!auth_url)
+ auth_url = "http://mkws.indexdata.com/service-proxy-auth";
+
+ debug("Run service proxy auth URL: " + auth_url);
+
+ var request = new pzHttpRequest(auth_url);
+ request.get(null, function(data) {
+ if (!jQuery.isXMLDoc(data)) {
+ alert("service proxy auth response document is not valid XML document, give up!");
+ return;
+ }
+ var status = $(data).find("status");
+ if (status.text() != "OK") {
+ alert("service proxy auth repsonse status: " + status.text() + ", give up!");
+ return;
+ }
+ });
+}
+
+/* create locale language menu */
+function mkws_html_lang(mkws_config) {
+ var lang_default = "en";
+ var lang = mkws_config.lang || lang_default;
+ var list = [];
+
+ /* display a list of configured languages, or all */
+ var lang_display = mkws_config.lang_display || [];
+ var hash = {};
+ for (var i = 0; i < lang_display.length; i++) {
+ hash[lang_display[i]] = 1;
+ }
+
+ for (var k in mkws_locale_lang) {
+ if (hash[k] == 1 || lang_display.length == 0)
+ list.push(k);
+ }
+
+ // add english link
+ if (lang_display.length == 0 || hash[lang_default] == 1)
+ list.push(lang_default);
+
+ debug("Language menu for: " + list.join(", "));
+
+ /* the HTML part */
+ var data = "";
+ for(var i = 0; i < list.length; i++) {
+ var l = list[i];
+
+ if (data)
+ data += ' | ';
+
+ if (lang == l) {
+ data += ' <span>' + l + '</span> ';
+ } else {
+ data += ' <a href="?lang=' + l + '">' + l + '</a> '
+ }
+ }
+
+ $("#mkwsLang").html(data);
+}
+
+/* locale */
+function M(word) {
+ var lang = mkws_config.lang;
+
+ if (!lang || !mkws_locale_lang[lang])
+ return word;
+
+ return mkws_locale_lang[lang][word] ? mkws_locale_lang[lang][word] : word;
+}
+
+/* implement jQuery.parseQuerystring() for parsing URL parameters */
+jQuery.extend({
+ parseQuerystring: function() {
+ var nvpair = {};
+ var qs = window.location.search.replace('?', '');
+ var pairs = qs.split('&');
+ $.each(pairs, function(i, v){
+ var pair = v.split('=');
+ nvpair[pair[0]] = pair[1];
+ });
+ return nvpair;
+ },
+ pazpar2: function(data) {
+ document.write('<div id="mkwsSwitch"></div>\
+ <div id="mkwsLang"></div>\
+ <div id="mkwsSearch"></div>\
+ <div id="mkwsRecords"></div>\
+ <div id="mkwsTargets"></div>\
+ <div id="footer">\
+ <div id="mkwsStat"></div>\
+ <span>Powered by MKWS © 2013 <a href="http://www.indexdata.com">Index Data</a></span>\
+ </div>');
+
+ mkws_html_all(mkws_config);
+ }
+});
+
+function debug(string) {
+ if (!mkws_debug)
+ return;
+
+ if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
+ return;
+ }
+
+ // you need to disable use strict at the top of the file!!!
+ if (mkws_debug >= 3) {
+ console.log(arguments.callee.caller);
+ } else if (mkws_debug >= 2) {
+ console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
+ }
+ console.log(string);
+}
+/* magic */
+$(document).ready(function() { mkws_html_all(mkws_config) });