X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=tools%2Fhtdocs%2Fmkws.js;h=2df7c1257f71f1c15c75bc81361ecc9372c4b649;hb=5f4b0ba8b12474e57a93010ce18900ccac950825;hp=d41cba3a5e611a0435c5e3e272f8cb48b62ed6a2;hpb=5defa893fdac809e2378d18bcf5c650722149a89;p=mkws-moved-to-github.git diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js index d41cba3..2df7c12 100644 --- a/tools/htdocs/mkws.js +++ b/tools/htdocs/mkws.js @@ -142,12 +142,50 @@ 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 = {}; } +// 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 @@ -174,6 +212,7 @@ function team($, teamName) { "last": $.now() }; var m_paz; // will be initialised below + var m_template = {}; var debug = function (s) { @@ -501,6 +540,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); } @@ -762,17 +803,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; @@ -873,7 +920,7 @@ function team($, teamName) { // ### 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) { + mkws.handle_node_with_team(this, function(tname) { $(node).html('\
\ \ @@ -943,7 +990,7 @@ function team($, teamName) { $('.mkwsSearchForm.mkwsTeam_' + m_teamName).each(function (i, obj) { debug("adding search-forms for team '" + m_teamName + "'"); var node = this; - mkws.handle_node_with_team(node, function(tname) { + mkws.handle_node_with_team(this, function(tname) { debug("adding search-form '" + tname + "' for team '" + m_teamName + "'"); $(node).submit(onFormSubmitEventHandler); }); @@ -1195,14 +1242,17 @@ function team($, teamName) { mkws.handle_node_with_team = function(node, callback) { var classes = node.className; var list = classes.split(/\s+/) - var tname; + var teamName, type; + for (var i = 0; i < list.length; i++) { var cname = list[i]; if (cname.match(/^mkwsTeam_/)) { - tname = cname.replace(/^mkwsTeam_/, ''); + teamName = cname.replace(/^mkwsTeam_/, ''); + } else if (cname.match(/^mkws/)) { + type = cname.replace(/^mkws/, ''); } } - callback(tname); + callback.call(this, teamName, type); } @@ -1394,15 +1444,19 @@ 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) { + mkws.handle_node_with_team(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) { authenticate_session(mkws_config.service_proxy_auth,