src/js/modules/mediafragment.js
author hamidouk
Mon, 19 Dec 2011 15:25:22 +0100
branchpopcorn-port
changeset 481 a46cfeee6d77
parent 466 d54cf60bb842
child 587 cd051898866e
permissions -rw-r--r--
using jquery ui draggable changes the state of an element from absolute to relative positioning, which breaks the way our seek button expands itself, so we need to force absolute positioning, quite uglily, using jquery.
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
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
     6
  this.mutex = false; /* a mutex because we access the url from two different functions */
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
     7
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
     8
  this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this, IriSP.MediaFragment.advanceTime));
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
     9
  this._Popcorn.listen( "pause", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    10
  this._Popcorn.listen( "seeked", IriSP.wrap(this, IriSP.MediaFragment.updateTime));
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    11
  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
    12
  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
    13
};
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    14
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    15
IriSP.MediaFragment.advanceTime = function() {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    16
             var url = window.location.href;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    17
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    18
              if ( url.split( "#" )[ 1 ] != null ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    19
                  pageoffset = url.split( "#" )[1];
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    20
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    21
                  if ( pageoffset.substring(0, 2) === "t=") {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    22
                    // timecode 
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    23
                    if ( pageoffset.substring( 2 ) != null ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    24
                    var offsettime = pageoffset.substring( 2 );
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    25
                    this._Popcorn.currentTime( parseFloat( offsettime ) );
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    26
                    }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    27
                  } 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
    28
                    // annotation
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    29
                    var annotationId = pageoffset.substring( 2 );
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    30
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    31
                    // 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
    32
                    // of possible race conditions
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    33
                    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
    34
                          IriSP.MediaFragment.lookupAnnotation.call(this, annotationId); 
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
};
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    39
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    40
IriSP.MediaFragment.updateTime = function() {
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    41
  if (this.mutex === true) {
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    42
    return;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    43
  }
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    44
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    45
  var history = window.history;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    46
  if ( !history.pushState ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    47
    return false;
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
  
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    50
  splitArr = window.location.href.split( "#" )
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    51
  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
    52
};
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
IriSP.MediaFragment.updateAnnotation = function(annotationId) {
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    56
  var _this = this;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    57
  this.mutex = true;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    58
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    59
  var history = window.history;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    60
  if ( !history.pushState ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    61
    return false;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    62
  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    63
  
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    64
  splitArr = window.location.href.split( "#" )
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    65
  history.replaceState( {}, "", splitArr[0] + "#a=" + annotationId);
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    66
 
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    67
  window.setTimeout(function() { _this.mutex = false }, 50);
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    68
};
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    69
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    70
// 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
    71
IriSP.MediaFragment.lookupAnnotation = function(annotationId) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    72
  var annotation = undefined;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    73
  var annotations = this._serializer._data.annotations;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    74
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    75
  var i;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    76
  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
    77
      if (annotations[i].id === annotationId) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    78
        annotation = annotations[i];
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    79
        break;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    80
      }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    81
  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    82
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    83
  if (typeof(annotation) !== "undefined") {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    84
    this._Popcorn.currentTime(annotation.begin / 1000);
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    85
  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    86
};