X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=adf6f6efe0aa1d167486f3219e31624a09d56f2a;hb=a69a66b88e3317022c375792df0b1c3689431496;hp=67fb2fb055998b5d0f38726ca38860f49aab2f78;hpb=384c4603e2c4745fbebc7aa5731fd8d80d0a3111;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 67fb2fb..fe29a5e 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); }); @@ -63,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: { @@ -84,6 +85,7 @@ var mkws = { "Authors": "Autoren", "Subjects": "Schlagwörter", "Sources": "Daten und Quellen", + "source": "datenquelle", "Termlists": "Termlisten", "Next": "Weiter", "Prev": "Zurück", @@ -97,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", @@ -110,6 +114,7 @@ var mkws = { "Authors": "Forfattere", "Subjects": "Emner", "Sources": "Kilder", + "source": "kilder", "Termlists": "Termlists", "Next": "Næste", "Prev": "Forrige", @@ -123,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", @@ -135,22 +142,67 @@ 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 = {}; } -// wrapper for jQuery lib -function _make_mkws_team($, teamName) { +// Factory function for widget objects. +function widget($, team, type, node) { + var that = { + team: team, + type: type, + node: node + }; + + // ### More to do here, surely: e.g. wiring into the team + mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node); + + return that; +} + + +// 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_termName = teamName; + var m_teamName = 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,313 +211,216 @@ function _make_mkws_team($, teamName) { "start": $.now(), "last": $.now() }; + var m_paz; // will be initialised below + var m_template = {}; - // if (console && console.log) // disabled, will fail in IE8 - // console.log("run _make_mkws_team(" + (teamName ? teamName : "") + ")"); - - - // Needs to be defined inside _make_mkws_team() so it can see m_debug_time - 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); - } - 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]); - } + mkws.debug(m_teamName + ": " + timestamp + s); } + 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 independend 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 independend 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 - if (!mkws.paz) { - mkws.paz = new pz2({ "onshow": my_onshow, - "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 }); - } - - if (!isNaN(parseInt(mkws_config.perpage_default))) { - m_recPerPage = parseInt(mkws_config.perpage_default); - } + m_paz = new pz2({ "windowid": teamName, + "pazpar2path": mkws_config.pazpar2_url, + "usesessions" : mkws_config.use_service_proxy ? false : true, + "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() { - mkws.paz.stat(); - mkws.paz.bytarget(); + function onInit(teamName) { + debug("init"); + m_paz.stat(); + m_paz.bytarget(); } - function my_onshow(data) { - m_totalRec = data.merged; - // move it out - var pager = document.getElementById("mkwsPager"); - if (pager) { - pager.innerHTML = ""; - pager.innerHTML +='
' + M('Displaying') + ': ' - + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) + - ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': ' - + data.total + ')
'; - drawPager(pager); - } + function onBytarget(data, teamName) { + debug("target"); + var node = findnode('.mkwsBytarget'); + if (node.length === 0) return; - // navi - var results = document.getElementById("mkwsRecords"); + var table ='' + + '' + + '' + + '' + + '' + + '' + + ''; - 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) { - html.push(renderDetails(m_curDetRecData)); - } + for (var i = 0; i < data.length; i++) { + table += ""; } - replaceHtml(results, html.join('')); - } - - function renderSummary(hit) - { - var template = loadTemplate("Summary"); - hit._id = "mkwsRec_" + hit.recid; - hit._onclick = "mkws.showDetails(this.id);return false;" - return template(hit); + table += '
' + M('Target ID') + '' + M('Hits') + '' + M('Diags') + '' + M('Records') + '' + M('State') + '
" + data[i].id + + "" + data[i].hits + + "" + data[i].diagnostic + + "" + data[i].records + + "" + data[i].state + "
'; + node.html(table); } - function my_onstat(data) { - var stat = document.getElementById("mkwsStat"); - if (stat == null) - return; + function onStat(data, teamName) { + debug("stat"); + var node = findnode('.mkwsStat'); + if (node.length === 0) return; - stat.innerHTML = '' + M('Status info') + '' + + node.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) { - // no facets + function onTerm(data, teamName) { + debug("term"); + var node = findnode(".mkwsTermlists"); + if (node.length == 0) return; + + // 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 = document.getElementById("mkwsTermlists"); - if (termlist) - replaceHtml(termlist, acc.join('')); + node.html(acc.join('')); } - function add_single_facet(acc, caption, data, max, pzIndex) { - acc.push('
'); - acc.push('
' + M(caption) + '
'); - for (var i = 0; i < data.length && i < max; i++) { - acc.push('
'); - acc.push('', + renderSummary(hit), + '
'); + if (hit.recid == m_curDetRecId) { + if (m_curDetRecData) + html.push(renderDetails(m_curDetRecData)); } - acc.push('onclick="' + action + ';return false;">' + data[i].name + '' - + ' ' + data[i].freq + ''); - acc.push('
'); } - acc.push(''); + results.html(html.join('')); } - function my_onrecord(data) { + function onRecord(data, args, teamName) { + debug("record: teamName=" + teamName + ", m_teamName=" + m_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); + var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid); if (detRecordDiv) return; m_curDetRecData = data; - var recordDiv = document.getElementById('mkwsRecdiv_'+m_curDetRecData.recid); + var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid); var html = renderDetails(m_curDetRecData); recordDiv.innerHTML += html; } - function my_onbytarget(data) { - var targetDiv = document.getElementById("mkwsBytarget"); - if (!targetDiv) { - // No mkwsTargets div. - return; + function addSingleFacet(acc, caption, data, max, pzIndex) { + acc.push('
'); + acc.push('
' + M(caption) + '
'); + for (var i = 0; i < data.length && i < max; i++) { + acc.push('
'); + acc.push('' + data[i].name + '' + + ' ' + data[i].freq + ''); + acc.push('
'); } + acc.push('
'); + } - var table ='' + - '' + - '' + - '' + - '' + - '' + - ''; - for (var i = 0; i < data.length; i++) { - table += ""; + function targetFiltered(id) { + for (var i = 0; i < m_filters.length; i++) { + if (m_filters[i].id === id || + m_filters[i].id === 'pz:id=' + id) { + return true; + } } - - table += '
' + M('Target ID') + '' + M('Hits') + '' + M('Diags') + '' + M('Records') + '' + M('State') + '
" + data[i].id + - "" + data[i].hits + - "" + data[i].diagnostic + - "" + data[i].records + - "" + data[i].state + "
'; - targetDiv.innerHTML = table; + return false; } + //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// - // wait until the DOM is ready - function domReady () - { - document.mkwsSearchForm.onsubmit = onFormSubmitEventHandler; - document.mkwsSearchForm.mkwsQuery.value = ''; - if (document.mkwsSelect) { - if (document.mkwsSelect.mkwsSort) - document.mkwsSelect.mkwsSort.onchange = onSelectDdChange; - if (document.mkwsSelect.mkwsPerpage) - document.mkwsSelect.mkwsPerpage.onchange = onSelectDdChange; - } - } - - // when search button pressed function onFormSubmitEventHandler() { - newSearch(document.mkwsSearchForm.mkwsQuery.value); + var val = findnode('.mkwsQuery').val(); + newSearch(val); return false; } - function newSearch(query, sort, targets, windowid) + function newSearch(query, sort, targets) { debug("newSearch: " + query); @@ -475,11 +430,11 @@ function _make_mkws_team($, teamName) { } m_filters = [] - redraw_navi(); // ### should use windowid - 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 + redrawNavi(); + resetPage(); + loadSelect(); + triggerSearch(query, sort, targets); + switchView('records'); // In case it's configured to start off as hidden m_submitted = true; } @@ -489,7 +444,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_perpage, m_sort); return false; } @@ -501,7 +456,7 @@ function _make_mkws_team($, teamName) { } - function triggerSearch (query, sort, targets, windowid) + function triggerSearch (query, sort, targets) { var pp2filter = ""; var pp2limit = ""; @@ -514,7 +469,6 @@ function _make_mkws_team($, teamName) { m_sort = sort; } if (targets) { - // ### should support multiple |-separated targets m_filters.push({ id: targets, name: targets }); } @@ -540,32 +494,36 @@ function _make_mkws_team($, teamName) { if (pp2limit) { params.limit = pp2limit; } - if (windowid) { - params.windowid = windowid; - } + 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); + // We can use: params.torusquery = "udb=NAME" + // Note: that won't work when running against raw pazpar2 + m_paz.search(m_query, m_perpage, m_sort, pp2filter, undefined, params); } function loadSelect () { - if (document.mkwsSelect) { - if (document.mkwsSelect.mkwsSort) - m_sort = document.mkwsSelect.mkwsSort.value; - if (document.mkwsSelect.mkwsPerpage) - m_recPerPage = document.mkwsSelect.mkwsPerpage.value; + 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 the query after clicking the facet - mkws.limitQuery = function (field, value) + // limit by target functions + that.limitTarget = function (id, name) { - debug("limitQuery(field=" + field + ", value=" + value + ")"); - m_filters.push({ field: field, value: value }); - redraw_navi(); + debug("limitTarget(id=" + id + ", name=" + name + ")"); + m_filters.push({ id: id, name: name }); + redrawNavi(); resetPage(); loadSelect(); triggerSearch(); @@ -573,12 +531,12 @@ function _make_mkws_team($, teamName) { } - // limit by target functions - mkws.limitTarget = function (id, name) + // limit the query after clicking the facet + that.limitQuery = function (field, value) { - debug("limitTarget(id=" + id + ", name=" + name + ")"); - m_filters.push({ id: id, name: name }); - redraw_navi(); + debug("limitQuery(field=" + field + ", value=" + value + ")"); + m_filters.push({ field: field, value: value }); + redrawNavi(); resetPage(); loadSelect(); triggerSearch(); @@ -586,15 +544,13 @@ function _make_mkws_team($, teamName) { } - mkws.delimitQuery = function (field, value) + that.delimitTarget = function (id) { - debug("delimitQuery(field=" + field + ", value=" + value + ")"); + debug("delimitTarget(id=" + id + ")"); var newFilters = []; for (var i in m_filters) { var filter = m_filters[i]; - if (filter.field && - field == filter.field && - value == filter.value) { + if (filter.id) { debug("delimitTarget() removing filter " + $.toJSON(filter)); } else { debug("delimitTarget() keeping filter " + $.toJSON(filter)); @@ -603,7 +559,7 @@ function _make_mkws_team($, teamName) { } m_filters = newFilters; - redraw_navi(); + redrawNavi(); resetPage(); loadSelect(); triggerSearch(); @@ -611,22 +567,24 @@ function _make_mkws_team($, teamName) { } - mkws.delimitTarget = function (id) + that.delimitQuery = function (field, value) { - debug("delimitTarget(id=" + id + ")"); + debug("delimitQuery(field=" + field + ", value=" + value + ")"); var newFilters = []; for (var i in m_filters) { var filter = m_filters[i]; - if (filter.id) { - debug("delimitTarget() removing filter " + $.toJSON(filter)); + if (filter.field && + field == filter.field && + value == filter.value) { + debug("delimitQuery() removing filter " + $.toJSON(filter)); } else { - debug("delimitTarget() keeping filter " + $.toJSON(filter)); + debug("delimitQuery() keeping filter " + $.toJSON(filter)); newFilters.push(filter); } } m_filters = newFilters; - redraw_navi(); + redrawNavi(); resetPage(); loadSelect(); triggerSearch(); @@ -634,9 +592,9 @@ function _make_mkws_team($, teamName) { } - function redraw_navi () + function redrawNavi () { - var navi = document.getElementById('mkwsNavi'); + var navi = findnode('.mkwsNavi'); if (!navi) return; var text = ""; @@ -646,24 +604,29 @@ function _make_mkws_team($, teamName) { } var filter = m_filters[i]; if (filter.id) { - text += 'Source: ' + filter.name + ''; + text += M('source') + ': ' + filter.name + ''; } else { - text += filter.field + ': ' + filter.value + ''; } } - navi.innerHTML = text; + navi.html(text); } - function drawPager (pagerDiv) + 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_recPerPage); + var pages = Math.ceil(m_totalRec / m_perpage); var firstClkbl = (m_curPage - onsides > 0) ? m_curPage - onsides @@ -673,9 +636,9 @@ function _make_mkws_team($, teamName) { ? firstClkbl + 2*onsides : pages; - var prev = '<< ' + M('Prev') + ' | '; + var prev = '<< ' + M('Prev') + ' | '; if (m_curPage > 1) - prev = '' + prev = '' +'<< ' + M('Prev') + ' | '; var middle = ''; @@ -684,13 +647,13 @@ function _make_mkws_team($, teamName) { if(i == m_curPage) numLabel = '' + i + ''; - middle += ' ' + middle += ' ' + numLabel + ' '; } - var next = ' | ' + M('Next') + ' >>'; + var next = ' | ' + M('Next') + ' >>'; if (pages - m_curPage > 0) - next = ' | ' + next = ' | ' + M('Next') + ' >>'; var predots = ''; @@ -701,61 +664,61 @@ function _make_mkws_team($, teamName) { if (lastClkbl < pages) postdots = '...'; - pagerDiv.innerHTML += '
' + s += '
' + prev + predots + middle + postdots + next + '
'; + + return s; } - mkws.showPage = function (pageNum) + that.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(); + that.pagerNext = function () { + if (m_totalRec - m_perpage*m_curPage > 0) { + m_paz.showNext(); m_curPage++; } } - mkws.pagerPrev = function () { - if (mkws.paz.showPrev() != false) + that.pagerPrev = function () { + if (m_paz.showPrev() != false) m_curPage--; } // switching view between targets and records - mkws.switchView = function(view) { - debug("switchView: " + view); - - var targets = document.getElementById('mkwsTargets'); - var results = document.getElementById('mkwsResults') || - document.getElementById('mkwsRecords'); - var blanket = document.getElementById('mkwsBlanket'); - var motd = document.getElementById('mkwsMOTD'); + function switchView(view) { + var targets = findnode('.mkwsTargets'); + var results = findnode('.mkwsResults,.mkwsRecords'); + var blanket = findnode('.mkwsBlanket'); + var motd = findnode('.mkwsMOTD'); 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(" + m_teamName + ", '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 + "'"); @@ -763,14 +726,17 @@ function _make_mkws_team($, teamName) { } + that.switchView = switchView; + + // detailed record drawing - mkws.showDetails = function (prefixRecId) { + that.showDetails = function (prefixRecId) { var recId = prefixRecId.replace('mkwsRec_', ''); var oldRecId = m_curDetRecId; m_curDetRecId = recId; // remove current detailed view if any - var detRecordDiv = document.getElementById('mkwsDet_'+oldRecId); + var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecId); // lovin DOM! if (detRecordDiv) detRecordDiv.parentNode.removeChild(detRecordDiv); @@ -782,53 +748,345 @@ function _make_mkws_team($, teamName) { return; } // request the record - mkws.paz.record(recId); + debug("showDetails() requesting 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"); - var details = template(data); - return '
' + details + '
'; - } + /* + * All the HTML stuff to render the search forms and + * result pages. + */ + function mkwsHtmlAll() { + mkwsSetLang(); + if (mkws_config.show_lang) + mkwsHtmlLang(); + debug("HTML search form"); + mkws.handleNodeWithTeam(findnode('.mkwsSearch'), function(tname) { + this.html('\ +
\ + \ + \ +
'); + }); - function loadTemplate(name) - { - var template = mkws['template' + name]; + debug("HTML records"); + // 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 (findnode(".mkwsResults").length) { + findnode(".mkwsResults").html('\ +\ + \ + \ + \ + \ + \ + \ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
'); + } - if (template === undefined) { - var source = $("#mkwsTemplate" + name).html(); - if (!source) { - source = defaultTemplate(name); + var node = findnode(".mkwsRanking"); + if (node.length) { + var ranking_data = ''; + ranking_data += '
'; + if (mkws_config.show_sort) { + ranking_data += M('Sort by') + ' ' + mkwsHtmlSort() + ' '; } + if (mkws_config.show_perpage) { + ranking_data += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.'; + } + ranking_data += '
'; - template = Handlebars.compile(source); - debug("compiled template '" + name + "'"); - mkws['template' + name] = template; + node.html(ranking_data); } - return template; - } + mkwsHtmlSwitch(); + 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); - function defaultTemplate(name) + // on first page, hide the termlist + $(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.appendTo(container); + } + } + + + function mkwsSetLang() { + var lang = parseQuerystring().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 mkwsHtmlSwitch() { + debug("HTML switch for team " + m_teamName); + + var node = findnode(".mkwsSwitch"); + node.append($('' + M('Records') + '')); + node.append($("", { text: " | " })); + node.append($('' + M('Targets') + '')); + + debug("HTML targets"); + var node = findnode(".mkwsTargets"); + node.html('\ +
\ + No information available yet.\ +
'); + node.css("display", "none"); + } + + + function mkwsHtmlSort() { + debug("HTML sort, m_sort = '" + m_sort + "'"); + var sort_html = ''; + + return sort_html; + } + + + function mkwsHtmlPerpage() { + debug("HTML perpage, m_perpage = " + m_perpage); + var perpage_html = ''; + + return perpage_html; + } + + + /* 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 += ' | '; + + if (lang == l) { + data += ' ' + l + ' '; + } else { + data += ' ' + l + ' ' + } + } + + findnode(".mkwsLang").html(data); + } + + + that.runAutoSearch = function() { + // ### should check mkwsTermlist as well, for facet-only teams + var node = findnode('.mkwsRecords'); + var query = node.attr('autosearch'); + if (!query) + return; + + if (query.match(/^!param!/)) { + var param = query.replace(/^!param!/, ''); + query = 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); + } + + + // 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; + } + + + // 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 */ + function M(word) { + var lang = mkws_config.lang; + + if (!lang || !mkws.locale_lang[lang]) + return word; + + return mkws.locale_lang[lang][word] || word; + } + 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 = defaultTemplate(name); + } + + template = Handlebars.compile(source); + debug("compiled template '" + name + "'"); + m_template[name] = template; + } + + return template; + } + + + function defaultTemplate(name) { if (name === 'Record') { return '\ @@ -859,10 +1117,10 @@ function _make_mkws_team($, teamName) { {{/if}}\ {{#if md-electronic-url}}\ \ - {{translate "URL"}}\ + {{translate "Links"}}\ \ {{#each md-electronic-url}}\ - {{this}}
\ + Link{{index1}}\ {{/each}}\ \ \ @@ -873,7 +1131,8 @@ function _make_mkws_team($, teamName) { \ {{#first location having="md-subject"}}\ {{#if md-subject}}\ - {{md-subject}}\ + {{#commaList md-subject}}\ + {{this}}{{/commaList}}\ {{/if}}\ {{/first}}\ \ @@ -908,208 +1167,182 @@ function _make_mkws_team($, teamName) { } - /* - * All the HTML stuff to render the search forms and - * result pages. - */ - function mkws_html_all() { - mkws_set_lang(); - if (mkws_config.show_lang) - mkws_html_lang(); - - // For some reason, doing this programmatically results in - // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML. - debug("HTML search form"); - $("#mkwsSearch").html('\ -
\ - \ - \ -
'); + // main + (function() { + try { + mkwsHtmlAll() + } - 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('\ -\ - \ - \ - \ - \ - \ - \ - \ -
\ -
\ -
\ -
\ -
\ -
\ -
\ -
\ -
\ -
'); + catch (e) { + mkws_config.error = e.message; + // alert(e.message); } + })(); - if ($("#mkwsRanking").length) { - var ranking_data = ''; - ranking_data += '
'; - if (mkws_config.show_sort) { - ranking_data += M('Sort by') + ' ' + mkws_html_sort() + ' '; - } - if (mkws_config.show_perpage) { - ranking_data += M('and show') + ' ' + mkws_html_perpage() + ' ' + M('per page') + '.'; - } - ranking_data += '
'; + // Bizarrely, 'that' is just an empty hash. All its state is in + // the closure variables defined earlier in this function. + return that; +}; - $("#mkwsRanking").html(ranking_data); - } - mkws_html_switch(); +// wrapper to call team() after page load +(function (j) { + mkws.debug = function (string) { + if (!mkws.debug_level) + return; - 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); - } else { - // raw pp2 - run_auto_searches(); + if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */ + return; } - 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() }); + // 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 + ' <<<'); } - - 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) { - // Move the MOTD from the provided element down into the container - motd.parentNode.removeChild(motd); - container.appendChild(motd); + 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 teamName, type; + + for (var i = 0; i < list.length; i++) { + var cname = list[i]; + if (cname.match(/^mkwsTeam_/)) { + teamName = cname.replace(/^mkwsTeam_/, ''); + } else if (cname.match(/^mkws/)) { + type = cname.replace(/^mkws/, ''); + } + } + callback.call(node, teamName, type); } - function run_auto_searches() { - debug("running auto searches"); + mkws.resizePage = function () { + var list = ["mkwsSwitch", "mkwsLang"]; - $('[id^="mkwsRecords"]').each(function () { - var node = $(this); - var query = node.attr('autosearch'); + var width = mkws_config.responsive_design_width; + var parent = $(".mkwsTermlists").parent(); - if (query) { - var windowid = undefined; - var id = node.attr('id'); - if (id.match(/^mkwsRecords_/, '')) { - windowid = id.replace(/^mkwsRecords_/, ''); + 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(); } - - 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); } - }); - } + } 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(); + } + } + } + }; - // 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; + mkws.switchView = function(tname, view) { + mkws.teams[tname].switchView(view); } - - function mkws_set_lang() { - var lang = parseQuerystring().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; + mkws.showDetails = function (tname, prefixRecId) { + mkws.teams[tname].showDetails(prefixRecId); } + mkws.limitTarget = function (tname, id, name) { + mkws.teams[tname].limitTarget(id, name); + } - function mkws_html_switch() { - debug("HTML switch"); - - $("#mkwsSwitch").append($('' + M('Records') + '')); - $("#mkwsSwitch").append($("", { text: " | " })); - $("#mkwsSwitch").append($('' + M('Targets') + '')); - - debug("HTML targets"); - $("#mkwsTargets").html('\ -
\ - No information available yet.\ -
'); - $("#mkwsTargets").css("display", "none"); + mkws.limitQuery = function (tname, field, value) { + mkws.teams[tname].limitQuery(field, value); } + mkws.delimitTarget = function (tname, id) { + mkws.teams[tname].delimitTarget(id); + } - function mkws_html_sort() { - debug("HTML sort, m_sort = '" + m_sort + "'"); - var sort_html = ''; + mkws.pagerPrev = function (tname) { + mkws.teams[tname].pagerPrev(); + } - return sort_html; + mkws.pagerNext = function (tname) { + mkws.teams[tname].pagerNext(); } - function mkws_html_perpage() { - debug("HTML perpage"); - var perpage_html = ''; - return perpage_html; + /* 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]); + } } @@ -1118,7 +1351,7 @@ function _make_mkws_team($, teamName) { * The username/password is configured in the apache config file * for the site. */ - function mkws_service_proxy_auth(auth_url, auth_domain, pp2_url) { + function authenticateSession(auth_url, auth_domain, pp2_url) { debug("Run service proxy auth URL: " + auth_url); if (!auth_domain) { @@ -1144,307 +1377,100 @@ function _make_mkws_team($, teamName) { debug("Service proxy auth successfully done"); mkws.authenticated = true; - run_auto_searches(); + runAutoSearches(); }); } - /* create locale language menu */ - function mkws_html_lang() { - 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 hash = {}; - for (var i = 0; i < lang_options.length; i++) { - hash[lang_options[i]] = 1; - } - - for (var k in mkws.locale_lang) { - if (hash[k] == 1 || lang_options.length == 0) - list.push(k); - } - - // add english link - if (lang_options.length == 0 || hash[lang_default] == 1) - 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 runAutoSearches() { + debug("running auto searches"); - if (lang == l) { - data += ' ' + l + ' '; - } else { - data += ' ' + l + ' ' - } + for (var teamName in mkws.teams) { + mkws.teams[teamName].runAutoSearch(); } - - $("#mkwsLang").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; - - if (!lang || !mkws.locale_lang[lang]) - return word; - - return mkws.locale_lang[lang][word] || word; - } - mkws.M = M; // so the Handlebars helper can use it - - - // main - (function() { - try { - mkws_html_all() - } - - 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; -}; - - -/* - * implement jQuery plugin $.pazpar2({}) - */ -function _mkws_jquery_plugin ($) { - var debug_level = 1; - - function debug (string) { - if (!debug_level) - return; - - if (typeof console === "undefined" || typeof console.log === "undefined") - return; - - console.log("jquery.pazpar2: " + string); - } - - 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"; - - debug("popup height: " + height + ", width: " + width); + $(document).ready(function() { + debug("on load ready"); + defaultMkwsConfig(); - // 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; + if (mkws_config.query_width < 5 || mkws_config.query_width > 150) { + debug("Reset query width: " + mkws_config.query_width); + mkws_config.query_width = 50; } - $(id_popup).dialog({ - closeOnEscape: true, - autoOpen: false, - height: height, - width: width, - modal: true, - resizable: true, - buttons: { - Cancel: function() { - $(this).dialog("close"); + 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 + "'"); } - }, - close: function() { } - }); - - $(id_button) - .button() - .click(function() { - $(id_popup).dialog("open"); - }); - }; - - $.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); } } - }); -}; + 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.resizePage() }); + // initial check after page load + $(document).ready(function() { mkws.resizePage() }); + } -// wrapper to call _make_mkws_team() after page load -(function (j) { - function log(s) { - if (console && console.log) 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"); + // 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", "Results", "Records", "Targets", "Ranking", - "Termlists", "Stat" ]; + "Termlists", "Stat", "MOTD" ]; for (var i = 0; i < ids.length; i++) { 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); + debug("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"); - - // Find all nodes with class (NOT id) mkwsRecords, and - // determine their team from the mkwsTeam_* class. So: - //
- $('.mkwsResults, .mkwsRecords, .mkwsTermlists').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_/, ''); - } - } - 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 { - mkws.teams[tname] = _make_mkws_team(j, tname); - log("Made MKWS team '" + tname + "'"); + // 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_/)) { + debug("adding AUTO team to node with class '" + this.className + "'"); + $(this).addClass('mkwsTeam_AUTO'); } }); + + // Find all nodes with an class, and determine their team from + // the mkwsTeam_* class. Make all team objects. + var then = $.now(); + $('[class^="mkws"],[class*=" mkws"]').each(function () { + mkws.handleNodeWithTeam(this, function(tname, type) { + if (!mkws.teams[tname]) { + mkws.teams[tname] = team(j, tname); + debug("Made MKWS team '" + tname + "'"); + } + var myTeam = mkws.teams[tname]; + var myWidget = widget(j, myTeam, type, this); + }); + }); + var now = $.now(); + debug("Walking MKWS nodes took " + (now-then) + " ms"); + + if (mkws_config.use_service_proxy) { + authenticateSession(mkws_config.service_proxy_auth, + mkws_config.service_proxy_auth_domain, + mkws_config.pazpar2_url); + } else { + // raw pp2 + runAutoSearches(); + } }); })(jQuery);