Popcorn.extend(Popcorn.p, (function () {
// todo: play, pause, mute should toggle
- var methods = "load play pause currentTime playbackRate mute volume",
+ var methods = "load play pause currentTime playbackRate mute volume duration",
ret = {};
// Build methods, store in object that is returned and passed to extend
return this;
}
- if ( arg ) {
+
+
+ if ( arg !== false && arg !== null && typeof arg !== "undefined" ) {
+
this.video[ name ] = arg;
return this;
// Provides some sugar, but ultimately extends
// the definition into Popcorn.p
- var plugin = {};
+ var plugin = {}, setup;
+
+
+
+ if ( typeof definition === "object" ) {
+
+ setup = definition;
+
+
+ definition = function ( options ) {
+
+ var self = this;
+
+ if ( !options ) {
+ return this;
+ }
+
+ if ( !( "start" in options ) ) {
+ options.start = 0;
+ }
+
+ if ( !( "end" in options ) ) {
+ options.end = this.duration();
+ }
+
+ this.video.addEventListener( "timeupdate", function( event ) {
+
+
+ if ( ~~self.currentTime() === options.start ||
+ self.currentTime() === options.start ) {
+
+ setup.start.call(self, event);
+ }
+
+ if ( self.currentTime() > options.start &&
+ self.currentTime() < options.end ) {
+
+ setup.timeupdate.call(self, event);
+
+ }
+
+ if ( ~~self.currentTime() === options.end ||
+ self.currentTime() === options.end ) {
+
+ setup.end.call(self, event);
+ }
+
+
+
+ });
+
+
+ return this;
+ };
+
+
+ console.log("setup", setup);
+ console.log("definition", definition);
+
+
+ }
+
plugin[ name ] = definition;
(function (Popcorn) {
+
+
+
+
+
+ Popcorn.plugin( "complicator", {
+
+
+
+ start: function () {
+
+ var $qhead = document.getElementById("qunit-header");
+
+ $qhead.innerHTML = "complicator STARTED!";
+
+ },
+
+
+
+ end: function () {
+
+ var $qhead = document.getElementById("qunit-header");
+
+ $qhead.innerHTML = "complicator ENDED!";
+
+ },
+
+
+
+ timeupdate: function () {
+
+ var $qhead = document.getElementById("qunit-header");
+
+ $qhead.innerHTML = $qhead.innerHTML + " " + this.currentTime();
+
+ }
+
+
+
+ });
+
+
+
+
+
+
Popcorn.plugin( "subtitler" , function ( options ) {
var subtitles = [],
if ( !div ) {
// if no existing frame, create it.
div = document.createElement("div");
- div.id = "subtitle-container";
+ div.id = "subtitle-container";
+
+ context.parentNode.appendChild(div);
Popcorn.extend(div.style, {
//border: "1px solid red",
- width: context.clientWidth + "px",
- height: context.clientHeight + "px",
+ width: context.clientWidth + "px", //offsetWidth
+ height: context.clientHeight + "px", //offsetHeight
top: context.offsetTop + "px",
left: context.offsetLeft + "px",
position: "absolute",
textAlign: "center"
});
- context.parentNode.appendChild(div);
+
}
if ( typeof options === "object" && "join" in options ) {
div.innerHTML = div.innerHTML + subtitle.html;
}
- if ( this.currentTime() >= subtitle.end ) {
-
- if ( temp.length ) {
- Popcorn.forEach(temp, function ( title ) {
+ if ( temp.length && this.currentTime() >= subtitle.end ) {
+
+ Popcorn.forEach(temp, function ( title ) {
+ if ( title && title.style ) {
title.style.display = "none";
- });
- }
+ }
+
+ });
+
}
}, this);
+
});
});
var p = Popcorn('#video');
+
+
+
p.play();
}
]);
+
+
+
+
+ p.complicator({
+ start: 1,
+ end: 10
+ });
test("Popcorn Plugin", function () {
+ QUnit.reset();
// needs expectation
var popped = Popcorn("#video"),
methods = "load play pause currentTime mute volume";
+
+ stop();
- Popcorn.plugin("subtitles", function ( ) {
+
+ Popcorn.plugin("subtitles", function () {
var self = this;
});
+ // Call plugin to test scope within
+ popped.subtitles();
- ok( "subtitles" in popped, "plugin is now available to instance" );
- popped.subtitles();
+ ok( "subtitles" in popped, "subtitles plugin is now available to instance" );
+ ok( Popcorn.registry.length === 1, "One item in the registry");
+
- ok( Popcorn.registry.length === 1, "One item in the registry");
+
+
+ Popcorn.plugin("complicator", {
+
+ start: function ( event ) {
+
+ console.log(this );
+ equals( ~~this.currentTime(), 1, "~~this.currentTime() === 1");
+
+ var self = this;
+
+ // These ensure that a popcorn instance is the value of `this` inside a plugin definition
+
+ methods.split(/\s+/g).forEach(function (k,v) {
+ ok( k in self, "instance has method: " + k );
+ });
+
+ ok( "video" in this, "instance has `video` property" );
+ ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
+
+ ok( "data" in this, "instance has `data` property" );
+ ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
+
+ ok( "tracks" in this.data, "instance has `tracks` property" );
+ ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" )
+
+ },
+ end: function () {
+
+ start();
+
+ },
+ timeupdate: function () {
+ }
+ });
+
+
+ ok( "complicator" in popped, "complicator plugin is now available to instance" );
+ ok( Popcorn.registry.length === 2, "Two items in the registry");
+
+
+
+ popped.currentTime(0);
+
+ popped.complicator({
+ start: 1,
+ end: 2
+ });
+
});
test("Popcorn Events Stored By Type", function () {
+ QUnit.reset();
+
expect(6)
var p = Popcorn("#video"),