src/js/modules/mediafragment.js
branchpopcorn-port
changeset 587 cd051898866e
parent 466 d54cf60bb842
child 647 2984fbbcbe35
equal deleted inserted replaced
586:a3705fc7e054 587:cd051898866e
     3 IriSP.MediaFragment = function(Popcorn, config, Serializer) {
     3 IriSP.MediaFragment = function(Popcorn, config, Serializer) {
     4   IriSP.Module.call(this, Popcorn, config, Serializer);
     4   IriSP.Module.call(this, Popcorn, config, Serializer);
     5 
     5 
     6   this.mutex = false; /* a mutex because we access the url from two different functions */
     6   this.mutex = false; /* a mutex because we access the url from two different functions */
     7 
     7 
     8   this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this, IriSP.MediaFragment.advanceTime));
     8   this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this,this.advanceTime));
     9   this._Popcorn.listen( "pause", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
     9   this._Popcorn.listen( "pause", IriSP.wrap(this,this.updateTime));
    10   this._Popcorn.listen( "seeked", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
    10   this._Popcorn.listen( "seeked", IriSP.wrap(this,this.updateTime));
    11   this._Popcorn.listen( "IriSP.PolemicTweet.click", IriSP.wrap(this, IriSP.MediaFragment.updateAnnotation));
    11   this._Popcorn.listen( "IriSP.PolemicTweet.click", IriSP.wrap(this,this.updateAnnotation));
    12   this._Popcorn.listen( "IriSP.SegmentsWidget.click", IriSP.wrap(this, IriSP.MediaFragment.updateAnnotation));
    12   this._Popcorn.listen( "IriSP.SegmentsWidget.click", IriSP.wrap(this,this.updateAnnotation));
       
    13   
       
    14   window.onhashchange = IriSP.wrap(this, this.advanceTime);
    13 };
    15 };
    14 
    16 
    15 IriSP.MediaFragment.advanceTime = function() {
    17 IriSP.MediaFragment.prototype = new IriSP.Module();
       
    18 
       
    19 IriSP.MediaFragment.prototype.advanceTime = function() {
    16              var url = window.location.href;
    20              var url = window.location.href;
    17 
    21 
    18               if ( url.split( "#" )[ 1 ] != null ) {
    22               if ( url.split( "#" )[ 1 ] != null ) {
    19                   pageoffset = url.split( "#" )[1];
    23                   pageoffset = url.split( "#" )[1];
    20 
    24 
    29                     var annotationId = pageoffset.substring( 2 );
    33                     var annotationId = pageoffset.substring( 2 );
    30 
    34 
    31                     // there's no better way than that because
    35                     // there's no better way than that because
    32                     // of possible race conditions
    36                     // of possible race conditions
    33                     this._serializer.sync(IriSP.wrap(this, function() {
    37                     this._serializer.sync(IriSP.wrap(this, function() {
    34                           IriSP.MediaFragment.lookupAnnotation.call(this, annotationId); 
    38                           this.lookupAnnotation.call(this, annotationId); 
    35                           }));
    39                           }));
    36                   }
    40                   }
    37               }
    41               }
    38 };
    42 };
    39 
    43 
    40 IriSP.MediaFragment.updateTime = function() {
    44 IriSP.MediaFragment.prototype.updateTime = function() {
    41   if (this.mutex === true) {
    45   if (this.mutex === true) {
    42     return;
    46     return;
    43   }
    47   }
    44 
    48 
    45   var history = window.history;
    49   var history = window.history;
    50   splitArr = window.location.href.split( "#" )
    54   splitArr = window.location.href.split( "#" )
    51   history.replaceState( {}, "", splitArr[0] + "#t=" + this._Popcorn.currentTime().toFixed( 2 ) );
    55   history.replaceState( {}, "", splitArr[0] + "#t=" + this._Popcorn.currentTime().toFixed( 2 ) );
    52 };
    56 };
    53 
    57 
    54 
    58 
    55 IriSP.MediaFragment.updateAnnotation = function(annotationId) {
    59 IriSP.MediaFragment.prototype.updateAnnotation = function(annotationId) {
    56   var _this = this;
    60   var _this = this;
    57   this.mutex = true;
    61   this.mutex = true;
    58 
    62 
    59   var history = window.history;
    63   var history = window.history;
    60   if ( !history.pushState ) {
    64   if ( !history.pushState ) {
    66  
    70  
    67   window.setTimeout(function() { _this.mutex = false }, 50);
    71   window.setTimeout(function() { _this.mutex = false }, 50);
    68 };
    72 };
    69 
    73 
    70 // lookup and seek to the beginning of an annotation
    74 // lookup and seek to the beginning of an annotation
    71 IriSP.MediaFragment.lookupAnnotation = function(annotationId) {
    75 IriSP.MediaFragment.prototype.lookupAnnotation = function(annotationId) {
       
    76   var _this = this;
       
    77   this.mutex = true;
       
    78 
    72   var annotation = undefined;
    79   var annotation = undefined;
    73   var annotations = this._serializer._data.annotations;
    80   var annotations = this._serializer._data.annotations;
    74 
    81 
    75   var i;
    82   var i;
    76   for (i = 0; i < annotations.length; i++) {
    83   for (i = 0; i < annotations.length; i++) {
    81   }
    88   }
    82 
    89 
    83   if (typeof(annotation) !== "undefined") {
    90   if (typeof(annotation) !== "undefined") {
    84     this._Popcorn.currentTime(annotation.begin / 1000);
    91     this._Popcorn.currentTime(annotation.begin / 1000);
    85   }
    92   }
       
    93   
       
    94   window.setTimeout(function() { _this.mutex = false }, 50);
    86 };
    95 };