From: ScottDowne Date: Fri, 17 Jun 2011 20:12:40 +0000 (-0400) Subject: [#t366] initial effects framework X-Git-Tag: v0.7^2~22^2~13 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=96482a76432246b2339b47d08c4eed0363b260f4;p=popcorn-js.git [#t366] initial effects framework --- diff --git a/effects/redflash/popcorn.redflash.html b/effects/redflash/popcorn.redflash.html new file mode 100755 index 0000000..2d74184 --- /dev/null +++ b/effects/redflash/popcorn.redflash.html @@ -0,0 +1,84 @@ + + + + Popcorn footnote Plug-in Demo + + + + + + +

Popcorn Footnote Plug-in Demo

+

A footnote displaying This video made exclusively for drumbeat.org' will appear at 5 seconds and disappear at 15 seconds.

+

A footnote displaying 'Visit webmademovies.org for more details' will appear at 20 seconds and disappear at 45 seconds.

+
+ +
+ + + diff --git a/effects/redflash/popcorn.redflash.js b/effects/redflash/popcorn.redflash.js new file mode 100755 index 0000000..0175602 --- /dev/null +++ b/effects/redflash/popcorn.redflash.js @@ -0,0 +1,88 @@ +// EFFECT: Redflash + +(function (Popcorn) { + + /** + * Footnote popcorn plug-in + * Adds text to an element on the page. + * Options parameter will need a start, end, target and text. + * Start is the time that you want this plug-in to execute + * End is the time that you want this plug-in to stop executing + * Text is the text that you want to appear in the target + * Target is the id of the document element that the text needs to be + * attached to, this target element must exist on the DOM + * + * @param {Object} options + * + * Example: + var p = Popcorn('#video') + .footnote({ + start: 5, // seconds + end: 15, // seconds + text: 'This video made exclusively for drumbeat.org', + target: 'footnotediv' + } ) + * + */ + Popcorn.effect( "redflash" , { + + manifest: { + about:{ + name: "Popcorn Redflash Effect", + version: "0.1", + author: "@scottdowne", + website: "scottdowne.wordpress.com" + }, + options:{ + effect: true + } + }, + _setup: function( options ) { + + console.log( "in effect setup" ); + }, + start: function( event, options ){ + + console.log( "in effect start" ); + }, + end: function( event, options ){ + + console.log( "in effect end" ); + }, + _teardown: function( options ) { + + console.log( "in effect teardown" ); + } + }); + Popcorn.effect( "redflash2" , { + + manifest: { + about:{ + name: "Popcorn Redflash Effect", + version: "0.1", + author: "@scottdowne", + website: "scottdowne.wordpress.com" + }, + options:{ + effect: true + } + }, + _setup: function( options ) { + + console.log( "in effect 2 setup" ); + }, + start: function( event, options ){ + + console.log( "in effect start" ); + }, + end: function( event, options ){ + + console.log( "in effect end" ); + }, + _teardown: function( options ) { + + console.log( "in effect teardown" ); + } + }); + +})( Popcorn ); diff --git a/effects/redflash/popcorn.redflash.unit.html b/effects/redflash/popcorn.redflash.unit.html new file mode 100755 index 0000000..6777a99 --- /dev/null +++ b/effects/redflash/popcorn.redflash.unit.html @@ -0,0 +1,43 @@ + + + + Popcorn API + + + + + + + + + + +

Popcorn Footnote Plugin

+

+
+

+
    +
    + + +
    + + diff --git a/effects/redflash/popcorn.redflash.unit.js b/effects/redflash/popcorn.redflash.unit.js new file mode 100755 index 0000000..2539c6d --- /dev/null +++ b/effects/redflash/popcorn.redflash.unit.js @@ -0,0 +1,64 @@ +test("Popcorn Footnote Plugin", function () { + + var popped = Popcorn("#video"), + expects = 8, + count = 0, + setupId, + footnotediv = document.getElementById('footnotediv'); + + expect(expects); + + function plus() { + if ( ++count===expects) { + start(); + } + } + + stop(); + + ok ('footnote' in popped, "footnote is a mehtod of the popped instance"); + plus(); + + equals ( footnotediv.childElementCount, 0, "initially, there is nothing inside the footnotediv" ); + plus(); + + popped.footnote({ + start: 0, // seconds + end: 2, // seconds + text: 'This video made exclusively for drumbeat.org', + target: 'footnotediv' + }) + .footnote({ + start: 2, // seconds + end: 4, // seconds + text: 'Visit webmademovies.org for more details', + target: 'footnotediv' + }); + + setupId = popped.getLastTrackEventId(); + + popped.exec( 0, function() { + equals ( footnotediv.childElementCount, 2, "footnotediv now has two inner elements" ); + plus(); + equals (footnotediv.children[0].innerHTML , "This video made exclusively for drumbeat.org", "footnote displaing correct information" ); + plus(); + equals (footnotediv.children[0].style.display , "inline", "footnote is visible on the page" ); + plus(); + }); + + popped.exec( 3, function() { + equals (footnotediv.children[1].style.display , "inline", "second footnote is visible on the page" ); + plus(); + }); + + popped.exec( 4, function() { + ok (footnotediv.children[1].style.display === 'none' && footnotediv.children[0].style.display === 'none', "footnote are no longer vidible on the page" ); + plus(); + + popped.pause().removeTrackEvent( setupId ); + ok( !footnotediv.children[1], "removed footnote was properly destroyed" ); + plus(); + }); + popped.play().volume(0); + +}); diff --git a/popcorn.js b/popcorn.js index efa17bf..d64682e 100644 --- a/popcorn.js +++ b/popcorn.js @@ -847,6 +847,15 @@ setup, isfn = typeof definition === "function"; + var extendFunction = function( first, second ) { + + return function() { + + first.apply( this, arguments ); + second.apply( this, arguments ); + }; + }; + // If `manifest` arg is undefined, check for manifest within the `definition` object // If no `definition.manifest`, an empty object is a sufficient fallback if ( !manifest ) { @@ -860,10 +869,25 @@ } // Storing the plugin natives - options._natives = setup; + options._natives = {}; //setup; + + Popcorn.extend( options._natives, setup ); + options._natives.type = name; options._running = false; + // default to an empty string if no effect exists + // split string into an array of effects + options.effect = options.effect && options.effect.split( " " ) || []; + + for ( var i = 0; i < options.effect.length; i++ ) { + + options._natives._setup = extendFunction( options._natives._setup, Popcorn.effects[ options.effect[ i ] ]._setup ); + options._natives._teardown = extendFunction( options._natives._teardown, Popcorn.effects[ options.effect[ i ] ]._teardown ); + options._natives.start = extendFunction( options._natives.start, Popcorn.effects[ options.effect[ i ] ].start ); + options._natives.end = extendFunction( options._natives.end, Popcorn.effects[ options.effect[ i ] ].end ); + } + // Ensure a manifest object, an empty object is a sufficient fallback options._natives.manifest = manifest; @@ -876,24 +900,19 @@ options.end = this.duration() || Number.MAX_VALUE; } - // If a _setup was declared, then call it before - // the events commence - if ( "_setup" in setup && typeof setup._setup === "function" ) { + // Resolves 239, 241, 242 + if ( !options.target ) { - // Resolves 239, 241, 242 - if ( !options.target ) { + // Sometimes the manifest may be missing entirely + // or it has an options object that doesn't have a `target` property - // Sometimes the manifest may be missing entirely - // or it has an options object that doesn't have a `target` property + var manifestopts = "options" in manifest && manifest.options; - var manifestopts = "options" in manifest && manifest.options; - - options.target = manifestopts && "target" in manifestopts && manifestopts.target; - } - - setup._setup.call( this, options ); + options.target = manifestopts && "target" in manifestopts && manifestopts.target; } + options._natives._setup && options._natives._setup.call( this, options ); + Popcorn.addTrackEvent( this, options ); // Future support for plugin event definitions @@ -1005,93 +1024,19 @@ } }; - - // Popcorn Plugin Inheritance Helper Methods - // Internal use only - Popcorn.plugin.getDefinition = function( name ) { - - var registry = Popcorn.registryByName; - - if ( registry[ name ] ) { - return registry[ name ]; - } - - Popcorn.error( "Cannot inherit from " + name + "; Object does not exist" ); - }; - - // Internal use only - Popcorn.plugin.delegate = function( instance, name, plugins ) { - - return function() { - var args = arguments; - plugins.forEach( function( plugin ) { - // The new plugin simply calls the delegated methods on - // all of its parents in the order they were specified. - plugin[ name ] && plugin[ name ].apply( instance, args ); - }); - }; - }; + Popcorn.effects = {}; // Plugin inheritance - Popcorn.plugin.inherit = function( name, parents, definition, manifest ) { - - - // Get the names of all of the ancestor classes, in the order that - // we will be calling them. The override is for the class we're - // currently defining, since it's not in the registry yet. - var ancestors = [], - pluginFn, entry; - - function getAncestors( name, override ) { - var parents = override || Popcorn.plugin.getDefinition( name ).parents; - for ( var i in parents ) { - if ( hasOwn.call( parents, i ) ) { - var p = parents[ i ]; - getAncestors( p ); - if ( ancestors.indexOf( p ) === -1 ) { - ancestors.push( p ); - } - } - } - } - - getAncestors( name, Popcorn.isArray( parents ) ? parents : [ parents ] ); - ancestors.push( name ); - - // Now create the requested plugin under the reqested name. - pluginFn = Popcorn.plugin( name, function( options ) { - - var self = this, - plugins; + Popcorn.effect = function( name, definition, manifest ) { - function instantiate( definition ) { - return definition.call && definition.call( self, options ) || definition; - } - - // When the newly-defined plugin is instantiated, it must - // explicitly instantiate all of its ancestors. - plugins = ancestors.map( function( name ) { - return instantiate( Popcorn.plugin.getDefinition( name ).base ); - }); - - return { - _setup: Popcorn.plugin.delegate( self, "_setup", plugins ), - start: Popcorn.plugin.delegate( self, "start", plugins ), - end: Popcorn.plugin.delegate( self, "end", plugins ) - }; - - }, manifest || definition.manifest ); - - entry = Popcorn.plugin.getDefinition( name ); - entry.base = definition; - entry.parents = parents; + // If `manifest` arg is undefined, check for manifest within the `definition` object + // If no `definition.manifest`, an empty object is a sufficient fallback + Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {}; - return pluginFn; + // register the effect by name + Popcorn.effects[ name ] = definition; }; - // Augment Popcorn; - Popcorn.inherit = Popcorn.plugin.inherit; - // stores parsers keyed on filetype Popcorn.parsers = {};