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