Rename variable.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index abf8c0a..2df7c12 100644 (file)
@@ -142,6 +142,29 @@ 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 = {};
@@ -149,13 +172,15 @@ if (mkws_config == null || typeof mkws_config != 'object') {
 
 
 // Factory function for widget objects.
-function widget($, team, node) {
+function widget($, team, type, node) {
     var that = {
        team: team,
+       type: type,
        node: node
     };
 
-    // ### More to do here, surely
+    // ### More to do here, surely: e.g. wiring into the team
+    mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
 
     return that;
 }
@@ -895,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('\
 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
   <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + mkws_config.query_width + '" />\
@@ -965,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);
            });
@@ -1217,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);
     }
 
 
@@ -1418,13 +1446,13 @@ function team($, teamName) {
        // the mkwsTeam_* class. Make all team objects.
        var then = $.now();
        $('[class^="mkws"],[class*=" mkws"]').each(function () {
-           mkws.handle_node_with_team(this, 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, this)
+               var myTeam = mkws.teams[tname];
+               var myWidget = widget(j, myTeam, type, this);
            });
        });
        var now = $.now();