Part of ACREP-35, and surely the last part?
[mkws-moved-to-github.git] / src / mkws-team.js
index c688f76..1fee70d 100644 (file)
@@ -5,7 +5,7 @@
 //
 // Some functions are visible as member-functions to be called from
 // outside code -- specifically, from generated HTML. These functions
-// are that.switchView(), showDetails(), limitTarget(), limitQuery(),
+// are that.switchView(), showDetails(),
 // limitCategory(), delimitTarget(), delimitQuery(), showPage(),
 // pagerPrev(), pagerNext().
 //
@@ -114,6 +114,56 @@ mkws.makeTeam = function($, teamName) {
     return x;
   }
 
+  that.handleChanges = function(oldState, newState) {
+    var reSearch = false;
+    var reShow = false;
+    var showPage = undefined;
+
+    for (var key in newState) {
+      var val = newState[key];
+      if (newState.hasOwnProperty(key) &&
+          (!oldState || val != oldState[key])) {
+        that.warn("changed property " + key + ": " + 
+                  (oldState ? ("'" + oldState[key] + "'") : "undefined") +
+                  " -> '" + val + "'");
+        if (key === 'query') {
+          m_state.query = val;
+          reSearch = true;
+        } else if (key === 'page') {
+          showPage = parseInt(val);
+        } else if (key === 'sort') {
+          that.set_sortOrder(val);
+          if (m_state.query) reShow = true;
+        } else if (key === 'size') {
+          that.set_perpage(val);
+          if (m_state.query) reShow = true;
+        } else if (key.indexOf('xt-') == 0) {
+          limitTarget(key.substring(3), val);
+          that.warn("limited target to " + val + ": m_state.query=" + m_state.query);
+          if (m_state.query) reSearch = true;
+        } else if (key.indexOf('xf-') == 0) {
+          var a = key.split('-');
+          limitQuery(a[1], a[2]);
+          if (m_state.query) reSearch = true;
+        } else {
+          that.warn("unsupported fragment property '" + key + "'='" + val + "'");
+        }
+      }
+    }
+
+    if (reSearch) {
+      that.warn("searching");
+      triggerSearch();
+    } else if (showPage) {
+      that.warn("showing page " + showPage);
+      that.showPage(showPage);
+    } else if (reShow) {
+      that.warn("showing");
+      that.reShow();      
+    }
+  };
+
+
   // The following PubSub code is modified from the jQuery manual:
   // http://api.jquery.com/jQuery.Callbacks/
   //
@@ -265,18 +315,16 @@ mkws.makeTeam = function($, teamName) {
   };
 
 
-  that.limitTarget = function(id, name) {
+  function limitTarget(id, name) {
     that.info("limitTarget(id=" + id + ", name=" + name + ")");
     m_state.filters.add(targetFilter(id, name));
-    if (m_state.query) triggerSearch();
     return false;
   };
 
 
-  that.limitQuery = function(field, value) {
+  function limitQuery(field, value) {
     that.info("limitQuery(field=" + field + ", value=" + value + ")");
     m_state.filters.add(fieldFilter(field, value));
-    if (m_state.query) triggerSearch();
     return false;
   };