X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=d03dcd50f365352535a70978f317b901f7394c7a;hb=697da818a4a182e4ae50c13b6937226fa3ba921f;hp=50eec65fd76f91d8f0c37738a4b530e8167a92ef;hpb=b9d32463b38074309f2c80145094f597ae98daa3;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 50eec65..d03dcd5 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -3,7 +3,7 @@ * See the file LICENSE for details */ -"use strict"; // HTML5: disable for debug_level >= 2 +"use strict"; // HTML5: disable for log_level >= 2 // Handlebars helpers @@ -76,8 +76,8 @@ Handlebars.registerHelper('index1', function(obj) { // var mkws = { authenticated: false, - debug_level: 1, // Will be overridden from mkws_config, but - // initial value allows jQuery popup to use logging. + log_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: { @@ -158,26 +158,38 @@ function widget($, team, type, node) { var M = mkws.M; - if (type === 'Targets') { - promoteTargets(); - } else if (type === 'Stat') { - promoteStat(); + var type2fn = { + Targets: promoteTargets, + Stat: promoteStat, + Termlists: promoteTermlists, + Pager: promotePager, + Records: promoteRecords, + Navi: promoteNavi, + Sort: promoteSort, + Perpage: promotePerpage + }; + + var promote = type2fn[type]; + if (promote) { + promote(); + log("made " + type + " widget(node=" + node + ")"); } else { - // ### Handle other types here + log("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")"); } - mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node); return that; + function log(s) { + team.log(s); + } + // Functions follow for promoting the regular widget object into // widgets of specific types. These could be moved outside of the // widget object, or even into their own source files. function promoteTargets() { team.queue("targets").subscribe(function(data) { - if (node.length === 0) alert("huh?!"); - var table ='' + '' + '' + @@ -212,6 +224,207 @@ function widget($, team, type, node) { '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + ''); }); } + + + function promoteTermlists() { + team.queue("termlists").subscribe(function(data) { + if (!node) { + alert("termlists event when there are no termlists"); + return; + } + + // no facets: this should never happen + if (!mkws_config.facets || mkws_config.facets.length == 0) { + alert("onTerm called even though we have no facets: " + $.toJSON(data)); + $(node).hide(); + return; + } + + // display if we first got results + $(node).show(); + + var acc = []; + acc.push('
' + M('Termlists') + '
'); + var facets = mkws_config.facets; + + for (var i = 0; i < facets.length; i++) { + 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] + "'"); + } + } + + $(node).html(acc.join('')); + + 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('
'); + } + }); + } + + + function promotePager() { + team.queue("pager").subscribe(function(data) { + $(node).html(drawPager(data)) + + 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(team.totalRecordCount() / team.perpage()); + var currentPage = team.currentPage(); + + var firstClkbl = (currentPage - onsides > 0) + ? currentPage - onsides + : 1; + + var lastClkbl = firstClkbl + 2*onsides < pages + ? firstClkbl + 2*onsides + : pages; + + var prev = '<< ' + M('Prev') + ' | '; + if (currentPage > 1) + prev = '' + +'<< ' + M('Prev') + ' | '; + + var middle = ''; + for(var i = firstClkbl; i <= lastClkbl; i++) { + var numLabel = i; + if(i == currentPage) + numLabel = '' + i + ''; + + middle += ' ' + + numLabel + ' '; + } + + var next = ' | ' + M('Next') + ' >>'; + if (pages - currentPage > 0) + next = ' | ' + + M('Next') + ' >>'; + + var predots = ''; + if (firstClkbl > 1) + predots = '...'; + + var postdots = ''; + if (lastClkbl < pages) + postdots = '...'; + + s += '
' + + prev + predots + middle + postdots + next + '
'; + + return s; + } + }); + } + + + function promoteRecords() { + team.queue("records").subscribe(function(data) { + var html = []; + for (var i = 0; i < data.hits.length; i++) { + var hit = data.hits[i]; + var divId = team.recordElementId(hit.recid[0]); + html.push('
', renderSummary(hit), '
'); + // ### At some point, we may be able to move the + // m_currentRecordId and m_currentRecordData members + // from the team object into this widget. + if (hit.recid == team.currentRecordId()) { + if (team.currentRecordData()) + html.push(team.renderDetails(team.currentRecordData())); + } + } + $(node).html(html.join('')); + + function renderSummary(hit) + { + var template = team.loadTemplate("Summary"); + hit._id = team.recordElementId(hit.recid[0]); + hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;" + return template(hit); + } + }); + } + + + function promoteNavi() { + team.queue("navi").subscribe(function() { + var filters = team.filters(); + var text = ""; + + for (var i in filters) { + if (text) { + text += " | "; + } + var filter = filters[i]; + if (filter.id) { + text += M('source') + ': ' + filter.name + ''; + } else { + text += M(filter.field) + ': ' + filter.value + ''; + } + } + + $(node).html(text); + }); + } + + + function promoteSort() { + // It seems this and the Perpage widget doen't need to + // subscribe to anything, since they produce events rather + // than consuming them. + $(node).change(function () { + team.set_sortOrder($(node).val()); + if (team.submitted()) { + team.resetPage(); + team.reShow(); + } + return false; + }); + } + + + function promotePerpage() { + $(node).change(function() { + team.set_perpage($(node).val()); + if (team.submitted()) { + team.resetPage(); + team.reShow(); + } + return false; + }); + } } @@ -226,17 +439,16 @@ function widget($, team, type, node) { function team($, teamName) { var that = {}; var m_teamName = teamName; - that.name = function() { return m_teamName; } var m_submitted = false; var m_query; // initially undefined var m_sortOrder; // will be set below var m_perpage; // will be set below var m_filters = []; - var m_totalRec = 0; - var m_curPage = 1; - var m_curDetRecId = ''; - var m_curDetRecData = null; - var m_debugTime = { + var m_totalRecordCount = 0; + var m_currentPage = 1; + var m_currentRecordId = ''; + var m_currentRecordData = null; + var m_logTime = { // Timestamps for logging "start": $.now(), "last": $.now() @@ -244,21 +456,34 @@ function team($, teamName) { var m_paz; // will be initialised below var m_template = {}; + that.name = function() { return m_teamName; } + that.submitted = function() { return m_submitted; } + that.perpage = function() { return m_perpage; } + that.totalRecordCount = function() { return m_totalRecordCount; } + that.currentPage = function() { return m_currentPage; } + that.currentRecordId = function() { return m_currentRecordId; } + that.currentRecordData = function() { return m_currentRecordData; } + that.filters = function() { return m_filters; } + + that.set_sortOrder = function(val) { m_sortOrder = val }; + that.set_perpage = function(val) { m_perpage = val }; + - var debug = function (s) { + var log = function (s) { var now = $.now(); - var timestamp = ((now - m_debugTime.start)/1000).toFixed(3) + " (+" + ((now - m_debugTime.last)/1000).toFixed(3) + ") " - m_debugTime.last = now; + var timestamp = ((now - m_logTime.start)/1000).toFixed(3) + " (+" + ((now - m_logTime.last)/1000).toFixed(3) + ") " + m_logTime.last = now; - mkws.debug(m_teamName + ": " + timestamp + s); + mkws.log(m_teamName + ": " + timestamp + s); } + that.log = log; - debug("start running MKWS"); + log("start running MKWS"); m_sortOrder = mkws_config.sort_default; m_perpage = mkws_config.perpage_default; - debug("Create main pz2 object"); + log("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 @@ -280,127 +505,69 @@ function team($, teamName) { // pz2.js event handlers: // function onInit() { - debug("init"); + log("init"); m_paz.stat(); m_paz.bytarget(); } function onBytarget(data) { - debug("target"); + log("target"); queue("targets").publish(data); } function onStat(data) { - debug("stat"); + log("stat"); queue("stat").publish(data); } function onTerm(data) { - 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) { - alert("onTerm called even though we have no facets: " + $.toJSON(data)); - node.hide(); - return; - } - - // display if we first got results - node.show(); - - var acc = []; - acc.push('
' + M('Termlists') + '
'); - var facets = mkws_config.facets; - - for(var i = 0; i < facets.length; i++) { - 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] + "'"); - } - } - - node.html(acc.join('')); + log("term"); + queue("termlists").publish(data); } function onShow(data, teamName) { - debug("show"); - m_totalRec = data.merged; - - var pager = findnode(".mkwsPager"); - if (pager.length) { - pager.html(drawPager(data)) - } - - var results = findnode(".mkwsRecords"); - if (!results.length) - return; - - 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) { - if (m_curDetRecData) - html.push(renderDetails(m_curDetRecData)); - } - } - results.html(html.join('')); + log("show"); + m_totalRecordCount = data.merged; + queue("pager").publish(data); + queue("records").publish(data); } function onRecord(data, args, teamName) { - debug("record"); + log("record"); // FIXME: record is async!! clearTimeout(m_paz.recordTimer); - // in case on_show was faster to redraw element - var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid); - if (detRecordDiv) return; - m_curDetRecData = data; - // Can't use jQuery's $('#x') syntax to find this ID, because it contains spaces. - var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid); - var html = renderDetails(m_curDetRecData); + // ##### restrict to current team + var detRecordDiv = document.getElementById(recordDetailsId(data.recid[0])); + if (detRecordDiv) { + // in case on_show was faster to redraw element + return; + } + m_currentRecordData = data; + var recordDiv = findnode('.' + recordElementId(m_currentRecordData.recid[0])); + var html = renderDetails(m_currentRecordData); $(recordDiv).append(html); } - 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('
'); + // Used by promoteRecords() and onRecord() + function recordElementId(s) { + return 'mkwsRec_' + s.replace(/[^a-z0-9]/ig, '_'); + } + that.recordElementId = recordElementId; + + // Used by onRecord(), showDetails() and renderDetails() + function recordDetailsId(s) { + return 'mkwsDet_' + s.replace(/[^a-z0-9]/ig, '_'); } + that.recordElementId = recordElementId; - function targetFiltered(id) { + that.targetFiltered = function(id) { for (var i = 0; i < m_filters.length; i++) { if (m_filters[i].id === id || m_filters[i].id === 'pz:id=' + id) { @@ -411,60 +578,6 @@ function team($, teamName) { } - function drawPager (data) - { - var s = '
' + M('Displaying') + ': ' - + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) + - ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': ' - + data.total + ')
'; - - //client indexes pages from 1 but pz2 from 0 - var onsides = 6; - var pages = Math.ceil(m_totalRec / m_perpage); - - var firstClkbl = (m_curPage - onsides > 0) - ? m_curPage - onsides - : 1; - - var lastClkbl = firstClkbl + 2*onsides < pages - ? firstClkbl + 2*onsides - : pages; - - var prev = '<< ' + M('Prev') + ' | '; - if (m_curPage > 1) - prev = '' - +'<< ' + M('Prev') + ' | '; - - var middle = ''; - for(var i = firstClkbl; i <= lastClkbl; i++) { - var numLabel = i; - if(i == m_curPage) - numLabel = '' + i + ''; - - middle += ' ' - + numLabel + ' '; - } - - var next = ' | ' + M('Next') + ' >>'; - if (pages - m_curPage > 0) - next = ' | ' - + M('Next') + ' >>'; - - var predots = ''; - if (firstClkbl > 1) - predots = '...'; - - var postdots = ''; - if (lastClkbl < pages) - postdots = '...'; - - s += '
' - + prev + predots + middle + postdots + next + '
'; - - return s; - } - - //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -480,7 +593,7 @@ function team($, teamName) { function newSearch(query, sortOrder, targets) { - debug("newSearch: " + query); + log("newSearch: " + query); if (mkws_config.use_service_proxy && !mkws.authenticated) { alert("searching before authentication"); @@ -488,78 +601,91 @@ function team($, teamName) { } m_filters = [] - redrawNavi(); - resetPage(); - loadSelect(); triggerSearch(query, sortOrder, targets); switchView('records'); // In case it's configured to start off as hidden m_submitted = true; } - function onSelectDdChange() + // limit by target functions + that.limitTarget = function (id, name) { - if (!m_submitted) return false; - resetPage(); - loadSelect(); - m_paz.show(0, m_perpage, m_sortOrder); + log("limitTarget(id=" + id + ", name=" + name + ")"); + m_filters.push({ id: id, name: name }); + triggerSearch(); return false; } - function redrawNavi () + // limit the query after clicking the facet + that.limitQuery = function (field, value) { - var navi = findnode('.mkwsNavi'); - if (!navi) return; + log("limitQuery(field=" + field + ", value=" + value + ")"); + m_filters.push({ field: field, value: value }); + triggerSearch(); + return false; + } - var text = ""; + + that.delimitTarget = function (id) + { + log("delimitTarget(id=" + id + ")"); + var newFilters = []; for (var i in m_filters) { - if (text) { - text += " | "; - } var filter = m_filters[i]; if (filter.id) { - text += M('source') + ': ' + filter.name + ''; + log("delimitTarget() removing filter " + $.toJSON(filter)); } else { - text += M(filter.field) + ': ' + filter.value + ''; + log("delimitTarget() keeping filter " + $.toJSON(filter)); + newFilters.push(filter); } } + m_filters = newFilters; - navi.html(text); + triggerSearch(); + return false; } - function resetPage() + that.delimitQuery = function (field, value) { - m_curPage = 1; - m_totalRec = 0; + log("delimitQuery(field=" + field + ", value=" + value + ")"); + var newFilters = []; + for (var i in m_filters) { + var filter = m_filters[i]; + if (filter.field && + field == filter.field && + value == filter.value) { + log("delimitQuery() removing filter " + $.toJSON(filter)); + } else { + log("delimitQuery() keeping filter " + $.toJSON(filter)); + newFilters.push(filter); + } + } + m_filters = newFilters; + + triggerSearch(); + return false; } - function loadSelect () + function resetPage() { - var node = findnode('.mkwsSort'); - if (node.length && node.val() != m_sortOrder) { - debug("changing m_sortOrder from " + m_sortOrder + " to " + node.val()); - m_sortOrder = 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(); - } + m_currentPage = 1; + m_totalRecordCount = 0; } + that.resetPage = resetPage; function triggerSearch (query, sortOrder, targets) { + resetPage(); + queue("navi").publish(); + var pp2filter = ""; var pp2limit = ""; - // Re-use previous query/sort-order if new ones are not specified + // Continue to use previous query/sort-order unless new ones are specified if (query) { m_query = query; } @@ -576,7 +702,7 @@ function team($, teamName) { if (pp2filter) pp2filter += ","; if (filter.id.match(/^[a-z:]+[=~]/)) { - debug("filter '" + filter.id + "' already begins with SETTING OP"); + log("filter '" + filter.id + "' already begins with SETTING OP"); } else { filter.id = 'pz:id=' + filter.id; } @@ -593,7 +719,7 @@ function team($, teamName) { params.limit = pp2limit; } - debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params)); + log("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params)); // We can use: params.torusquery = "udb=NAME" // Note: that won't work when running against raw pazpar2 @@ -601,99 +727,31 @@ function team($, teamName) { } - // limit by target functions - that.limitTarget = function (id, name) - { - debug("limitTarget(id=" + id + ", name=" + name + ")"); - m_filters.push({ id: id, name: name }); - redrawNavi(); - resetPage(); - loadSelect(); - triggerSearch(); - return false; - } - - - // limit the query after clicking the facet - that.limitQuery = function (field, value) - { - debug("limitQuery(field=" + field + ", value=" + value + ")"); - m_filters.push({ field: field, value: value }); - redrawNavi(); - resetPage(); - loadSelect(); - triggerSearch(); - return false; - } - - - that.delimitTarget = function (id) - { - debug("delimitTarget(id=" + id + ")"); - var newFilters = []; - for (var i in m_filters) { - var filter = m_filters[i]; - if (filter.id) { - debug("delimitTarget() removing filter " + $.toJSON(filter)); - } else { - debug("delimitTarget() keeping filter " + $.toJSON(filter)); - newFilters.push(filter); - } - } - m_filters = newFilters; - - redrawNavi(); - resetPage(); - loadSelect(); - triggerSearch(); - return false; + that.reShow = function() { + m_paz.show(0, m_perpage, m_sortOrder); } - that.delimitQuery = function (field, value) - { - debug("delimitQuery(field=" + field + ", value=" + value + ")"); - var newFilters = []; - for (var i in m_filters) { - var filter = m_filters[i]; - if (filter.field && - field == filter.field && - value == filter.value) { - debug("delimitQuery() removing filter " + $.toJSON(filter)); - } else { - debug("delimitQuery() keeping filter " + $.toJSON(filter)); - newFilters.push(filter); - } - } - m_filters = newFilters; - - redrawNavi(); - resetPage(); - loadSelect(); - triggerSearch(); - return false; - } - that.showPage = function (pageNum) { - m_curPage = pageNum; - m_paz.showPage(m_curPage - 1); + m_currentPage = pageNum; + m_paz.showPage(m_currentPage - 1); } // simple paging functions that.pagerNext = function () { - if (m_totalRec - m_perpage*m_curPage > 0) { + if (m_totalRecordCount - m_perpage*m_currentPage > 0) { m_paz.showNext(); - m_curPage++; + m_currentPage++; } } that.pagerPrev = function () { if (m_paz.showPrev() != false) - m_curPage--; + m_currentPage--; } @@ -728,31 +786,29 @@ function team($, teamName) { alert("Unknown view '" + view + "'"); } } - - that.switchView = switchView; // detailed record drawing - that.showDetails = function (prefixRecId) { - var recId = prefixRecId.replace('mkwsRec_', ''); - var oldRecId = m_curDetRecId; - m_curDetRecId = recId; + that.showDetails = function (recId) { + var oldRecordId = m_currentRecordId; + m_currentRecordId = recId; // remove current detailed view if any - var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecId); + // ##### restrict to current team + var detRecordDiv = document.getElementById(recordDetailsId(oldRecordId)); // lovin DOM! if (detRecordDiv) detRecordDiv.parentNode.removeChild(detRecordDiv); // if the same clicked, just hide - if (recId == oldRecId) { - m_curDetRecId = ''; - m_curDetRecData = null; + if (recId == oldRecordId) { + m_currentRecordId = ''; + m_currentRecordData = null; return; } // request the record - debug("showDetails() requesting record '" + recId + "'"); + log("showDetails() requesting record '" + recId + "'"); m_paz.record(recId); } @@ -766,7 +822,7 @@ function team($, teamName) { if (mkws_config.show_lang) mkwsHtmlLang(); - debug("HTML search form"); + log("HTML search form"); mkws.handleNodeWithTeam(findnode('.mkwsSearch'), function(tname) { this.html('\ \ @@ -775,7 +831,7 @@ function team($, teamName) { '); }); - debug("HTML records"); + log("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 @@ -785,8 +841,7 @@ function team($, teamName) { // .mkwsPager // .mkwsNavi // .mkwsRecords - if (findnode(".mkwsResults").length) { - findnode(".mkwsResults").html('\ + findnode(".mkwsResults").html('\
' + M('Target ID') + '' + M('Hits') + '
\ \ \ \
\ @@ -805,43 +860,27 @@ function team($, teamName) {
'); - } - 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 += '
'; - - node.html(ranking_data); + var 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 += '
'; + findnode(".mkwsRanking").html(ranking_data); 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); + findnode('.mkwsSearchForm').submit(onFormSubmitEventHandler); // 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) { + if (container.length) { // Move the MOTD from the provided element down into the container - motd.appendTo(container); + findnode(".mkwsMOTD").appendTo(container); } } @@ -854,7 +893,7 @@ function team($, teamName) { mkws_config.lang = lang; } - debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none")); + log("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none")); return mkws_config.lang; } @@ -881,7 +920,7 @@ function team($, teamName) { if (lang_options.length == 0 || toBeIncluded[lang_default]) list.push(lang_default); - debug("Language menu for: " + list.join(", ")); + log("Language menu for: " + list.join(", ")); /* the HTML part */ var data = ""; @@ -903,7 +942,7 @@ function team($, teamName) { function mkwsHtmlSort() { - debug("HTML sort, m_sortOrder = '" + m_sortOrder + "'"); + log("HTML sort, m_sortOrder = '" + m_sortOrder + "'"); var sort_html = ''; for(var i = 0; i < mkws_config.perpage_options.length; i++) { @@ -943,14 +982,14 @@ function team($, teamName) { function mkwsHtmlSwitch() { - debug("HTML switch for team " + m_teamName); + log("HTML switch for team " + m_teamName); var node = findnode(".mkwsSwitch"); node.append($('' + M('Records') + '')); node.append($("", { text: " | " })); node.append($('' + M('Targets') + '')); - debug("HTML targets"); + log("HTML targets"); var node = findnode(".mkwsTargets"); node.html('\
\ @@ -969,7 +1008,7 @@ function team($, teamName) { if (query.match(/^!param!/)) { var param = query.replace(/^!param!/, ''); query = mkws.getParameterByName(param); - debug("obtained query '" + query + "' from param '" + param + "'"); + log("obtained query '" + query + "' from param '" + param + "'"); if (!query) { alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter"); } @@ -977,20 +1016,20 @@ function team($, teamName) { var index = query.replace(/^!path!/, ''); var path = window.location.pathname.split('/'); query = path[path.length - index]; - debug("obtained query '" + query + "' from path-component '" + index + "'"); + log("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); + log("node=" + node + ", class='" + node.className + "', query=" + query); var sortOrder = node.attr('sort'); var targets = node.attr('targets'); var s = "running auto search: '" + query + "'"; if (sortOrder) s += " sorted by '" + sortOrder + "'"; if (targets) s += " in targets '" + targets + "'"; - debug(s); + log(s); newSearch(query, sortOrder, targets); } @@ -1027,21 +1066,13 @@ function team($, teamName) { } - 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 + '
'; + return '
' + details + '
'; } + that.renderDetails = renderDetails; function loadTemplate(name) @@ -1061,12 +1092,13 @@ function team($, teamName) { } template = Handlebars.compile(source); - debug("compiled template '" + name + "'"); + log("compiled template '" + name + "'"); m_template[name] = template; } return template; } + that.loadTemplate = loadTemplate; // The following PubSub code is modified from the jQuery manual: @@ -1088,7 +1120,6 @@ function team($, teamName) { } return queues[id]; } - that.queue = queue; @@ -1112,8 +1143,8 @@ function team($, teamName) { // wrapper to call team() after page load (function (j) { - mkws.debug = function (string) { - if (!mkws.debug_level) + mkws.log = function (string) { + if (!mkws.log_level) return; if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */ @@ -1121,14 +1152,14 @@ function team($, teamName) { } // you need to disable use strict at the top of the file!!! - if (mkws.debug_level >= 3) { + if (mkws.log_level >= 3) { console.log(arguments.callee.caller); - } else if (mkws.debug_level >= 2) { + } else if (mkws.log_level >= 2) { console.log(">>> called from function " + arguments.callee.caller.name + ' <<<'); } console.log(string); } - var debug = mkws.debug; + var log = mkws.log; mkws.handleNodeWithTeam = function(node, callback) { @@ -1139,7 +1170,7 @@ function team($, teamName) { // 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"); + mkws.log("handleNodeWithTeam() called on node with no classes"); return; } var list = classes.split(/\s+/) @@ -1165,7 +1196,7 @@ function team($, teamName) { if ($(window).width() <= width && parent.hasClass("mkwsTermlistContainer1")) { - debug("changing from wide to narrow: " + $(window).width()); + log("changing from wide to narrow: " + $(window).width()); $(".mkwsTermlistContainer1").hide(); $(".mkwsTermlistContainer2").show(); for (var tname in mkws.teams) { @@ -1176,7 +1207,7 @@ function team($, teamName) { } } else if ($(window).width() > width && parent.hasClass("mkwsTermlistContainer2")) { - debug("changing from narrow to wide: " + $(window).width()); + log("changing from narrow to wide: " + $(window).width()); $(".mkwsTermlistContainer1").show(); $(".mkwsTermlistContainer2").hide(); for (var tname in mkws.teams) { @@ -1335,21 +1366,25 @@ function team($, teamName) { 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 */ + log_level: 1, /* log 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; + // Set global log_level flag early so that log() works + // Fall back to old "debug_level" setting for backwards compatibility + var tmp = mkws_config.log_level; + if (typeof(tmp) === 'undefined') tmp = mkws_config.debug_level; + + if (typeof(tmp) !== 'undefined') { + mkws.log_level = tmp; + } else if (typeof(config_default.log_level) !== 'undefined') { + mkws.log_level = config_default.log_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...."); + log("ERROR: mkws_config is not an JS object, ignore it...."); mkws_config = {}; } @@ -1357,7 +1392,7 @@ function team($, teamName) { 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]); + //log("Set config: " + k + ' => ' + mkws_config[k]); } } @@ -1368,11 +1403,11 @@ function team($, teamName) { * for the site. */ function authenticateSession(auth_url, auth_domain, pp2_url) { - debug("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'); - debug("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) { @@ -1391,7 +1426,7 @@ function team($, teamName) { return; } - debug("Service proxy auth successfully done"); + log("Service proxy auth successfully done"); mkws.authenticated = true; runAutoSearches(); }); @@ -1399,7 +1434,7 @@ function team($, teamName) { function runAutoSearches() { - debug("running auto searches"); + log("running auto searches"); for (var teamName in mkws.teams) { mkws.teams[teamName].runAutoSearch(); @@ -1408,11 +1443,11 @@ function team($, teamName) { $(document).ready(function() { - debug("on load ready"); + log("on load ready"); defaultMkwsConfig(); if (mkws_config.query_width < 5 || mkws_config.query_width > 150) { - debug("Reset query width: " + mkws_config.query_width); + log("Reset query width: " + mkws_config.query_width); mkws_config.query_width = 50; } @@ -1422,7 +1457,7 @@ function team($, teamName) { var lang = key.replace(/^language_/, ""); // Copy custom languages into list mkws.locale_lang[lang] = mkws_config[key]; - debug("Added locally configured language '" + lang + "'"); + log("Added locally configured language '" + lang + "'"); } } } @@ -1438,7 +1473,7 @@ function team($, teamName) { // 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); + log("adjust protocol independent links: " + mkws_config.pazpar2_url); } // Backwards compatibility: set new magic class names on any @@ -1451,7 +1486,7 @@ function team($, teamName) { var node = $('#' + id); if (node.attr('id')) { node.addClass(id); - debug("added magic class to '" + node.attr('id') + "'"); + log("added magic class to '" + node.attr('id') + "'"); } } @@ -1459,26 +1494,34 @@ function team($, teamName) { // 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 + "'"); + log("adding AUTO team to node with class '" + this.className + "'"); $(this).addClass('mkwsTeam_AUTO'); } }); - // Find all nodes with an class, and determine their team from + // Find all nodes with an MKWS 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 + "'"); + log("Made MKWS team '" + tname + "'"); } + }); + }); + // Second pass: make the individual widget objects. This has + // to be done separately, and after the team-creation, since + // that sometimes makes new widget nodes (e.g. creating + // mkwsTermlists inside mkwsResults. + $('[class^="mkws"],[class*=" mkws"]').each(function () { + mkws.handleNodeWithTeam(this, function(tname, type) { var myTeam = mkws.teams[tname]; var myWidget = widget(j, myTeam, type, this); }); }); var now = $.now(); - debug("Walking MKWS nodes took " + (now-then) + " ms"); + log("Walking MKWS nodes took " + (now-then) + " ms"); if (mkws_config.use_service_proxy) { authenticateSession(mkws_config.service_proxy_auth,