X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=3c273eb64cd3930f8e7c689500cf1ca24436349d;hb=3465362833b175a4e4fbe6cdd4a6d63ddc031f3f;hp=5cdea81c9e1df4576857cb9f91fc8a58b440434c;hpb=7a2cb465ed9158c92b8fe41aa3d0130809d7e017;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index 5cdea81..3c273eb 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -65,13 +65,19 @@ Handlebars.registerHelper('commaList', function(items, options) { }); +Handlebars.registerHelper('index1', function(obj) { + return obj.data.index + 1; +}); + + // 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_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: { @@ -142,6 +148,19 @@ if (mkws_config == null || typeof mkws_config != 'object') { } +// Factory function for widget objects. +function widget($, team, node) { + var that = { + team: team, + node: node + }; + + // ### More to do here, surely + + 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 @@ -168,6 +187,7 @@ function team($, teamName) { "last": $.now() }; var m_paz; // will be initialised below + var m_template = {}; var debug = function (s) { @@ -348,7 +368,6 @@ function team($, teamName) { function target_filtered(id) { - debug("target_filtered(" + id + ")"); for (var i = 0; i < m_filters.length; i++) { if (m_filters[i].id === id || m_filters[i].id === 'pz:id=' + id) { @@ -496,6 +515,8 @@ function team($, teamName) { } debug("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 m_paz.search(m_query, m_perpage, m_sort, pp2filter, undefined, params); } @@ -757,17 +778,23 @@ function team($, teamName) { 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 = $(".mkwsTemplate_" + name + ".mkwsTeam_" + m_teamName) + if (!node.length) { + node = $(".mkwsTemplate_" + name + ".mkwsTeam_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; @@ -805,10 +832,10 @@ function team($, teamName) { {{/if}}\ {{#if md-electronic-url}}\ \ - {{translate "URL"}}\ + {{translate "Links"}}\ \ {{#each md-electronic-url}}\ - {{this}}
\ + Link{{index1}}\ {{/each}}\ \ \ @@ -819,7 +846,8 @@ function team($, teamName) { \ {{#first location having="md-subject"}}\ {{#if md-subject}}\ - {{md-subject}}\ + {{#commaList md-subject}}\ + {{this}}{{/commaList}}\ {{/if}}\ {{/first}}\ \ @@ -1088,6 +1116,53 @@ function team($, teamName) { } + that.run_auto_search = function() { + // ### should check mkwsTermlist as well, for facet-only teams + var node = $('.mkwsRecords.mkwsTeam_' + m_teamName); + 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); + + this.newSearch(query, sort, targets, m_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 + 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; @@ -1307,23 +1382,7 @@ function team($, teamName) { debug("running auto searches"); for (var teamName in mkws.teams) { - // ### should check mkwsTermlist as well, for facet-only teams - var node = $('.mkwsRecords.mkwsTeam_' + teamName); - var query = node.attr('autosearch'); - debug("teamName '" + teamName + "', node=" + node + ", class='" + node.className + "', query=" + query); - - if (query) { - var sort = node.attr('sort'); - var targets = node.attr('targets'); - var s = "running auto search: '" + query + "'"; - if (teamName) s += " [teamName '" + teamName + "']"; - if (sort) s += " sorted by '" + sort + "'"; - if (targets) s += " in targets '" + targets + "'"; - debug(s); - var team = mkws.teams[teamName]; - debug($.toJSON(team)); - team.newSearch(query, sort, targets, teamName); - } + mkws.teams[teamName].run_auto_search(); } } @@ -1357,6 +1416,7 @@ function team($, teamName) { // 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 () { var node = this; mkws.handle_node_with_team(node, function(tname) { @@ -1364,8 +1424,12 @@ function team($, teamName) { mkws.teams[tname] = team(j, tname); debug("Made MKWS team '" + tname + "'"); } + var myTeam = mkws.teams[tname] + var myWidget = widget(j, myTeam, node) }); }); + var now = $.now(); + debug("Walking MKWS nodes took " + (now-then) + " ms"); if (mkws_config.use_service_proxy) { authenticate_session(mkws_config.service_proxy_auth,