Simplify pager-text generation.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index b694bde..aedef95 100644 (file)
@@ -259,15 +259,10 @@ function team($, teamName) {
     function my_onshow(data, teamName) {
        debug("show");
        m_totalRec = data.merged;
-       // move it out
+
        var pager = document.getElementById("mkwsPager");
        if (pager) {
-           pager.innerHTML = "";
-           pager.innerHTML +='<div style="float: right">' + M('Displaying') + ': '
-               + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
-               ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
-               + data.total + ')</div>';
-           drawPager(pager);
+           drawPager(pager, data);
        }
 
        // navi
@@ -426,7 +421,6 @@ function team($, teamName) {
        });
 
        if (document.mkwsSelect) {
-           debug("messing with mkwsSelect");
            if (document.mkwsSelect.mkwsSort)
                document.mkwsSelect.mkwsSort.onchange = onSelectDdChange;
            if (document.mkwsSelect.mkwsPerpage)
@@ -461,7 +455,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(m_teamName, 'records'); // In case it's configured to start off as hidden
+       that.switchView('records'); // In case it's configured to start off as hidden
        m_submitted = true;
     }
 
@@ -641,8 +635,13 @@ function team($, teamName) {
     }
 
 
-    function drawPager (pagerDiv)
+    function drawPager (pagerDiv, data)
     {
+       var s = '<div style="float: right">' + M('Displaying') + ': '
+           + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
+           ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
+           + data.total + ')</div>';
+
        //client indexes pages from 1 but pz2 from 0
        var onsides = 6;
        var pages = Math.ceil(m_totalRec / m_recPerPage);
@@ -683,8 +682,9 @@ function team($, teamName) {
        if (lastClkbl < pages)
             postdots = '...';
 
-       pagerDiv.innerHTML += '<div style="float: clear">'
+       s += '<div style="float: clear">'
             + prev + predots + middle + postdots + next + '</div>';
+       pagerDiv.innerHTML = s;
     }
 
 
@@ -711,13 +711,11 @@ function team($, teamName) {
 
 
     // switching view between targets and records
-    mkws.switchView = function(tname, view) {
-       debug("switchView(" + tname + ", " + view + ")");
-
-       var targets = $('.mkwsTargets.mkwsTeam_' + tname);
-       var results = $('.mkwsResults.mkwsTeam_' + tname + ',.mkwsRecords.mkwsTeam_' + tname);
-       var blanket = $('.mkwsBlanket.mkwsTeam_' + tname);
-       var motd    = $('.mkwsMOTD.mkwsTeam_' + tname);
+    that.switchView = function(view) {
+       var targets = $('.mkwsTargets.mkwsTeam_' + m_teamName);
+       var results = $('.mkwsResults.mkwsTeam_' + m_teamName + ',.mkwsRecords.mkwsTeam_' + m_teamName);
+       var blanket = $('.mkwsBlanket.mkwsTeam_' + m_teamName);
+       var motd    = $('.mkwsMOTD.mkwsTeam_' + m_teamName);
 
        switch(view) {
         case 'targets':
@@ -733,7 +731,7 @@ function team($, teamName) {
             if (motd) motd.css('display', 'none');
             break;
        case 'none':
-           alert("mkws.switchView(" + tname + ", 'none') shouldn't happen");
+           alert("mkws.switchView(" + m_teamName + ", 'none') shouldn't happen");
             if (targets) targets.css('display', 'none');
             if (results) results.css('display', 'none');
             if (blanket) blanket.css('display', 'none');
@@ -764,6 +762,7 @@ function team($, teamName) {
             return;
        }
        // request the record
+       debug("showDetails() requesting record '" + recId + "'");
        m_paz.record(recId);
     }
 
@@ -955,15 +954,12 @@ function team($, teamName) {
            $(document).ready(function() { mkws.resize_page() });
        }
 
-       debug("before domReady()");
        domReady();
-       debug("after domReady()");
 
        // 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);
-       debug("for team '" + m_teamName + "', motd=" + motd + "(" + motd.length + "), container=" + container + "(" + container.length + ")");
        if (motd.length && container.length) {
            // Move the MOTD from the provided element down into the container
            motd.appendTo(container);
@@ -1283,57 +1279,6 @@ function _mkws_jquery_plugin ($) {
     // enable before page load, so we could call it before mkws() runs
     _mkws_jquery_plugin(j);
 
-    $(document).ready(function() {
-       log("on load ready");
-       default_mkws_config();
-
-       // Backwards compatibility: set new magic class names on any
-       // elements that have the old magic IDs.
-       var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
-                   "Results", "Records", "Targets", "Ranking",
-                   "Termlists", "Stat" ];
-       for (var i = 0; i < ids.length; i++) {
-           var id = 'mkws' + ids[i];
-           var node = $('#' + id);
-           if (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.
-       $('[class^="mkws"],[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 an class, and determine their team from
-       // the mkwsTeam_* class. Make all team objects.
-       $('[class^="mkws"],[class*=" mkws"]').each(function () {
-           var node = this;
-           mkws.handle_node_with_team(node, function(tname) {
-               if (mkws.teams[tname]) {
-                   log("MKWS team '" + tname + "' already exists, skipping");
-               } else {
-                   mkws.teams[tname] = team(j, tname);
-                   log("Made MKWS team '" + tname + "'");
-               }
-           });
-       });
-
-       if (mkws_config.use_service_proxy) {
-           authenticate_session(mkws_config.service_proxy_auth,
-                                mkws_config.service_proxy_auth_domain,
-                                mkws_config.pazpar2_url);
-       } else {
-           // raw pp2
-           run_auto_searches();
-       }
-    });
-
 
     mkws.handle_node_with_team = function(node, callback) {
        var classes = node.className;
@@ -1381,6 +1326,10 @@ function _mkws_jquery_plugin ($) {
     };
 
 
+    mkws.switchView = function(tname, view) {
+       mkws.teams[tname].switchView(view);
+    }
+
     mkws.showDetails = function (tname, prefixRecId) {
        mkws.teams[tname].showDetails(prefixRecId);
     }
@@ -1506,4 +1455,54 @@ function _mkws_jquery_plugin ($) {
            }
        }
     }
+
+
+    $(document).ready(function() {
+       log("on load ready");
+       default_mkws_config();
+
+       // Backwards compatibility: set new magic class names on any
+       // elements that have the old magic IDs.
+       var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
+                   "Results", "Records", "Targets", "Ranking",
+                   "Termlists", "Stat" ];
+       for (var i = 0; i < ids.length; i++) {
+           var id = 'mkws' + ids[i];
+           var node = $('#' + id);
+           if (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.
+       $('[class^="mkws"],[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 an class, and determine their team from
+       // the mkwsTeam_* class. Make all team objects.
+       $('[class^="mkws"],[class*=" mkws"]').each(function () {
+           var node = this;
+           mkws.handle_node_with_team(node, function(tname) {
+               if (!mkws.teams[tname]) {
+                   mkws.teams[tname] = team(j, tname);
+                   log("Made MKWS team '" + tname + "'");
+               }
+           });
+       });
+
+       if (mkws_config.use_service_proxy) {
+           authenticate_session(mkws_config.service_proxy_auth,
+                                mkws_config.service_proxy_auth_domain,
+                                mkws_config.pazpar2_url);
+       } else {
+           // raw pp2
+           run_auto_searches();
+       }
+    });
 })(jQuery);