src/widgets/Mediafragment.js
branchnew-model
changeset 1020 198c2b79f5e1
parent 1019 3ab36f402b0c
--- a/src/widgets/Mediafragment.js	Thu Jan 02 16:40:25 2014 +0100
+++ b/src/widgets/Mediafragment.js	Thu Jan 02 16:49:20 2014 +0100
@@ -4,33 +4,23 @@
     this.last_hash_value = "";
     window.onhashchange = this.functionWrapper("goToHash");
     if (typeof window.addEventListener !== "undefined") {
-        var _this = this;
         window.addEventListener('message', function(_msg) {
             if (/^#/.test(_msg.data)) {
-                _this.setWindowHash(_msg.data);
+                this.setWindowHash(_msg.data);
             }
-        });
+        })
     };
-    this.onMdpEvent("Mediafragment.setHashToAnnotation","setHashToAnnotation");
+    this.bindPopcorn("pause","setHashToTime");
+    this.bindPopcorn("seeked","setHashToTime");
+    this.bindPopcorn("IriSP.Mediafragment.setHashToAnnotation","setHashToAnnotation");
     this.blocked = false;
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype = new IriSP.Widgets.Widget();
 
 IriSP.Widgets.Mediafragment.prototype.draw = function() {
-    this.onMediaEvent("setpause","setHashToTime");
-    var _this = this;
-    this.getWidgetAnnotations().forEach(function(_annotation) {
-        _annotation.on("click", function() {
-            _this.setHashToAnnotation(_annotation.id);
-        });
-    });
-    if (this.media.loadedMetadata) {
-        this.goToHash();
-    } else {
-        this.onMediaEvent("loadedmetadata","goToHash");
-    }
-};
+    this.goToHash();
+}
 
 IriSP.Widgets.Mediafragment.prototype.setWindowHash = function(_hash) {
     if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") {
@@ -38,7 +28,7 @@
     } else {
         document.location.hash = _hash;
     }
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype.getLastHash = function() {
     var _tab = document.location.hash.replace(/^#/,'').split('&');
@@ -49,7 +39,7 @@
         _tab.push(this.last_hash_key + '=' + this.last_hash_value);
     }
     return '#' + _tab.join('&');
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype.goToHash = function() {
     if (document.location.hash !== this.getLastHash()) {
@@ -62,25 +52,27 @@
                 if (this.last_hash_key == "id") {
                     var _annotation = this.source.getElement(this.last_hash_value);
                     if (typeof _annotation !== "undefined") {
-                        this.media.setCurrentTime(_annotation.begin);
+                        this.player.popcorn.currentTime(_annotation.begin.getSeconds());
                     }
                 }
                 if (this.last_hash_key == "t") {
-                    this.media.setCurrentTime(1000*this.last_hash_value);
+                    this.player.popcorn.currentTime(this.last_hash_value);
                 }
                 break;
             }
         }
     }
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) {
     this.setHash( 'id', _annotationId );
-};
+}
 
-IriSP.Widgets.Mediafragment.prototype.setHashToTime = function() {
-    this.setHash( 't', this.media.getCurrentTime().getSeconds() );
-};
+IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) {
+    if (_time !== NaN) {
+        this.setHash( 't', this.player.popcorn.currentTime() );
+    }
+}
 
 IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value) {
     if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) {
@@ -89,11 +81,11 @@
         var _hash = this.getLastHash();
         this.setWindowHash(_hash);
         if (window.parent !== window) {
-            window.parent.postMessage(_hash,"*");
+            window.parent.postMessage(_hash,"*")
         }
         this.block();
     }
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype.unblock = function() {
     if (typeof this.blockTimeout !== "undefined") {
@@ -101,12 +93,12 @@
     }
     this.blockTimeout = undefined;
     this.blocked = false;
-};
+}
 
 IriSP.Widgets.Mediafragment.prototype.block = function() {
     if (typeof this.blockTimeout !== "undefined") {
         window.clearTimeout(this.blockTimeout);
     }
     this.blocked = true;
-    this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1500);
-};
+    this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1000);
+}