\
@@ -980,16 +1000,15 @@ function team($, teamName) {
that.runAutoSearch = function() {
- // ### should check mkwsTermlist as well, for facet-only teams
- var node = findnode('.mkwsRecords');
+ var node = findnode('.mkwsRecords,.mkwsTermlists');
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 + "'");
+ query = mkws.getParameterByName(param);
+ log("obtained query '" + query + "' from param '" + param + "'");
if (!query) {
alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
}
@@ -997,50 +1016,30 @@ function team($, teamName) {
var index = query.replace(/^!path!/, '');
var path = window.location.pathname.split('/');
query = path[path.length - index];
- debug("obtained query '" + query + "' from path-component '" + index + "'");
+ log("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);
+ log("node=" + node + ", class='" + node.className + "', query=" + query);
- var sort = node.attr('sort');
+ var sortOrder = node.attr('sort');
var targets = node.attr('targets');
var s = "running auto search: '" + query + "'";
- if (sort) s += " sorted by '" + sort + "'";
+ if (sortOrder) s += " sorted by '" + sortOrder + "'";
if (targets) s += " in targets '" + targets + "'";
- debug(s);
+ log(s);
- newSearch(query, sort, targets);
+ newSearch(query, sortOrder, targets);
}
- // implement $.parseQuerystring() for parsing URL parameters
- function parseQuerystring() {
- 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;
- }
-
-
- // This function is taken from a StackOverflow answer
- // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
- // ### should we unify this and parseQuerystring()?
- 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, " "));
- }
-
-
- /* locale */
+ // Translation function. At present, this is properly a
+ // global-level function (hence the assignment to mkws.M) but we
+ // want to make it per-team so different teams can operate in
+ // different languages.
+ //
function M(word) {
var lang = mkws_config.lang;
@@ -1059,7 +1058,7 @@ function team($, teamName) {
function findnode(selector, teamName) {
teamName = teamName || m_teamName;
- selector = selector.split(',').map(function(s) {
+ selector = $.map(selector.split(','), function(s, i) {
return s + '.mkwsTeam_' + teamName;
}).join(',');
@@ -1067,21 +1066,13 @@ function team($, teamName) {
}
- function renderSummary(hit)
- {
- var template = loadTemplate("Summary");
- hit._id = "mkwsRec_" + hit.recid;
- hit._onclick = "mkws.showDetails('" + m_teamName + "', this.id);return false;"
- return template(hit);
- }
-
-
function renderDetails(data, marker)
{
var template = loadTemplate("Record");
var details = template(data);
- return '
' + details + '
';
+ return '
' + details + '
';
}
+ that.renderDetails = renderDetails;
function loadTemplate(name)
@@ -1097,97 +1088,39 @@ function team($, teamName) {
var source = node.html();
if (!source) {
- source = defaultTemplate(name);
+ source = mkws.defaultTemplate(name);
}
template = Handlebars.compile(source);
- debug("compiled template '" + name + "'");
+ log("compiled template '" + name + "'");
m_template[name] = template;
}
return template;
}
+ that.loadTemplate = loadTemplate;
- function defaultTemplate(name)
- {
- if (name === 'Record') {
- return '\
-
\
- \
- {{translate "Title"}} | \
- \
- {{md-title}}\
- {{#if md-title-remainder}}\
- ({{md-title-remainder}})\
- {{/if}}\
- {{#if md-title-responsibility}}\
- {{md-title-responsibility}}\
- {{/if}}\
- | \
-
\
- {{#if md-date}}\
- \
- {{translate "Date"}} | \
- {{md-date}} | \
-
\
- {{/if}}\
- {{#if md-author}}\
- \
- {{translate "Author"}} | \
- {{md-author}} | \
-
\
- {{/if}}\
- {{#if md-electronic-url}}\
- \
- {{translate "Links"}} | \
- \
- {{#each md-electronic-url}}\
- Link{{index1}}\
- {{/each}}\
- | \
-
\
- {{/if}}\
- {{#if-any location having="md-subject"}}\
- \
- {{translate "Subject"}} | \
- \
- {{#first location having="md-subject"}}\
- {{#if md-subject}}\
- {{#commaList md-subject}}\
- {{this}}{{/commaList}}\
- {{/if}}\
- {{/first}}\
- | \
-
\
- {{/if-any}}\
- \
- {{translate "Locations"}} | \
- \
- {{#commaList location}}\
- {{attr "@name"}}{{/commaList}}\
- | \
-
\
-
\
-';
- } else if (name === "Summary") {
- return '\
-
\
- {{md-title}}\
-\
-{{#if md-title-remainder}}\
-
{{md-title-remainder}}\
-{{/if}}\
-{{#if md-title-responsibility}}\
-
{{md-title-responsibility}}\
-{{/if}}\
-';
+ // The following PubSub code is modified from the jQuery manual:
+ // https://api.jquery.com/jQuery.Callbacks/
+ //
+ // Use as:
+ // team.queue("eventName").subscribe(function(param1, param2 ...) { ... });
+ // team.queue("eventName").publish(arg1, arg2, ...);
+
+ var queues = {};
+ var queue = function(id) {
+ if (!queues[id]) {
+ var callbacks = $.Callbacks();
+ queues[id] = {
+ publish: callbacks.fire,
+ subscribe: callbacks.add,
+ unsubscribe: callbacks.remove
+ };
}
-
- var s = "There is no default '" + name +"' template!";
- alert(s);
- return s;
+ return queues[id];
}
+ that.queue = queue;
// main
@@ -1210,8 +1143,8 @@ function team($, teamName) {
// wrapper to call team() after page load
(function (j) {
- mkws.debug = function (string) {
- if (!mkws.debug_level)
+ mkws.log = function (string) {
+ if (!mkws.log_level)
return;
if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
@@ -1219,14 +1152,14 @@ function team($, teamName) {
}
// you need to disable use strict at the top of the file!!!
- if (mkws.debug_level >= 3) {
+ if (mkws.log_level >= 3) {
console.log(arguments.callee.caller);
- } else if (mkws.debug_level >= 2) {
+ } else if (mkws.log_level >= 2) {
console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
}
console.log(string);
}
- var debug = mkws.debug;
+ var log = mkws.log;
mkws.handleNodeWithTeam = function(node, callback) {
@@ -1237,7 +1170,7 @@ function team($, teamName) {
// undefined, we don't get an error message, but this
// function and its callers, up several stack level,
// silently return. What a crock.
- mkws.debug("handleNodeWithTeam() called on node with no classes");
+ mkws.log("handleNodeWithTeam() called on node with no classes");
return;
}
var list = classes.split(/\s+/)
@@ -1263,7 +1196,7 @@ function team($, teamName) {
if ($(window).width() <= width &&
parent.hasClass("mkwsTermlistContainer1")) {
- debug("changing from wide to narrow: " + $(window).width());
+ log("changing from wide to narrow: " + $(window).width());
$(".mkwsTermlistContainer1").hide();
$(".mkwsTermlistContainer2").show();
for (var tname in mkws.teams) {
@@ -1274,7 +1207,7 @@ function team($, teamName) {
}
} else if ($(window).width() > width &&
parent.hasClass("mkwsTermlistContainer2")) {
- debug("changing from narrow to wide: " + $(window).width());
+ log("changing from narrow to wide: " + $(window).width());
$(".mkwsTermlistContainer1").show();
$(".mkwsTermlistContainer2").hide();
for (var tname in mkws.teams) {
@@ -1324,6 +1257,97 @@ function team($, teamName) {
}
+ // This function is taken from a StackOverflow answer
+ // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
+ mkws.getParameterByName = function(name) {
+ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
+ var regex = new RegExp("[\\?&]" + name + "=([^]*)"),
+ results = regex.exec(location.search);
+ return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+ }
+
+
+ mkws.defaultTemplate = function(name)
+ {
+ if (name === 'Record') {
+ return '\
+
\
+ \
+ {{translate "Title"}} | \
+ \
+ {{md-title}}\
+ {{#if md-title-remainder}}\
+ ({{md-title-remainder}})\
+ {{/if}}\
+ {{#if md-title-responsibility}}\
+ {{md-title-responsibility}}\
+ {{/if}}\
+ | \
+
\
+ {{#if md-date}}\
+ \
+ {{translate "Date"}} | \
+ {{md-date}} | \
+
\
+ {{/if}}\
+ {{#if md-author}}\
+ \
+ {{translate "Author"}} | \
+ {{md-author}} | \
+
\
+ {{/if}}\
+ {{#if md-electronic-url}}\
+ \
+ {{translate "Links"}} | \
+ \
+ {{#each md-electronic-url}}\
+ Link{{index1}}\
+ {{/each}}\
+ | \
+
\
+ {{/if}}\
+ {{#if-any location having="md-subject"}}\
+ \
+ {{translate "Subject"}} | \
+ \
+ {{#first location having="md-subject"}}\
+ {{#if md-subject}}\
+ {{#commaList md-subject}}\
+ {{this}}{{/commaList}}\
+ {{/if}}\
+ {{/first}}\
+ | \
+
\
+ {{/if-any}}\
+ \
+ {{translate "Locations"}} | \
+ \
+ {{#commaList location}}\
+ {{attr "@name"}}{{/commaList}}\
+ | \
+
\
+
\
+';
+ } 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;
+ }
+
+
function defaultMkwsConfig() {
/* default mkws config */
var config_default = {
@@ -1342,21 +1366,25 @@ function team($, teamName) {
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 */
+ log_level: 1, /* log 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;
+ // Set global log_level flag early so that log() works
+ // Fall back to old "debug_level" setting for backwards compatibility
+ var tmp = mkws_config.log_level;
+ if (typeof(tmp) === 'undefined') tmp = mkws_config.debug_level;
+
+ if (typeof(tmp) !== 'undefined') {
+ mkws.log_level = tmp;
+ } else if (typeof(config_default.log_level) !== 'undefined') {
+ mkws.log_level = config_default.log_level;
}
// make sure the mkws_config is a valid hash
if (!$.isPlainObject(mkws_config)) {
- debug("ERROR: mkws_config is not an JS object, ignore it....");
+ log("ERROR: mkws_config is not an JS object, ignore it....");
mkws_config = {};
}
@@ -1364,7 +1392,7 @@ function team($, teamName) {
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]);
+ //log("Set config: " + k + ' => ' + mkws_config[k]);
}
}
@@ -1375,11 +1403,11 @@ function team($, teamName) {
* for the site.
*/
function authenticateSession(auth_url, auth_domain, pp2_url) {
- debug("Run service proxy auth URL: " + auth_url);
+ log("Run service proxy auth URL: " + auth_url);
if (!auth_domain) {
auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
- debug("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
+ log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
}
var request = new pzHttpRequest(auth_url, function(err) {
@@ -1398,7 +1426,7 @@ function team($, teamName) {
return;
}
- debug("Service proxy auth successfully done");
+ log("Service proxy auth successfully done");
mkws.authenticated = true;
runAutoSearches();
});
@@ -1406,7 +1434,7 @@ function team($, teamName) {
function runAutoSearches() {
- debug("running auto searches");
+ log("running auto searches");
for (var teamName in mkws.teams) {
mkws.teams[teamName].runAutoSearch();
@@ -1415,11 +1443,11 @@ function team($, teamName) {
$(document).ready(function() {
- debug("on load ready");
+ log("on load ready");
defaultMkwsConfig();
if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
- debug("Reset query width: " + mkws_config.query_width);
+ log("Reset query width: " + mkws_config.query_width);
mkws_config.query_width = 50;
}
@@ -1429,7 +1457,7 @@ function team($, teamName) {
var lang = key.replace(/^language_/, "");
// Copy custom languages into list
mkws.locale_lang[lang] = mkws_config[key];
- debug("Added locally configured language '" + lang + "'");
+ log("Added locally configured language '" + lang + "'");
}
}
}
@@ -1445,7 +1473,7 @@ function team($, teamName) {
// protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
if (mkws_config.pazpar2_url.match(/^\/\//)) {
mkws_config.pazpar2_url = document.location.protocol + mkws_config.pazpar2_url;
- debug("adjust protocol independent links: " + mkws_config.pazpar2_url);
+ log("adjust protocol independent links: " + mkws_config.pazpar2_url);
}
// Backwards compatibility: set new magic class names on any
@@ -1458,7 +1486,7 @@ function team($, teamName) {
var node = $('#' + id);
if (node.attr('id')) {
node.addClass(id);
- debug("added magic class to '" + node.attr('id') + "'");
+ log("added magic class to '" + node.attr('id') + "'");
}
}
@@ -1466,26 +1494,34 @@ function team($, teamName) {
// 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 + "'");
+ log("adding AUTO team to node with class '" + this.className + "'");
$(this).addClass('mkwsTeam_AUTO');
}
});
- // Find all nodes with an class, and determine their team from
+ // Find all nodes with an MKWS class, and determine their team from
// the mkwsTeam_* class. Make all team objects.
var then = $.now();
$('[class^="mkws"],[class*=" mkws"]').each(function () {
mkws.handleNodeWithTeam(this, function(tname, type) {
if (!mkws.teams[tname]) {
mkws.teams[tname] = team(j, tname);
- debug("Made MKWS team '" + tname + "'");
+ log("Made MKWS team '" + tname + "'");
}
+ });
+ });
+ // Second pass: make the individual widget objects. This has
+ // to be done separately, and after the team-creation, since
+ // that sometimes makes new widget nodes (e.g. creating
+ // mkwsTermlists inside mkwsResults.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
+ mkws.handleNodeWithTeam(this, function(tname, type) {
var myTeam = mkws.teams[tname];
var myWidget = widget(j, myTeam, type, this);
});
});
var now = $.now();
- debug("Walking MKWS nodes took " + (now-then) + " ms");
+ log("Walking MKWS nodes took " + (now-then) + " ms");
if (mkws_config.use_service_proxy) {
authenticateSession(mkws_config.service_proxy_auth,