X-Git-Url: http://git.indexdata.com/?p=mkws-moved-to-github.git;a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=e04721562aa1679350e98ec9441070d7d3487636;hp=9cbd28323a30ae79b52e7f5bf063378fab3a691c;hb=9102c6dd9e9aa1c442bbfe5bf1baa6a3da7ffce8;hpb=5d67f02b01d5d376478471eda5393958507e3587 diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 9cbd283..e047215 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -142,29 +142,6 @@ 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 = {}; @@ -179,10 +156,62 @@ function widget($, team, type, node) { node: node }; - // ### More to do here, surely: e.g. wiring into the team - mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node); + var M = mkws.M; + + if (type === 'Targets') { + promoteTargets(); + } else if (type === 'Stat') { + promoteStat(); + } else { + // ### Handle other types here + } + mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node); return that; + + + // 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 ='' + + '' + + '' + + '' + + '' + + '' + + ''; + + for (var i = 0; i < data.length; i++) { + table += ""; + } + + 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 + "
'; + var subnode = $(node).children('.mkwsBytarget'); + subnode.html(table); + }); + } + + + function promoteStat() { + team.queue("stat").subscribe(function(data) { + if (node.length === 0) alert("huh?!"); + + $(node).html('' + M('Status info') + '' + + ' -- ' + + '' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' + + ' -- ' + + '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + ''); + }); + } } @@ -197,6 +226,7 @@ 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_sort; // will be set below @@ -249,53 +279,26 @@ function team($, teamName) { // // pz2.js event handlers: // - function onInit(teamName) { + function onInit() { debug("init"); m_paz.stat(); m_paz.bytarget(); } - function onBytarget(data, teamName) { + function onBytarget(data) { debug("target"); - var node = findnode('.mkwsBytarget'); - if (node.length === 0) return; - - var table ='' + - '' + - '' + - '' + - '' + - '' + - ''; - - for (var i = 0; i < data.length; i++) { - table += ""; - } - - 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); + queue("targets").publish(data); } - function onStat(data, teamName) { + function onStat(data) { debug("stat"); - var node = findnode('.mkwsStat'); - if (node.length === 0) return; - - node.html('' + M('Status info') + '' + - ' -- ' + - '' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '' + - ' -- ' + - '' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + ''); + queue("stat").publish(data); } - function onTerm(data, teamName) { + function onTerm(data) { debug("term"); var node = findnode(".mkwsTermlists"); if (node.length == 0) return; @@ -366,9 +369,10 @@ function team($, teamName) { 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); - recordDiv.innerHTML += html; + $(recordDiv).append(html); } @@ -843,7 +847,7 @@ function team($, teamName) { function mkwsSetLang() { - var lang = parseQuerystring().lang || mkws_config.lang; + var lang = mkws.getParameterByName("lang") || mkws_config.lang; if (!lang || !mkws.locale_lang[lang]) { mkws_config.lang = "" } else { @@ -957,15 +961,14 @@ function team($, teamName) { that.runAutoSearch = function() { - // ### should check mkwsTermlist as well, for facet-only teams - var node = findnode('.mkwsRecords'); + var node = findnode('.mkwsRecords,.mkwsTermlists'); var query = node.attr('autosearch'); if (!query) return; if (query.match(/^!param!/)) { var param = query.replace(/^!param!/, ''); - query = getParameterByName(param); + query = mkws.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"); @@ -993,31 +996,11 @@ function team($, teamName) { } - // 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 */ + // Translation function. At present, this is properly a + // global-level function (hence the assignment to mkws.M) but we + // want to make it per-team so different teams can operate in + // different languages. + // function M(word) { var lang = mkws_config.lang; @@ -1074,7 +1057,7 @@ function team($, teamName) { var source = node.html(); if (!source) { - source = defaultTemplate(name); + source = mkws.defaultTemplate(name); } template = Handlebars.compile(source); @@ -1086,86 +1069,28 @@ function team($, teamName) { } - function defaultTemplate(name) - { - if (name === 'Record') { - return '\ -\ - \ - \ - \ - \ - {{#if md-date}}\ - \ - \ - \ - \ - {{/if}}\ - {{#if md-author}}\ - \ - \ - \ - \ - {{/if}}\ - {{#if md-electronic-url}}\ - \ - \ - \ - \ - {{/if}}\ - {{#if-any location having="md-subject"}}\ - \ - \ - \ - \ - {{/if-any}}\ - \ - \ - \ - \ -
{{translate "Title"}}\ - {{md-title}}\ - {{#if md-title-remainder}}\ - ({{md-title-remainder}})\ - {{/if}}\ - {{#if md-title-responsibility}}\ - {{md-title-responsibility}}\ - {{/if}}\ -
{{translate "Date"}}{{md-date}}
{{translate "Author"}}{{md-author}}
{{translate "Links"}}\ - {{#each md-electronic-url}}\ - Link{{index1}}\ - {{/each}}\ -
{{translate "Subject"}}\ - {{#first location having="md-subject"}}\ - {{#if md-subject}}\ - {{#commaList md-subject}}\ - {{this}}{{/commaList}}\ - {{/if}}\ - {{/first}}\ -
{{translate "Locations"}}\ - {{#commaList location}}\ - {{attr "@name"}}{{/commaList}}\ -
\ -'; - } else if (name === "Summary") { - return '\ -\ - {{md-title}}\ -\ -{{#if md-title-remainder}}\ - {{md-title-remainder}}\ -{{/if}}\ -{{#if md-title-responsibility}}\ - {{md-title-responsibility}}\ -{{/if}}\ -'; + // The following PubSub code is modified from the jQuery manual: + // https://api.jquery.com/jQuery.Callbacks/ + // + // Use as: + // team.queue("eventName").subscribe(function(param1, param2 ...) { ... }); + // team.queue("eventName").publish(arg1, arg2, ...); + + var queues = {}; + var queue = function(id) { + if (!queues[id]) { + var callbacks = $.Callbacks(); + queues[id] = { + publish: callbacks.fire, + subscribe: callbacks.add, + unsubscribe: callbacks.remove + }; } - - var s = "There is no default '" + name +"' template!"; - alert(s); - return s; + return queues[id]; } + that.queue = queue; + // main (function() { @@ -1301,6 +1226,97 @@ function team($, teamName) { } + // This function is taken from a StackOverflow answer + // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144 + mkws.getParameterByName = function(name) { + name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); + var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), + results = regex.exec(location.search); + return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); + } + + + mkws.defaultTemplate = function(name) + { + if (name === 'Record') { + return '\ +\ + \ + \ + \ + \ + {{#if md-date}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if md-author}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if md-electronic-url}}\ + \ + \ + \ + \ + {{/if}}\ + {{#if-any location having="md-subject"}}\ + \ + \ + \ + \ + {{/if-any}}\ + \ + \ + \ + \ +
{{translate "Title"}}\ + {{md-title}}\ + {{#if md-title-remainder}}\ + ({{md-title-remainder}})\ + {{/if}}\ + {{#if md-title-responsibility}}\ + {{md-title-responsibility}}\ + {{/if}}\ +
{{translate "Date"}}{{md-date}}
{{translate "Author"}}{{md-author}}
{{translate "Links"}}\ + {{#each md-electronic-url}}\ + Link{{index1}}\ + {{/each}}\ +
{{translate "Subject"}}\ + {{#first location having="md-subject"}}\ + {{#if md-subject}}\ + {{#commaList md-subject}}\ + {{this}}{{/commaList}}\ + {{/if}}\ + {{/first}}\ +
{{translate "Locations"}}\ + {{#commaList location}}\ + {{attr "@name"}}{{/commaList}}\ +
\ +'; + } else if (name === "Summary") { + return '\ +\ + {{md-title}}\ +\ +{{#if md-title-remainder}}\ + {{md-title-remainder}}\ +{{/if}}\ +{{#if md-title-responsibility}}\ + {{md-title-responsibility}}\ +{{/if}}\ +'; + } + + var s = "There is no default '" + name +"' template!"; + alert(s); + return s; + } + + function defaultMkwsConfig() { /* default mkws config */ var config_default = {