Introduce new m_state object inside the team.
[mkws-moved-to-github.git] / src / mkws-team.js
index d090744..7f86471 100644 (file)
@@ -1,3 +1,4 @@
+"use strict";
 // Factory function for team objects. As much as possible, this uses
 // only member variables (prefixed "m_") and inner functions with
 // private scope.
 //
 mkws.makeTeam = function($, teamName) {
   var that = {};
-  var m_teamName = teamName;
-  var m_submitted = false;
-  var m_query; // initially undefined
+
+  // Member variables are separated into two categories
+
+  // 1. Persistent state (to be coded in URL fragment)
+  var m_state = {
+    query: null // initially undefined
+  }
   var m_sortOrder; // will be set below
   var m_perpage; // will be set below
   var m_filterSet = filterSet(that);
-  var m_totalRecordCount = 0;
   var m_currentPage = 1;
   var m_currentRecordId = '';
+
+  // 2. Internal state (not to be coded)
+  var m_teamName = teamName;
+  var m_paz; // will be initialised below
+  var m_submitted = false;
+  var m_totalRecordCount = 0;
   var m_currentRecordData = null;
   var m_logTime = {
     // Timestamps for logging
     "start": $.now(),
     "last": $.now()
   };
-  var m_paz; // will be initialised below
   var m_templateText = {}; // widgets can register templates to be compiled
   var m_template = {}; // compiled templates, from any source
   var m_widgets = {}; // Maps widget-type to array of widget objects
@@ -44,7 +53,7 @@ mkws.makeTeam = function($, teamName) {
   that.submitted = function() { return m_submitted; };
   that.sortOrder = function() { return m_sortOrder; };
   that.perpage = function() { return m_perpage; };
-  that.query = function() { return m_query; };
+  that.query = function() { return m_state.query; };
   that.totalRecordCount = function() { return m_totalRecordCount; };
   that.currentPage = function() { return m_currentPage; };
   that.currentRecordId = function() { return m_currentRecordId; };
@@ -88,9 +97,6 @@ mkws.makeTeam = function($, teamName) {
     that.queue("log").publish(m_teamName, timestamp, s);
   }
 
-  // ### transitional placeholder function until we have promoted all invocations
-  that.log = function (x) { _log(mkws.log, x) };
-
   that.trace = function(x) { _log(mkws.trace, x) };
   that.debug = function(x) { _log(mkws.debug, x) };
   that.info = function(x) { _log(mkws.info, x) };
@@ -176,7 +182,7 @@ mkws.makeTeam = function($, teamName) {
       params.onbytarget = onBytarget;
       that.info("setting bytarget callback");
     }
-    if (m_queues.stat) {
+    if (m_queues.stat || m_queues.firstrecords || m_queues.complete) {
       params.onstat = onStat;
       that.info("setting stat callback");
     }
@@ -217,7 +223,7 @@ mkws.makeTeam = function($, teamName) {
   that.limitTarget = function(id, name) {
     that.info("limitTarget(id=" + id + ", name=" + name + ")");
     m_filterSet.add(targetFilter(id, name));
-    if (m_query) triggerSearch();
+    if (m_state.query) triggerSearch();
     return false;
   };
 
@@ -225,7 +231,7 @@ mkws.makeTeam = function($, teamName) {
   that.limitQuery = function(field, value) {
     that.info("limitQuery(field=" + field + ", value=" + value + ")");
     m_filterSet.add(fieldFilter(field, value));
-    if (m_query) triggerSearch();
+    if (m_state.query) triggerSearch();
     return false;
   };
 
@@ -235,7 +241,7 @@ mkws.makeTeam = function($, teamName) {
     // Only one category filter at a time
     m_filterSet.removeMatching(function(f) { return f.type === 'category' });
     if (id !== '') m_filterSet.add(categoryFilter(id));
-    if (m_query) triggerSearch();
+    if (m_state.query) triggerSearch();
     return false;
   };
 
@@ -243,7 +249,7 @@ mkws.makeTeam = function($, teamName) {
   that.delimitTarget = function(id) {
     that.info("delimitTarget(id=" + id + ")");
     m_filterSet.removeMatching(function(f) { return f.type === 'target' });
-    if (m_query) triggerSearch();
+    if (m_state.query) triggerSearch();
     return false;
   };
 
@@ -252,7 +258,7 @@ mkws.makeTeam = function($, teamName) {
     that.info("delimitQuery(field=" + field + ", value=" + value + ")");
     m_filterSet.removeMatching(function(f) { return f.type == 'field' &&
                                              field == f.field && value == f.value });
-    if (m_query) triggerSearch();
+    if (m_state.query) triggerSearch();
     return false;
   };
 
@@ -309,10 +315,9 @@ mkws.makeTeam = function($, teamName) {
 
   function triggerSearch(query, sortOrder, maxrecs, perpage, limit, targets, torusquery) {
     resetPage();
-    queue("navi").publish();
 
     // Continue to use previous query/sort-order unless new ones are specified
-    if (query) m_query = query;
+    if (query) m_state.query = query;
     if (sortOrder) m_sortOrder = sortOrder;
     if (perpage) m_perpage = perpage;
     if (targets) m_filterSet.add(targetFilter(targets, targets));
@@ -333,10 +338,11 @@ mkws.makeTeam = function($, teamName) {
       params.torusquery = torusquery;
     }
 
-    that.info("triggerSearch(" + m_query + "): filters = " + m_filterSet.toJSON() + ", " +
+    that.info("triggerSearch(" + m_state.query + "): filters = " + m_filterSet.toJSON() + ", " +
         "pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
 
-    m_paz.search(m_query, m_perpage, m_sortOrder, pp2filter, undefined, params);
+    m_paz.search(m_state.query, m_perpage, m_sortOrder, pp2filter, undefined, params);
+    queue("searchtriggered").publish();
   }
 
   // fetch record details to be retrieved from the record queue
@@ -502,8 +508,7 @@ mkws.makeTeam = function($, teamName) {
       }
     }
     return undefined;
-  }
-
+  };
 
   return that;
 };