\
+';
+ } else if (name === "Summary") {
+ return '\
+\
+ {{md-title}}\
+\
+{{#if md-title-remainder}}\
+ {{md-title-remainder}}\
+{{/if}}\
+{{#if md-title-responsibility}}\
+ {{md-title-responsibility}}\
+{{/if}}\
';
+ }
+
+ var s = "There is no default '" + name +"' template!";
+ alert(s);
+ return s;
}
- var s = "There is no default '" + name +"' template!";
- alert(s);
- return s;
-}
+ /*
+ * All the HTML stuff to render the search forms and
+ * result pages.
+ */
+ function mkws_html_all() {
+ mkws_set_lang();
+ if (mkws_config.show_lang)
+ mkws_html_lang();
+
+ debug("HTML search form");
+ // ### There is only one match here by design: fix not to bother looping
+ $('.mkwsSearch.mkwsTeam_' + m_teamName).each(function (i, obj) {
+ var node = this;
+ mkws.handle_node_with_team(node, function(tname) {
+ $(node).html('\
+');
+ });
+ });
-/*
- * All the HTML stuff to render the search forms and
- * result pages.
- */
-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.
- debug("HTML search form");
- $("#mkwsSearch").html('\
- ');
-
- debug("HTML records");
- // If the application has an #mkwsResults, populate it in the
- // usual way. If not, assume that it's a smarter application that
- // defines its own subcomponents:
- // #mkwsTermlists
- // #mkwsRanking
- // #mkwsPager
- // #mkwsNavi
- // #mkwsRecords
- if ($("#mkwsResults").length) {
- $("#mkwsResults").html('\
-
\
-
\
-
\
- \
-
\
-
\
- \
- \
- \
- \
-
\
-
\
-
\
-
\
- \
-
\
-
\
-
');
+ debug("HTML records");
+ // If the team has a .mkwsResults, populate it in the usual
+ // way. If not, assume that it's a smarter application that
+ // defines its own subcomponents, some or all of the
+ // following:
+ // .mkwsTermlists
+ // .mkwsRanking
+ // .mkwsPager
+ // .mkwsNavi
+ // .mkwsRecords
+ if ($(".mkwsResults.mkwsTeam_" + m_teamName).length) {
+ $(".mkwsResults.mkwsTeam_" + m_teamName).html('\
+
\
+
\
+
\
+ \
+
\
+
\
+ \
+ \
+ \
+ \
+
\
+
\
+
\
+
\
+ \
+
\
+
\
+
');
+ }
+
+ var node = $(".mkwsRanking.mkwsTeam_" + m_teamName);
+ if (node.length) {
+ var ranking_data = '';
+ ranking_data += '';
+
+ node.html(ranking_data);
+ }
+
+ mkws_html_switch();
+
+ // ### Should not be in the team code, since window size is global
+ 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() });
+ // initial check after page load
+ $(document).ready(function() { mkws.resize_page() });
+ }
+
+ $('.mkwsSearchForm.mkwsTeam_' + m_teamName).each(function (i, obj) {
+ debug("adding search-forms for team '" + m_teamName + "'");
+ var node = this;
+ mkws.handle_node_with_team(node, function(tname) {
+ debug("adding search-form '" + tname + "' for team '" + m_teamName + "'");
+ $(node).submit(onFormSubmitEventHandler);
+ });
+ });
+
+ node = $('.mkwsSort.mkwsTeam_' + m_teamName);
+ if (node.length)
+ node.change(onSelectDdChange);
+ node = $('.mkwsPerpage.mkwsTeam_' + m_teamName);
+ if (node.length)
+ node.change(onSelectDdChange);
+
+ // on first page, hide the termlist
+ $(document).ready(function() { $(".mkwsTermlists.mkwsTeam_" + m_teamName).hide(); });
+ var motd = $(".mkwsMOTD.mkwsTeam_" + m_teamName);
+ var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName);
+ if (motd.length && container.length) {
+ // Move the MOTD from the provided element down into the container
+ motd.appendTo(container);
+ }
}
- if ($("#mkwsRanking").length) {
- var ranking_data = '';
- ranking_data += '';
+ sort_html += '';
- $("#mkwsRanking").html(ranking_data);
+ return sort_html;
}
- mkws_html_switch();
- if (mkws_config.use_service_proxy)
- mkws_service_proxy_auth(mkws_config.service_proxy_auth,
- mkws_config.service_proxy_auth_domain,
- mkws_config.pazpar2_url);
+ function mkws_html_perpage() {
+ debug("HTML perpage, m_perpage = " + m_perpage);
+ var perpage_html = '';
- 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() });
- // initial check after page load
- $(document).ready(function() { mkws_resize_page() });
+ return perpage_html;
}
- domReady();
- // on first page, hide the termlist
- $(document).ready(function() { $("#mkwsTermlists").hide(); } );
- var motd = document.getElementById("mkwsMOTD");
- var container = document.getElementById("mkwsMOTDContainer");
- if (motd && container) {
- // Move the MOTD from the provided element down into the container
- motd.parentNode.removeChild(motd);
- container.appendChild(motd);
+ /* create locale language menu */
+ 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_options = mkws_config.lang_options || [];
+ var toBeIncluded = {};
+ for (var i = 0; i < lang_options.length; i++) {
+ toBeIncluded[lang_options[i]] = true;
+ }
+
+ for (var k in mkws.locale_lang) {
+ if (toBeIncluded[k] || lang_options.length == 0)
+ list.push(k);
+ }
+
+ // add english link
+ if (lang_options.length == 0 || toBeIncluded[lang_default])
+ 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 += ' ' + l + ' ';
+ } else {
+ data += ' ' + l + ' '
+ }
+ }
+
+ $(".mkwsLang.mkwsTeam_" + m_teamName).html(data);
}
-}
-function run_auto_searches() {
- var node = $('#mkwsRecords');
- if (node.attr('autosearch')) {
+ that.run_auto_search = function() {
+ // ### should check mkwsTermlist as well, for facet-only teams
+ var node = $('.mkwsRecords.mkwsTeam_' + m_teamName);
var query = node.attr('autosearch');
+ if (!query)
+ return;
+
+ if (query.match(/^!param!/)) {
+ var param = query.replace(/^!param!/, '');
+ query = getParameterByName(param);
+ debug("obtained query '" + query + "' from param '" + param + "'");
+ if (!query) {
+ alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
+ }
+ } else if (query.match(/^!path!/)) {
+ var index = query.replace(/^!path!/, '');
+ var path = window.location.pathname.split('/');
+ query = path[path.length - index];
+ debug("obtained query '" + query + "' from path-component '" + index + "'");
+ if (!query) {
+ alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
+ }
+ }
+
+ debug("node=" + node + ", class='" + node.className + "', query=" + query);
+
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);
+
+ this.newSearch(query, sort, targets, m_teamName);
}
-}
-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;
+ // This function is taken from a StackOverflow answer
+ // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
+ function getParameterByName(name) {
+ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
+ var regex = new RegExp("[\\?&]" + name + "=([^]*)"),
+ results = regex.exec(location.search);
+ return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
- debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
- return mkws_config.lang;
-}
-function mkws_html_switch() {
- debug("HTML switch");
+ /* locale */
+ function M(word) {
+ var lang = mkws_config.lang;
- $("#mkwsSwitch").append($('' + M('Records') + ''));
- $("#mkwsSwitch").append($("", { text: " | " }));
- $("#mkwsSwitch").append($('' + M('Targets') + ''));
+ if (!lang || !mkws.locale_lang[lang])
+ return word;
- debug("HTML targets");
- $("#mkwsTargets").html('\
-
\
- No information available yet.\
-
');
- $("#mkwsTargets").css("display", "none");
-}
+ return mkws.locale_lang[lang][word] || word;
+ }
+ mkws.M = M; // so the Handlebars helper can use it
-function mkws_html_sort() {
- debug("HTML sort, mkws.sort = '" + mkws.sort + "'");
- var sort_html = '';
+ })();
- return sort_html;
-}
+ // Bizarrely, 'that' is just an empty hash. All its state is in
+ // the closure variables defined earlier in this function.
+ return that;
+};
-function mkws_html_perpage() {
- debug("HTML perpage");
- var perpage_html = '';
- return perpage_html;
-}
+ // you need to disable use strict at the top of the file!!!
+ if (mkws.debug_level >= 3) {
+ console.log(arguments.callee.caller);
+ } else if (mkws.debug_level >= 2) {
+ console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
+ }
+ console.log(string);
+ }
+ var debug = mkws.debug;
-/*
- * 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(auth_url, auth_domain, pp2_url) {
- debug("Run service proxy auth URL: " + auth_url);
- if (!auth_domain) {
- auth_domain = pp2_url.replace(/^http:\/\/(.*?)\/.*/, '$1');
- debug("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
+ mkws.handle_node_with_team = function(node, callback) {
+ var classes = node.className;
+ var list = classes.split(/\s+/)
+ var tname;
+ for (var i = 0; i < list.length; i++) {
+ var cname = list[i];
+ if (cname.match(/^mkwsTeam_/)) {
+ tname = cname.replace(/^mkwsTeam_/, '');
+ }
+ }
+ callback(tname);
}
- var request = new pzHttpRequest(auth_url, function(err) {
- alert("HTTP call for authentication failed: " + err)
- return;
- }, auth_domain);
- request.get(null, function(data) {
- if (!$.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;
+ mkws.resize_page = function () {
+ var list = ["mkwsSwitch", "mkwsLang"];
+
+ var width = mkws_config.responsive_design_width;
+ var parent = $(".mkwsTermlists").parent();
+
+ if ($(window).width() <= width &&
+ parent.hasClass("mkwsTermlistContainer1")) {
+ debug("changing from wide to narrow: " + $(window).width());
+ $(".mkwsTermlistContainer1").hide();
+ $(".mkwsTermlistContainer2").show();
+ for (var tname in mkws.teams) {
+ $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer2.mkwsTeam_" + tname));
+ for(var i = 0; i < list.length; i++) {
+ $("." + list[i] + ".mkwsTeam_" + tname).hide();
+ }
+ }
+ } else if ($(window).width() > width &&
+ parent.hasClass("mkwsTermlistContainer2")) {
+ debug("changing from narrow to wide: " + $(window).width());
+ $(".mkwsTermlistContainer1").show();
+ $(".mkwsTermlistContainer2").hide();
+ for (var tname in mkws.teams) {
+ $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer1.mkwsTeam_" + tname));
+ for(var i = 0; i < list.length; i++) {
+ $("." + list[i] + ".mkwsTeam_" + tname).show();
+ }
+ }
}
+ };
- debug("Service proxy auth successfully done");
- mkws.authenticated = true;
- run_auto_searches();
- });
-}
-/* create locale language menu */
-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_options = mkws_config.lang_options || [];
- var hash = {};
- for (var i = 0; i < lang_options.length; i++) {
- hash[lang_options[i]] = 1;
+ mkws.switchView = function(tname, view) {
+ mkws.teams[tname].switchView(view);
}
- for (var k in mkws.locale_lang) {
- if (hash[k] == 1 || lang_options.length == 0)
- list.push(k);
+ mkws.showDetails = function (tname, prefixRecId) {
+ mkws.teams[tname].showDetails(prefixRecId);
}
- // add english link
- if (lang_options.length == 0 || hash[lang_default] == 1)
- list.push(lang_default);
+ mkws.limitTarget = function (tname, id, name) {
+ mkws.teams[tname].limitTarget(id, name);
+ }
- debug("Language menu for: " + list.join(", "));
+ mkws.limitQuery = function (tname, field, value) {
+ mkws.teams[tname].limitQuery(field, value);
+ }
- /* the HTML part */
- var data = "";
- for(var i = 0; i < list.length; i++) {
- var l = list[i];
+ mkws.delimitTarget = function (tname, id) {
+ mkws.teams[tname].delimitTarget(id);
+ }
- if (data)
- data += ' | ';
+ mkws.delimitQuery = function (tname, field, value) {
+ mkws.teams[tname].delimitQuery(field, value);
+ }
- if (lang == l) {
- data += ' ' + l + ' ';
- } else {
- data += ' ' + l + ' '
- }
+ mkws.showPage = function (tname, pageNum) {
+ mkws.teams[tname].showPage(pageNum);
}
- $("#mkwsLang").html(data);
-}
+ mkws.pagerPrev = function (tname) {
+ mkws.teams[tname].pagerPrev();
+ }
-function mkws_resize_page () {
- var list = ["mkwsSwitch"];
+ mkws.pagerNext = function (tname) {
+ mkws.teams[tname].pagerNext();
+ }
- var width = mkws_config.responsive_design_width;
- var parentId = $("#mkwsTermlists").parent().attr('id');
- if ($(window).width() <= width &&
- parentId === "mkwsTermlistContainer1") {
- debug("changing from wide to narrow: " + $(window).width());
- $("#mkwsTermlists").appendTo($("#mkwsTermlistContainer2"));
- $("#mkwsTermlistContainer1").hide();
- $("#mkwsTermlistContainer2").show();
- for(var i = 0; i < list.length; i++) {
- $("#" + list[i]).hide();
+ function default_mkws_config() {
+ /* default mkws config */
+ var config_default = {
+ use_service_proxy: true,
+ pazpar2_url: "//mkws.indexdata.com/service-proxy/",
+ service_proxy_auth: "//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: ["xtargets", "subject", "author"], /* 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 so that debug() works */
+ 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;
}
- } else if ($(window).width() > width &&
- parentId === "mkwsTermlistContainer2") {
- debug("changing from narrow to wide: " + $(window).width());
- $("#mkwsTermlists").appendTo($("#mkwsTermlistContainer1"));
- $("#mkwsTermlistContainer1").show();
- $("#mkwsTermlistContainer2").hide();
- for(var i = 0; i < list.length; i++) {
- $("#" + list[i]).show();
+
+ // make sure the mkws_config is a valid hash
+ if (!$.isPlainObject(mkws_config)) {
+ debug("ERROR: mkws_config is not an JS object, ignore it....");
+ mkws_config = {};
+ }
+
+ /* 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]);
}
}
-};
-/* locale */
-function M(word) {
- var lang = mkws_config.lang;
- if (!lang || !mkws.locale_lang[lang])
- return word;
+ /*
+ * Run service-proxy authentication in background (after page load).
+ * The username/password is configured in the apache config file
+ * for the site.
+ */
+ function authenticate_session(auth_url, auth_domain, pp2_url) {
+ debug("Run service proxy auth URL: " + auth_url);
- return mkws.locale_lang[lang][word] || word;
-}
-
-/*
- * implement jQuery plugins
- */
-$.extend({
- // implement $.parseQuerystring() for parsing URL parameters
- 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;
- },
-
- debug2: function(string) { // delayed debug, internal variables are set after dom ready
- setTimeout(function() { debug(string); }, 500);
- },
-
- // service-proxy or pazpar2
- pazpar2: function(config) {
- var id_popup = config.id_popup || "#mkwsPopup";
- id_popup = id_popup.replace(/^#/, "");
-
- // simple layout
- var div = '\
- \
- \
- \
- \
- ';
-
- // new table layout
- var table = '\
- \
- \
-
\
-
\
-
\
- \
- \
- \
-
\
-
\
-
\
-
\
-
\
- \
- \
- \
- \
- \
-
\
-
\
-
\
-
\
-
\
-
\
- \
-
\
-
\
-
\
-
\
-
\
- \
-
\
-
\
-
';
-
- var popup = '\
- \
-
\
- \
- \
- \
- \
- \
-
'
-
- if (config && config.layout == 'div') {
- this.debug2("jquery plugin layout: div");
- document.write(div);
- } else if (config && config.layout == 'popup') {
- this.debug2("jquery plugin layout: popup with id: " + id_popup);
- document.write(popup);
- $(document).ready( function() { init_popup(config); } );
- } else {
- this.debug2("jquery plugin layout: table");
- document.write(table);
+ if (!auth_domain) {
+ auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
+ debug("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
}
- }
-});
-function init_popup(obj) {
- var config = obj ? obj : {};
-
- var height = config.height || 760;
- var width = config.width || 880;
- var id_button = config.id_button || "input#mkwsButton";
- var id_popup = config.id_popup || "#mkwsPopup";
+ var request = new pzHttpRequest(auth_url, function(err) {
+ alert("HTTP call for authentication failed: " + err)
+ return;
+ }, auth_domain);
- debug("popup height: " + height + ", width: " + width);
+ request.get(null, function(data) {
+ if (!$.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;
+ }
- // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
- //
- if (!$.ui) {
- debug("Error: jquery-ui.js is missing, did you included it after jquery core in the HTML file?");
- return;
+ debug("Service proxy auth successfully done");
+ mkws.authenticated = true;
+ run_auto_searches();
+ });
}
- $(id_popup).dialog({
- closeOnEscape: true,
- autoOpen: false,
- height: height,
- width: width,
- modal: true,
- resizable: true,
- buttons: {
- Cancel: function() {
- $(this).dialog("close");
- }
- },
- close: function() { }
- });
- $(id_button)
- .button()
- .click(function() {
- $(id_popup).dialog("open");
- });
-};
+ function run_auto_searches() {
+ debug("running auto searches");
+ for (var teamName in mkws.teams) {
+ mkws.teams[teamName].run_auto_search();
+ }
+ }
+ $(document).ready(function() {
+ debug("on load ready");
+ default_mkws_config();
+
+ // Backwards compatibility: set new magic class names on any
+ // elements that have the old magic IDs.
+ var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
+ "Results", "Records", "Targets", "Ranking",
+ "Termlists", "Stat" ];
+ for (var i = 0; i < ids.length; i++) {
+ var id = 'mkws' + ids[i];
+ var node = $('#' + id);
+ if (node.attr('id')) {
+ node.addClass(id);
+ debug("added magic class to '" + node.attr('id') + "'");
+ }
+ }
-/* magic */
-$(document).ready(function() {
- try {
- mkws_html_all()
- }
+ // For all MKWS-classed nodes that don't have a team
+ // specified, set the team to AUTO.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
+ if (!this.className.match(/mkwsTeam_/)) {
+ debug("adding AUTO team to node with class '" + this.className + "'");
+ $(this).addClass('mkwsTeam_AUTO');
+ }
+ });
- catch (e) {
- mkws_config.error = e.message;
- // alert(e.message);
- }
-});
+ // Find all nodes with an class, and determine their team from
+ // the mkwsTeam_* class. Make all team objects.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
+ var node = this;
+ mkws.handle_node_with_team(node, function(tname) {
+ if (!mkws.teams[tname]) {
+ mkws.teams[tname] = team(j, tname);
+ debug("Made MKWS team '" + tname + "'");
+ }
+ });
+ });
+ if (mkws_config.use_service_proxy) {
+ authenticate_session(mkws_config.service_proxy_auth,
+ mkws_config.service_proxy_auth_domain,
+ mkws_config.pazpar2_url);
+ } else {
+ // raw pp2
+ run_auto_searches();
+ }
+ });
})(jQuery);