metadataplayer edwin/metadataplayer/Mediafragment.js
changeset 2 3cc57cb48aea
child 4 ed1126cd2b80
equal deleted inserted replaced
1:ed19b689de87 2:3cc57cb48aea
       
     1 IriSP.Widgets.Mediafragment = function(player, config) {
       
     2     IriSP.Widgets.Widget.call(this, player, config);
       
     3     this.last_hash_key = "";
       
     4     this.last_hash_value = "";
       
     5     window.onhashchange = this.functionWrapper("goToHash");
       
     6     if (typeof window.addEventListener !== "undefined") {
       
     7         window.addEventListener('message', function(_msg) {
       
     8             if (/^#/.test(_msg.data)) {
       
     9                 this.setWindowHash(_msg.data);
       
    10             }
       
    11         })
       
    12     };
       
    13     this.bindPopcorn("pause","setHashToTime");
       
    14     this.bindPopcorn("seeked","setHashToTime");
       
    15     this.bindPopcorn("IriSP.Mediafragment.setHashToAnnotation","setHashToAnnotation");
       
    16     this.blocked = false;
       
    17 }
       
    18 
       
    19 IriSP.Widgets.Mediafragment.prototype = new IriSP.Widgets.Widget();
       
    20 
       
    21 IriSP.Widgets.Mediafragment.prototype.draw = function() {
       
    22     this.goToHash();
       
    23 }
       
    24 
       
    25 IriSP.Widgets.Mediafragment.prototype.setWindowHash = function(_hash) {
       
    26     if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") {
       
    27         window.history.replaceState({}, "", _hash);
       
    28     } else {
       
    29         document.location.hash = _hash;
       
    30     }
       
    31 }
       
    32 
       
    33 IriSP.Widgets.Mediafragment.prototype.getLastHash = function() {
       
    34     var _tab = document.location.hash.replace(/^#/,'').split('&');
       
    35     _tab = IriSP._(_tab).filter(function(_el) {
       
    36         return _el && !/^(id|t)=/.test(_el);
       
    37     });
       
    38     if (this.last_hash_key) {
       
    39         _tab.push(this.last_hash_key + '=' + this.last_hash_value);
       
    40     }
       
    41     return '#' + _tab.join('&');
       
    42 }
       
    43 
       
    44 IriSP.Widgets.Mediafragment.prototype.goToHash = function() {
       
    45     if (document.location.hash !== this.getLastHash()) {
       
    46         var _tab = document.location.hash.replace(/^#/,'').split('&');
       
    47         for (var _i = 0; _i < _tab.length; _i++) {
       
    48             var _subtab = _tab[_i].split("=");
       
    49             if (_subtab[0] == "id" || _subtab[0] == "t") {
       
    50                 this.last_hash_key = _subtab[0];
       
    51                 this.last_hash_value = _subtab[1];
       
    52                 if (this.last_hash_key == "id") {
       
    53                     var _annotation = this.source.getElement(this.last_hash_value);
       
    54                     if (typeof _annotation !== "undefined") {
       
    55                         this.player.popcorn.currentTime(_annotation.begin.getSeconds());
       
    56                     }
       
    57                 }
       
    58                 if (this.last_hash_key == "t") {
       
    59                     this.player.popcorn.currentTime(this.last_hash_value);
       
    60                 }
       
    61                 break;
       
    62             }
       
    63         }
       
    64     }
       
    65 }
       
    66 
       
    67 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) {
       
    68     this.setHash( 'id', this.source.unNamespace(_annotationId) );
       
    69 }
       
    70 
       
    71 IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) {
       
    72     if (_time !== NaN) {
       
    73         this.setHash( 't', this.player.popcorn.currentTime() );
       
    74     }
       
    75 }
       
    76 
       
    77 IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value) {
       
    78     if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) {
       
    79         this.last_hash_key = _key;
       
    80         this.last_hash_value = _value;
       
    81         var _hash = this.getLastHash();
       
    82         this.setWindowHash(_hash);
       
    83         if (window.parent !== window) {
       
    84             window.parent.postMessage(_hash,"*")
       
    85         }
       
    86         this.block();
       
    87     }
       
    88 }
       
    89 
       
    90 IriSP.Widgets.Mediafragment.prototype.unblock = function() {
       
    91     if (typeof this.blockTimeout !== "undefined") {
       
    92         window.clearTimeout(this.blockTimeout);
       
    93     }
       
    94     this.blockTimeout = undefined;
       
    95     this.blocked = false;
       
    96 }
       
    97 
       
    98 IriSP.Widgets.Mediafragment.prototype.block = function() {
       
    99     if (typeof this.blockTimeout !== "undefined") {
       
   100         window.clearTimeout(this.blockTimeout);
       
   101     }
       
   102     this.blocked = true;
       
   103     this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1000);
       
   104 }