Compatibility: set new magic classes into elements with old magic IDs.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index 2b34b21..ae6740b 100644 (file)
@@ -71,14 +71,14 @@ Handlebars.registerHelper('commaList', function(items, options) {
 // Handlebars helper 'translate'
 
 
-// Set up global mkws object. Contains a hash of session objects,
+// Set up global mkws object. Contains a hash of team objects,
 // indexed by windowid.
 var mkws = {
     authenticated: false,
-    init: false,
     debug_function: undefined, // will be set during initialisation
     debug_level: undefined, // will be initialised from mkws_config
-    sessions: {},
+    paz: undefined, // will be set up during initialisation
+    teams: {},
     locale_lang: {
        "de": {
            "Authors": "Autoren",
@@ -143,6 +143,8 @@ if (mkws_config == null || typeof mkws_config != 'object') {
 
 // wrapper for jQuery lib
 function _make_mkws_team($, teamName) {
+    var that = {};
+    var m_termName = teamName;
     var m_submitted = false;
     var m_query; // initially undefined
     var m_sort = 'relevance';
@@ -159,12 +161,6 @@ function _make_mkws_team($, teamName) {
     };
 
 
-    // call this function only once
-    if (mkws.init) {
-       alert("_make_mkws_team() called twice: how did that happen?!");
-       return;
-    }
-
     // if (console && console.log) // disabled, will fail in IE8
     // console.log("run _make_mkws_team(" + (teamName ? teamName : "") + ")");
 
@@ -218,7 +214,7 @@ function _make_mkws_team($, teamName) {
            dummy: "dummy"
        };
 
-       /* set global debug_level flag early */
+       /* Set global debug_level flag early so that debug() works */
        if (typeof mkws_config.debug_level !== 'undefined') {
            mkws.debug_level = mkws_config.debug_level;
        } else if (typeof config_default.debug_level !== 'undefined') {
@@ -269,7 +265,7 @@ function _make_mkws_team($, teamName) {
     // create a parameters array and pass it to the pz2's constructor
     // then register the form submit event with the pz2.search function
     // autoInit is set to true on default
-    var m_paz = new pz2( { "onshow": my_onshow,
+    mkws.paz = new pz2( { "onshow": my_onshow,
                           "showtime": 500,            //each timer (show, stat, term, bytarget) can be specified this way
                           "pazpar2path": mkws_config.pazpar2_url,
                           "oninit": my_oninit,
@@ -290,8 +286,8 @@ function _make_mkws_team($, teamName) {
     // pz2.js event handlers:
     //
     function my_oninit() {
-       m_paz.stat();
-       m_paz.bytarget();
+       mkws.paz.stat();
+       mkws.paz.bytarget();
     }
 
 
@@ -403,7 +399,7 @@ function _make_mkws_team($, teamName) {
 
     function my_onrecord(data) {
        // FIXME: record is async!!
-       clearTimeout(m_paz.recordTimer);
+       clearTimeout(mkws.paz.recordTimer);
        // in case on_show was faster to redraw element
        var detRecordDiv = document.getElementById('mkwsDet_'+data.recid);
        if (detRecordDiv) return;
@@ -491,7 +487,7 @@ function _make_mkws_team($, teamName) {
        if (!m_submitted) return false;
        resetPage();
        loadSelect();
-       m_paz.show(0, m_recPerPage, m_sort);
+       mkws.paz.show(0, m_recPerPage, m_sort);
        return false;
     }
 
@@ -547,7 +543,7 @@ function _make_mkws_team($, teamName) {
        }
        debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
 
-       m_paz.search(m_query, m_recPerPage, m_sort, pp2filter, undefined, params);
+       mkws.paz.search(m_query, m_recPerPage, m_sort, pp2filter, undefined, params);
     }
 
 
@@ -711,21 +707,21 @@ function _make_mkws_team($, teamName) {
     mkws.showPage = function (pageNum)
     {
        m_curPage = pageNum;
-       m_paz.showPage( m_curPage - 1 );
+       mkws.paz.showPage( m_curPage - 1 );
     }
 
 
     // simple paging functions
     mkws.pagerNext = function () {
        if ( m_totalRec - m_recPerPage*m_curPage > 0) {
-            m_paz.showNext();
+            mkws.paz.showNext();
             m_curPage++;
        }
     }
 
 
     mkws.pagerPrev = function () {
-       if ( m_paz.showPrev() != false )
+       if ( mkws.paz.showPrev() != false )
             m_curPage--;
     }
 
@@ -784,7 +780,7 @@ function _make_mkws_team($, teamName) {
             return;
        }
        // request the record
-       m_paz.record(recId);
+       mkws.paz.record(recId);
     }
 
 
@@ -1246,8 +1242,9 @@ function _make_mkws_team($, teamName) {
        }
     })();
 
-    // done
-    mkws.init = true;
+    // Bizarrely, 'that' is just an empty hash. All its state is in
+    // the closure variables defined earlier in this function.
+    return that;
 };
 
 
@@ -1396,11 +1393,56 @@ function _mkws_jquery_plugin ($) {
 
 // wrapper to call _make_mkws_team() after page load
 (function (j) {
+    function log(s) {
+       if (console && console.log) console.log(s);
+    }
     // enable before page load, so we could call it before mkws() runs
     _mkws_jquery_plugin(j);
 
     $(document).ready(function() {
-       // if (console && console.log) console.log("on load ready");
-       _make_mkws_team(j, null);
+       log("on load ready");
+
+       // 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 + " mkwsTeam_AUTO");
+               log("added magic classes to '" + node.attr('id') + "'");
+           }
+       }
+
+       // Backwards compatibility: the special-case undefined team
+       mkws.teams[''] = _make_mkws_team(j, undefined);
+       log("Made the unnamed MKWS team");
+
+       // Find all nodes with class (NOT id) mkwsRecords, and
+       // determine their team from the mkwsTeam_* class. So:
+       //      <div class="mkwsRecords mkwsTeam_foo"/>
+       $('.mkwsResults, .mkwsRecords, .mkwsTermlists').each(function () {
+           var node = this;
+           var classes = this.className;
+           var list = classes.split(/\s+/)
+           var tname;
+           for (var i = 0; i < list.length; i++) {
+               var cname = list[i];
+               if (cname.match(/^mkwsTeam_/)) {
+                   tname = cname.replace(/^mkwsTeam_/, '');
+               }
+           }
+           if (!tname) {
+               alert("No MKWS team specified for mkwsRecords element with classes '" + classes + "'");
+           } else if (mkws.teams[tname]) {
+               log("MKWS team '" + tname + "' already exists, skipping");
+           } else {
+               mkws.teams[tname] = _make_mkws_team(j, tname);
+               log("Made MKWS team '" + tname + "'");
+           }
+       });
+       
     });
 })(jQuery);