/* A very simple client that shows a basic usage of the pz2.js
*/
-"use strict"; // HTML5: disable for debug >= 2
+"use strict"; // HTML5: disable for debug_level >= 2
+// Set up namespace and some state.
+var mkws = {};
/*
* 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 = {}; // for the guys who forgot to define mkws_config...
-
-if (typeof mkws_config.use_service_proxy === 'undefined')
- mkws_config.use_service_proxy = true;
-
-// global debug flag
-var mkws_debug;
-
-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_config = {};
+// Wrapper for jQuery
(function ($) {
-var mkws_locale_lang = {
+
+mkws.locale_lang = {
"de": {
"Authors": "Autoren",
"Subjects": "Schlagwörter",
"Date": "Datum",
"Subject": "Schlagwort",
"Location": "Ort",
+ // ### to add: Records, Targets
"dummy": "dummy"
},
"Date": "Dato",
"Subject": "Emneord",
"Location": "Lokation",
+ // ### to add: Records, Targets
"dummy": "dummy"
}
};
+// keep time state for debugging
+mkws.debug_time = {
+ "start": $.now(),
+ "last": $.now()
+};
+
+mkws.debug_function = function (string) {
+ if (!mkws.debug_level)
+ return;
+
+ if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
+ return;
+ }
+
+ var now = $.now();
+ var timestamp = (now - mkws.debug_time.start)/1000 + " (+" + (now - mkws.debug_time.last)/1000 + ") "
+ mkws.debug_time.last = now;
+
+ // you need to disable use strict at the top of the file!!!
+ if (mkws.debug_level >= 3) {
+ console.log(timestamp + arguments.callee.caller);
+ } else if (mkws.debug_level >= 2) {
+ console.log(timestamp + ">>> called from function " + arguments.callee.caller.name + ' <<<');
+ }
+ console.log(timestamp + string);
+}
+var debug = mkws.debug_function; // local alias
+
+{
+ /* 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];
+ 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": 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" : usesessions,
+ "usesessions" : mkws_config.use_service_proxy ? false : true,
"showResponseType": '', // or "json" (for debugging?)
"onrecord": my_onrecord } );
+
+mkws.my_paz = my_paz; // export
+
// some state vars
var curPage = 1;
var recPerPage = 20;
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="showDetails(this.id);return false;"><b>'
+ +'" 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>');
stat.innerHTML = '<span class="head">Status info</span>' +
' -- ' +
- '<span class="clients">' + data.activeclients + '/' + data.clients + '</span>' +
+ '<span class="clients">Active clients: ' + data.activeclients + '/' + data.clients + '</span>' +
' -- ' +
- '<span class="records">' + data.records + '/' + data.hits + '</span>';
+ '<span class="records">Retrieved records: ' + data.records + '/' + data.hits + '</span>';
}
function my_onterm(data) {
if (!cclIndex) {
// Special case: target selection
acc.push('target_id='+data[i].id+' ');
- action = 'limitTarget(this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
+ action = 'mkws.limitTarget(this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
} else {
- action = 'limitQuery(\'' + cclIndex + '\', this.firstChild.nodeValue)';
+ action = 'mkws.limitQuery(\'' + cclIndex + '\', this.firstChild.nodeValue)';
}
acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
+ ' <span>' + data[i].freq + '</span>');
function my_onbytarget(data) {
var targetDiv = document.getElementById("mkwsBytarget");
+ if (!targetDiv) {
+ // No mkwsTargets div.
+ 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>';
resetPage();
loadSelect();
triggerSearch();
- switchView('records'); // In case it's configured to start off as hidden
+ mkws.switchView('records'); // In case it's configured to start off as hidden
submitted = true;
return false;
}
}
// limit the query after clicking the facet
-function limitQuery (field, value)
+mkws.limitQuery = function (field, value)
{
- document.mkwsSearchForm.query.value += ' and ' + field + '="' + value + '"';
+ document.mkwsSearchForm.mkwsQuery.value += ' and ' + field + '="' + value + '"';
onFormSubmitEventHandler();
}
// limit by target functions
-function limitTarget (id, name)
+mkws.limitTarget = function (id, name)
{
var navi = document.getElementById('mkwsNavi');
navi.innerHTML =
- 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
+ 'Source: <a class="crossout" href="#" onclick="mkws.delimitTarget();return false;">'
+ name + '</a>';
curFilter = 'pz:id=' + id;
resetPage();
return false;
}
-function delimitTarget ()
+mkws.delimitTarget = function ()
{
var navi = document.getElementById('mkwsNavi');
navi.innerHTML = '';
var prev = '<span id="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
if (curPage > 1)
- prev = '<a href="#" id="mkwsPrev" onclick="pagerPrev();">'
+ prev = '<a href="#" id="mkwsPrev" onclick="mkws.pagerPrev();">'
+'<< ' + M('Prev') + '</a><b> | </b>';
var middle = '';
if(i == curPage)
numLabel = '<b>' + i + '</b>';
- middle += '<a href="#" onclick="showPage(' + i + ')"> '
+ middle += '<a href="#" onclick="mkws.showPage(' + i + ')"> '
+ numLabel + ' </a>';
}
var next = '<b> | </b><span id="mkwsNext">' + M('Next') + ' >></span>';
if (pages - curPage > 0)
- next = '<b> | </b><a href="#" id="mkwsNext" onclick="pagerNext()">'
+ next = '<b> | </b><a href="#" id="mkwsNext" onclick="mkws.pagerNext()">'
+ M('Next') + ' >></a>';
var predots = '';
+ prev + predots + middle + postdots + next + '</div>';
}
-function showPage (pageNum)
+mkws.showPage = function (pageNum)
{
curPage = pageNum;
my_paz.showPage( curPage - 1 );
// simple paging functions
-function pagerNext() {
+mkws.pagerNext = function () {
if ( totalRec - recPerPage*curPage > 0) {
my_paz.showNext();
curPage++;
}
}
-function pagerPrev() {
+mkws.pagerPrev = function () {
if ( my_paz.showPrev() != false )
curPage--;
}
// switching view between targets and records
-function switchView(view) {
+mkws.switchView = function(view) {
+ debug("switchView: " + view);
+
var targets = document.getElementById('mkwsTargets');
var results = document.getElementById('mkwsResults') ||
document.getElementById('mkwsRecords');
}
// detailed record drawing
-function showDetails (prefixRecId) {
+mkws.showDetails = function (prefixRecId) {
var recId = prefixRecId.replace('mkwsRec_', '');
var oldRecId = curDetRecId;
curDetRecId = recId;
* All the HTML stuff to render the search forms and
* result pages.
*/
-function mkws_html_all(config) {
-
- /* 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,
- switch_menu: false, /* show/hide Records|Targets menu */
- lang_menu: true, /* show/hide language menu */
- sort_menu: true, /* show/hide sort menu */
- perpage_menu: true, /* show/hide perpage menu */
- lang_display: [], /* display languages links for given languages, [] for all */
- facets: ["sources", "subjects", "authors"], /* display facets, in this order, [] for none */
- responsive_design_width: 980, /* a page with less pixel width considered as narrow */
- debug: 1, /* debug level for development: 0..2 */
-
- dummy: "dummy"
- };
-
- /* set global debug flag early */
- if (typeof config.debug !== 'undefined') {
- mkws_debug = config.debug;
- } else if (typeof mkws_config_default.debug !== 'undefined') {
- mkws_debug = mkws_config_default.debug;
- }
-
- /* override standard config values by function parameters */
- 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]);
- }
-
- 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);
+function mkws_html_all() {
+ mkws_set_lang();
+ if (mkws_config.show_lang)
+ mkws_html_lang();
// For some reason, doing this programmatically results in
// document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML.
if ($("#mkwsRanking").length) {
var ranking_data = '';
ranking_data += '<form name="mkwsSelect" id="mkwsSelect" action="" >';
- if (config.sort_menu) {
- ranking_data += M('Sort by') + ' ' + mkws_html_sort(config) + ' ';
+ if (mkws_config.show_sort) {
+ ranking_data += M('Sort by') + ' ' + mkws_html_sort() + ' ';
}
- if (config.perpage_menu) {
- ranking_data += M('and show') + ' ' + mkws_html_perpage(config) + ' ' + M('per page') + '.';
+ if (mkws_config.show_perpage) {
+ ranking_data += M('and show') + ' ' + mkws_html_perpage() + ' ' + M('per page') + '.';
}
ranking_data += '</form>';
$("#mkwsRanking").html(ranking_data);
}
- mkws_html_switch(config);
+ mkws_html_switch();
if (mkws_config.use_service_proxy)
- mkws_service_proxy_auth(config.service_proxy_auth);
+ mkws_service_proxy_auth(mkws_config.service_proxy_auth);
- if (mkws_config.responsive_design) {
+ if (mkws_config.responsive_design_width) {
// Responsive web design - change layout on the fly based on
// current screen width. Required for mobile devices.
$(window).resize( function(e) { mkws_resize_page() });
}
}
-function mkws_set_lang(mkws_config) {
- var lang = $.parseQuerystring().lang || mkws_config.lang || "";
- if (!lang || !mkws_locale_lang[lang]) {
+function mkws_set_lang() {
+ var lang = $.parseQuerystring().lang || mkws_config.lang;
+ if (!lang || !mkws.locale_lang[lang]) {
mkws_config.lang = ""
} else {
mkws_config.lang = lang;
return mkws_config.lang;
}
-function mkws_html_switch(config) {
+function mkws_html_switch() {
debug("HTML switch");
- $("#mkwsSwitch").html($("<a/>", {
- href: '#',
- onclick: "switchView(\'records\')",
- text: M("Records")
- }));
+ $("#mkwsSwitch").append($('<a href="#" id="mkwsSwitch_records" onclick="mkws.switchView(\'records\')">' + M('Records') + '</a>'));
$("#mkwsSwitch").append($("<span/>", { text: " | " }));
- $("#mkwsSwitch").append($("<a/>", {
- href: '#',
- onclick: "switchView(\'targets\')",
- text: M("Targets")
- }));
+ $("#mkwsSwitch").append($('<a href="#" id="mkwsSwitch_targets" onclick="mkws.switchView(\'targets\')">' + M('Targets') + '</a>'));
debug("HTML targets");
$("#mkwsTargets").html('\
No information available yet.\
</div>');
$("#mkwsTargets").css("display", "none");
-
- if (!config.switch_menu) {
- debug("disable switch menu");
- $("#mkwsSwitch").css("display", "none");
- }
}
-function mkws_html_sort(config) {
+function mkws_html_sort() {
debug("HTML sort");
var sort_html = '<select name="mkwsSort" id="mkwsSort">';
- 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];
+ for(var i = 0; i < mkws_config.sort_options.length; i++) {
+ var opt = mkws_config.sort_options[i];
+ var key = opt[0];
+ var val = opt.length == 1 ? opt[0] : opt[1];
sort_html += '<option value="' + key + '"';
- if (key == config.sort_default) {
+ if (key == mkws_config.sort_default) {
sort_html += ' selected="selected"';
}
- sort_html += '>' + val + '</option>';
+ sort_html += '>' + M(val) + '</option>';
}
sort_html += '</select>';
return sort_html;
}
-function mkws_html_perpage(config) {
+function mkws_html_perpage() {
debug("HTML perpage");
var perpage_html = '<select name="mkwsPerpage" id="mkwsPerpage">';
- for(var i = 0; i < config.perpage.length; i++) {
- var key = config.perpage[i];
+ for(var i = 0; i < mkws_config.perpage_options.length; i++) {
+ var key = mkws_config.perpage_options[i];
perpage_html += '<option value="' + key + '"';
- if (key == config.perpage_default) {
+ if (key == mkws_config.perpage_default) {
perpage_html += ' selected="selected"';
}
perpage_html += '>' + key + '</option>';
* for the site.
*/
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);
+ var request = new pzHttpRequest(auth_url, function(err) {
+ alert("HTTP call for authentication failed: " + err)
+ return;
+ });
+
request.get(null, function(data) {
if (!$.isXMLDoc(data)) {
alert("service proxy auth response document is not valid XML document, give up!");
alert("service proxy auth repsonse status: " + status.text() + ", give up!");
return;
}
+
+ debug("Service proxy auth successfully done");
+ mkws.service_proxy_auth = true;
});
}
/* create locale language menu */
-function mkws_html_lang(mkws_config) {
+function mkws_html_lang() {
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 lang_options = mkws_config.lang_options || [];
var hash = {};
- for (var i = 0; i < lang_display.length; i++) {
- hash[lang_display[i]] = 1;
+ for (var i = 0; i < lang_options.length; i++) {
+ hash[lang_options[i]] = 1;
}
- for (var k in mkws_locale_lang) {
- if (hash[k] == 1 || lang_display.length == 0)
+ for (var k in mkws.locale_lang) {
+ if (hash[k] == 1 || lang_options.length == 0)
list.push(k);
}
// add english link
- if (lang_display.length == 0 || hash[lang_default] == 1)
+ if (lang_options.length == 0 || hash[lang_default] == 1)
list.push(lang_default);
debug("Language menu for: " + list.join(", "));
function mkws_resize_page () {
var list = ["mkwsSwitch"];
- var width = mkws_config.responsive_design_width || 980;
+ var width = mkws_config.responsive_design_width;
var parentId = $("#mkwsTermlists").parent().attr('id');
if ($(window).width() <= width &&
function M(word) {
var lang = mkws_config.lang;
- if (!lang || !mkws_locale_lang[lang])
+ 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;
}
/*
// service-proxy or pazpar2
pazpar2: function(config) {
+ var id_popup = config.id_popup || "#mkwsPopup";
+ id_popup = id_popup.replace(/^#/, "");
+
// simple layout
var div = '<div id="mkwsSwitch"></div>\
<div id="mkwsLang"></div>\
var popup = '\
<div id="mkwsSearch"></div>\
- <div id="mkwsPopup">\
+ <div id="' + id_popup + '">\
<div id="mkwsSwitch"></div>\
<div id="mkwsLang"></div>\
<div id="mkwsResults"></div>\
this.debug2("jquery plugin layout: div");
document.write(div);
} else if (config && config.layout == 'popup') {
- this.debug2("jquery plugin layout: popup");
+ this.debug2("jquery plugin layout: popup with id: " + id_popup);
document.write(popup);
$(document).ready( function() { init_popup(config); } );
} else {
var height = config.height || 760;
var width = config.width || 880;
- var id_button = config.button || "input#mkwsButton";
- var id_popup = config.popup || "#mkwsPopup";
+ var id_button = config.id_button || "input#mkwsButton";
+ var id_popup = config.id_popup || "#mkwsPopup";
debug("popup height: " + height + ", width: " + width);
+ // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
+ // <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
+ if (!$.ui) {
+ debug("Error: jquery-ui.js is missing, did you included it after jquery core in the HTML file?");
+ return;
+ }
+
$(id_popup).dialog({
closeOnEscape: true,
autoOpen: false,
});
};
-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) });
+$(document).ready(function() {
+ try {
+ mkws_html_all()
+ }
+
+ catch (e) {
+ mkws_config.error = e.message;
+ // alert(e.message);
+ }
+});
})(jQuery);