From 962f1e9e2cf6e85e07c07636a7d08b874b6d9777 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Thu, 23 Jun 2011 12:18:55 -0400 Subject: [PATCH] [#582] Reconstructing defaults implementation --- popcorn.js | 73 +++++++++++++++++++--- test/popcorn.unit.js | 143 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 7 deletions(-) diff --git a/popcorn.js b/popcorn.js index a5968c1..b2d5512 100644 --- a/popcorn.js +++ b/popcorn.js @@ -495,11 +495,44 @@ return this; }, + + // Get the client bounding box of an instance element position: function() { return Popcorn.position( this.media ); - }, + }, + + // Toggle a plugin's playback behaviour (on or off) per instance toggle: function( plugin ) { return Popcorn[ this.data.disabled.indexOf( plugin ) > -1 ? "enable" : "disable" ]( this, plugin ); + }, + + // Set default values for plugin options objects per instance + defaults: function( plugin, defaults ) { + + // If an array of default configurations is provided, + // iterate and apply each to this instance + if ( Popcorn.isArray( plugin ) ) { + + Popcorn.forEach( plugin, function( obj ) { + for ( var name in obj ) { + this.defaults( name, obj[ name ] ); + } + }, this ); + + return this; + } + + if ( !this.options.defaults ) { + this.options.defaults = {}; + } + + if ( !this.options.defaults[ plugin ] ) { + this.options.defaults[ plugin ] = {}; + } + + Popcorn.extend( this.options.defaults[ plugin ], defaults ); + + return this; } }); @@ -714,6 +747,14 @@ // Internal Only - Adds track events to the instance object Popcorn.addTrackEvent = function( obj, track ) { + // Determine if this track has default options set for it + // If so, apply them to the track object + if ( track && track._natives && track._natives.type && + ( obj.options.defaults && obj.options.defaults[ track._natives.type ] ) ) { + + track = Popcorn.extend( {}, obj.options.defaults[ track._natives.type ], track ); + } + if ( track._natives ) { // Supports user defined track event id track._id = !track.id ? Popcorn.guid( track._natives.type ) : track.id; @@ -945,7 +986,9 @@ // Storing the plugin natives var natives = options._natives = {}, - compose = ""; + compose = "", + defaults = this.options.defaults && this.options.defaults[ options._natives && options._natives.type ], + originalOpts, manifestOpts, mergedSetupOpts; Popcorn.extend( natives, setup ); @@ -984,20 +1027,36 @@ options.end = this.duration() || Number.MAX_VALUE; } + // Store original options object as a restore point + originalOpts = options; + + // Merge with defaults if they exist, make sure per call is prioritized + mergedSetupOpts = defaults ? Popcorn.extend( {}, defaults, options ) : + options; + // Resolves 239, 241, 242 - if ( !options.target ) { + if ( !mergedSetupOpts.target ) { // Sometimes the manifest may be missing entirely // or it has an options object that doesn't have a `target` property + manifestOpts = "options" in manifest && manifest.options; - var manifestopts = "options" in manifest && manifest.options; + mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target; + } + + // Resolves 239, 241, 242 + if ( !mergedSetupOpts.target ) { + + // Sometimes the manifest may be missing entirely + // or it has an options object that doesn't have a `target` property + manifestOpts = "options" in manifest && manifest.options; - options.target = manifestopts && "target" in manifestopts && manifestopts.target; + mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target; } - options._natives._setup && options._natives._setup.call( this, options ); + options._natives._setup && options._natives._setup.call( this, mergedSetupOpts ); - Popcorn.addTrackEvent( this, options ); + Popcorn.addTrackEvent( this, originalOpts ); // Future support for plugin event definitions // for all of the native events diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index d9033c4..a0ca5a2 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1118,6 +1118,149 @@ test("Manifest", function () { }); +test("Configurable Defaults", function () { + + var expects = 12, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + Popcorn.removePlugin( "configurable" ); + Popcorn.removePlugin( "multiconfig" ); + Popcorn.removePlugin( "overridden" ); + } + } + + stop(); + + Popcorn.plugin( "configurable", function () { + return { + _setup: function( options ) { + + equal( options.target, "foo", 'options.target, "foo" in configurable _setup'); + plus(); + }, + start: function( event, options ) { + + // target: "foo" + // text: "bar" + // type: "thinger" + equal( options.target, "foo", 'options.target, "foo" in configurable start'); + plus(); + equal( options.text, "bar", 'options.text, "bar" in configurable start'); + plus(); + equal( options.type, "thinger", 'options.type, "thinger" in configurable start'); + plus(); + }, + end: function( event, options ) { + ok( true, "end fired"); + plus(); + } + }; + }, + { + about:{ + name: "Popcorn Configurable Plugin", + version: "0.0", + author: "Rick Waldron", + website: "" + }, + options: { + target: "manifest" + } + }); + + Popcorn.plugin( "multiconfig", function () { + return { + start: function( event, options ) { + equal( options.target, "quux", 'options.target, "quux" in multiconfig start'); + plus(); + }, + end: function( event, options ) { + ok( true, "end fired"); + plus(); + } + }; + }); + + Popcorn.plugin( "overridden", function () { + return { + _setup: function( options ) { + equal( options.text, "hello!", 'options.text, overriden with "hello!" in overridden _setup'); + plus(); + + equal( options.target, "custom", 'options.target, overriden with "custom" in overridden _setup'); + plus(); + }, + start: function( event, options ) { + equal( options.text, "hello!", 'options.text, overriden with "hello!" in overridden start'); + plus(); + + equal( options.target, "custom", 'options.target, overriden with "custom" in overridden start'); + plus(); + }, + end: function( event, options ) { + ok( true, "end fired"); + plus(); + } + }; + }); + + var p = Popcorn("#video", { + defaults: { + overridden: { + target: "default" + } + } + }); + + p.defaults( "configurable", { + + // set a default element target id + target: "foo" + + }).defaults([ + { + multiconfig: { + target: "quux" + } + }, + { + configurable: { + text: "bar", + type: "thinger" + } + } + ]).configurable({ + + // all calls will have: + // target: "foo" + // text: "bar" + // type: "thinger" + start: 3, + end: 4 + + }).multiconfig({ + + // all calls will have: + // target: "quux" + start: 4, + end: 5 + + }).overridden({ + + // has a default set + // we need limitless overriding + start: 4, + end: 5, + target: "custom", + text: "hello!" + + }).currentTime( 2 ).play(); + +}); + test("Update Timer", function () { QUnit.reset(); -- 2.43.0