X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=e04721562aa1679350e98ec9441070d7d3487636;hb=2fab8dcd05783b47f38ec46188e612187e84223d;hp=2484229c192c98d2ece24c55325de40e6b007323;hpb=72516b553876f01f55df93e0001fdfc4fc51b0aa;p=mkws-moved-to-github.git
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index 2484229..e047215 100644
--- a/tools/htdocs/mkws.js
+++ b/tools/htdocs/mkws.js
@@ -142,29 +142,6 @@ var mkws = {
};
-// 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 = {};
@@ -179,10 +156,62 @@ function widget($, team, type, node) {
node: node
};
- // ### More to do here, surely: e.g. wiring into the team
- mkws.debug("made widget(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 ='
');
acc.push('
' + M(caption) + '
');
@@ -402,47 +411,60 @@ function team($, teamName) {
}
- function onRecord(data, args, teamName) {
- debug("record: teamName=" + teamName + ", m_teamName=" + m_teamName);
- // 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;
- var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid);
- var html = renderDetails(m_curDetRecData);
- recordDiv.innerHTML += html;
- }
+ function drawPager (data)
+ {
+ var s = '
' + M('Displaying') + ': '
+ + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
+ ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
+ + data.total + ')
';
+ //client indexes pages from 1 but pz2 from 0
+ var onsides = 6;
+ var pages = Math.ceil(m_totalRec / m_perpage);
- function onBytarget(data, teamName) {
- debug("target");
- var targetDiv = findnode('.mkwsBytarget');
- if (!targetDiv) {
- return;
- }
+ var firstClkbl = (m_curPage - onsides > 0)
+ ? m_curPage - onsides
+ : 1;
+
+ var lastClkbl = firstClkbl + 2*onsides < pages
+ ? firstClkbl + 2*onsides
+ : pages;
+
+ var prev = '
<< ' + M('Prev') + ' | ';
+ if (m_curPage > 1)
+ prev = '
'
+ +'<< ' + M('Prev') + ' | ';
+
+ var middle = '';
+ for(var i = firstClkbl; i <= lastClkbl; i++) {
+ var numLabel = i;
+ if(i == m_curPage)
+ numLabel = '
' + i + '';
- 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 + " |
";
+ middle += ' '
+ + numLabel + ' ';
}
- table += '
';
- targetDiv.html(table);
+ var next = '
| ' + M('Next') + ' >>';
+ if (pages - m_curPage > 0)
+ next = '
| '
+ + M('Next') + ' >>';
+
+ var predots = '';
+ if (firstClkbl > 1)
+ predots = '...';
+
+ var postdots = '';
+ if (lastClkbl < pages)
+ postdots = '...';
+
+ s += '
'
+ + prev + predots + middle + postdots + next + '
';
+
+ return s;
}
+
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -485,6 +507,31 @@ function team($, teamName) {
}
+ function redrawNavi ()
+ {
+ var navi = findnode('.mkwsNavi');
+ if (!navi) return;
+
+ var text = "";
+ for (var i in m_filters) {
+ if (text) {
+ text += " | ";
+ }
+ var filter = m_filters[i];
+ if (filter.id) {
+ text += M('source') + ':
' + filter.name + '';
+ } else {
+ text += M(filter.field) + ':
' + filter.value + '';
+ }
+ }
+
+ navi.html(text);
+ }
+
+
function resetPage()
{
m_curPage = 1;
@@ -492,6 +539,21 @@ function team($, teamName) {
}
+ function loadSelect ()
+ {
+ var node = findnode('.mkwsSort');
+ if (node.length && node.val() != m_sort) {
+ debug("changing m_sort from " + m_sort + " to " + node.val());
+ m_sort = node.val();
+ }
+ node = findnode('.mkwsPerpage');
+ if (node.length && node.val() != m_perpage) {
+ debug("changing m_perpage from " + m_perpage + " to " + node.val());
+ m_perpage = node.val();
+ }
+ }
+
+
function triggerSearch (query, sort, targets)
{
var pp2filter = "";
@@ -539,21 +601,6 @@ function team($, teamName) {
}
- function loadSelect ()
- {
- var node = findnode('.mkwsSort');
- if (node.length && node.val() != m_sort) {
- debug("changing m_sort from " + m_sort + " to " + node.val());
- m_sort = node.val();
- }
- node = findnode('.mkwsPerpage');
- if (node.length && node.val() != m_perpage) {
- debug("changing m_perpage from " + m_perpage + " to " + node.val());
- m_perpage = node.val();
- }
- }
-
-
// limit by target functions
that.limitTarget = function (id, name)
{
@@ -628,85 +675,6 @@ function team($, teamName) {
}
- function redrawNavi ()
- {
- var navi = findnode('.mkwsNavi');
- if (!navi) return;
-
- var text = "";
- for (var i in m_filters) {
- if (text) {
- text += " | ";
- }
- var filter = m_filters[i];
- if (filter.id) {
- text += M('source') + ':
' + filter.name + '';
- } else {
- text += M(filter.field) + ':
' + filter.value + '';
- }
- }
-
- navi.html(text);
- }
-
-
- function drawPager (data)
- {
- var s = '
' + M('Displaying') + ': '
- + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
- ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
- + data.total + ')
';
-
- //client indexes pages from 1 but pz2 from 0
- var onsides = 6;
- var pages = Math.ceil(m_totalRec / m_perpage);
-
- var firstClkbl = (m_curPage - onsides > 0)
- ? m_curPage - onsides
- : 1;
-
- var lastClkbl = firstClkbl + 2*onsides < pages
- ? firstClkbl + 2*onsides
- : pages;
-
- var prev = '
<< ' + M('Prev') + ' | ';
- if (m_curPage > 1)
- prev = '
'
- +'<< ' + M('Prev') + ' | ';
-
- var middle = '';
- for(var i = firstClkbl; i <= lastClkbl; i++) {
- var numLabel = i;
- if(i == m_curPage)
- numLabel = '
' + i + '';
-
- middle += '
'
- + numLabel + ' ';
- }
-
- var next = '
| ' + M('Next') + ' >>';
- if (pages - m_curPage > 0)
- next = '
| '
- + M('Next') + ' >>';
-
- var predots = '';
- if (firstClkbl > 1)
- predots = '...';
-
- var postdots = '';
- if (lastClkbl < pages)
- postdots = '...';
-
- s += '
'
- + prev + predots + middle + postdots + next + '
';
-
- return s;
- }
-
-
that.showPage = function (pageNum)
{
m_curPage = pageNum;
@@ -789,120 +757,6 @@ function team($, teamName) {
}
- 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 = defaultTemplate(name);
- }
-
- template = Handlebars.compile(source);
- debug("compiled template '" + name + "'");
- m_template[name] = template;
- }
-
- return template;
- }
-
-
- 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}}\
-';
- }
-
- var s = "There is no default '" + name +"' template!";
- alert(s);
- return s;
- }
-
-
/*
* All the HTML stuff to render the search forms and
* result pages.
@@ -992,47 +846,59 @@ function team($, teamName) {
}
- // 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 mkwsSetLang() {
+ var lang = mkws.getParameterByName("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 mkwsSetLang() {
- var lang = parseQuerystring().lang || mkws_config.lang;
- if (!lang || !mkws.locale_lang[lang]) {
- mkws_config.lang = ""
- } else {
- mkws_config.lang = lang;
+ /* create locale language menu */
+ function mkwsHtmlLang() {
+ 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);
}
- debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
- return mkws_config.lang;
- }
+ // add english link
+ if (lang_options.length == 0 || toBeIncluded[lang_default])
+ list.push(lang_default);
+ debug("Language menu for: " + list.join(", "));
- function mkwsHtmlSwitch() {
- debug("HTML switch for team " + m_teamName);
+ /* the HTML part */
+ var data = "";
+ for(var i = 0; i < list.length; i++) {
+ var l = list[i];
- var node = findnode(".mkwsSwitch");
- node.append($('
' + M('Records') + ''));
- node.append($("
", { text: " | " }));
- node.append($('
' + M('Targets') + ''));
+ if (data)
+ data += ' | ';
- debug("HTML targets");
- var node = findnode(".mkwsTargets");
- node.html('\
-
\
- No information available yet.\
-
');
- node.css("display", "none");
+ if (lang == l) {
+ data += '
' + l + ' ';
+ } else {
+ data += '
' + l + ' '
+ }
+ }
+
+ findnode(".mkwsLang").html(data);
}
@@ -1076,59 +942,33 @@ function team($, teamName) {
}
- /* create locale language menu */
- function mkwsHtmlLang() {
- 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 += ' | ';
+ function mkwsHtmlSwitch() {
+ debug("HTML switch for team " + m_teamName);
- if (lang == l) {
- data += '
' + l + ' ';
- } else {
- data += '
' + l + ' '
- }
- }
+ var node = findnode(".mkwsSwitch");
+ node.append($('
' + M('Records') + ''));
+ node.append($("
", { text: " | " }));
+ node.append($('
' + M('Targets') + ''));
- findnode(".mkwsLang").html(data);
+ debug("HTML targets");
+ var node = findnode(".mkwsTargets");
+ node.html('\
+
\
+ No information available yet.\
+
');
+ node.css("display", "none");
}
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);
+ 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");
@@ -1156,18 +996,11 @@ 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
- // ### 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;
@@ -1179,6 +1012,86 @@ 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 = selector.split(',').map(function(s) {
+ return s + '.mkwsTeam_' + teamName;
+ }).join(',');
+
+ return $(selector);
+ }
+
+
+ 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 + '
';
+ }
+
+
+ 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;
+ }
+
+
+ // 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 {
@@ -1313,6 +1226,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 = {
@@ -1431,6 +1435,12 @@ function team($, teamName) {
$(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.
var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",