src/js/modules/mediafragment.js
author hamidouk
Thu, 05 Jan 2012 14:58:56 +0100
branchpopcorn-port
changeset 587 cd051898866e
parent 466 d54cf60bb842
child 647 2984fbbcbe35
permissions -rw-r--r--
implemented hashchange event handler.
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
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
     8
  this._Popcorn.listen( "loadedmetadata", IriSP.wrap(this,this.advanceTime));
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
     9
  this._Popcorn.listen( "pause", IriSP.wrap(this,this.updateTime));
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    10
  this._Popcorn.listen( "seeked", IriSP.wrap(this,this.updateTime));
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    11
  this._Popcorn.listen( "IriSP.PolemicTweet.click", IriSP.wrap(this,this.updateAnnotation));
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    12
  this._Popcorn.listen( "IriSP.SegmentsWidget.click", IriSP.wrap(this,this.updateAnnotation));
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    13
  
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    14
  window.onhashchange = IriSP.wrap(this, this.advanceTime);
462
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
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    17
IriSP.MediaFragment.prototype = new IriSP.Module();
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    18
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    19
IriSP.MediaFragment.prototype.advanceTime = function() {
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    20
             var url = window.location.href;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    21
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    22
              if ( url.split( "#" )[ 1 ] != null ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    23
                  pageoffset = url.split( "#" )[1];
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
                  if ( pageoffset.substring(0, 2) === "t=") {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    26
                    // timecode 
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    27
                    if ( pageoffset.substring( 2 ) != null ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    28
                    var offsettime = pageoffset.substring( 2 );
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    29
                    this._Popcorn.currentTime( parseFloat( offsettime ) );
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
                  } 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
    32
                    // annotation
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    33
                    var annotationId = pageoffset.substring( 2 );
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
                    // 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
    36
                    // of possible race conditions
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    37
                    this._serializer.sync(IriSP.wrap(this, function() {
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    38
                          this.lookupAnnotation.call(this, annotationId); 
462
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
                  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    41
              }
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
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    44
IriSP.MediaFragment.prototype.updateTime = function() {
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    45
  if (this.mutex === true) {
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    46
    return;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    47
  }
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    48
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    49
  var history = window.history;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    50
  if ( !history.pushState ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    51
    return false;
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
  splitArr = window.location.href.split( "#" )
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    55
  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
    56
};
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
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    59
IriSP.MediaFragment.prototype.updateAnnotation = function(annotationId) {
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    60
  var _this = this;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    61
  this.mutex = true;
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    62
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    63
  var history = window.history;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    64
  if ( !history.pushState ) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    65
    return false;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    66
  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    67
  
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    68
  splitArr = window.location.href.split( "#" )
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    69
  history.replaceState( {}, "", splitArr[0] + "#a=" + annotationId);
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    70
 
466
d54cf60bb842 annotation requests and timecode request now both work.
hamidouk
parents: 462
diff changeset
    71
  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
    72
};
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    73
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    74
// lookup and seek to the beginning of an annotation
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    75
IriSP.MediaFragment.prototype.lookupAnnotation = function(annotationId) {
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    76
  var _this = this;
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    77
  this.mutex = true;
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    78
462
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    79
  var annotation = undefined;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    80
  var annotations = this._serializer._data.annotations;
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
  var i;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    83
  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
    84
      if (annotations[i].id === annotationId) {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    85
        annotation = annotations[i];
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    86
        break;
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    87
      }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    88
  }
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    89
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    90
  if (typeof(annotation) !== "undefined") {
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    91
    this._Popcorn.currentTime(annotation.begin / 1000);
3583ef3b208b added a modules dir to the build. moved mediafragment.js module inside it.
hamidouk
parents:
diff changeset
    92
  }
587
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    93
  
cd051898866e implemented hashchange event handler.
hamidouk
parents: 466
diff changeset
    94
  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
    95
};