When the class-based selector is used (i.e. in the default case when
[mkws-moved-to-github.git] / src / mkws-core.js
index ccba3ef..af59625 100644 (file)
@@ -10,6 +10,7 @@
 // authentication, and a hash of team objects, indexed by team-name.
 //
 var mkws = {
+  $: $, // Our own local copy of the jQuery object
   authenticated: false,
   log_level: 1, // Will be overridden from mkws.config, but
                 // initial value allows jQuery popup to use logging.
@@ -145,10 +146,10 @@ mkws.setMkwsConfig = function(overrides) {
     sort_default: "relevance",
     perpage_default: 20,
     query_width: 50,
-    show_lang: true,   /* show/hide language menu */
-    show_sort: true,   /* show/hide sort menu */
-    show_perpage: true,        /* show/hide perpage menu */
-    lang_options: [],  /* display languages links for given languages, [] for all */
+    show_lang: true,    /* show/hide language menu */
+    show_sort: true,    /* show/hide sort menu */
+    show_perpage: true,         /* show/hide perpage menu */
+    lang_options: [],   /* display languages links for given languages, [] for all */
     facets: ["xtargets", "subject", "author"], /* display facets, in this order, [] for none */
     responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
     log_level: 1,     /* log level for development: 0..2 */
@@ -184,10 +185,10 @@ mkws.defaultTemplate = function(name) {
     <td>\
       {{md-title}}\
       {{#if md-title-remainder}}\
-       ({{md-title-remainder}})\
+        ({{md-title-remainder}})\
       {{/if}}\
       {{#if md-title-responsibility}}\
-       <i>{{md-title-responsibility}}</i>\
+        <i>{{md-title-responsibility}}</i>\
       {{/if}}\
     </td>\
   </tr>\
@@ -208,7 +209,7 @@ mkws.defaultTemplate = function(name) {
     <th>{{mkws-translate "Links"}}</th>\
     <td>\
       {{#each md-electronic-url}}\
-       <a href="{{this}}">Link{{mkws-index1}}</a>\
+        <a href="{{this}}">Link{{mkws-index1}}</a>\
       {{/each}}\
     </td>\
   </tr>\
@@ -218,10 +219,10 @@ mkws.defaultTemplate = function(name) {
     <th>{{mkws-translate "Subject"}}</th>\
     <td>\
       {{#mkws-first location having="md-subject"}}\
-       {{#if md-subject}}\
-         {{#mkws-commaList md-subject}}\
-           {{this}}{{/mkws-commaList}}\
-       {{/if}}\
+        {{#if md-subject}}\
+          {{#mkws-commaList md-subject}}\
+            {{this}}{{/mkws-commaList}}\
+        {{/if}}\
       {{/mkws-first}}\
     </td>\
   </tr>\
@@ -230,7 +231,7 @@ mkws.defaultTemplate = function(name) {
     <th>{{mkws-translate "Locations"}}</th>\
     <td>\
       {{#mkws-commaList location}}\
-       {{mkws-attr "@name"}}{{/mkws-commaList}}\
+        {{mkws-attr "@name"}}{{/mkws-commaList}}\
     </td>\
   </tr>\
 </table>\
@@ -251,9 +252,9 @@ mkws.defaultTemplate = function(name) {
     return '\
       <a href="#" id="{{_id}}" onclick="{{_onclick}}">\
         {{#mkws-first md-thumburl}}\
-         <img src="{{this}}" alt="{{../md-title}}"/>\
+          <img src="{{this}}" alt="{{../md-title}}"/>\
         {{/mkws-first}}\
-       <br/>\
+        <br/>\
       </a>\
 ';
   }
@@ -311,6 +312,7 @@ mkws.pagerNext = function(tname) {
 // wrapper to call team() after page load
 (function(j) {
   var log = mkws.log;
+  var $ = j; // XXX
 
   function handleNodeWithTeam(node, callback) {
     // First branch for DOM objects; second branch for jQuery objects
@@ -329,9 +331,9 @@ mkws.pagerNext = function(tname) {
     for (var i = 0; i < list.length; i++) {
       var cname = list[i];
       if (cname.match(/^mkwsTeam_/)) {
-       teamName = cname.replace(/^mkwsTeam_/, '');
+        teamName = cname.replace(/^mkwsTeam_/, '');
       } else if (cname.match(/^mkws/)) {
-       type = cname.replace(/^mkws/, '');
+        type = cname.replace(/^mkws/, '');
       }
     }
 
@@ -362,11 +364,11 @@ mkws.pagerNext = function(tname) {
           var w1 = team.widget(t + "-Container-" + from);
           var w2 = team.widget(t + "-Container-" + to);
           if (w1) {
-            $(w1.node).hide();
+            w1.jqnode.hide();
           }
           if (w2) {
-            $(w2.node).show();
-           $(w.node).appendTo($(w2.node));
+            w2.jqnode.show();
+            w.jqnode.appendTo(w2.jqnode);
           }
         });
         team.queue("resize-" + to).publish();
@@ -395,13 +397,13 @@ mkws.pagerNext = function(tname) {
 
     request.get(null, function(data) {
       if (!$.isXMLDoc(data)) {
-       alert("service proxy auth response document is not valid XML document, give up!");
-       return;
+        alert("service proxy auth response document is not valid XML document, give up!");
+        return;
       }
       var status = $(data).find("status");
       if (status.text() != "OK") {
-       alert("service proxy auth response status: " + status.text() + ", give up!");
-       return;
+        alert("service proxy auth response status: " + status.text() + ", give up!");
+        return;
       }
 
       log("Service proxy auth successfully done");
@@ -410,7 +412,7 @@ mkws.pagerNext = function(tname) {
       // You'd think there would be a better way to do this:
       var realm = $(data).find("realm:not(realmAttributes realm)").text();
       for (var teamName in mkws.teams) {
-       mkws.teams[teamName].queue("authenticated").publish(authName, realm);
+        mkws.teams[teamName].queue("authenticated").publish(authName, realm);
       }
 
       runAutoSearches();
@@ -427,11 +429,45 @@ mkws.pagerNext = function(tname) {
   }
 
 
+  function selectorForAllWidgets() {
+    if (mkws.config.scan_all_nodes) {
+      // This is the old version, which works by telling jQuery to
+      // find every node that has a class beginning with "mkws". In
+      // theory it should be slower than the class-based selector; but
+      // instrumentation suprisnigly shows this is consistently
+      // faster. It also has the advantage that any widgets of
+      // non-registered types are logged as warnings rather than
+      // silently ignored.
+      return '[class^="mkws"],[class*=" mkws"]';
+    } else {
+      // This is the new version, which works by looking up the
+      // specific classes of all registered widget types and their
+      // resize containers. Because all it requires jQuery to do is
+      // some hash lookups in pre-built tables, it should be very
+      // fast; but it silently ignores widgets of unregistered types.
+      var s = "";
+      for (var type in mkws.widgetType2function) {
+       if (s) s += ',';
+       s += '.mkws' + type;
+       s += ',.mkws' + type + "-Container-wide";
+       s += ',.mkws' + type + "-Container-narrow";
+      }
+      log("selector is '" + s + "'");
+      return s;
+    }
+  }
+
+
   function makeWidgetsWithin(level, node) {
-    node.find('[class^="mkws"],[class*=" mkws"]').each(function() {
+    node.find(selectorForAllWidgets()).each(function() {
       handleNodeWithTeam(this, function(tname, type) {
-        var oldHTML = this.innerHTML;
         var myTeam = mkws.teams[tname];
+        if (!myTeam) {
+          myTeam = mkws.teams[tname] = team(j, tname);
+          log("Made MKWS team '" + tname + "'");
+        }
+
+        var oldHTML = this.innerHTML;
         var myWidget = widget(j, myTeam, type, this);
         myTeam.addWidget(myWidget);
         var newHTML = this.innerHTML;
@@ -457,12 +493,12 @@ mkws.pagerNext = function(tname) {
 
     for (var key in mkws.config) {
       if (mkws.config.hasOwnProperty(key)) {
-       if (key.match(/^language_/)) {
-         var lang = key.replace(/^language_/, "");
-         // Copy custom languages into list
-         mkws.locale_lang[lang] = mkws.config[key];
-         log("Added locally configured language '" + lang + "'");
-       }
+        if (key.match(/^language_/)) {
+          var lang = key.replace(/^language_/, "");
+          // Copy custom languages into list
+          mkws.locale_lang[lang] = mkws.config[key];
+          log("Added locally configured language '" + lang + "'");
+        }
       }
     }
 
@@ -497,32 +533,21 @@ mkws.pagerNext = function(tname) {
     // 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", "MOTD" ];
+                "Results", "Records", "Targets", "Ranking",
+                "Termlists", "Stat", "MOTD" ];
     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') + "'");
+        node.addClass(id);
+        log("added magic class to '" + node.attr('id') + "'");
       }
     }
 
-    // Find all nodes with an MKWS class, and determine their team from
-    // the mkwsTeam_* class. Make all team objects.
     var then = $.now();
-    $('[class^="mkws"],[class*=" mkws"]').each(function() {
-      handleNodeWithTeam(this, function(tname, type) {
-       if (!mkws.teams[tname]) {
-         mkws.teams[tname] = team(j, tname);
-         log("Made MKWS team '" + tname + "'");
-       }
-      });
-    });
-
     makeWidgetsWithin(1, $(':root'));
-    
     var now = $.now();
+
     log("Walking MKWS nodes took " + (now-then) + " ms");
 
     /*
@@ -537,8 +562,8 @@ mkws.pagerNext = function(tname) {
 
     if (mkws.config.use_service_proxy) {
       authenticateSession(mkws.config.service_proxy_auth,
-                         mkws.config.service_proxy_auth_domain,
-                         mkws.config.pazpar2_url);
+                          mkws.config.service_proxy_auth_domain,
+                          mkws.config.pazpar2_url);
     } else {
       // raw pp2
       runAutoSearches();