var natives = Popcorn.events.all,
reserved = [ "start", "end", "timeupdate" ],
- plugin = {},
+ plugin = {},
+ pluginFn,
setup;
-
-
if ( typeof definition === "object" ) {
setup = definition;
setup.timeupdate = Popcorn.nop;
}
- definition = function ( options ) {
+ pluginFn = function ( options ) {
var self = this,
fired = {
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;
};
});
module("Popcorn Plugin")
-test("Plugin API", function () {
+test("Plugin Factory", function () {
QUnit.reset();
var popped = Popcorn("#video"),
methods = "load play pause currentTime mute volume roundTime exec",
- expects = 21,
+ expects = 34,
count = 0;
expect(expects);
}
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", {
// 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();
},
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
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();
start: 1,
end: 2
});
+
+
+
+
+
});
test("Protected Names", function () {