All five members of the mkws global hash are now explicitly initialised.
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index 0ea7223..f322382 100644 (file)
@@ -2,11 +2,19 @@
 
 "use strict"; // HTML5: disable for debug_level >= 2
 
+// Some functions are visible to be called from outside code, namely
+// generated HTML: mkws.switchView(), showDetails(), limitTarget(),
+// limitQuery(), delimitTarget(), delimitQuery(), pagerPrev(),
+// pagerNext(), showPage()
+
 // Set up global mkws object. Contains a hash of session objects,
 // indexed by windowid.
 var mkws = {
     authenticated: false,
-    sessions: {}
+    init: false,
+    debug_function: undefined, // will be set during initialisation
+    debug_level: undefined, // will be initialised from mkws_config
+    sessions: {},
 };
 
 mkws.locale_lang = {
@@ -268,6 +276,7 @@ function _make_mkws_team($, teamName) {
     var SourceMax = 16;
     var SubjectMax = 10;
     var AuthorMax = 10;
+    var m_query; // initially undefined
 
     if (!isNaN(parseInt(mkws_config.perpage_default))) {
        recPerPage = parseInt(mkws_config.perpage_default);
@@ -313,10 +322,10 @@ function _make_mkws_team($, teamName) {
 
     function renderSummary(hit)
     {
-       loadTemplate("Summary");
+       var template = loadTemplate("Summary");
        hit._id = "mkwsRec_" + hit.recid;
        hit._onclick = "mkws.showDetails(this.id);return false;"
-       return mkws.templateSummary(hit);
+       return template(hit);
     }
 
 
@@ -486,7 +495,7 @@ function _make_mkws_team($, teamName) {
 
        // Re-use previous query/sort if new ones are not specified
        if (query) {
-           mkws.query = query;
+           m_query = query;
        }
        if (sort) {
            m_sort = sort;
@@ -521,9 +530,9 @@ function _make_mkws_team($, teamName) {
        if (windowid) {
            params.windowid = windowid;
        }
-       debug("triggerSearch(" + mkws.query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
+       debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
 
-       m_paz.search(mkws.query, recPerPage, m_sort, pp2filter, undefined, params);
+       m_paz.search(m_query, recPerPage, m_sort, pp2filter, undefined, params);
     }
 
     function loadSelect ()
@@ -772,8 +781,7 @@ function _make_mkws_team($, teamName) {
 
     function renderDetails(data, marker)
     {
-       loadTemplate("Record");
-       var template = mkws.templateRecord;
+       var template = loadTemplate("Record");
        var details = template(data);
        return '<div class="details" id="mkwsDet_' + data.recid + '">' + details + '</div>';
     }
@@ -781,17 +789,20 @@ function _make_mkws_team($, teamName) {
 
     function loadTemplate(name)
     {
-       if (mkws['template' + name])
-           return; // It's already been done
+       var template = mkws['template' + name];
+
+       if (template === undefined) {
+           var source = $("#mkwsTemplate" + name).html();
+           if (!source) {
+               source = defaultTemplate(name);
+           }
 
-       var source = $("#mkwsTemplate" + name).html();
-       if (!source) {
-           source = defaultTemplate(name);
+           template = Handlebars.compile(source);
+           debug("compiled template '" + name + "'");
+           mkws['template' + name] = template;
        }
 
-       var template = Handlebars.compile(source);
-       debug("compiled template '" + name + "'");
-       mkws['template' + name] = template;
+       return template;
     }