src/js/modules/mediafragment.js
branchnew-model
changeset 881 f11b234497f7
parent 880 4c7b33bf2795
child 882 61c384dda19e
equal deleted inserted replaced
880:4c7b33bf2795 881:f11b234497f7
     1 /* mediafragment module */
       
     2 
       
     3 IriSP.MediaFragment = function(Popcorn, config, Serializer) {
       
     4   IriSP.Module.call(this, Popcorn, config, Serializer);
       
     5 
       
     6   this.mutex = false; /* a mutex because we access the url from two different functions */
       
     7 
       
     8   this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this,this.advanceTime));
       
     9   this._Popcorn.listen( "pause", IriSP.wrap(this,this.updateTime));
       
    10   this._Popcorn.listen( "seeked", IriSP.wrap(this,this.updateTime));
       
    11   this._Popcorn.listen( "IriSP.PolemicTweet.click", IriSP.wrap(this,this.updateAnnotation));
       
    12   this._Popcorn.listen( "IriSP.SegmentsWidget.click", IriSP.wrap(this,this.updateAnnotation));
       
    13   
       
    14   window.onhashchange = IriSP.wrap(this, this.advanceTime);
       
    15 };
       
    16 
       
    17 IriSP.MediaFragment.prototype = new IriSP.Module();
       
    18 
       
    19 IriSP.MediaFragment.prototype.advanceTime = function() {
       
    20              var url = window.location.href;
       
    21 
       
    22               if ( url.split( "#" )[ 1 ] != null ) {
       
    23                   pageoffset = url.split( "#" )[1];
       
    24 
       
    25                   if ( pageoffset.substring(0, 2) === "t=") {
       
    26                     // timecode 
       
    27                     if ( pageoffset.substring( 2 ) != null ) {
       
    28                     var offsettime = pageoffset.substring( 2 );
       
    29                     this._Popcorn.currentTime( parseFloat(offsettime) );
       
    30                     
       
    31                     /* we have to trigger this signal manually because of a
       
    32                      bug in the jwplayer */
       
    33                     this._Popcorn.trigger("seeked", parseFloat(offsettime));
       
    34                     }
       
    35                   } else if ( pageoffset.substring(0, 3) === "id=") {
       
    36                     // annotation
       
    37                     var annotationId = pageoffset.substring( 3 );
       
    38                     // there's no better way than that because
       
    39                     // of possible race conditions
       
    40                     this._serializer.sync(IriSP.wrap(this, function() {
       
    41                           this.lookupAnnotation.call(this, annotationId); 
       
    42                           }));
       
    43                   }
       
    44               }
       
    45 };
       
    46 
       
    47 /** handler for the seeked signal. It may have or may have not an argument.
       
    48     @param time if not undefined, the time we're seeking to 
       
    49 */
       
    50 IriSP.MediaFragment.prototype.updateTime = function(time) {
       
    51   if (this.mutex === true) {
       
    52     return;
       
    53   }
       
    54 
       
    55   var history = window.history;
       
    56   if ( !history.pushState ) {
       
    57     return false;
       
    58   }
       
    59     
       
    60   if (IriSP.null_or_undefined(time) || typeof(time) != "number") {
       
    61     var ntime = this._Popcorn.currentTime().toFixed(2)
       
    62   } else {
       
    63     var ntime = time.toFixed(2);
       
    64   }
       
    65 
       
    66   // used to relay the new hash to the embedder
       
    67   this._Popcorn.trigger("IriSP.Mediafragment.hashchange", "#t=" + ntime);
       
    68   
       
    69   splitArr = window.location.href.split( "#" )
       
    70   history.replaceState( {}, "", splitArr[0] + "#t=" + ntime );
       
    71 };
       
    72 
       
    73 
       
    74 IriSP.MediaFragment.prototype.updateAnnotation = function(annotationId) {
       
    75   var _this = this;
       
    76   this.mutex = true;
       
    77 
       
    78   var history = window.history;
       
    79   if ( !history.pushState ) {
       
    80     return false;
       
    81   }
       
    82  
       
    83   
       
    84   // used to relay the new hash to the embedder
       
    85   this._Popcorn.trigger("IriSP.Mediafragment.hashchange", "#id=" + annotationId);
       
    86   
       
    87   splitArr = window.location.href.split( "#" )
       
    88   history.replaceState( {}, "", splitArr[0] + "#id=" + annotationId);
       
    89 
       
    90   
       
    91   // reset the mutex afterwards to prevent the module from reacting to his own changes.
       
    92   window.setTimeout(function() { _this.mutex = false }, 50);
       
    93 };
       
    94 
       
    95 // lookup and seek to the beginning of an annotation
       
    96 IriSP.MediaFragment.prototype.lookupAnnotation = function(annotationId) {
       
    97   var _this = this;
       
    98   this.mutex = true;
       
    99 
       
   100   var annotation = undefined;
       
   101   var annotations = this._serializer._data.annotations;
       
   102 
       
   103   var i;
       
   104   for (i = 0; i < annotations.length; i++) {
       
   105       if (annotations[i].id === annotationId) {
       
   106         annotation = annotations[i];
       
   107         break;
       
   108       }
       
   109   }
       
   110 
       
   111   if (typeof(annotation) !== "undefined") {
       
   112     this._Popcorn.currentTime(annotation.begin / 1000);
       
   113 
       
   114     /* we have to trigger this signal manually because of a
       
   115      bug in the jwplayer */
       
   116     this._Popcorn.trigger("seeked", annotation.begin / 1000);
       
   117     this._Popcorn.trigger("IriSP.Mediafragment.showAnnotation", annotationId);
       
   118   }
       
   119   
       
   120   window.setTimeout(function() { _this.mutex = false }, 50);
       
   121 };