From: rwldrn Date: Sun, 5 Dec 2010 00:15:33 +0000 (-0500) Subject: Implements new plugin signature; updates demos; new unit tests X-Git-Tag: v0.2~68^2~9 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=73f36d405d643801e6617ab0d940a619d7994d7c;p=popcorn-js.git Implements new plugin signature; updates demos; new unit tests --- diff --git a/popcorn.js b/popcorn.js index 095a903..cd1c669 100644 --- a/popcorn.js +++ b/popcorn.js @@ -108,7 +108,7 @@ 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 @@ -122,7 +122,10 @@ return this; } - if ( arg ) { + + + if ( arg !== false && arg !== null && typeof arg !== "undefined" ) { + this.video[ name ] = arg; return this; @@ -246,7 +249,68 @@ // 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; diff --git a/test/demo.js b/test/demo.js index 7bb8266..240b728 100644 --- a/test/demo.js +++ b/test/demo.js @@ -2,6 +2,52 @@ (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 = [], @@ -12,12 +58,14 @@ 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", @@ -28,7 +76,7 @@ textAlign: "center" }); - context.parentNode.appendChild(div); + } if ( typeof options === "object" && "join" in options ) { @@ -47,15 +95,18 @@ 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); + }); }); @@ -69,6 +120,9 @@ $(function () { var p = Popcorn('#video'); + + + p.play(); @@ -107,6 +161,14 @@ $(function () { } ]); + + + + + p.complicator({ + start: 1, + end: 10 + }); diff --git a/test/popcorn-api-draft-unit.js b/test/popcorn-api-draft-unit.js index 98d67fb..a41229e 100644 --- a/test/popcorn-api-draft-unit.js +++ b/test/popcorn-api-draft-unit.js @@ -73,14 +73,18 @@ test("Popcorn Object", function () { 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; @@ -101,19 +105,72 @@ test("Popcorn Plugin", function () { }); + // 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"),