Add nine ###-comments arising from code-read with Heikki.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index 40d703f..8ffc6f9 100644 (file)
@@ -172,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;
 }
@@ -226,11 +228,13 @@ function team($, teamName) {
     m_sort = mkws_config.sort_default;
     debug("copied mkws_config.sort_default '" + mkws_config.sort_default + "' to m_sort");
 
+    // ### should be in global code
     if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
        debug("Reset query width: " + mkws_config.query_width);
        mkws_config.query_width = 50;
     }
 
+    // ### should be in global code
     for (var key in mkws_config) {
        if (mkws_config.hasOwnProperty(key)) {
            if (key.match(/^language_/)) {
@@ -284,6 +288,7 @@ function team($, teamName) {
        debug("show");
        m_totalRec = data.merged;
 
+       // ### Here and elsewhere we should use a findnode() utility function
        var pager = $(".mkwsPager.mkwsTeam_" + m_teamName);
        if (pager.length) {
            pager.html(drawPager(data))
@@ -447,6 +452,7 @@ function team($, teamName) {
 
 
     // when search button pressed
+    // ### This is closure, so can always just operate on its own team
     function onFormSubmitEventHandler()
     {
        mkws.handle_node_with_team(this, function (tname) {
@@ -458,6 +464,8 @@ function team($, teamName) {
     }
 
 
+    // ### won't need to be externally visible once onFormSubmitEventHandler() is fixed.
+    // ### doesn't need windowid
     that.newSearch = function(query, sort, targets, windowid)
     {
        debug("newSearch: " + query);
@@ -494,6 +502,7 @@ function team($, teamName) {
     }
 
 
+    // ### doesn't need windowid
     function triggerSearch (query, sort, targets, windowid)
     {
        var pp2filter = "";
@@ -909,22 +918,20 @@ 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();
 
        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(this, 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");
@@ -985,15 +992,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(this, 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);
@@ -1178,6 +1180,7 @@ function team($, 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
+    // ### should we unify this and parseQuerystring()?
     function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
@@ -1238,16 +1241,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.call(this, tname);
+       callback.call(node, teamName, type);
     }
 
 
@@ -1441,13 +1456,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();