X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=2d9b452780e646dd9addabc6fa15b058e939b00a;hb=588ec81e46383127b62dc1e9d0b454dfe2637967;hp=2a6d7bada0c2914b86514b682dbc1ae781ac9143;hpb=63194ff14d2e3e893de4a54bf18c6e6ee669f152;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 2a6d7ba..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,9 +150,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'; @@ -159,13 +167,15 @@ function _make_mkws_team($, teamName) { "start": $.now(), "last": $.now() }; + var m_paz; // will be initialised below // 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; @@ -189,53 +199,6 @@ function _make_mkws_team($, teamName) { var debug = mkws.debug_function; // local alias debug("start running MKWS"); - - { - - /* 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", - lang: "", - sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]], - perpage_options: [10, 20, 30, 50], - sort_default: "relevance", - perpage_default: 20, - query_width: 50, - show_lang: true, /* show/hide language menu */ - 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 */ - responsive_design_width: undefined, /* a page with less pixel width considered as narrow */ - debug_level: 1, /* debug level for development: 0..2 */ - - dummy: "dummy" - }; - - /* Set global debug_level flag early so that debug() works */ - if (typeof mkws_config.debug_level !== 'undefined') { - mkws.debug_level = mkws_config.debug_level; - } else if (typeof config_default.debug_level !== 'undefined') { - mkws.debug_level = config_default.debug_level; - } - - // make sure the mkws_config is a valid hash - if (!$.isPlainObject(mkws_config)) { - debug("ERROR: mkws_config is not an JS object, ignore it...."); - mkws_config = {}; - } - - /* override standard config values by function parameters */ - for (var k in config_default) { - if (typeof mkws_config[k] === 'undefined') - mkws_config[k] = config_default[k]; - //debug("Set config: " + k + ' => ' + mkws_config[k]); - } - } - - m_sort = mkws_config.sort_default; debug("copied mkws_config.sort_default '" + mkws_config.sort_default + "' to m_sort"); @@ -265,20 +228,18 @@ function _make_mkws_team($, teamName) { // 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 - if (!mkws.paz) { - mkws.paz = new pz2({ "onshow": my_onshow, - "windowid": teamName, - "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way - "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 }); - } + m_paz = new pz2({ "onshow": my_onshow, + "windowid": teamName, + "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way + "pazpar2path": mkws_config.pazpar2_url, + "oninit": my_oninit, + "onstat": my_onstat, + "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?) + "onrecord": my_onrecord }); if (!isNaN(parseInt(mkws_config.perpage_default))) { m_recPerPage = parseInt(mkws_config.perpage_default); @@ -290,8 +251,8 @@ function _make_mkws_team($, teamName) { // function my_oninit(teamName) { debug("init for " + teamName); - mkws.paz.stat(); - mkws.paz.bytarget(); + m_paz.stat(); + m_paz.bytarget(); } @@ -310,7 +271,7 @@ function _make_mkws_team($, teamName) { } // navi - var results = document.getElementById("mkwsRecords"); + var results = $(".mkwsRecords.mkwsTeam_" + m_teamName); var html = []; for (var i = 0; i < data.hits.length; i++) { @@ -319,10 +280,11 @@ 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)); } } - replaceHtml(results, html.join('')); + results.html(html.join('')); } @@ -330,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 = document.getElementById("mkwsTermlists"); - if (termlist) - replaceHtml(termlist, acc.join('')); + node.html(acc.join('')); } @@ -404,10 +368,10 @@ 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(mkws.paz.recordTimer); + clearTimeout(m_paz.recordTimer); // in case on_show was faster to redraw element var detRecordDiv = document.getElementById('mkwsDet_'+data.recid); if (detRecordDiv) return; @@ -420,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; } @@ -443,7 +406,7 @@ function _make_mkws_team($, teamName) { } table += ''; - targetDiv.innerHTML = table; + targetDiv.html(table); } //////////////////////////////////////////////////////////////////////////////// @@ -453,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) @@ -467,12 +438,16 @@ function _make_mkws_team($, teamName) { // when search button pressed function onFormSubmitEventHandler() { - 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; } - function newSearch(query, sort, targets, windowid) + that.newSearch = function(query, sort, targets, windowid) { debug("newSearch: " + query); @@ -486,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; } @@ -496,7 +471,7 @@ function _make_mkws_team($, teamName) { if (!m_submitted) return false; resetPage(); loadSelect(); - mkws.paz.show(0, m_recPerPage, m_sort); + m_paz.show(0, m_recPerPage, m_sort); return false; } @@ -552,7 +527,7 @@ function _make_mkws_team($, teamName) { } debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params)); - mkws.paz.search(m_query, m_recPerPage, m_sort, pp2filter, undefined, params); + m_paz.search(m_query, m_recPerPage, m_sort, pp2filter, undefined, params); } @@ -653,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 + ''; } @@ -716,53 +691,53 @@ function _make_mkws_team($, teamName) { mkws.showPage = function (pageNum) { m_curPage = pageNum; - mkws.paz.showPage(m_curPage - 1); + m_paz.showPage(m_curPage - 1); } // simple paging functions mkws.pagerNext = function () { if (m_totalRec - m_recPerPage*m_curPage > 0) { - mkws.paz.showNext(); + m_paz.showNext(); m_curPage++; } } mkws.pagerPrev = function () { - if (mkws.paz.showPrev() != false) + if (m_paz.showPrev() != false) m_curPage--; } // 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 + "'"); @@ -771,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; @@ -789,25 +764,10 @@ function _make_mkws_team($, teamName) { return; } // request the record - mkws.paz.record(recId); + m_paz.record(recId); } - function replaceHtml(el, html) { - var oldEl = typeof el === "string" ? document.getElementById(el) : el; - /*@cc_on // Pure innerHTML is slightly faster in IE - oldEl.innerHTML = html; - return oldEl; - @*/ - var newEl = oldEl.cloneNode(false); - newEl.innerHTML = html; - oldEl.parentNode.replaceChild(newEl, oldEl); - /* Since we just removed the old element from the DOM, return a reference - to the new element, which can be used to restore variable references. */ - return newEl; - }; - - function renderDetails(data, marker) { var template = loadTemplate("Record"); @@ -927,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('\ \ \ - \ - \ \ \ \ \
\ -
\ +
\ +
\
\ -
\ -
\ -
\ -
\ +
\ +
\ +
\ +
\ +
\
\ -
\ +
\
'); @@ -983,52 +950,27 @@ 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); } } - function run_auto_searches() { - debug("running auto searches"); - - $('[id^="mkwsRecords"]').each(function () { - var node = $(this); - var query = node.attr('autosearch'); - - if (query) { - var windowid = undefined; - var id = node.attr('id'); - if (id.match(/^mkwsRecords_/, '')) { - windowid = id.replace(/^mkwsRecords_/, ''); - } - - var sort = node.attr('sort'); - var targets = node.attr('targets'); - var s = "running auto search: '" + query + "'"; - if (windowid) s += " [windowid '" + windowid + "']"; - if (sort) s += " sorted by '" + sort + "'"; - if (targets) s += " in targets '" + targets + "'"; - debug(s); - newSearch(query, sort, targets, windowid); - } - }); - } - - // implement $.parseQuerystring() for parsing URL parameters function parseQuerystring() { var nvpair = {}; @@ -1056,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"); } @@ -1111,42 +1055,6 @@ function _make_mkws_team($, teamName) { } - /* - * Run service-proxy authentication in background (after page load). - * The username/password is configured in the apache config file - * for the site. - */ - mkws.service_proxy_auth = function(auth_url, auth_domain, pp2_url) { - debug("Run service proxy auth URL: " + auth_url); - - if (!auth_domain) { - auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2'); - debug("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'"); - } - - var request = new pzHttpRequest(auth_url, function(err) { - alert("HTTP call for authentication failed: " + err) - return; - }, auth_domain); - - request.get(null, function(data) { - if (!$.isXMLDoc(data)) { - alert("service proxy auth response document is not valid XML document, give up!"); - return; - } - var status = $(data).find("status"); - if (status.text() != "OK") { - alert("service proxy auth repsonse status: " + status.text() + ", give up!"); - return; - } - - debug("Service proxy auth successfully done"); - mkws.authenticated = true; - run_auto_searches(); - }); - } - - /* create locale language menu */ function mkws_html_lang() { var lang_default = "en"; @@ -1155,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(", ")); @@ -1186,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; @@ -1391,16 +1271,21 @@ 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 (console && console.log) console.log(s); + if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */ + return; + } + console.log(s); } + // 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(); // Backwards compatibility: set new magic class names on any // elements that have the old magic IDs. @@ -1411,50 +1296,198 @@ 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[''] = _make_mkws_team(j, undefined); - 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) { - mkws.service_proxy_auth(mkws_config.service_proxy_auth, - mkws_config.service_proxy_auth_domain, - mkws_config.pazpar2_url); + authenticate_session(mkws_config.service_proxy_auth, + mkws_config.service_proxy_auth_domain, + mkws_config.pazpar2_url); } else { // raw pp2 run_auto_searches(); } }); + + + 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: "//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], + sort_default: "relevance", + perpage_default: 20, + query_width: 50, + show_lang: true, /* show/hide language menu */ + 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: ["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 */ + + dummy: "dummy" + }; + + /* Set global debug_level flag early so that debug() works */ + if (typeof mkws_config.debug_level !== 'undefined') { + mkws.debug_level = mkws_config.debug_level; + } else if (typeof config_default.debug_level !== 'undefined') { + mkws.debug_level = config_default.debug_level; + } + + // make sure the mkws_config is a valid hash + if (!$.isPlainObject(mkws_config)) { + debug("ERROR: mkws_config is not an JS object, ignore it...."); + mkws_config = {}; + } + + /* override standard config values by function parameters */ + for (var k in config_default) { + if (typeof mkws_config[k] === 'undefined') + mkws_config[k] = config_default[k]; + //debug("Set config: " + k + ' => ' + mkws_config[k]); + } + } + + + /* + * Run service-proxy authentication in background (after page load). + * The username/password is configured in the apache config file + * for the site. + */ + function authenticate_session(auth_url, auth_domain, pp2_url) { + log("Run service proxy auth URL: " + auth_url); + + if (!auth_domain) { + auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2'); + log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'"); + } + + var request = new pzHttpRequest(auth_url, function(err) { + alert("HTTP call for authentication failed: " + err) + return; + }, auth_domain); + + request.get(null, function(data) { + if (!$.isXMLDoc(data)) { + alert("service proxy auth response document is not valid XML document, give up!"); + return; + } + var status = $(data).find("status"); + if (status.text() != "OK") { + alert("service proxy auth repsonse status: " + status.text() + ", give up!"); + return; + } + + log("Service proxy auth successfully done"); + mkws.authenticated = true; + run_auto_searches(); + }); + } + + + function run_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'); + log("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 + "'"; + log(s); + var team = mkws.teams[teamName]; + log($.toJSON(team)); + team.newSearch(query, sort, targets, teamName); + } + } + } })(jQuery);