Templates are specified by class rather then ID, like other elements.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index eba4b27..1d0b793 100644 (file)
@@ -65,6 +65,11 @@ Handlebars.registerHelper('commaList', function(items, options) {
 });
 
 
+Handlebars.registerHelper('index1', function(obj) {
+    return obj.data.index + 1;
+});
+
+
 
 // Set up global mkws object. Contains truly global state such as SP
 // authentication, and a hash of team objects, indexed by windowid.
@@ -169,6 +174,7 @@ function team($, teamName) {
        "last": $.now()
     };
     var m_paz; // will be initialised below
+    var m_template = {};
 
 
     var debug = function (s) {
@@ -757,17 +763,23 @@ function team($, teamName) {
 
     function loadTemplate(name)
     {
-       var template = mkws['template' + name];
+       var template = m_template[name];
 
        if (template === undefined) {
-           var source = $("#mkwsTemplate" + name).html();
+           // Fall back to generic template if there is no team-specific one
+           var node = $(".mkwsTemplate_" + name + ".mkwsTeam_" + m_teamName)
+           if (!node.length) {
+               node = $(".mkwsTemplate_" + name + ".mkwsTeam_ALL")
+           }
+
+           var source = node.html();
            if (!source) {
                source = defaultTemplate(name);
            }
 
            template = Handlebars.compile(source);
            debug("compiled template '" + name + "'");
-           mkws['template' + name] = template;
+           m_template[name] = template;
        }
 
        return template;
@@ -805,10 +817,10 @@ function team($, teamName) {
   {{/if}}\
   {{#if md-electronic-url}}\
   <tr>\
-    <th>{{translate "URL"}}</th>\
+    <th>{{translate "Links"}}</th>\
     <td>\
       {{#each md-electronic-url}}\
-       <a href="{{this}}">{{this}}</a><br/>\
+       <a href="{{this}}">Link{{index1}}</a>\
       {{/each}}\
     </td>\
   </tr>\
@@ -819,7 +831,8 @@ function team($, teamName) {
     <td>\
       {{#first location having="md-subject"}}\
        {{#if md-subject}}\
-         {{md-subject}}\
+         {{#commaList md-subject}}\
+           {{this}}{{/commaList}}\
        {{/if}}\
       {{/first}}\
     </td>\
@@ -1088,6 +1101,53 @@ function team($, teamName) {
     }
 
 
+    that.run_auto_search = function() {
+       // ### should check mkwsTermlist as well, for facet-only teams
+       var node = $('.mkwsRecords.mkwsTeam_' + m_teamName);
+       var query = node.attr('autosearch');
+       if (!query)
+           return;
+
+       if (query.match(/^!param!/)) {
+           var param = query.replace(/^!param!/, '');
+           query = getParameterByName(param);
+           debug("obtained query '" + query + "' from param '" + param + "'");
+           if (!query) {
+               alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
+           }
+       } else if (query.match(/^!path!/)) {
+           var index = query.replace(/^!path!/, '');
+           var path = window.location.pathname.split('/');
+           query = path[path.length - index];
+           debug("obtained query '" + query + "' from path-component '" + index + "'");
+           if (!query) {
+               alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
+           }
+       }
+
+       debug("node=" + node + ", class='" + node.className + "', query=" + query);
+
+       var sort = node.attr('sort');
+       var targets = node.attr('targets');
+       var s = "running auto search: '" + query + "'";
+       if (sort) s += " sorted by '" + sort + "'";
+       if (targets) s += " in targets '" + targets + "'";
+       debug(s);
+
+       this.newSearch(query, sort, targets, m_teamName);
+    }
+
+
+    // This function is taken from a StackOverflow answer
+    // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
+    function getParameterByName(name) {
+       name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
+       var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
+           results = regex.exec(location.search);
+       return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+    }
+
+
     /* locale */
     function M(word) {
        var lang = mkws_config.lang;
@@ -1307,45 +1367,11 @@ function team($, teamName) {
        debug("running auto searches");
 
        for (var teamName in mkws.teams) {
-           // ### should check mkwsTermlist as well, for facet-only teams
-           var node = $('.mkwsRecords.mkwsTeam_' + teamName);
-           var query = node.attr('autosearch');
-           if (query.match(/^!param!/)) {
-               var param = query.replace(/^!param!/, '');
-               query = getParameterByName(param);
-               debug("obtained query '" + query + "' from param '" + param + "'");
-               if (!query) {
-                   alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
-               }
-           }
-           debug("teamName '" + teamName + "', node=" + node + ", class='" + node.className + "', query=" + query);
-
-           if (query) {
-               var sort = node.attr('sort');
-               var targets = node.attr('targets');
-               var s = "running auto search: '" + query + "'";
-               if (teamName) s += " [teamName '" + teamName + "']";
-               if (sort) s += " sorted by '" + sort + "'";
-               if (targets) s += " in targets '" + targets + "'";
-               debug(s);
-               var team = mkws.teams[teamName];
-               debug($.toJSON(team));
-               team.newSearch(query, sort, targets, teamName);
-           }
+           mkws.teams[teamName].run_auto_search();
        }
     }
 
 
-    // This function is taken from a StackOverflow answer
-    // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
-    function getParameterByName(name) {
-       name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
-       var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
-           results = regex.exec(location.search);
-       return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
-    }
-
-
     $(document).ready(function() {
        debug("on load ready");
        default_mkws_config();