X-Git-Url: http://git.indexdata.com/?p=mkws-moved-to-github.git;a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=2e0c5fd2d192f0a1b5d0b0a5522e54bab96119c7;hp=52a2270f916eb99b74792438799cab2d2c59828c;hb=23b60f99a77df0448ca337205716ea682b469611;hpb=d3ed2f66d323f81fbda5b562c564b3e14436ae77 diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 52a2270..2e0c5fd 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -65,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: that.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 windowid. +// 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: { @@ -143,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 +// 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_teamName = teamName; var m_submitted = false; var m_query; // initially undefined 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; @@ -168,56 +212,22 @@ function team($, teamName) { "last": $.now() }; var m_paz; // will be initialised below + var m_template = {}; - // if (console && console.log) // disabled, will fail in IE8 - // console.log("run team(" + (teamName ? teamName : "") + ")"); - - - // Needs to be defined inside team() so it can see m_debug_time - // ### member access won't work: there is only one instance of this function - mkws.debug_function = function (string) { - if (!mkws.debug_level) - return; - - 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(m_teamName + ": " + timestamp + string); + mkws.debug(m_teamName + ": " + timestamp + s); } - var debug = mkws.debug_function; // local alias + 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 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; @@ -242,7 +252,22 @@ function team($, teamName) { "onrecord": my_onrecord }); if (!isNaN(parseInt(mkws_config.perpage_default))) { - m_recPerPage = parseInt(mkws_config.perpage_default); + m_perpage = parseInt(mkws_config.perpage_default); + } + + + // 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); } @@ -260,18 +285,19 @@ function team($, teamName) { debug("show"); m_totalRec = data.merged; - var pager = $(".mkwsPager.mkwsTeam_" + m_teamName); + var pager = findnode(".mkwsPager"); if (pager.length) { pager.html(drawPager(data)) } - // navi - var results = $(".mkwsRecords.mkwsTeam_" + m_teamName); + 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('
', + html.push('
', renderSummary(hit), '
'); if (hit.recid == m_curDetRecId) { @@ -294,7 +320,7 @@ function team($, teamName) { function my_onstat(data, teamName) { debug("stat"); - var stat = $('.mkwsStat.mkwsTeam_' + teamName); + var stat = findnode('.mkwsStat'); if (stat.length === 0) return; @@ -308,7 +334,7 @@ function team($, teamName) { function my_onterm(data, teamName) { debug("term"); - var node = $(".mkwsTermlists.mkwsTeam_" + teamName); + var node = findnode(".mkwsTermlists"); if (node.length == 0) return; // no facets: this should never happen @@ -347,11 +373,13 @@ function team($, teamName) { for (var i = 0; i < data.length && i < max; i++) { acc.push('
'); acc.push('' + prev = '' +'<< ' + M('Prev') + ' | '; var middle = ''; @@ -669,13 +693,13 @@ function 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 = ''; @@ -693,7 +717,7 @@ function team($, teamName) { } - mkws.showPage = function (pageNum) + that.showPage = function (pageNum) { m_curPage = pageNum; m_paz.showPage(m_curPage - 1); @@ -701,15 +725,15 @@ function team($, teamName) { // simple paging functions - mkws.pagerNext = function () { - if (m_totalRec - m_recPerPage*m_curPage > 0) { + that.pagerNext = function () { + if (m_totalRec - m_perpage*m_curPage > 0) { m_paz.showNext(); m_curPage++; } } - mkws.pagerPrev = function () { + that.pagerPrev = function () { if (m_paz.showPrev() != false) m_curPage--; } @@ -717,10 +741,10 @@ function team($, teamName) { // switching view between targets and records that.switchView = function(view) { - var targets = $('.mkwsTargets.mkwsTeam_' + m_teamName); - var results = $('.mkwsResults.mkwsTeam_' + m_teamName + ',.mkwsRecords.mkwsTeam_' + m_teamName); - var blanket = $('.mkwsBlanket.mkwsTeam_' + m_teamName); - var motd = $('.mkwsMOTD.mkwsTeam_' + m_teamName); + var targets = findnode('.mkwsTargets'); + var results = findnode('.mkwsResults,.mkwsRecords'); + var blanket = findnode('.mkwsBlanket'); + var motd = findnode('.mkwsMOTD'); switch(view) { case 'targets': @@ -755,7 +779,7 @@ function team($, teamName) { 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); @@ -776,23 +800,29 @@ function team($, teamName) { { var template = loadTemplate("Record"); var details = template(data); - return '
' + details + '
'; + return '
' + details + '
'; } function loadTemplate(name) { - var template = mkws['template' + name]; + var template = m_template[name]; if (template === undefined) { - var source = $("#mkwsTemplate" + name).html(); + // 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 + "'"); - mkws['template' + name] = template; + m_template[name] = template; } return template; @@ -830,10 +860,10 @@ function team($, teamName) { {{/if}}\ {{#if md-electronic-url}}\ \ - {{translate "URL"}}\ + {{translate "Links"}}\ \ {{#each md-electronic-url}}\ - {{this}}
\ + Link{{index1}}\ {{/each}}\ \ \ @@ -844,7 +874,8 @@ function team($, teamName) { \ {{#first location having="md-subject"}}\ {{#if md-subject}}\ - {{md-subject}}\ + {{#commaList md-subject}}\ + {{this}}{{/commaList}}\ {{/if}}\ {{/first}}\ \ @@ -883,24 +914,19 @@ function team($, teamName) { * All the HTML stuff to render the search forms and * result pages. */ + // ### This and other multi-word identifiers should be camelCase 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"); - // ### There is only one match here by design: fix not to bother looping - $('.mkwsSearch.mkwsTeam_' + m_teamName).each(function (i, obj) { - var node = this; - mkws.handle_node_with_team(node, function(tname) { - $(node).html('\ + mkws.handle_node_with_team(findnode('.mkwsSearch'), function(tname) { + this.html('\
\ - \ - \ + \ + \
'); - }); }); debug("HTML records"); @@ -913,18 +939,18 @@ function team($, teamName) { // .mkwsPager // .mkwsNavi // .mkwsRecords - if ($(".mkwsResults.mkwsTeam_" + m_teamName).length) { - $(".mkwsResults.mkwsTeam_" + m_teamName).html('\ + if (findnode(".mkwsResults").length) { + findnode(".mkwsResults").html('\ \ \ \ \ \ \ @@ -935,7 +961,7 @@ function team($, teamName) {
\ -
\ +
\
\ -
\ -
\ -
\ -
\ +
\ +
\ +
\ +
\
'); } - var node = $(".mkwsRanking.mkwsTeam_" + m_teamName); + var node = findnode(".mkwsRanking"); if (node.length) { var ranking_data = ''; ranking_data += '
'; @@ -952,6 +978,7 @@ function team($, teamName) { mkws_html_switch(); + // ### Should not be in the team code, since window size is global if (mkws_config.responsive_design_width) { // Responsive web design - change layout on the fly based on // current screen width. Required for mobile devices. @@ -960,12 +987,21 @@ function team($, teamName) { $(document).ready(function() { mkws.resize_page() }); } - domReady(); + 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); // on first page, hide the termlist - $(document).ready(function() { $(".mkwsTermlists.mkwsTeam_" + m_teamName).hide(); }); - var motd = $(".mkwsMOTD.mkwsTeam_" + m_teamName); - var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName); + $(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); @@ -1002,13 +1038,13 @@ function team($, teamName) { function mkws_html_switch() { debug("HTML switch for team " + m_teamName); - var node = $(".mkwsSwitch.mkwsTeam_" + m_teamName); + var node = findnode(".mkwsSwitch"); node.append($('' + M('Records') + '')); node.append($("", { text: " | " })); node.append($('' + M('Targets') + '')); debug("HTML targets"); - var node = $(".mkwsTargets.mkwsTeam_" + m_teamName); + var node = findnode(".mkwsTargets"); node.html('\
\ No information available yet.\ @@ -1039,14 +1075,14 @@ function team($, teamName) { function mkws_html_perpage() { - debug("HTML perpage"); + debug("HTML perpage, m_perpage = " + m_perpage); var perpage_html = '