web/res/metadataplayer/Mediafragment.js
changeset 1558 761ba7426984
parent 1557 7c67caaafdeb
child 1559 796b49572291
equal deleted inserted replaced
1557:7c67caaafdeb 1558:761ba7426984
     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     this.last_extra_key = "";
       
     6     this.last_extra_value = "";
       
     7 
       
     8     window.onhashchange = this.functionWrapper("goToHash");
       
     9     if (typeof window.addEventListener !== "undefined") {
       
    10         var _this = this;
       
    11         window.addEventListener('message', function(_msg) {
       
    12             if (/^#/.test(_msg.data)) {
       
    13                 _this.setWindowHash(_msg.data);
       
    14             }
       
    15         });
       
    16     };
       
    17     this.onMdpEvent("Mediafragment.setHashToAnnotation","setHashToAnnotation");
       
    18     this.blocked = false;
       
    19 };
       
    20 
       
    21 IriSP.Widgets.Mediafragment.prototype = new IriSP.Widgets.Widget();
       
    22 
       
    23 IriSP.Widgets.Mediafragment.prototype.draw = function() {
       
    24     this.onMediaEvent("setpause","setHashToTime");
       
    25     var _this = this;
       
    26     this.getWidgetAnnotations().forEach(function(_annotation) {
       
    27         _annotation.on("click", function() {
       
    28             _this.setHashToAnnotation(_annotation);
       
    29         });
       
    30     });
       
    31     if (this.media.loadedMetadata) {
       
    32         this.goToHash();
       
    33     } else {
       
    34         this.onMediaEvent("loadedmetadata","goToHash");
       
    35     }
       
    36 };
       
    37 
       
    38 IriSP.Widgets.Mediafragment.prototype.setWindowHash = function(_hash) {
       
    39     if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") {
       
    40         window.history.replaceState({}, "", _hash);
       
    41     } else {
       
    42         document.location.hash = _hash;
       
    43     }
       
    44 };
       
    45 
       
    46 IriSP.Widgets.Mediafragment.prototype.getLastHash = function() {
       
    47     var _tab = document.location.hash.replace(/^#/,'').split('&');
       
    48     _tab = IriSP._(_tab).filter(function(_el) {
       
    49         return _el && !/^(id|t)=/.test(_el);
       
    50     });
       
    51     if (this.last_hash_key) {
       
    52         _tab.push(this.last_hash_key + '=' + this.last_hash_value);
       
    53     }
       
    54     if (this.last_extra_key) {
       
    55         _tab.push(this.last_extra_key + '=' + this.last_extra_value);
       
    56     }
       
    57     return '#' + _tab.join('&');
       
    58 };
       
    59 
       
    60 IriSP.Widgets.Mediafragment.prototype.goToHash = function() {
       
    61     if (document.location.hash !== this.getLastHash()) {
       
    62         var _tab = document.location.hash.replace(/^#/,'').split('&');
       
    63         for (var _i = 0; _i < _tab.length; _i++) {
       
    64             var _subtab = _tab[_i].split("=");
       
    65             if (_subtab[0] == "id" || _subtab[0] == "t") {
       
    66                 this.last_hash_key = _subtab[0];
       
    67                 this.last_hash_value = _subtab[1];
       
    68                 if (this.last_hash_key == "id") {
       
    69                     var _annotation = this.source.getElement(this.last_hash_value);
       
    70                     if (typeof _annotation !== "undefined") {
       
    71                         this.media.setCurrentTime(_annotation.begin);
       
    72                     } else {
       
    73                         /* Proceed parsing elements, maybe a t was specified */
       
    74                         continue;
       
    75                     }
       
    76                 }
       
    77                 if (this.last_hash_key == "t") {
       
    78                     this.media.setCurrentTime(1000 * this.last_hash_value);
       
    79                 }
       
    80                 break;
       
    81             }
       
    82         }
       
    83     }
       
    84 };
       
    85 
       
    86 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotation) {
       
    87     this.setHash( 'id', _annotation.id, 't', _annotation.begin / 1000.0 );
       
    88 };
       
    89 
       
    90 IriSP.Widgets.Mediafragment.prototype.setHashToTime = function() {
       
    91     this.setHash( 't', this.media.getCurrentTime().getSeconds() );
       
    92 };
       
    93 
       
    94 IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value, _key2, _value2) {
       
    95     if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) {
       
    96         this.last_hash_key = _key;
       
    97         this.last_hash_value = _value;
       
    98         this.last_extra_key = _key2;
       
    99         this.last_extra_value = _value2;
       
   100 
       
   101         var _hash = this.getLastHash();
       
   102         this.setWindowHash(_hash);
       
   103         if (window.parent !== window) {
       
   104             window.parent.postMessage(_hash,"*");
       
   105         }
       
   106         this.block();
       
   107     }
       
   108 };
       
   109 
       
   110 IriSP.Widgets.Mediafragment.prototype.unblock = function() {
       
   111     if (typeof this.blockTimeout !== "undefined") {
       
   112         window.clearTimeout(this.blockTimeout);
       
   113     }
       
   114     this.blockTimeout = undefined;
       
   115     this.blocked = false;
       
   116 };
       
   117 
       
   118 IriSP.Widgets.Mediafragment.prototype.block = function() {
       
   119     if (typeof this.blockTimeout !== "undefined") {
       
   120         window.clearTimeout(this.blockTimeout);
       
   121     }
       
   122     this.blocked = true;
       
   123     this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1500);
       
   124 };