Navigation Menu

Skip to content

Commit

Permalink
[mozilla#209] Drop support for callback only plugins; replace with an…
Browse files Browse the repository at this point in the history
… executed callback result
  • Loading branch information
rwaldron committed Dec 8, 2010
1 parent b62618f commit 98d244d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 27 deletions.
38 changes: 26 additions & 12 deletions popcorn.js
Expand Up @@ -337,11 +337,10 @@

var natives = Popcorn.events.all,
reserved = [ "start", "end", "timeupdate" ],
plugin = {},
plugin = {},
pluginFn,
setup;



if ( typeof definition === "object" ) {

setup = definition;
Expand All @@ -350,7 +349,7 @@
setup.timeupdate = Popcorn.nop;
}

definition = function ( options ) {
pluginFn = function ( options ) {

var self = this,
fired = {
Expand Down Expand Up @@ -418,19 +417,34 @@
return this;
};
}


plugin[ name ] = definition;
// If a function is passed...
if ( typeof definition === "function" ) {

// Execute and capture returned object
setup = definition.call(this);

// Ensure an object was returned
// it has properties and isnt an array
if ( typeof setup === "object" &&
!( "length" in setup ) ) {

Popcorn.plugin( name, setup );
}
return;
}

// Assign new named definition
plugin[ name ] = pluginFn;

// Extend Popcorn.p with new named definition
Popcorn.extend( Popcorn.p, plugin );

Popcorn.registry.push({
name: name,
plugin: plugin
});
// Push into the registry
Popcorn.registry.push(plugin);


// within the context of a plugin
// any of the events can be listened to
return plugin;
};


Expand Down
77 changes: 62 additions & 15 deletions test/popcorn.unit.js
Expand Up @@ -361,15 +361,15 @@ test("UI/Mouse", function () {
});

module("Popcorn Plugin")
test("Plugin API", function () {
test("Plugin Factory", function () {

QUnit.reset();

// needs expectation

var popped = Popcorn("#video"),
methods = "load play pause currentTime mute volume roundTime exec",
expects = 21,
expects = 34,
count = 0;

expect(expects);
Expand All @@ -381,7 +381,53 @@ test("Plugin API", function () {
}

stop();

Popcorn.plugin("executor", function () {

return {

start: function () {
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, "executor instance has method: " + k );

plus();
});

ok( "video" in this, "executor instance has `video` property" );
plus();
ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
plus();

ok( "data" in this, "executor instance has `data` property" );
plus();
ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
plus();

ok( "tracks" in this.data, "executor instance has `tracks` property" );
plus();
ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "executor tracks property is an array" )
plus();
},
end: function () {

}
};
});

ok( "executor" in popped, "executor plugin is now available to instance" );
plus();
ok( Popcorn.registry.length === 1, "One item in the registry");
plus();

popped.executor({
start: 1,
end: 20
});


Popcorn.plugin("complicator", {

Expand All @@ -396,24 +442,24 @@ test("Plugin API", function () {
// 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( k in self, "complicator instance has method: " + k );

plus();
});

ok( "video" in this, "instance has `video` property" );
ok( "video" in this, "complicator instance has `video` property" );
plus();
ok( Object.prototype.toString.call(popped.video) === "[object HTMLVideoElement]", "video property is a HTMLVideoElement" );
plus();

ok( "data" in this, "instance has `data` property" );
ok( "data" in this, "complicator instance has `data` property" );
plus();
ok( Object.prototype.toString.call(popped.data) === "[object Object]", "data property is an object" );
ok( Object.prototype.toString.call(popped.data) === "[object Object]", "complicator data property is an object" );
plus();

ok( "tracks" in this.data, "instance has `tracks` property" );
ok( "tracks" in this.data, " complicatorinstance has `tracks` property" );
plus();
ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "tracks property is an array" )
ok( Object.prototype.toString.call(popped.data.tracks) === "[object Array]", "complicator tracks property is an array" )
plus();

},
Expand All @@ -429,13 +475,9 @@ test("Plugin API", function () {

ok( "complicator" in popped, "complicator plugin is now available to instance" );
plus();
ok( Popcorn.registry.length === 1, "Two items in the registry");
ok( Popcorn.registry.length === 2, "Two items in the registry");
plus();





popped.complicator({
start: 1,
end: 20
Expand All @@ -457,14 +499,14 @@ test("Plugin API", function () {

breaker.start++;

ok(true, "plugin:breaker started");
ok(true, "breaker started");
plus();
},
end: function () {

breaker.end++;

ok(true, "plugin:ended started");
ok(true, "breaker started");
plus();


Expand All @@ -485,6 +527,11 @@ test("Plugin API", function () {
start: 1,
end: 2
});





});

test("Protected Names", function () {
Expand Down

0 comments on commit 98d244d

Please sign in to comment.