mkws.switchView() now operates on a named team.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index 03cd18e..45564dd 100644 (file)
@@ -271,7 +271,8 @@ function team($, teamName) {
                      renderSummary(hit),
                      '</div>');
            if (hit.recid == m_curDetRecId) {
-               html.push(renderDetails(m_curDetRecData));
+               if (m_curDetRecData)
+                   html.push(renderDetails(m_curDetRecData));
            }
        }
        results.html(html.join(''));
@@ -372,7 +373,7 @@ function team($, teamName) {
 
     function my_onbytarget(data, teamName) {
        debug("target for " + teamName);
-       var targetDiv = document.getElementById("mkwsBytarget");
+       var targetDiv = $('.mkwsBytarget.mkwsTeam_' + teamName);
        if (!targetDiv) {
            // No mkwsTargets div.
            return;
@@ -395,7 +396,7 @@ function team($, teamName) {
        }
 
        table += '</tbody></table>';
-       targetDiv.innerHTML = table;
+       targetDiv.html(table);
     }
 
     ////////////////////////////////////////////////////////////////////////////////
@@ -405,10 +406,11 @@ function team($, teamName) {
     // wait until the DOM is ready
     function domReady ()
     {
-       $('.mkwsSearchForm').each(function (i, obj) {
+       $('.mkwsSearchForm.mkwsTeam_' + m_teamName).each(function (i, obj) {
+           debug("adding search-forms for team '" + m_teamName + "'");
            var node = this;
-           debug("adding class 'foo' to node " + node);
            mkws.handle_node_with_team(node, function(tname) {
+               debug("adding search-form '" + tname + "' for team '" + m_teamName + "'");
                $(node).submit(onFormSubmitEventHandler);
            });
        });
@@ -449,7 +451,7 @@ function team($, teamName) {
        resetPage(); // ### the globals it resents should be indexed by windowid
        loadSelect(); // ### should use windowid
        triggerSearch(query, sort, targets, windowid);
-       mkws.switchView('records'); // In case it's configured to start off as hidden
+       mkws.switchView(m_teamName, 'records'); // In case it's configured to start off as hidden
        m_submitted = true;
     }
 
@@ -699,33 +701,34 @@ function team($, teamName) {
 
 
     // switching view between targets and records
-    mkws.switchView = function(view) {
-       debug("switchView: " + view);
+    mkws.switchView = function(tname, view) {
+       debug("switchView(" + tname + ", " + view + ")");
 
-       var targets = document.getElementById('mkwsTargets');
-       var results = document.getElementById('mkwsResults') ||
-           document.getElementById('mkwsRecords');
-       var blanket = document.getElementById('mkwsBlanket');
-       var motd    = document.getElementById('mkwsMOTD');
+       //var targets = document.getElementById('mkwsTargets');
+       var targets = $('.mkwsTargets.mkwsTeam_' + tname);
+       var results = $('.mkwsResults.mkwsTeam_' + tname + ',.mkwsRecords.mkwsTeam_' + tname);
+       var blanket = $('#mkwsBlanket');
+       var motd    = $('#mkwsMOTD');
 
        switch(view) {
         case 'targets':
-            if (targets) targets.style.display = "block";
-            if (results) results.style.display = "none";
-            if (blanket) blanket.style.display = "none";
-            if (motd) motd.style.display = "none";
+            if (targets) targets.css('display', 'block');
+            if (results) results.css('display', 'none');
+            if (blanket) blanket.css('display', 'none');
+            if (motd) motd.css('display', 'none');
             break;
         case 'records':
-            if (targets) targets.style.display = "none";
-            if (results) results.style.display = "block";
-            if (blanket) blanket.style.display = "block";
-            if (motd) motd.style.display = "none";
+            if (targets) targets.css('display', 'none');
+            if (results) results.css('display', 'block');
+            if (blanket) blanket.css('display', 'block');
+            if (motd) motd.css('display', 'none');
             break;
        case 'none':
-            if (targets) targets.style.display = "none";
-            if (results) results.style.display = "none";
-            if (blanket) blanket.style.display = "none";
-            if (motd) motd.style.display = "none";
+           alert("mkws.switchView(" + tname + ", 'none') shouldn't happen");
+            if (targets) targets.css('display', 'none');
+            if (results) results.css('display', 'none');
+            if (blanket) blanket.css('display', 'none');
+            if (motd) motd.css('display', 'none');
             break;
         default:
             alert("Unknown view '" + view + "'");
@@ -875,7 +878,8 @@ function team($, teamName) {
        // For some reason, doing this programmatically results in
        // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML.
        debug("HTML search form");
-       $('.mkwsSearch').each(function (i, obj) {
+       // ### 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('\
@@ -887,16 +891,17 @@ function team($, teamName) {
        });
 
        debug("HTML records");
-       // If the application has an #mkwsResults, populate it in the
-       // usual way. If not, assume that it's a smarter application that
-       // defines its own subcomponents:
-       //      #mkwsTermlists
-       //      #mkwsRanking
-       //      #mkwsPager
-       //      #mkwsNavi
-       //      #mkwsRecords
-       if ($("#mkwsResults").length) {
-           $("#mkwsResults").html('\
+       // If the team has a .mkwsResults, populate it in the usual
+       // way. If not, assume that it's a smarter application that
+       // defines its own subcomponents, some or all of the
+       // following:
+       //      .mkwsTermlists
+       //      .mkwsRanking
+       //      .mkwsPager
+       //      .mkwsNavi
+       //      .mkwsRecords
+       if ($(".mkwsResults.mkwsTeam_" + m_teamName).length) {
+           $(".mkwsResults.mkwsTeam_" + m_teamName).html('\
 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
   <tr>\
     <td id="mkwsTermlistContainer1" class="mkwsTermlistContainer1 mkwsTeam_AUTO" width="250" valign="top">\
@@ -982,18 +987,20 @@ function team($, teamName) {
 
 
     function mkws_html_switch() {
-       debug("HTML switch");
+       debug("HTML switch for team " + m_teamName);
 
-       $("#mkwsSwitch").append($('<a href="#" id="mkwsSwitch_records" onclick="mkws.switchView(\'records\')">' + M('Records') + '</a>'));
-       $("#mkwsSwitch").append($("<span/>", { text: " | " }));
-       $("#mkwsSwitch").append($('<a href="#" id="mkwsSwitch_targets" onclick="mkws.switchView(\'targets\')">' + M('Targets') + '</a>'));
+       var node = $(".mkwsSwitch.mkwsTeam_" + m_teamName);
+       node.append($('<a href="#" onclick="mkws.switchView(\'' + m_teamName + '\', \'records\')">' + M('Records') + '</a>'));
+       node.append($("<span/>", { text: " | " }));
+       node.append($('<a href="#" onclick="mkws.switchView(\'' + m_teamName + '\', \'targets\')">' + M('Targets') + '</a>'));
 
        debug("HTML targets");
-       $("#mkwsTargets").html('\
-<div id="mkwsBytarget" class="mkwsBytarget mkwsTeam_AUTO">\
+       var node = $(".mkwsTargets.mkwsTeam_" + m_teamName);
+       node.html('\
+<div class="mkwsBytarget mkwsTeam_' + m_teamName + '">\
   No information available yet.\
 </div>');
-       $("#mkwsTargets").css("display", "none");
+       node.css("display", "none");
     }
 
 
@@ -1093,7 +1100,7 @@ function team($, teamName) {
            $("#mkwsTermlistContainer1").hide();
            $("#mkwsTermlistContainer2").show();
            for(var i = 0; i < list.length; i++) {
-               $("#" + list[i]).hide();
+               $("#" + list[i]).hide(); // ### make team-aware
            }
        } else if ($(window).width() > width &&
                   parentId === "mkwsTermlistContainer2") {
@@ -1102,7 +1109,7 @@ function team($, teamName) {
            $("#mkwsTermlistContainer1").show();
            $("#mkwsTermlistContainer2").hide();
            for(var i = 0; i < list.length; i++) {
-               $("#" + list[i]).show();
+               $("#" + list[i]).show(); // ### make team-aware
            }
        }
     };
@@ -1306,11 +1313,20 @@ function _mkws_jquery_plugin ($) {
            var id = 'mkws' + ids[i];
            var node = $('#' + id);
            if (node.attr('id')) {
-               node.addClass(id + " mkwsTeam_AUTO");
-               log("added magic classes to '" + node.attr('id') + "'");
+               node.addClass(id);
+               log("added magic class to '" + node.attr('id') + "'");
            }
        }
 
+       // For all MKWS-classed nodes that don't have a team
+       // specified, set the team to AUTO.
+       $('div[class^="mkws"],div[class*=" mkws"]').each(function () {
+           if (!this.className.match(/mkwsTeam_/)) {
+               log("adding AUTO team to node with class '" + this.className + "'");
+               $(this).addClass('mkwsTeam_AUTO');
+           }
+       });
+
        // Find all nodes with class (NOT id) mkwsRecords, and
        // determine their team from the mkwsTeam_* class. So:
        //      <div class="mkwsRecords mkwsTeam_foo"/>
@@ -1347,8 +1363,6 @@ function _mkws_jquery_plugin ($) {
                tname = cname.replace(/^mkwsTeam_/, '');
            }
        }
-       if (!tname)
-           tname = "AUTO";
        callback(tname);
     }