src/js/modules/mediafragment.js
branchpopcorn-port
changeset 462 3583ef3b208b
child 466 d54cf60bb842
equal deleted inserted replaced
461:a9c5eeca190c 462:3583ef3b208b
       
     1 /* mediafragment module */
       
     2 
       
     3 IriSP.MediaFragment = function(Popcorn, config, Serializer) {
       
     4   IriSP.Module.call(this, Popcorn, config, Serializer);
       
     5 
       
     6   this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this, IriSP.MediaFragment.advanceTime));
       
     7 //  this._Popcorn.listen( "pause", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
       
     8 //  this._Popcorn.listen( "seeked", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
       
     9   this._Popcorn.listen( "IriSP.PolemicTweet.click", IriSP.wrap(this, IriSP.MediaFragment.updateAnnotation));
       
    10   this._Popcorn.listen( "IriSP.SegmentsWidget.click", IriSP.wrap(this, IriSP.MediaFragment.updateAnnotation));
       
    11 };
       
    12 
       
    13 IriSP.MediaFragment.advanceTime = function() {
       
    14              var url = window.location.href;
       
    15 
       
    16               if ( url.split( "#" )[ 1 ] != null ) {
       
    17                   pageoffset = url.split( "#" )[1];
       
    18 
       
    19                   if ( pageoffset.substring(0, 2) === "t=") {
       
    20                     // timecode 
       
    21                     if ( pageoffset.substring( 2 ) != null ) {
       
    22                     var offsettime = pageoffset.substring( 2 );
       
    23                     this._Popcorn.currentTime( parseFloat( offsettime ) );
       
    24                     }
       
    25                   } else if ( pageoffset.substring(0, 2) === "a=") {
       
    26                     // annotation
       
    27                     var annotationId = pageoffset.substring( 2 );
       
    28 
       
    29                     // there's no better way than that because
       
    30                     // of possible race conditions
       
    31                     this._serializer.sync(IriSP.wrap(this, function() {
       
    32                           IriSP.MediaFragment.lookupAnnotation.call(this, annotationId); 
       
    33                           }));
       
    34                   }
       
    35               }
       
    36 };
       
    37 
       
    38 IriSP.MediaFragment.updateTime = function() {
       
    39   var history = window.history;
       
    40   if ( !history.pushState ) {
       
    41     return false;
       
    42   }
       
    43   
       
    44   splitArr = window.location.href.split( "#" )
       
    45   history.replaceState( {}, "", splitArr[0] + "#t=" + this._Popcorn.currentTime().toFixed( 2 ) );
       
    46 };
       
    47 
       
    48 
       
    49 IriSP.MediaFragment.updateAnnotation = function(annotationId) {
       
    50   var history = window.history;
       
    51   if ( !history.pushState ) {
       
    52     return false;
       
    53   }
       
    54   
       
    55   splitArr = window.location.href.split( "#" )
       
    56   history.replaceState( {}, "", splitArr[0] + "#a=" + annotationId);
       
    57  
       
    58 };
       
    59 
       
    60 // lookup and seek to the beginning of an annotation
       
    61 IriSP.MediaFragment.lookupAnnotation = function(annotationId) {
       
    62   var annotation = undefined;
       
    63   var annotations = this._serializer._data.annotations;
       
    64 
       
    65   var i;
       
    66   for (i = 0; i < annotations.length; i++) {
       
    67       if (annotations[i].id === annotationId) {
       
    68         annotation = annotations[i];
       
    69         break;
       
    70       }
       
    71   }
       
    72 
       
    73   if (typeof(annotation) !== "undefined") {
       
    74     this._Popcorn.currentTime(annotation.begin / 1000);
       
    75   }
       
    76 };