web/res/metadataplayer/Mediafragment.js
changeset 598 d366aa22bd79
child 618 f65c1c3f77ea
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/res/metadataplayer/Mediafragment.js	Thu May 03 17:52:07 2012 +0200
@@ -0,0 +1,77 @@
+IriSP.Widgets.Mediafragment = function(player, config) {
+    IriSP.Widgets.Widget.call(this, player, config);
+    this.last_hash = "";
+    window.onhashchange = this.functionWrapper("goToHash");
+    if (typeof window.addEventListener !== "undefined") {
+        window.addEventListener('message', function(_msg) {
+            if (_msg.data.type === "hashchange") {
+                document.location.hash = _msg.data.hash;
+            }
+        })
+    };
+    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.goToHash();
+}
+
+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 (_tab[0] === '#t') {
+            this.player.popcorn.currentTime(_tab[1]);
+        }
+    }
+}
+
+IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) {
+    this.setHash( '#id=' + this.source.unNamespace(_annotationId) );
+}
+
+IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) {
+    _time = (typeof _time !== "undefined" ? _time : this.player.popcorn.currentTime() );
+    this.setHash( '#t=' + _time );
+}
+
+IriSP.Widgets.Mediafragment.prototype.setHash = function(_hash) {
+    if (!this.blocked && this.last_hash !== _hash) {
+        this.last_hash = _hash;
+        document.location.hash = _hash;
+        if (window.parent !== window) {
+            window.parent.postMessage({
+                type: "hashchange",
+                hash: _hash
+            })
+        }
+        this.block();
+    }
+}
+
+IriSP.Widgets.Mediafragment.prototype.unblock = function() {
+    if (typeof this.blockTimeout !== "undefined") {
+        window.clearTimeout(this.blockTimeout);
+    }
+    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"), 1000);
+}