Merge branch 'master' of ssh://git.indexdata.com:222/home/git/private/mkws
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index b844b58..1292b5f 100644 (file)
@@ -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
@@ -502,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);
     }
 
@@ -877,16 +917,13 @@ function team($, teamName) {
            mkws_html_lang();
 
        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($('.mkwsSearch.mkwsTeam_' + m_teamName),
+                                  function(tname) {
+           this.html('\
 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
   <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + mkws_config.query_width + '" />\
   <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
 </form>');
-           });
        });
 
        debug("HTML records");
@@ -947,15 +984,10 @@ function team($, teamName) {
            $(document).ready(function() { mkws.resize_page() });
        }
 
-       $('.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) {
-               debug("adding search-form '" + tname + "' for team '" + m_teamName + "'");
-               $(node).submit(onFormSubmitEventHandler);
-           });
-       });
-
+       var node;
+       node = $('.mkwsSearchForm.mkwsTeam_' + m_teamName);
+       if (node.length)
+           node.submit(onFormSubmitEventHandler);
        node = $('.mkwsSort.mkwsTeam_' + m_teamName);
        if (node.length)
            node.change(onSelectDdChange);
@@ -966,7 +998,7 @@ function team($, teamName) {
        // 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);
+        var container = $(".mkwsMOTDContainer.mkwsTeam_" + m_teamName);
        if (motd.length && container.length) {
            // Move the MOTD from the provided element down into the container
            motd.appendTo(container);
@@ -1200,16 +1232,28 @@ function team($, teamName) {
 
 
     mkws.handle_node_with_team = function(node, callback) {
-       var classes = node.className;
+       // First branch for DOM objects; second branch for jQuery objects
+       var classes = node.className || node.attr('class');
+       if (!classes) {
+           // For some reason, if we try to proceed when classes is
+           // 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("handle_node_with_team() called on node with no classes");
+           return;
+       }
        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(node, teamName, type);
     }
 
 
@@ -1380,7 +1424,7 @@ function team($, teamName) {
        // elements that have the old magic IDs.
        var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
                    "Results", "Records", "Targets", "Ranking",
-                   "Termlists", "Stat" ];
+                   "Termlists", "Stat", "MOTD" ];
        for (var i = 0; i < ids.length; i++) {
            var id = 'mkws' + ids[i];
            var node = $('#' + id);
@@ -1403,12 +1447,13 @@ function team($, teamName) {
        // 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();