Part of MKWS-64.
[mkws-moved-to-github.git] / src / mkws-core.js
index 528ed94..ce4402e 100644 (file)
@@ -1,5 +1,5 @@
 /*! MKWS, the MasterKey Widget Set.
- *  Copyright (C) 2013-2014 Index Data
+ *  Copyright (C) 2013-2015 Index Data
  *  See the file LICENSE for details
  */
 
@@ -48,6 +48,11 @@ window.mkws = {
       "Location": "Ort",
       "Records": "Datensätze",
       "Targets": "Datenbanken",
+      "State": "Status",
+      "relevance": "Relevanz",
+      "title": "Titel",
+      "newest": "Neueste",
+      "oldest": "Älteste",
 
       "dummy": "dummy"
     },
@@ -77,6 +82,11 @@ window.mkws = {
       "Location": "Lokation",
       "Records": "Poster",
       "Targets": "Baser",
+      "State": "Status",
+      "relevance": "Relevans",
+      "title": "Titel",
+      "newest": "Nyeste",
+      "oldest": "Ældste",
 
       "dummy": "dummy"
     }
@@ -159,6 +169,12 @@ mkws.registerWidgetType = function(name, fn) {
   mkws.info("registered widget-type '" + name + "'");
 };
 
+mkws.aliasWidgetType = function(newName, oldName) {
+  mkws.widgetType2function[newName] = mkws.widgetType2function[oldName];
+  mkws.info("aliased widget-type '" + newName + "' to '" + oldName + "'");
+
+};
+
 mkws.promotionFunction = function(name) {
   return mkws.widgetType2function[name];
 };
@@ -532,7 +548,7 @@ mkws.info("Using window.name '" + window.name + "'");
   // or a selector string you would like to constrain the search for widgets to.
   //
   // This function has no side effects if run again on an operating session,
-  // even if the element/selector passed causes existing widgets to be reparsed: 
+  // even if the element/selector passed causes existing widgets to be reparsed:
   //
   // (TODO: that last bit isn't true and we currently have to avoid reinitialising
   // widgets, MKWS-261)
@@ -543,7 +559,7 @@ mkws.info("Using window.name '" + window.name + "'");
   mkws.init = function(message, rootsel) {
     var greet = "MKWS initialised";
     if (rootsel) greet += " (limited to " + rootsel + ")"
-    if (message) greet += " :: " + message; 
+    if (message) greet += " :: " + message;
     mkws.info(greet);
 
     // MKWS is not active until init() has been run against an object with widget nodes.
@@ -615,11 +631,6 @@ mkws.info("Using window.name '" + window.name + "'");
       var myTeam = mkws.teams[tName]
       myTeam.makePz2();
       myTeam.info("made PZ2 object");
-      /*
-        myTeam.visitWidgets(function(t, w) {
-          mkws.debug("  has widget of type '" + t + "': " + w);
-        });
-      */
     }
 
     function sp_auth_url(config) {
@@ -652,8 +663,11 @@ mkws.info("Using window.name '" + window.name + "'");
     } else if (!mkws.authenticating) {
       // raw pp2 or we have a session already open
       runAutoSearches();
+      for (var teamName in mkws.teams) {
+        mkws.teams[teamName].queue("authenticated").publish();
+      }
     }
-    
+
     mkws.isActive = true;
     return true;
   };
@@ -671,4 +685,50 @@ mkws.info("Using window.name '" + window.name + "'");
       mkws.logger.setOptions({ "level": mkws.stringToLevel(tmp) });
     }
   }
+
+  // This function is adapted from Yarin's code at
+  // https://stackoverflow.com/questions/4197591/parsing-url-hash-fragment-identifier-with-javascript#4198132
+  function parse_fragment(s) {
+    var hashParams = {};
+    var e,
+        a = /\+/g,  // Regex for replacing addition symbol with a space
+        r = /([^&;=]+)=?([^&;]*)/g,
+        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
+        q = s.substring(1);
+
+    while (e = r.exec(q)) {
+      var key = d(e[1]);
+      if (key === 'mkws') {
+        key = 'AUTO';
+      } else {
+        key = key.replace('mkws', '');
+      }
+      var team = mkws.teams[key];
+      if (team) {
+        hashParams[key] = team.parseFragment(d(e[2]));
+      } else {
+        alert("can't resolve team name '" + key + "'");
+      }
+    }
+
+    return hashParams;
+  }
+
+  var oldHash = location.hash;
+  $(window).bind('hashchange', function(e) {
+    mkws.warn("hashchange: old='" + oldHash + "', new='" + location.hash + "'");
+    var oldStates = parse_fragment(oldHash);
+    var newStates = parse_fragment(location.hash);
+    oldHash = location.hash;
+
+    for (var teamName in newStates) {
+      var team = mkws.teams[teamName];
+      if (!team) {
+        alert("can't resolve team name '" + teamName + "'");
+        continue;
+      }
+
+      team.handleChanges(oldStates[teamName], newStates[teamName]);
+    }
+  });
 })(mkws.$);