src/js/popcorn.code.js
author hamidouk
Wed, 19 Oct 2011 11:54:24 +0200
branchpopcorn-port
changeset 95 da3ab0bfadf3
parent 92 e03b41037be1
permissions -rw-r--r--
the widget now displays correctly the annotations.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
92
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     1
// PLUGIN: Code
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     2
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     3
(function ( Popcorn ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     4
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     5
  /**
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     6
   * Code Popcorn Plug-in
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     7
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     8
   * Adds the ability to run arbitrary code (JavaScript functions) according to video timing.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
     9
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    10
   * @param {Object} options
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    11
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    12
   * Required parameters: start, end, template, data, and target.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    13
   * Optional parameter: static.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    14
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    15
   *   start: the time in seconds when the mustache template should be rendered
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    16
   *          in the target div.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    17
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    18
   *   end: the time in seconds when the rendered mustache template should be
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    19
   *        removed from the target div.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    20
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    21
   *   onStart: the function to be run when the start time is reached.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    22
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    23
   *   onFrame: [optional] a function to be run on each paint call
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    24
   *            (e.g., called ~60 times per second) between the start and end times.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    25
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    26
   *   onEnd: [optional] a function to be run when the end time is reached.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    27
   *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    28
   * Example:
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    29
     var p = Popcorn('#video')
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    30
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    31
        // onStart function only
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    32
        .code({
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    33
          start: 1,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    34
          end: 4,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    35
          onStart: function( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    36
            // called on start
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    37
          }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    38
        })
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    39
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    40
        // onStart + onEnd only
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    41
        .code({
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    42
          start: 6,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    43
          end: 8,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    44
          onStart: function( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    45
            // called on start
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    46
          },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    47
          onEnd: function ( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    48
            // called on end
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    49
          }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    50
        })
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    51
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    52
        // onStart, onEnd, onFrame
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    53
        .code({
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    54
          start: 10,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    55
          end: 14,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    56
          onStart: function( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    57
            // called on start
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    58
          },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    59
          onFrame: function ( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    60
            // called on every paint frame between start and end.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    61
            // uses mozRequestAnimationFrame, webkitRequestAnimationFrame,
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    62
            // or setTimeout with 16ms window.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    63
          },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    64
          onEnd: function ( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    65
            // called on end
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    66
          }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    67
        });
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    68
  *
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    69
  */
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    70
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    71
  Popcorn.plugin( "code" , function( options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    72
    var running = false;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    73
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    74
    // Setup a proper frame interval function (60fps), favouring paint events.
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    75
    var step = (function() {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    76
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    77
      var buildFrameRunner = function( runner ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    78
        return function( f, options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    79
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    80
          var _f = function() {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    81
            running && f();
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    82
            running && runner( _f );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    83
          };
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    84
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    85
          _f();
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    86
        };
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    87
      };
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    88
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    89
      // Figure out which level of browser support we have for this
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    90
      if ( window.webkitRequestAnimationFrame ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    91
        return buildFrameRunner( window.webkitRequestAnimationFrame );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    92
      } else if ( window.mozRequestAnimationFrame ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    93
        return buildFrameRunner( window.mozRequestAnimationFrame );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    94
      } else {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    95
        return buildFrameRunner( function( f ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    96
          window.setTimeout( f, 16 );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    97
        });
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    98
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
    99
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   100
    })();
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   101
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   102
    if ( !options.onStart || typeof options.onStart !== "function" ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   103
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   104
      if ( Popcorn.plugin.debug ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   105
        throw new Error( "Popcorn Code Plugin Error: onStart must be a function." );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   106
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   107
      options.onStart = Popcorn.nop;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   108
    }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   109
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   110
    if ( options.onEnd && typeof options.onEnd !== "function" ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   111
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   112
      if ( Popcorn.plugin.debug ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   113
        throw new Error( "Popcorn Code Plugin Error: onEnd  must be a function." );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   114
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   115
      options.onEnd = undefined;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   116
    }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   117
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   118
    if ( options.onFrame && typeof options.onFrame !== "function" ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   119
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   120
      if ( Popcorn.plugin.debug ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   121
        throw new Error( "Popcorn Code Plugin Error: onFrame  must be a function." );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   122
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   123
      options.onFrame = undefined;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   124
    }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   125
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   126
    return {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   127
      start: function( event, options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   128
        options.onStart( options );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   129
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   130
        if ( options.onFrame ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   131
          running = true;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   132
          step( options.onFrame, options );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   133
        }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   134
      },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   135
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   136
      end: function( event, options ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   137
        if ( options.onFrame ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   138
          running = false;
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   139
        }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   140
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   141
        if ( options.onEnd ) {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   142
          options.onEnd( options );
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   143
        }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   144
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   145
    };
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   146
  },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   147
  {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   148
    about: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   149
      name: "Popcorn Code Plugin",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   150
      version: "0.1",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   151
      author: "David Humphrey (@humphd)",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   152
      website: "http://vocamus.net/dave"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   153
    },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   154
    options: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   155
      start: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   156
       elem: "input",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   157
       type: "text",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   158
       label: "In"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   159
      },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   160
      end: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   161
        elem: "input",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   162
        type: "text",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   163
        label: "Out"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   164
      },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   165
      onStart: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   166
        elem: "input",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   167
        type: "function",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   168
        label: "onStart"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   169
      },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   170
      onFrame: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   171
        elem: "input",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   172
        type: "function",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   173
        label: "onFrame"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   174
      },
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   175
      onEnd: {
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   176
        elem: "input",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   177
        type: "function",
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   178
        label: "onEnd"
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   179
      }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   180
    }
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   181
  });
e03b41037be1 added popcorn code plugin to the build.
hamidouk
parents:
diff changeset
   182
})( Popcorn );