Towards multiple search forms.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index 5a18aa9..b5dc51d 100644 (file)
@@ -10,7 +10,6 @@ Handlebars.registerHelper('json', function(obj) {
 
 
 Handlebars.registerHelper('translate', function(s) {
-    debug("translating '" + s + "'");
     return mkws.M(s);
 });
 
@@ -262,7 +261,7 @@ function _make_mkws_team($, teamName) {
        }
 
        // navi
-       var results = $("#mkwsRecords");
+       var results = $(".mkwsRecords.mkwsTeam_" + m_teamName);
 
        var html = [];
        for (var i = 0; i < data.hits.length; i++) {
@@ -405,7 +404,14 @@ function _make_mkws_team($, teamName) {
     // wait until the DOM is ready
     function domReady ()
     {
-       document.mkwsSearchForm.onsubmit = onFormSubmitEventHandler;
+       $('.mkwsSearchForm').each(function (i, obj) {
+           var node = this;
+           debug("adding class 'foo' to node " + node);
+           mkws.handle_node_with_team(node, function(tname) {
+               $(node).submit(onFormSubmitEventHandler);
+           });
+       });
+
        document.mkwsSearchForm.mkwsQuery.value = '';
        if (document.mkwsSelect) {
            if (document.mkwsSelect.mkwsSort)
@@ -864,11 +870,16 @@ function _make_mkws_team($, teamName) {
        // For some reason, doing this programmatically results in
        // document.mkwsSearchForm.mkwsQuery being undefined, hence the raw HTML.
        debug("HTML search form");
-       $("#mkwsSearch").html('\
-<form name="mkwsSearchForm" action="" >\
-  <input id="mkwsQuery" type="text" size="' + mkws_config.query_width + '" />\
-  <input id="mkwsButton" type="submit" value="' + M('Search') + '" />\
+       $('.mkwsSearch').each(function (i, obj) {
+           var node = this;
+           mkws.handle_node_with_team(node, function(tname) {
+               $(node).html('\
+<form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
+  <input id="mkwsQuery" class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + mkws_config.query_width + '" />\
+  <input id="mkwsButton" class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
 </form>');
+           });
+       });
 
        debug("HTML records");
        // If the application has an #mkwsResults, populate it in the
@@ -883,19 +894,19 @@ function _make_mkws_team($, teamName) {
            $("#mkwsResults").html('\
 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
   <tr>\
-    <td id="mkwsTermlistContainer1" width="250" valign="top">\
-      <div id="mkwsTermlists"></div>\
+    <td id="mkwsTermlistContainer1" class="mkwsTermlistContainer1 mkwsTeam_AUTO" width="250" valign="top">\
+      <div id="mkwsTermlists" class="mkwsTermlists mkwsTeam_AUTO"></div>\
     </td>\
     <td id="mkwsMOTDContainer" valign="top">\
-      <div id="mkwsRanking"></div>\
-      <div id="mkwsPager"></div>\
-      <div id="mkwsNavi"></div>\
-      <div id="mkwsRecords"></div>\
+      <div id="mkwsRanking" class="mkwsRanking mkwsTeam_AUTO"></div>\
+      <div id="mkwsPager" class="mkwsPager mkwsTeam_AUTO"></div>\
+      <div id="mkwsNavi" class="mkwsNavi mkwsTeam_AUTO"></div>\
+      <div id="mkwsRecords" class="mkwsRecords mkwsTeam_AUTO"></div>\
     </td>\
   </tr>\
   <tr>\
     <td colspan="2">\
-      <div id="mkwsTermlistContainer2"></div>\
+      <div id="mkwsTermlistContainer2" class="mkwsTermlistContainer2 mkwsTeam_AUTO"></div>\
     </td>\
   </tr>\
 </table>');
@@ -974,7 +985,7 @@ function _make_mkws_team($, teamName) {
 
        debug("HTML targets");
        $("#mkwsTargets").html('\
-<div id="mkwsBytarget">\
+<div id="mkwsBytarget" class="mkwsBytarget mkwsTeam_AUTO">\
   No information available yet.\
 </div>');
        $("#mkwsTargets").css("display", "none");
@@ -1295,36 +1306,19 @@ function _mkws_jquery_plugin ($) {
            }
        }
 
-       // Backwards compatibility: the special-case undefined team
-       // ### Will not be necessary when non-default teams are working
-       mkws.teams['AUTO'] = _make_mkws_team(j, "AUTO");
-       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 () {
+       $('.mkwsSearch, .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_/, '');
+           mkws.handle_node_with_team(node, function(tname) {
+               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 + "'");
                }
-           }
-           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 if (tname === "AUTO") {
-               // ### For now: later, this will be how the backwards-compatibility is done
-               log("Skipping MKWS team '" + tname + "'");
-           } else {
-               mkws.teams[tname] = _make_mkws_team(j, tname);
-               log("Made MKWS team '" + tname + "'");
-           }
+           });
        });
 
        if (mkws_config.use_service_proxy) {
@@ -1338,12 +1332,28 @@ function _mkws_jquery_plugin ($) {
     });
 
 
+    mkws.handle_node_with_team = function(node, callback) {
+       var classes = node.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)
+           tname = "AUTO";
+       callback(tname);
+    }
+
+
     function default_mkws_config() {
        /* default mkws config */
        var config_default = {
            use_service_proxy: true,
-           pazpar2_url: "http://mkws.indexdata.com/service-proxy/",
-           service_proxy_auth: "http://mkws.indexdata.com/service-proxy-auth",
+           pazpar2_url: "//mkws.indexdata.com/service-proxy/",
+           service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
            lang: "",
            sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
            perpage_options: [10, 20, 30, 50],