X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=2896c44d5b7c926a20a40015ff964eafbb3813df;hb=0071d64575e2c1850fd18d536d504cf40fcd1e4a;hp=604d5090c0811fe6d49368ca1987341b83ca5984;hpb=fd71e199523e8cbfea692732cfe9ff361a050b35;p=mkws-moved-to-github.git
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index 604d509..2896c44 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
@@ -10,7 +13,6 @@ Handlebars.registerHelper('json', function(obj) {
Handlebars.registerHelper('translate', function(s) {
- debug("translating '" + s + "'");
return mkws.M(s);
});
@@ -142,9 +144,9 @@ if (mkws_config == null || typeof mkws_config != 'object') {
// wrapper for jQuery lib
-function _make_mkws_team($, teamName) {
+function team($, teamName) {
var that = {};
- var m_termName = teamName;
+ var m_teamName = teamName;
var m_submitted = false;
var m_query; // initially undefined
var m_sort = 'relevance';
@@ -163,10 +165,11 @@ function _make_mkws_team($, teamName) {
// if (console && console.log) // disabled, will fail in IE8
- // console.log("run _make_mkws_team(" + (teamName ? teamName : "") + ")");
+ // console.log("run team(" + (teamName ? teamName : "") + ")");
- // Needs to be defined inside _make_mkws_team() so it can see m_debug_time
+ // 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;
@@ -225,8 +228,8 @@ function _make_mkws_team($, teamName) {
"pazpar2path": mkws_config.pazpar2_url,
"oninit": my_oninit,
"onstat": my_onstat,
- "onterm": my_onterm,
- "termlist": "xtargets,subject,author",
+ "onterm": (mkws_config.facets.length ? my_onterm : undefined),
+ "termlist": mkws_config.facets.join(','),
"onbytarget": my_onbytarget,
"usesessions" : mkws_config.use_service_proxy ? false : true,
"showResponseType": '', // or "json" (for debugging?)
@@ -262,7 +265,7 @@ function _make_mkws_team($, teamName) {
}
// navi
- var results = $("#mkwsRecords");
+ var results = $(".mkwsRecords.mkwsTeam_" + m_teamName);
var html = [];
for (var i = 0; i < data.hits.length; i++) {
@@ -271,7 +274,8 @@ function _make_mkws_team($, teamName) {
renderSummary(hit),
'');
if (hit.recid == m_curDetRecId) {
- html.push(renderDetails(m_curDetRecData));
+ if (m_curDetRecData)
+ html.push(renderDetails(m_curDetRecData));
}
}
results.html(html.join(''));
@@ -282,55 +286,57 @@ function _make_mkws_team($, teamName) {
{
var template = loadTemplate("Summary");
hit._id = "mkwsRec_" + hit.recid;
- hit._onclick = "mkws.showDetails(this.id);return false;"
+ hit._onclick = "mkws.showDetails(this.id, '" + m_teamName + "');return false;"
return template(hit);
}
function my_onstat(data, teamName) {
debug("stat for " + teamName);
- var stat = document.getElementById("mkwsStat");
- if (stat == null)
+ var stat = $('.mkwsStat.mkwsTeam_' + teamName);
+ if (stat.length === 0)
return;
- stat.innerHTML = '' + M('Status info') + '' +
+ stat.html('' + M('Status info') + '' +
' -- ' +
'' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' +
' -- ' +
- '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '';
+ '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '');
}
function my_onterm(data, teamName) {
debug("term for " + teamName);
- // no facets
+ var node = $(".mkwsTermlists.mkwsTeam_" + teamName);
+ if (node.length == 0) return;
+
+ // no facets: this should never happen
if (!mkws_config.facets || mkws_config.facets.length == 0) {
- $("#mkwsTermlists").hide();
+ alert("my_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('
\
+ var node = $(".mkwsTargets.mkwsTeam_" + m_teamName);
+ node.html('\
+
\
No information available yet.\
');
- $("#mkwsTargets").css("display", "none");
+ node.css("display", "none");
}
@@ -1029,18 +1057,18 @@ function _make_mkws_team($, teamName) {
/* display a list of configured languages, or all */
var lang_options = mkws_config.lang_options || [];
- var hash = {};
+ var toBeIncluded = {};
for (var i = 0; i < lang_options.length; i++) {
- hash[lang_options[i]] = 1;
+ toBeIncluded[lang_options[i]] = true;
}
for (var k in mkws.locale_lang) {
- if (hash[k] == 1 || lang_options.length == 0)
+ if (toBeIncluded[k] || lang_options.length == 0)
list.push(k);
}
// add english link
- if (lang_options.length == 0 || hash[lang_default] == 1)
+ if (lang_options.length == 0 || toBeIncluded[lang_default])
list.push(lang_default);
debug("Language menu for: " + list.join(", "));
@@ -1060,38 +1088,10 @@ function _make_mkws_team($, teamName) {
}
}
- $("#mkwsLang").html(data);
+ $(".mkwsLang.mkwsTeam_" + m_teamName).html(data);
}
- function mkws_resize_page () {
- var list = ["mkwsSwitch"];
-
- var width = mkws_config.responsive_design_width;
- var parentId = $("#mkwsTermlists").parent().attr('id');
-
- if ($(window).width() <= width &&
- parentId === "mkwsTermlistContainer1") {
- debug("changing from wide to narrow: " + $(window).width());
- $("#mkwsTermlists").appendTo($("#mkwsTermlistContainer2"));
- $("#mkwsTermlistContainer1").hide();
- $("#mkwsTermlistContainer2").show();
- for(var i = 0; i < list.length; i++) {
- $("#" + list[i]).hide();
- }
- } else if ($(window).width() > width &&
- parentId === "mkwsTermlistContainer2") {
- debug("changing from narrow to wide: " + $(window).width());
- $("#mkwsTermlists").appendTo($("#mkwsTermlistContainer1"));
- $("#mkwsTermlistContainer1").show();
- $("#mkwsTermlistContainer2").hide();
- for(var i = 0; i < list.length; i++) {
- $("#" + list[i]).show();
- }
- }
- };
-
-
/* locale */
function M(word) {
var lang = mkws_config.lang;
@@ -1265,7 +1265,7 @@ function _mkws_jquery_plugin ($) {
};
-// wrapper to call _make_mkws_team() after page load
+// wrapper to call team() after page load
(function (j) {
function log(s) {
if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
@@ -1290,41 +1290,32 @@ function _mkws_jquery_plugin ($) {
var id = 'mkws' + ids[i];
var node = $('#' + id);
if (node.attr('id')) {
- node.addClass(id + " mkwsTeam_AUTO");
- log("added magic classes to '" + node.attr('id') + "'");
+ node.addClass(id);
+ log("added magic class to '" + node.attr('id') + "'");
}
}
- // Backwards compatibility: the special-case undefined team
- // ### Will not be necessary when non-default teams are working
- mkws.teams['AUTO'] = _make_mkws_team(j, "AUTO");
- log("Made the unnamed MKWS team");
+ // For all MKWS-classed nodes that don't have a team
+ // specified, set the team to AUTO.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
+ if (!this.className.match(/mkwsTeam_/)) {
+ log("adding AUTO team to node with class '" + this.className + "'");
+ $(this).addClass('mkwsTeam_AUTO');
+ }
+ });
- // Find all nodes with class (NOT id) mkwsRecords, and
- // determine their team from the mkwsTeam_* class. So:
- //
- $('.mkwsResults, .mkwsRecords, .mkwsTermlists').each(function () {
+ // Find all nodes with an class, and determine their team from
+ // the mkwsTeam_* class. Make all team objects.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
var node = this;
- var classes = this.className;
- var list = classes.split(/\s+/)
- var tname;
- for (var i = 0; i < list.length; i++) {
- var cname = list[i];
- if (cname.match(/^mkwsTeam_/)) {
- tname = cname.replace(/^mkwsTeam_/, '');
+ 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 + "'");
}
- }
- if (!tname) {
- alert("No MKWS team specified for mkwsRecords element with classes '" + classes + "'");
- } else if (mkws.teams[tname]) {
- log("MKWS team '" + tname + "' already exists, skipping");
- } else if (tname === "AUTO") {
- // ### For now: later, this will be how the backwards-compatibility is done
- log("Skipping MKWS team '" + tname + "'");
- } else {
- mkws.teams[tname] = _make_mkws_team(j, tname);
- log("Made MKWS team '" + tname + "'");
- }
+ });
});
if (mkws_config.use_service_proxy) {
@@ -1338,12 +1329,63 @@ function _mkws_jquery_plugin ($) {
});
+ mkws.handle_node_with_team = function(node, callback) {
+ var classes = node.className;
+ var list = classes.split(/\s+/)
+ var tname;
+ for (var i = 0; i < list.length; i++) {
+ var cname = list[i];
+ if (cname.match(/^mkwsTeam_/)) {
+ tname = cname.replace(/^mkwsTeam_/, '');
+ }
+ }
+ callback(tname);
+ }
+
+
+ mkws.resize_page = function () {
+ var list = ["mkwsSwitch", "mkwsLang"];
+
+ var width = mkws_config.responsive_design_width;
+ var parent = $(".mkwsTermlists").parent();
+
+ if ($(window).width() <= width &&
+ parent.hasClass("mkwsTermlistContainer1")) {
+ log("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")) {
+ log("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.showDetails = function (prefixRecId, tname) {
+ mkws.teams[tname].showDetails(prefixRecId);
+ }
+
+
function default_mkws_config() {
/* default mkws config */
var config_default = {
use_service_proxy: true,
- pazpar2_url: "http://mkws.indexdata.com/service-proxy/",
- service_proxy_auth: "http://mkws.indexdata.com/service-proxy-auth",
+ pazpar2_url: "//mkws.indexdata.com/service-proxy/",
+ service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
lang: "",
sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
perpage_options: [10, 20, 30, 50],
@@ -1354,7 +1396,7 @@ function _mkws_jquery_plugin ($) {
show_sort: true, /* show/hide sort menu */
show_perpage: true, /* show/hide perpage menu */
lang_options: [], /* display languages links for given languages, [] for all */
- facets: ["sources", "subjects", "authors"], /* display facets, in this order, [] for none */
+ 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 */
@@ -1389,11 +1431,11 @@ function _mkws_jquery_plugin ($) {
* for the site.
*/
function authenticate_session(auth_url, auth_domain, pp2_url) {
- console.log("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');
- console.log("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) {
@@ -1412,7 +1454,7 @@ function _mkws_jquery_plugin ($) {
return;
}
- console.log("Service proxy auth successfully done");
+ log("Service proxy auth successfully done");
mkws.authenticated = true;
run_auto_searches();
});
@@ -1420,13 +1462,13 @@ function _mkws_jquery_plugin ($) {
function run_auto_searches() {
- console.log("running auto searches");
+ log("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');
- console.log("teamName '" + teamName + "', node=" + node + ", class='" + node.className + "', query=" + query);
+ log("teamName '" + teamName + "', node=" + node + ", class='" + node.className + "', query=" + query);
if (query) {
var sort = node.attr('sort');
@@ -1435,9 +1477,9 @@ function _mkws_jquery_plugin ($) {
if (teamName) s += " [teamName '" + teamName + "']";
if (sort) s += " sorted by '" + sort + "'";
if (targets) s += " in targets '" + targets + "'";
- console.log(s);
+ log(s);
var team = mkws.teams[teamName];
- console.log($.toJSON(team));
+ log($.toJSON(team));
team.newSearch(query, sort, targets, teamName);
}
}