\
\
@@ -976,135 +853,55 @@ function team($, teamName) {
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() });
- }
-
- var node;
- node = findnode('.mkwsSearchForm');
- if (node.length)
- node.submit(onFormSubmitEventHandler);
- node = findnode('.mkwsSort');
- if (node.length)
- node.change(onSelectDdChange);
- node = findnode('.mkwsPerpage');
- if (node.length)
- node.change(onSelectDdChange);
-
- // on first page, hide the termlist
- $(document).ready(function() { findnode(".mkwsTermlists").hide(); });
- var motd = findnode(".mkwsMOTD");
- var container = findnode(".mkwsMOTDContainer");
- if (motd.length && container.length) {
- // Move the MOTD from the provided element down into the container
- motd.appendTo(container);
- }
- }
-
-
- // 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;
- }
-
-
- 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;
- }
-
- debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
- return mkws_config.lang;
- }
-
-
- function mkws_html_switch() {
- debug("HTML switch for team " + m_teamName);
-
- var node = findnode(".mkwsSwitch");
- node.append($('' + M('Records') + ' '));
- node.append($(" ", { text: " | " }));
- node.append($('' + M('Targets') + ' '));
-
- debug("HTML targets");
- var node = findnode(".mkwsTargets");
- node.html('\
-\
- No information available yet.\
-
');
- node.css("display", "none");
- }
-
-
- function mkws_html_sort() {
- debug("HTML sort, m_sort = '" + m_sort + "'");
- var sort_html = '';
-
- 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 += '' + M(val) + ' ';
+ ranking_data += '';
+
+ node.html(ranking_data);
}
- sort_html += ' ';
- return sort_html;
- }
+ mkwsHtmlSwitch();
+ var node;
+ node = findnode('.mkwsSearchForm');
+ if (node.length)
+ node.submit(onFormSubmitEventHandler);
+ node = findnode('.mkwsSort');
+ if (node.length)
+ node.change(onSelectDdChange);
+ node = findnode('.mkwsPerpage');
+ if (node.length)
+ node.change(onSelectDdChange);
- function mkws_html_perpage() {
- debug("HTML perpage, m_perpage = " + m_perpage);
- var perpage_html = '';
+ // on first page, hide the termlist
+ $(document).ready(function() { findnode(".mkwsTermlists").hide(); });
+ var motd = findnode(".mkwsMOTD");
+ var container = findnode(".mkwsMOTDContainer");
+ if (motd.length && container.length) {
+ // Move the MOTD from the provided element down into the container
+ motd.appendTo(container);
+ }
+ }
- for(var i = 0; i < mkws_config.perpage_options.length; i++) {
- var key = mkws_config.perpage_options[i];
- perpage_html += '' + key + ' ';
+ function mkwsSetLang() {
+ var lang = mkws.getParameterByName("lang") || mkws_config.lang;
+ if (!lang || !mkws.locale_lang[lang]) {
+ mkws_config.lang = ""
+ } else {
+ mkws_config.lang = lang;
}
- perpage_html += ' ';
- return perpage_html;
+ debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
+ return mkws_config.lang;
}
/* create locale language menu */
- function mkws_html_lang() {
+ function mkwsHtmlLang() {
var lang_default = "en";
var lang = mkws_config.lang || lang_default;
var list = [];
@@ -1146,16 +943,73 @@ function team($, teamName) {
}
- that.run_auto_search = function() {
- // ### should check mkwsTermlist as well, for facet-only teams
- var node = findnode('.mkwsRecords');
+ function mkwsHtmlSort() {
+ debug("HTML sort, m_sortOrder = '" + m_sortOrder + "'");
+ var sort_html = '';
+
+ 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 += '' + M(val) + ' ';
+ }
+ sort_html += ' ';
+
+ return sort_html;
+ }
+
+
+ function mkwsHtmlPerpage() {
+ debug("HTML perpage, m_perpage = " + m_perpage);
+ var perpage_html = '';
+
+ for(var i = 0; i < mkws_config.perpage_options.length; i++) {
+ var key = mkws_config.perpage_options[i];
+
+ perpage_html += '' + key + ' ';
+ }
+ perpage_html += ' ';
+
+ return perpage_html;
+ }
+
+
+ function mkwsHtmlSwitch() {
+ debug("HTML switch for team " + m_teamName);
+
+ var node = findnode(".mkwsSwitch");
+ node.append($('' + M('Records') + ' '));
+ node.append($(" ", { text: " | " }));
+ node.append($('' + M('Targets') + ' '));
+
+ debug("HTML targets");
+ var node = findnode(".mkwsTargets");
+ node.html('\
+\
+ No information available yet.\
+
');
+ node.css("display", "none");
+ }
+
+
+ that.runAutoSearch = function() {
+ 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);
+ query = mkws.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");
@@ -1172,29 +1026,22 @@ function team($, teamName) {
debug("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);
- this.newSearch(query, sort, targets, m_teamName);
+ newSearch(query, sortOrder, targets);
}
- // 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;
@@ -1206,10 +1053,82 @@ function team($, teamName) {
mkws.M = M; // so the Handlebars helper can use it
+ // Finds the node of the specified class within the current team
+ // Multiple OR-clauses separated by commas are handled
+ // More complex cases may not work
+ //
+ function findnode(selector, teamName) {
+ teamName = teamName || m_teamName;
+
+ selector = $.map(selector.split(','), function(s, i) {
+ return s + '.mkwsTeam_' + teamName;
+ }).join(',');
+
+ return $(selector);
+ }
+
+
+ function renderDetails(data, marker)
+ {
+ var template = loadTemplate("Record");
+ var details = template(data);
+ return '' + details + '
';
+ }
+
+
+ function loadTemplate(name)
+ {
+ var template = m_template[name];
+
+ if (template === undefined) {
+ // Fall back to generic template if there is no team-specific one
+ var node = findnode(".mkwsTemplate_" + name);
+ if (!node.length) {
+ node = findnode(".mkwsTemplate_" + name, "ALL");
+ }
+
+ var source = node.html();
+ if (!source) {
+ source = mkws.defaultTemplate(name);
+ }
+
+ template = Handlebars.compile(source);
+ debug("compiled template '" + name + "'");
+ m_template[name] = template;
+ }
+
+ return template;
+ }
+ that.loadTemplate = loadTemplate;
+
+
+ // 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
+ };
+ }
+ return queues[id];
+ }
+
+ that.queue = queue;
+
+
// main
(function() {
try {
- mkws_html_all()
+ mkwsHtmlAll()
}
catch (e) {
@@ -1245,7 +1164,7 @@ function team($, teamName) {
var debug = mkws.debug;
- mkws.handle_node_with_team = function(node, callback) {
+ mkws.handleNodeWithTeam = function(node, callback) {
// First branch for DOM objects; second branch for jQuery objects
var classes = node.className || node.attr('class');
if (!classes) {
@@ -1253,7 +1172,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("handle_node_with_team() called on node with no classes");
+ mkws.debug("handleNodeWithTeam() called on node with no classes");
return;
}
var list = classes.split(/\s+/)
@@ -1271,7 +1190,7 @@ function team($, teamName) {
}
- mkws.resize_page = function () {
+ mkws.resizePage = function () {
var list = ["mkwsSwitch", "mkwsLang"];
var width = mkws_config.responsive_design_width;
@@ -1340,7 +1259,98 @@ function team($, teamName) {
}
- function default_mkws_config() {
+ // 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 = {
use_service_proxy: true,
@@ -1390,7 +1400,7 @@ function team($, teamName) {
* The username/password is configured in the apache config file
* for the site.
*/
- function authenticate_session(auth_url, auth_domain, pp2_url) {
+ function authenticateSession(auth_url, auth_domain, pp2_url) {
debug("Run service proxy auth URL: " + auth_url);
if (!auth_domain) {
@@ -1416,23 +1426,53 @@ function team($, teamName) {
debug("Service proxy auth successfully done");
mkws.authenticated = true;
- run_auto_searches();
+ runAutoSearches();
});
}
- function run_auto_searches() {
+ function runAutoSearches() {
debug("running auto searches");
for (var teamName in mkws.teams) {
- mkws.teams[teamName].run_auto_search();
+ mkws.teams[teamName].runAutoSearch();
}
}
$(document).ready(function() {
debug("on load ready");
- default_mkws_config();
+ defaultMkwsConfig();
+
+ 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];
+ debug("Added locally configured language '" + lang + "'");
+ }
+ }
+ }
+
+ 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.resizePage() });
+ // initial check after page load
+ $(document).ready(function() { mkws.resizePage() });
+ }
+
+ // 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);
+ }
// Backwards compatibility: set new magic class names on any
// elements that have the old magic IDs.
@@ -1457,15 +1497,23 @@ function team($, teamName) {
}
});
- // 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.handle_node_with_team(this, function(tname, type) {
+ mkws.handleNodeWithTeam(this, function(tname, type) {
if (!mkws.teams[tname]) {
mkws.teams[tname] = team(j, tname);
debug("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 instead 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);
});
@@ -1474,12 +1522,12 @@ function team($, teamName) {
debug("Walking MKWS nodes took " + (now-then) + " ms");
if (mkws_config.use_service_proxy) {
- authenticate_session(mkws_config.service_proxy_auth,
- mkws_config.service_proxy_auth_domain,
- mkws_config.pazpar2_url);
+ authenticateSession(mkws_config.service_proxy_auth,
+ mkws_config.service_proxy_auth_domain,
+ mkws_config.pazpar2_url);
} else {
// raw pp2
- run_auto_searches();
+ runAutoSearches();
}
});
})(jQuery);