X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=2d9b452780e646dd9addabc6fa15b058e939b00a;hb=588ec81e46383127b62dc1e9d0b454dfe2637967;hp=5a18aa9e87a8e107484827cc0a12cda96152d316;hpb=0f1ee05a66d356ff5fbf62353e6a599980ec9c02;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 5a18aa9..2d9b452 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); }); @@ -84,6 +86,7 @@ var mkws = { "Authors": "Autoren", "Subjects": "Schlagwörter", "Sources": "Daten und Quellen", + "source": "datenquelle", "Termlists": "Termlisten", "Next": "Weiter", "Prev": "Zurück", @@ -97,8 +100,10 @@ var mkws = { "found": "gefunden", "Title": "Titel", "Author": "Autor", + "author": "autor", "Date": "Datum", "Subject": "Schlagwort", + "subject": "schlagwort", "Location": "Ort", "Records": "Datensätze", "Targets": "Datenbanken", @@ -110,6 +115,7 @@ var mkws = { "Authors": "Forfattere", "Subjects": "Emner", "Sources": "Kilder", + "source": "kilder", "Termlists": "Termlists", "Next": "Næste", "Prev": "Forrige", @@ -123,8 +129,10 @@ var mkws = { "found": "fandt", "Title": "Title", "Author": "Forfatter", + "author": "forfatter", "Date": "Dato", "Subject": "Emneord", + "subject": "emneord", "Location": "Lokation", "Records": "Poster", "Targets": "Baser", @@ -142,7 +150,7 @@ if (mkws_config == null || typeof mkws_config != 'object') { // wrapper for jQuery lib -function _make_mkws_team($, teamName) { +function team($, teamName) { var that = {}; var m_teamName = teamName; var m_submitted = false; @@ -163,10 +171,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 +234,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 +271,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 +280,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 +292,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('
' + M('Termlists') + '
'); var facets = mkws_config.facets; for(var i = 0; i < facets.length; i++) { - if (facets[i] == "sources") { + if (facets[i] == "xtargets") { add_single_facet(acc, "Sources", data.xtargets, 16, null); - } else if (facets[i] == "subjects") { + } else if (facets[i] == "subject") { add_single_facet(acc, "Subjects", data.subject, 10, "subject"); - } else if (facets[i] == "authors") { + } else if (facets[i] == "author") { add_single_facet(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('')); } @@ -356,7 +368,7 @@ function _make_mkws_team($, teamName) { } - function my_onrecord(data, teamName) { + function my_onrecord(data, args, teamName) { debug("record for " + teamName); // FIXME: record is async!! clearTimeout(m_paz.recordTimer); @@ -372,9 +384,8 @@ function _make_mkws_team($, teamName) { function my_onbytarget(data, teamName) { debug("target for " + teamName); - var targetDiv = document.getElementById("mkwsBytarget"); + var targetDiv = $('.mkwsBytarget.mkwsTeam_' + teamName); if (!targetDiv) { - // No mkwsTargets div. return; } @@ -395,7 +406,7 @@ function _make_mkws_team($, teamName) { } table += ''; - targetDiv.innerHTML = table; + targetDiv.html(table); } //////////////////////////////////////////////////////////////////////////////// @@ -405,9 +416,17 @@ function _make_mkws_team($, teamName) { // wait until the DOM is ready function domReady () { - document.mkwsSearchForm.onsubmit = onFormSubmitEventHandler; - document.mkwsSearchForm.mkwsQuery.value = ''; + $('.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); + }); + }); + if (document.mkwsSelect) { + debug("messing with mkwsSelect"); if (document.mkwsSelect.mkwsSort) document.mkwsSelect.mkwsSort.onchange = onSelectDdChange; if (document.mkwsSelect.mkwsPerpage) @@ -419,7 +438,11 @@ function _make_mkws_team($, teamName) { // when search button pressed function onFormSubmitEventHandler() { - that.newSearch(document.mkwsSearchForm.mkwsQuery.value); + mkws.handle_node_with_team(this, function (tname) { + var val = $('.mkwsQuery.mkwsTeam_' + tname).val(); + mkws.teams[tname].newSearch(val); + }); + return false; } @@ -438,7 +461,7 @@ function _make_mkws_team($, teamName) { resetPage(); // ### the globals it resents should be indexed by windowid loadSelect(); // ### should use windowid triggerSearch(query, sort, targets, windowid); - mkws.switchView('records'); // In case it's configured to start off as hidden + mkws.switchView(m_teamName, 'records'); // In case it's configured to start off as hidden m_submitted = true; } @@ -605,10 +628,10 @@ function _make_mkws_team($, teamName) { } var filter = m_filters[i]; if (filter.id) { - text += 'Source: ' + filter.name + ''; } else { - text += filter.field + ': ' + filter.value + ''; } @@ -688,33 +711,33 @@ function _make_mkws_team($, teamName) { // switching view between targets and records - mkws.switchView = function(view) { - debug("switchView: " + view); + mkws.switchView = function(tname, view) { + debug("switchView(" + tname + ", " + view + ")"); - var targets = document.getElementById('mkwsTargets'); - var results = document.getElementById('mkwsResults') || - document.getElementById('mkwsRecords'); - var blanket = document.getElementById('mkwsBlanket'); - var motd = document.getElementById('mkwsMOTD'); + var targets = $('.mkwsTargets.mkwsTeam_' + tname); + var results = $('.mkwsResults.mkwsTeam_' + tname + ',.mkwsRecords.mkwsTeam_' + tname); + var blanket = $('.mkwsBlanket.mkwsTeam_' + tname); + var motd = $('.mkwsMOTD.mkwsTeam_' + tname); switch(view) { case 'targets': - if (targets) targets.style.display = "block"; - if (results) results.style.display = "none"; - if (blanket) blanket.style.display = "none"; - if (motd) motd.style.display = "none"; + if (targets) targets.css('display', 'block'); + if (results) results.css('display', 'none'); + if (blanket) blanket.css('display', 'none'); + if (motd) motd.css('display', 'none'); break; case 'records': - if (targets) targets.style.display = "none"; - if (results) results.style.display = "block"; - if (blanket) blanket.style.display = "block"; - if (motd) motd.style.display = "none"; + if (targets) targets.css('display', 'none'); + if (results) results.css('display', 'block'); + if (blanket) blanket.css('display', 'block'); + if (motd) motd.css('display', 'none'); break; case 'none': - if (targets) targets.style.display = "none"; - if (results) results.style.display = "none"; - if (blanket) blanket.style.display = "none"; - if (motd) motd.style.display = "none"; + alert("mkws.switchView(" + tname + ", 'none') shouldn't happen"); + if (targets) targets.css('display', 'none'); + if (results) results.css('display', 'none'); + if (blanket) blanket.css('display', 'none'); + if (motd) motd.css('display', 'none'); break; default: alert("Unknown view '" + view + "'"); @@ -723,7 +746,7 @@ function _make_mkws_team($, teamName) { // detailed record drawing - mkws.showDetails = function (prefixRecId) { + that.showDetails = function (prefixRecId) { var recId = prefixRecId.replace('mkwsRec_', ''); var oldRecId = m_curDetRecId; m_curDetRecId = recId; @@ -864,38 +887,45 @@ function _make_mkws_team($, teamName) { // For some reason, doing this programmatically results in // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML. debug("HTML search form"); - $("#mkwsSearch").html('\ -
\ - \ - \ + // ### 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('\ +\ + \ + \
'); + }); + }); debug("HTML records"); - // If the application has an #mkwsResults, populate it in the - // usual way. If not, assume that it's a smarter application that - // defines its own subcomponents: - // #mkwsTermlists - // #mkwsRanking - // #mkwsPager - // #mkwsNavi - // #mkwsRecords - if ($("#mkwsResults").length) { - $("#mkwsResults").html('\ + // If the team has a .mkwsResults, populate it in the usual + // way. If not, assume that it's a smarter application that + // defines its own subcomponents, some or all of the + // following: + // .mkwsTermlists + // .mkwsRanking + // .mkwsPager + // .mkwsNavi + // .mkwsRecords + if ($(".mkwsResults.mkwsTeam_" + m_teamName).length) { + $(".mkwsResults.mkwsTeam_" + m_teamName).html('\ \ \ - \ - \ \ \ \ \
\ -
\ +
\ +
\
\ -
\ -
\ -
\ -
\ +
\ +
\ +
\ +
\ +
\
\ -
\ +
\
'); @@ -920,21 +950,23 @@ function _make_mkws_team($, teamName) { 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() }); + $(window).resize(function(e) { mkws.resize_page() }); // initial check after page load - $(document).ready(function() { mkws_resize_page() }); + $(document).ready(function() { mkws.resize_page() }); } + debug("before domReady()"); domReady(); + debug("after domReady()"); // 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() { $(".mkwsTermlists.mkwsTeam_" + m_teamName).hide(); }); + var motd = $(".mkwsMOTD.mkwsTeam_" + m_teamName); + var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName); + debug("for team '" + m_teamName + "', motd=" + motd + "(" + motd.length + "), container=" + container + "(" + container.length + ")"); + 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); } } @@ -966,18 +998,20 @@ function _make_mkws_team($, teamName) { function mkws_html_switch() { - debug("HTML switch"); + debug("HTML switch for team " + m_teamName); - $("#mkwsSwitch").append($('' + M('Records') + '')); - $("#mkwsSwitch").append($("", { text: " | " })); - $("#mkwsSwitch").append($('' + M('Targets') + '')); + var node = $(".mkwsSwitch.mkwsTeam_" + m_teamName); + node.append($('' + M('Records') + '')); + node.append($("", { text: " | " })); + node.append($('' + M('Targets') + '')); debug("HTML targets"); - $("#mkwsTargets").html('\ -
\ + var node = $(".mkwsTargets.mkwsTeam_" + m_teamName); + node.html('\ +
\ No information available yet.\
'); - $("#mkwsTargets").css("display", "none"); + node.css("display", "none"); } @@ -1029,18 +1063,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 +1094,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 +1271,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 +1296,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 +1335,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 +1402,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 +1437,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 +1460,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 +1468,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 +1483,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); } }