X-Git-Url: http://git.indexdata.com/?p=mkws-moved-to-github.git;a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=194a59503ae04cf8e9f63a8ee30cefd037bb600a;hp=dbbc2c598f0fdba3a99a358b829a4a7e3f95dac5;hb=9102c6dd9e9aa1c442bbfe5bf1baa6a3da7ffce8;hpb=c5097d60efca95cf757b497eba28171dad109e41
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index dbbc2c5..e047215 100644
--- a/tools/htdocs/mkws.js
+++ b/tools/htdocs/mkws.js
@@ -1,4 +1,7 @@
-/*! MKWS, the MasterKey Widget Set. Copyright (C) 2013-2014, Index Data */
+/*! MKWS, the MasterKey Widget Set.
+ * Copyright (C) 2013-2014 Index Data
+ * See the file LICENSE for details
+ */
"use strict"; // HTML5: disable for debug_level >= 2
@@ -62,20 +65,19 @@ Handlebars.registerHelper('commaList', function(items, options) {
});
+Handlebars.registerHelper('index1', function(obj) {
+ return obj.data.index + 1;
+});
-// Some functions are visible to be called from outside code, namely
-// generated HTML: mkws.switchView(), showDetails(), limitTarget(),
-// limitQuery(), delimitTarget(), delimitQuery(), pagerPrev(),
-// pagerNext(), showPage(). Also mkws.M() is made available for the
-// Handlebars helper 'translate'
-// Set up global mkws object. Contains a hash of team objects,
-// indexed by windowid.
+// Set up global mkws object. Contains truly global state such as SP
+// authentication, and a hash of team objects, indexed by team-name.
+//
var mkws = {
authenticated: false,
- debug_function: undefined, // will be set during initialisation
- debug_level: undefined, // will be initialised from mkws_config
+ debug_level: 1, // Will be overridden from mkws_config, but
+ // initial value allows jQuery popup to use logging.
paz: undefined, // will be set up during initialisation
teams: {},
locale_lang: {
@@ -83,6 +85,7 @@ var mkws = {
"Authors": "Autoren",
"Subjects": "Schlagwörter",
"Sources": "Daten und Quellen",
+ "source": "datenquelle",
"Termlists": "Termlisten",
"Next": "Weiter",
"Prev": "Zurück",
@@ -96,8 +99,10 @@ var mkws = {
"found": "gefunden",
"Title": "Titel",
"Author": "Autor",
+ "author": "autor",
"Date": "Datum",
"Subject": "Schlagwort",
+ "subject": "schlagwort",
"Location": "Ort",
"Records": "Datensätze",
"Targets": "Datenbanken",
@@ -109,6 +114,7 @@ var mkws = {
"Authors": "Forfattere",
"Subjects": "Emner",
"Sources": "Kilder",
+ "source": "kilder",
"Termlists": "Termlists",
"Next": "Næste",
"Prev": "Forrige",
@@ -122,8 +128,10 @@ var mkws = {
"found": "fandt",
"Title": "Title",
"Author": "Forfatter",
+ "author": "forfatter",
"Date": "Dato",
"Subject": "Emneord",
+ "subject": "emneord",
"Location": "Lokation",
"Records": "Poster",
"Targets": "Baser",
@@ -140,16 +148,91 @@ if (mkws_config == null || typeof mkws_config != 'object') {
}
-// wrapper for jQuery lib
+// Factory function for widget objects.
+function widget($, team, type, node) {
+ var that = {
+ team: team,
+ type: type,
+ node: node
+ };
+
+ var M = mkws.M;
+
+ if (type === 'Targets') {
+ promoteTargets();
+ } else if (type === 'Stat') {
+ promoteStat();
+ } else {
+ // ### Handle other types here
+ }
+
+ mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
+ return that;
+
+
+ // Functions follow for promoting the regular widget object into
+ // widgets of specific types. These could be moved outside of the
+ // widget object, or even into their own source files.
+
+ function promoteTargets() {
+ team.queue("targets").subscribe(function(data) {
+ if (node.length === 0) alert("huh?!");
+
+ var table ='
' +
+ '
' + M('Target ID') + '
' +
+ '
' + M('Hits') + '
' +
+ '
' + M('Diags') + '
' +
+ '
' + M('Records') + '
' +
+ '
' + M('State') + '
' +
+ '
';
+
+ for (var i = 0; i < data.length; i++) {
+ table += "
" + data[i].id +
+ "
" + data[i].hits +
+ "
" + data[i].diagnostic +
+ "
" + data[i].records +
+ "
" + data[i].state + "
";
+ }
+
+ table += '
';
+ var subnode = $(node).children('.mkwsBytarget');
+ subnode.html(table);
+ });
+ }
+
+
+ function promoteStat() {
+ team.queue("stat").subscribe(function(data) {
+ if (node.length === 0) alert("huh?!");
+
+ $(node).html('' + M('Status info') + '' +
+ ' -- ' +
+ '' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' +
+ ' -- ' +
+ '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '');
+ });
+ }
+}
+
+
+// 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
+// called from outside code -- specifically, from generated
+// HTML. These functions are that.switchView(), showDetails(),
+// limitTarget(), limitQuery(), delimitTarget(), delimitQuery(),
+// pagerPrev(), pagerNext(), showPage().
+//
function team($, teamName) {
var that = {};
var m_teamName = teamName;
+ that.name = function() { return m_teamName; }
var m_submitted = false;
var m_query; // initially undefined
- var m_sort = 'relevance';
+ var m_sort; // will be set below
+ var m_perpage; // will be set below
var m_filters = [];
var m_totalRec = 0;
- var m_recPerPage = 20;
var m_curPage = 1;
var m_curDetRecId = '';
var m_curDetRecData = null;
@@ -159,195 +242,155 @@ function team($, teamName) {
"last": $.now()
};
var m_paz; // will be initialised below
+ var m_template = {};
- // if (console && console.log) // disabled, will fail in IE8
- // console.log("run team(" + (teamName ? teamName : "") + ")");
-
-
- // Needs to be defined inside team() so it can see m_debug_time
- // ### member access won't work: there is only one instance of this function
- mkws.debug_function = function (string) {
- if (!mkws.debug_level)
- return;
-
- if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
- return;
- }
-
+ var debug = function (s) {
var now = $.now();
var timestamp = ((now - m_debug_time.start)/1000).toFixed(3) + " (+" + ((now - m_debug_time.last)/1000).toFixed(3) + ") "
m_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);
+ mkws.debug(m_teamName + ": " + timestamp + s);
}
- var debug = mkws.debug_function; // local alias
+
debug("start running MKWS");
m_sort = mkws_config.sort_default;
- debug("copied mkws_config.sort_default '" + mkws_config.sort_default + "' to m_sort");
-
- 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 + "'");
- }
- }
- }
-
- // 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);
- }
+ m_perpage = mkws_config.perpage_default;
debug("Create main pz2 object");
// create a parameters array and pass it to the pz2's constructor
// then register the form submit event with the pz2.search function
// autoInit is set to true on default
- m_paz = new pz2({ "onshow": my_onshow,
- "windowid": teamName,
- "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
+ m_paz = new pz2({ "windowid": teamName,
"pazpar2path": mkws_config.pazpar2_url,
- "oninit": my_oninit,
- "onstat": my_onstat,
- "onterm": my_onterm,
- "termlist": "xtargets,subject,author",
- "onbytarget": my_onbytarget,
"usesessions" : mkws_config.use_service_proxy ? false : true,
- "showResponseType": '', // or "json" (for debugging?)
- "onrecord": my_onrecord });
-
- if (!isNaN(parseInt(mkws_config.perpage_default))) {
- m_recPerPage = parseInt(mkws_config.perpage_default);
- }
+ "oninit": onInit,
+ "onbytarget": onBytarget,
+ "onstat": onStat,
+ "onterm": (mkws_config.facets.length ? onTerm : undefined),
+ "onshow": onShow,
+ "onrecord": onRecord,
+ "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
+ "termlist": mkws_config.facets.join(',')
+ });
//
// pz2.js event handlers:
//
- function my_oninit(teamName) {
- debug("init for " + teamName);
+ function onInit() {
+ debug("init");
m_paz.stat();
m_paz.bytarget();
}
- function my_onshow(data, teamName) {
- debug("show for " + teamName);
- m_totalRec = data.merged;
- // move it out
- var pager = document.getElementById("mkwsPager");
- if (pager) {
- pager.innerHTML = "";
- pager.innerHTML +='
';
- drawPager(pager);
- }
-
- // navi
- var results = $(".mkwsRecords.mkwsTeam_" + m_teamName);
-
- var html = [];
- for (var i = 0; i < data.hits.length; i++) {
- var hit = data.hits[i];
- html.push('
',
- renderSummary(hit),
- '
');
- if (hit.recid == m_curDetRecId) {
- if (m_curDetRecData)
- html.push(renderDetails(m_curDetRecData));
- }
- }
- results.html(html.join(''));
+ function onBytarget(data) {
+ debug("target");
+ queue("targets").publish(data);
}
- function renderSummary(hit)
- {
- var template = loadTemplate("Summary");
- hit._id = "mkwsRec_" + hit.recid;
- hit._onclick = "mkws.showDetails(this.id, '" + m_teamName + "');return false;"
- return template(hit);
+ function onStat(data) {
+ debug("stat");
+ queue("stat").publish(data);
}
- function my_onstat(data, teamName) {
- debug("stat for " + teamName);
- var stat = document.getElementById("mkwsStat");
- if (stat == null)
- return;
-
- stat.innerHTML = '' + M('Status info') + '' +
- ' -- ' +
- '' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' +
- ' -- ' +
- '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '';
- }
-
+ function onTerm(data) {
+ debug("term");
+ var node = findnode(".mkwsTermlists");
+ if (node.length == 0) return;
- function my_onterm(data, teamName) {
- debug("term for " + teamName);
- // no facets
+ // no facets: this should never happen
if (!mkws_config.facets || mkws_config.facets.length == 0) {
- $("#mkwsTermlists").hide();
+ alert("onTerm called even though we have no facets: " + $.toJSON(data));
+ node.hide();
return;
}
// display if we first got results
- $("#mkwsTermlists").show();
+ node.show();
var acc = [];
acc.push('
' + M('Termlists') + '
');
var facets = mkws_config.facets;
for(var i = 0; i < facets.length; i++) {
- if (facets[i] == "sources") {
- add_single_facet(acc, "Sources", data.xtargets, 16, null);
- } else if (facets[i] == "subjects") {
- add_single_facet(acc, "Subjects", data.subject, 10, "subject");
- } else if (facets[i] == "authors") {
- add_single_facet(acc, "Authors", data.author, 10, "author");
+ if (facets[i] == "xtargets") {
+ addSingleFacet(acc, "Sources", data.xtargets, 16, null);
+ } else if (facets[i] == "subject") {
+ addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
+ } else if (facets[i] == "author") {
+ addSingleFacet(acc, "Authors", data.author, 10, "author");
} else {
alert("bad facet configuration: '" + facets[i] + "'");
}
}
- var termlist = $("#mkwsTermlists");
- if (termlist)
- termlist.html(acc.join(''));
+ node.html(acc.join(''));
+ }
+
+
+ function onShow(data, teamName) {
+ debug("show");
+ m_totalRec = data.merged;
+
+ var pager = findnode(".mkwsPager");
+ if (pager.length) {
+ pager.html(drawPager(data))
+ }
+
+ var results = findnode(".mkwsRecords");
+ if (!results.length)
+ return;
+
+ var html = [];
+ for (var i = 0; i < data.hits.length; i++) {
+ var hit = data.hits[i];
+ html.push('
',
+ renderSummary(hit),
+ '
');
+ if (hit.recid == m_curDetRecId) {
+ if (m_curDetRecData)
+ html.push(renderDetails(m_curDetRecData));
+ }
+ }
+ results.html(html.join(''));
+ }
+
+
+ function onRecord(data, args, teamName) {
+ debug("record");
+ // FIXME: record is async!!
+ clearTimeout(m_paz.recordTimer);
+ // in case on_show was faster to redraw element
+ var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid);
+ if (detRecordDiv) return;
+ m_curDetRecData = data;
+ // Can't use jQuery's $('#x') syntax to find this ID, because it contains spaces.
+ var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid);
+ var html = renderDetails(m_curDetRecData);
+ $(recordDiv).append(html);
}
- function add_single_facet(acc, caption, data, max, pzIndex) {
- acc.push('
\
-';
- } 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;
- }
-
-
/*
* All the HTML stuff to render the search forms and
* result pages.
*/
- function mkws_html_all() {
- mkws_set_lang();
+ function mkwsHtmlAll() {
+ mkwsSetLang();
if (mkws_config.show_lang)
- mkws_html_lang();
+ mkwsHtmlLang();
- // For some reason, doing this programmatically results in
- // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML.
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.handleNodeWithTeam(findnode('.mkwsSearch'), function(tname) {
+ this.html('\
');
- });
});
debug("HTML records");
@@ -898,81 +785,69 @@ function team($, teamName) {
// .mkwsPager
// .mkwsNavi
// .mkwsRecords
- if ($(".mkwsResults.mkwsTeam_" + m_teamName).length) {
- $(".mkwsResults.mkwsTeam_" + m_teamName).html('\
+ if (findnode(".mkwsResults").length) {
+ findnode(".mkwsResults").html('\
\
\
-
\
- \
+
\
+ \
\
-
\
- \
- \
- \
- \
+
\
+ \
+ \
+ \
+ \
\
\
\
\
- \
+ \
\
\
');
}
- if ($("#mkwsRanking").length) {
+ var node = findnode(".mkwsRanking");
+ if (node.length) {
var ranking_data = '';
- ranking_data += '';
- $("#mkwsRanking").html(ranking_data);
+ node.html(ranking_data);
}
- mkws_html_switch();
+ mkwsHtmlSwitch();
- 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() });
- }
-
- domReady();
+ 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() { $("#mkwsTermlists").hide(); });
- var motd = document.getElementById("mkwsMOTD");
- var container = document.getElementById("mkwsMOTDContainer");
- if (motd && container) {
+ $(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.parentNode.removeChild(motd);
- container.appendChild(motd);
+ 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;
+ function mkwsSetLang() {
+ var lang = mkws.getParameterByName("lang") || mkws_config.lang;
if (!lang || !mkws.locale_lang[lang]) {
mkws_config.lang = ""
} else {
@@ -984,66 +859,8 @@ function team($, teamName) {
}
- function mkws_html_switch() {
- debug("HTML switch for team " + m_teamName);
-
- var node = $(".mkwsSwitch.mkwsTeam_" + m_teamName);
- node.append($('' + M('Records') + ''));
- node.append($("", { text: " | " }));
- node.append($('' + M('Targets') + ''));
-
- debug("HTML targets");
- var node = $(".mkwsTargets.mkwsTeam_" + m_teamName);
- node.html('\
-
\
- No information available yet.\
-
');
- node.css("display", "none");
- }
-
-
- function mkws_html_sort() {
- debug("HTML sort, m_sort = '" + m_sort + "'");
- var sort_html = '';
-
- return sort_html;
- }
-
-
- function mkws_html_perpage() {
- debug("HTML perpage");
- var perpage_html = '';
-
- return perpage_html;
- }
-
-
/* create locale language menu */
- function mkws_html_lang() {
+ function mkwsHtmlLang() {
var lang_default = "en";
var lang = mkws_config.lang || lang_default;
var list = [];
@@ -1081,296 +898,426 @@ function team($, teamName) {
}
}
- $("#mkwsLang").html(data);
+ findnode(".mkwsLang").html(data);
}
- function mkws_resize_page () {
- var list = ["mkwsSwitch"];
+ function mkwsHtmlSort() {
+ debug("HTML sort, m_sort = '" + m_sort + "'");
+ var sort_html = '';
- return mkws.locale_lang[lang][word] || word;
+ return sort_html;
}
- mkws.M = M; // so the Handlebars helper can use it
- // main
- (function() {
- try {
- mkws_html_all()
- }
+ function mkwsHtmlPerpage() {
+ debug("HTML perpage, m_perpage = " + m_perpage);
+ var perpage_html = '';
- // Bizarrely, 'that' is just an empty hash. All its state is in
- // the closure variables defined earlier in this function.
- return that;
-};
+ return perpage_html;
+ }
-/*
- * implement jQuery plugin $.pazpar2({})
- */
-function _mkws_jquery_plugin ($) {
- var debug_level = 1;
+ function mkwsHtmlSwitch() {
+ debug("HTML switch for team " + m_teamName);
- function debug (string) {
- if (!debug_level)
- return;
+ var node = findnode(".mkwsSwitch");
+ node.append($('' + M('Records') + ''));
+ node.append($("", { text: " | " }));
+ node.append($('' + M('Targets') + ''));
- if (typeof console === "undefined" || typeof console.log === "undefined")
+ 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;
- console.log("jquery.pazpar2: " + string);
+ if (query.match(/^!param!/)) {
+ var param = query.replace(/^!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");
+ }
+ } 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);
}
- 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";
+ // 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;
- debug("popup height: " + height + ", width: " + width);
+ if (!lang || !mkws.locale_lang[lang])
+ return word;
- // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
- //
- if (!$.ui) {
- debug("Error: jquery-ui.js is missing, did you include it after jQuery core in the HTML file?");
- return;
- }
+ return mkws.locale_lang[lang][word] || word;
+ }
+ mkws.M = M; // so the Handlebars helper can use it
- $(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");
- });
- };
+ // 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;
- $.extend({
-
- // 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') {
- debug("jquery plugin layout: div");
- document.write(div);
- } else if (config && config.layout == 'popup') {
- debug("jquery plugin layout: popup with id: " + id_popup);
- document.write(popup);
- $(document).ready(function() { init_popup(config); });
- } else {
- debug("jquery plugin layout: table");
- document.write(table);
- }
- }
- });
-};
+ selector = selector.split(',').map(function(s) {
+ return s + '.mkwsTeam_' + teamName;
+ }).join(',');
+ return $(selector);
+ }
-// wrapper to call team() after page load
-(function (j) {
- function log(s) {
- if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
- return;
- }
- console.log(s);
+
+ 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);
}
- // enable before page load, so we could call it before mkws() runs
- _mkws_jquery_plugin(j);
- $(document).ready(function() {
- log("on load ready");
- default_mkws_config();
+ function renderDetails(data, marker)
+ {
+ var template = loadTemplate("Record");
+ var details = template(data);
+ return '
' + details + '
';
+ }
- // 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);
- log("added magic class to '" + node.attr('id') + "'");
+
+ 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");
}
- }
- // For all MKWS-classed nodes that don't have a team
- // specified, set the team to AUTO.
- $('div[class^="mkws"],div[class*=" mkws"]').each(function () {
- if (!this.className.match(/mkwsTeam_/)) {
- log("adding AUTO team to node with class '" + this.className + "'");
- $(this).addClass('mkwsTeam_AUTO');
+ var source = node.html();
+ if (!source) {
+ source = mkws.defaultTemplate(name);
}
- });
- // Find all nodes with class (NOT id) mkwsRecords, and
- // determine their team from the mkwsTeam_* class. So:
- //
- $('.mkwsSearch, .mkwsResults, .mkwsRecords, .mkwsTermlists').each(function () {
- var node = this;
- mkws.handle_node_with_team(node, function(tname) {
- if (mkws.teams[tname]) {
- log("MKWS team '" + tname + "' already exists, skipping");
- } else {
- mkws.teams[tname] = team(j, tname);
- log("Made MKWS team '" + tname + "'");
- }
- });
- });
+ template = Handlebars.compile(source);
+ debug("compiled template '" + name + "'");
+ m_template[name] = template;
+ }
- 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();
+ return template;
+ }
+
+
+ // 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 {
+ mkwsHtmlAll()
+ }
+
+ catch (e) {
+ mkws_config.error = e.message;
+ // alert(e.message);
+ }
+ })();
+
+ // Bizarrely, 'that' is just an empty hash. All its state is in
+ // the closure variables defined earlier in this function.
+ return that;
+};
+
+
+// wrapper to call team() after page load
+(function (j) {
+ mkws.debug = function (string) {
+ if (!mkws.debug_level)
+ return;
+ if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
+ return;
+ }
- mkws.handle_node_with_team = function(node, callback) {
- var classes = node.className;
+ // 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;
+
+
+ mkws.handleNodeWithTeam = function(node, callback) {
+ // 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("handleNodeWithTeam() 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);
}
- mkws.showDetails = function (prefixRecId, tname) {
+ mkws.resizePage = 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();
+ }
+ }
+ }
+ };
+
+
+ mkws.switchView = function(tname, view) {
+ mkws.teams[tname].switchView(view);
+ }
+
+ mkws.showDetails = function (tname, prefixRecId) {
mkws.teams[tname].showDetails(prefixRecId);
}
+ mkws.limitTarget = function (tname, id, name) {
+ mkws.teams[tname].limitTarget(id, name);
+ }
+
+ mkws.limitQuery = function (tname, field, value) {
+ mkws.teams[tname].limitQuery(field, value);
+ }
+
+ mkws.delimitTarget = function (tname, id) {
+ mkws.teams[tname].delimitTarget(id);
+ }
+
+ mkws.delimitQuery = function (tname, field, value) {
+ mkws.teams[tname].delimitQuery(field, value);
+ }
+
+ mkws.showPage = function (tname, pageNum) {
+ mkws.teams[tname].showPage(pageNum);
+ }
+
+ mkws.pagerPrev = function (tname) {
+ mkws.teams[tname].pagerPrev();
+ }
+
+ mkws.pagerNext = function (tname) {
+ mkws.teams[tname].pagerNext();
+ }
+
+
+ // 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 '\
+