src/widgets/Mediafragment.js
branchnew-model
changeset 905 029a4efe9e24
parent 883 d35ad8111c5e
child 906 4b6e154ae8de
--- a/src/widgets/Mediafragment.js	Fri May 18 18:23:51 2012 +0200
+++ b/src/widgets/Mediafragment.js	Mon May 21 13:17:47 2012 +0200
@@ -1,6 +1,7 @@
 IriSP.Widgets.Mediafragment = function(player, config) {
     IriSP.Widgets.Widget.call(this, player, config);
-    this.last_hash = "";
+    this.last_hash_key = "";
+    this.last_hash_value = "";
     window.onhashchange = this.functionWrapper("goToHash");
     if (typeof window.addEventListener !== "undefined") {
         window.addEventListener('message', function(_msg) {
@@ -21,35 +22,55 @@
     this.goToHash();
 }
 
+IriSP.Widgets.Mediafragment.prototype.getLastHash = function() {
+    var _tab = document.location.hash.replace(/^#/,'').split('&');
+    _tab = IriSP._(_tab).filter(function(_el) {
+        return _el && !/^(id|t)=/.test(_el);
+    });
+    if (this.last_hash_key) {
+        _tab.push(this.last_hash_key + '=' + this.last_hash_value);
+    }
+    return '#' + _tab.join('&');
+}
+
 IriSP.Widgets.Mediafragment.prototype.goToHash = function() {
-    if (document.location.hash !== this.last_hash) {
-        this.last_hash = document.location.hash;
-        var _tab = this.last_hash.split("=");
-        if (_tab[0] === '#id') {
-            var _annotation = this.source.getElement(_tab[1]);
-            if (typeof _annotation !== "undefined") {
-                this.player.popcorn.currentTime(_annotation.begin.getSeconds());
+    if (document.location.hash !== this.getLastHash()) {
+        var _tab = document.location.hash.replace(/^#/,'').split('&');
+        for (var _i = 0; _i < _tab.length; _i++) {
+            var _subtab = _tab[_i].split("=");
+            if (_subtab[0] == "id" || _subtab[0] == "t") {
+                this.last_hash_key = _subtab[0];
+                this.last_hash_value = _subtab[1];
+                if (this.last_hash_key == "id") {
+                    var _annotation = this.source.getElement(this.last_hash_value);
+                    if (typeof _annotation !== "undefined") {
+                        this.player.popcorn.currentTime(_annotation.begin.getSeconds());
+                    }
+                }
+                if (this.last_hash_key == "t") {
+                    this.player.popcorn.currentTime(this.last_hash_value);
+                }
+                break;
             }
         }
-        if (_tab[0] === '#t') {
-            this.player.popcorn.currentTime(_tab[1]);
-        }
     }
 }
 
 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) {
-    this.setHash( '#id=' + this.source.unNamespace(_annotationId) );
+    this.setHash( 'id', this.source.unNamespace(_annotationId) );
 }
 
 IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) {
     if (_time !== NaN) {
-        this.setHash( '#t=' + this.player.popcorn.currentTime() );
+        this.setHash( 't', this.player.popcorn.currentTime() );
     }
 }
 
-IriSP.Widgets.Mediafragment.prototype.setHash = function(_hash) {
-    if (!this.blocked && this.last_hash !== _hash) {
-        this.last_hash = _hash;
+IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value) {
+    if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) {
+        this.last_hash_key = _key;
+        this.last_hash_value = _value;
+        var _hash = this.getLastHash();
         document.location.hash = _hash;
         if (window.parent !== window) {
             window.parent.postMessage({