Handlebars.registerHelper('index1', function(obj) {
- mkws.debug($.toJSON(obj));
return obj.data.index + 1;
});
};
+// The following PubSub code is modified from the jQuery manual:
+// https://api.jquery.com/jQuery.Callbacks/
+//
+// Use as:
+// mkws.queue("eventName").subscribe(function(param1, param2 ...) { ... });
+// mkws.queue("eventName").publish(arg1, arg2, ...);
+
+(function() {
+ var queues = {};
+ mkws.queue = function(id) {
+ if (!queues[id]) {
+ var callbacks = $.Callbacks();
+ queues[id] = {
+ publish: callbacks.fire,
+ subscribe: callbacks.add,
+ unsubscribe: callbacks.remove
+ };
+ }
+ return queues[id];
+ }
+}());
+
+
// Define empty mkws_config for simple applications that don't define it.
if (mkws_config == null || typeof mkws_config != 'object') {
var mkws_config = {};
}
+// Factory function for widget objects.
+function widget($, team, type, node) {
+ var that = {
+ team: team,
+ type: type,
+ node: node
+ };
+
+ // ### More to do here, surely: e.g. wiring into the team
+ mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
+
+ return that;
+}
+
+
// Factory function for team objects. As much as possible, this uses
// only member variables (prefixed "m_") and inner functions with
// private scope. Some functions are visibl as member-functions to be
"last": $.now()
};
var m_paz; // will be initialised below
+ var m_template = {};
var debug = function (s) {
}
debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
+ // We can use: params.torusquery = "udb=NAME"
+ // Note: that won't work when running against raw pazpar2
m_paz.search(m_query, m_perpage, m_sort, pp2filter, undefined, params);
}
function loadTemplate(name)
{
- var template = mkws['template' + name];
+ var template = m_template[name];
if (template === undefined) {
- var source = $("#mkwsTemplate" + name).html();
+ // Fall back to generic template if there is no team-specific one
+ var node = $(".mkwsTemplate_" + name + ".mkwsTeam_" + m_teamName)
+ if (!node.length) {
+ node = $(".mkwsTemplate_" + name + ".mkwsTeam_ALL")
+ }
+
+ var source = node.html();
if (!source) {
source = defaultTemplate(name);
}
template = Handlebars.compile(source);
debug("compiled template '" + name + "'");
- mkws['template' + name] = template;
+ m_template[name] = template;
}
return template;
<td>\
{{#first location having="md-subject"}}\
{{#if md-subject}}\
- {{md-subject}}\
+ {{#commaList md-subject}}\
+ {{this}}{{/commaList}}\
{{/if}}\
{{/first}}\
</td>\
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('\
+ mkws.handle_node_with_team($('.mkwsSearch.mkwsTeam_' + m_teamName),
+ function(tname) {
+ this.html('\
<form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
<input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + mkws_config.query_width + '" />\
<input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
</form>');
- });
});
debug("HTML records");
$(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);
- });
- });
-
+ var node;
+ node = $('.mkwsSearchForm.mkwsTeam_' + m_teamName);
+ if (node.length)
+ node.submit(onFormSubmitEventHandler);
node = $('.mkwsSort.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);
+ 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);
}
+ 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);
+
+ this.newSearch(query, sort, targets, m_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
+ 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 */
function M(word) {
var lang = mkws_config.lang;
mkws.handle_node_with_team = function(node, callback) {
- var classes = node.className;
+ // First branch for DOM objects; second branch for jQuery objects
+ var classes = node.className || node.attr('class');
+ if (!classes) {
+ // For some reason, if we try to proceed when classes is
+ // 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");
+ return;
+ }
var list = classes.split(/\s+/)
- var tname;
+ var teamName, type;
+
for (var i = 0; i < list.length; i++) {
var cname = list[i];
if (cname.match(/^mkwsTeam_/)) {
- tname = cname.replace(/^mkwsTeam_/, '');
+ teamName = cname.replace(/^mkwsTeam_/, '');
+ } else if (cname.match(/^mkws/)) {
+ type = cname.replace(/^mkws/, '');
}
}
- callback(tname);
+ callback.call(node, teamName, type);
}
debug("running auto searches");
for (var teamName in mkws.teams) {
- // ### should check mkwsTermlist as well, for facet-only teams
- var node = $('.mkwsRecords.mkwsTeam_' + teamName);
- var query = node.attr('autosearch');
-
- 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("teamName '" + teamName + "', node=" + node + ", class='" + node.className + "', query=" + query);
-
- if (query) {
- var sort = node.attr('sort');
- var targets = node.attr('targets');
- var s = "running auto search: '" + query + "'";
- if (teamName) s += " [teamName '" + teamName + "']";
- if (sort) s += " sorted by '" + sort + "'";
- if (targets) s += " in targets '" + targets + "'";
- debug(s);
- var team = mkws.teams[teamName];
- debug($.toJSON(team));
- team.newSearch(query, sort, targets, teamName);
- }
+ mkws.teams[teamName].run_auto_search();
}
}
- // 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, " "));
- }
-
-
$(document).ready(function() {
debug("on load ready");
default_mkws_config();
// elements that have the old magic IDs.
var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
"Results", "Records", "Targets", "Ranking",
- "Termlists", "Stat" ];
+ "Termlists", "Stat", "MOTD" ];
for (var i = 0; i < ids.length; i++) {
var id = 'mkws' + ids[i];
var node = $('#' + id);
// Find all nodes with an class, and determine their team from
// the mkwsTeam_* class. Make all team objects.
+ var then = $.now();
$('[class^="mkws"],[class*=" mkws"]').each(function () {
- var node = this;
- mkws.handle_node_with_team(node, function(tname) {
+ mkws.handle_node_with_team(this, function(tname, type) {
if (!mkws.teams[tname]) {
mkws.teams[tname] = team(j, tname);
debug("Made MKWS team '" + tname + "'");
}
+ var myTeam = mkws.teams[tname];
+ var myWidget = widget(j, myTeam, type, this);
});
});
+ var now = $.now();
+ debug("Walking MKWS nodes took " + (now-then) + " ms");
if (mkws_config.use_service_proxy) {
authenticate_session(mkws_config.service_proxy_auth,