Move mkws.defaultTemplate() out of the team structure.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index ee05520..78f1e46 100644 (file)
@@ -142,29 +142,6 @@ var mkws = {
 };
 
 
-// The following PubSub code is modified from the jQuery manual:
-// https://api.jquery.com/jQuery.Callbacks/
-//
-// Use as:
-//     mkws.queue("eventName").subscribe(function(param1, param2 ...) { ... });
-//     mkws.queue("eventName").publish(arg1, arg2, ...);
-
-(function() {
-  var queues = {};
-  mkws.queue = function(id) {
-    if (!queues[id]) {
-      var callbacks = $.Callbacks();
-      queues[id] = {
-       publish: callbacks.fire,
-       subscribe: callbacks.add,
-       unsubscribe: callbacks.remove
-      };
-    }
-    return queues[id];
-  }
-}());
-
-
 // Define empty mkws_config for simple applications that don't define it.
 if (mkws_config == null || typeof mkws_config != 'object') {
     var mkws_config = {};
@@ -193,8 +170,12 @@ function widget($, team, type, node) {
     return that;
 
 
+    // Functions follow for promoting the regular widget object into
+    // widgets of specific types. These could be moved outside of the
+    // widget object, or even into their own source files.
+
     function promoteTargets() {
-       mkws.queue("targets." + team.name()).subscribe(function(data) {
+       team.queue("targets").subscribe(function(data) {
            if (node.length === 0) alert("huh?!");
 
            var table ='<table><thead><tr>' +
@@ -221,7 +202,7 @@ function widget($, team, type, node) {
 
 
     function promoteStat() {
-       mkws.queue("stat." + team.name()).subscribe(function(data) {
+       team.queue("stat").subscribe(function(data) {
            if (node.length === 0)  alert("huh?!");
 
            $(node).html('<span class="head">' + M('Status info') + '</span>' +
@@ -307,13 +288,13 @@ function team($, teamName) {
 
     function onBytarget(data) {
        debug("target");
-       mkws.queue("targets." + m_teamName).publish(data);
+       queue("targets").publish(data);
     }
 
 
     function onStat(data) {
        debug("stat");
-       mkws.queue("stat." + m_teamName).publish(data);
+       queue("stat").publish(data);
     }
 
 
@@ -388,9 +369,10 @@ function team($, teamName) {
        var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid);
        if (detRecordDiv) return;
        m_curDetRecData = data;
+       // Can't use jQuery's $('#x') syntax to find this ID, because it contains spaces.
        var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid);
        var html = renderDetails(m_curDetRecData);
-       recordDiv.innerHTML += html;
+       $(recordDiv).append(html);
     }
 
 
@@ -1096,7 +1078,7 @@ function team($, teamName) {
 
            var source = node.html();
            if (!source) {
-               source = defaultTemplate(name);
+               source = mkws.defaultTemplate(name);
            }
 
            template = Handlebars.compile(source);
@@ -1108,86 +1090,28 @@ function team($, teamName) {
     }
 
 
-    function defaultTemplate(name)
-    {
-       if (name === 'Record') {
-           return '\
-<table>\
-  <tr>\
-    <th>{{translate "Title"}}</th>\
-    <td>\
-      {{md-title}}\
-      {{#if md-title-remainder}}\
-       ({{md-title-remainder}})\
-      {{/if}}\
-      {{#if md-title-responsibility}}\
-       <i>{{md-title-responsibility}}</i>\
-      {{/if}}\
-    </td>\
-  </tr>\
-  {{#if md-date}}\
-  <tr>\
-    <th>{{translate "Date"}}</th>\
-    <td>{{md-date}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-author}}\
-  <tr>\
-    <th>{{translate "Author"}}</th>\
-    <td>{{md-author}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-electronic-url}}\
-  <tr>\
-    <th>{{translate "Links"}}</th>\
-    <td>\
-      {{#each md-electronic-url}}\
-       <a href="{{this}}">Link{{index1}}</a>\
-      {{/each}}\
-    </td>\
-  </tr>\
-  {{/if}}\
-  {{#if-any location having="md-subject"}}\
-  <tr>\
-    <th>{{translate "Subject"}}</th>\
-    <td>\
-      {{#first location having="md-subject"}}\
-       {{#if md-subject}}\
-         {{#commaList md-subject}}\
-           {{this}}{{/commaList}}\
-       {{/if}}\
-      {{/first}}\
-    </td>\
-  </tr>\
-  {{/if-any}}\
-  <tr>\
-    <th>{{translate "Locations"}}</th>\
-    <td>\
-      {{#commaList location}}\
-       {{attr "@name"}}{{/commaList}}\
-    </td>\
-  </tr>\
-</table>\
-';
-       } else if (name === "Summary") {
-           return '\
-<a href="#" id="{{_id}}" onclick="{{_onclick}}">\
-  <b>{{md-title}}</b>\
-</a>\
-{{#if md-title-remainder}}\
-  <span>{{md-title-remainder}}</span>\
-{{/if}}\
-{{#if md-title-responsibility}}\
-  <span><i>{{md-title-responsibility}}</i></span>\
-{{/if}}\
-';
+    // The following PubSub code is modified from the jQuery manual:
+    // https://api.jquery.com/jQuery.Callbacks/
+    //
+    // Use as:
+    // team.queue("eventName").subscribe(function(param1, param2 ...) { ... });
+    // team.queue("eventName").publish(arg1, arg2, ...);
+
+    var queues = {};
+    var queue = function(id) {
+       if (!queues[id]) {
+           var callbacks = $.Callbacks();
+           queues[id] = {
+               publish: callbacks.fire,
+               subscribe: callbacks.add,
+               unsubscribe: callbacks.remove
+           };
        }
-
-       var s = "There is no default '" + name +"' template!";
-       alert(s);
-       return s;
+       return queues[id];
     }
 
+    that.queue = queue;
+
 
     // main
     (function() {
@@ -1323,6 +1247,87 @@ function team($, teamName) {
     }
 
 
+    mkws.defaultTemplate = function(name)
+    {
+       if (name === 'Record') {
+           return '\
+<table>\
+  <tr>\
+    <th>{{translate "Title"}}</th>\
+    <td>\
+      {{md-title}}\
+      {{#if md-title-remainder}}\
+       ({{md-title-remainder}})\
+      {{/if}}\
+      {{#if md-title-responsibility}}\
+       <i>{{md-title-responsibility}}</i>\
+      {{/if}}\
+    </td>\
+  </tr>\
+  {{#if md-date}}\
+  <tr>\
+    <th>{{translate "Date"}}</th>\
+    <td>{{md-date}}</td>\
+  </tr>\
+  {{/if}}\
+  {{#if md-author}}\
+  <tr>\
+    <th>{{translate "Author"}}</th>\
+    <td>{{md-author}}</td>\
+  </tr>\
+  {{/if}}\
+  {{#if md-electronic-url}}\
+  <tr>\
+    <th>{{translate "Links"}}</th>\
+    <td>\
+      {{#each md-electronic-url}}\
+       <a href="{{this}}">Link{{index1}}</a>\
+      {{/each}}\
+    </td>\
+  </tr>\
+  {{/if}}\
+  {{#if-any location having="md-subject"}}\
+  <tr>\
+    <th>{{translate "Subject"}}</th>\
+    <td>\
+      {{#first location having="md-subject"}}\
+       {{#if md-subject}}\
+         {{#commaList md-subject}}\
+           {{this}}{{/commaList}}\
+       {{/if}}\
+      {{/first}}\
+    </td>\
+  </tr>\
+  {{/if-any}}\
+  <tr>\
+    <th>{{translate "Locations"}}</th>\
+    <td>\
+      {{#commaList location}}\
+       {{attr "@name"}}{{/commaList}}\
+    </td>\
+  </tr>\
+</table>\
+';
+       } else if (name === "Summary") {
+           return '\
+<a href="#" id="{{_id}}" onclick="{{_onclick}}">\
+  <b>{{md-title}}</b>\
+</a>\
+{{#if md-title-remainder}}\
+  <span>{{md-title-remainder}}</span>\
+{{/if}}\
+{{#if md-title-responsibility}}\
+  <span><i>{{md-title-responsibility}}</i></span>\
+{{/if}}\
+';
+       }
+
+       var s = "There is no default '" + name +"' template!";
+       alert(s);
+       return s;
+    }
+
+
     function defaultMkwsConfig() {
        /* default mkws config */
        var config_default = {