]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#582] Reconstructing defaults implementation
authorrwldrn <waldron.rick@gmail.com>
Thu, 23 Jun 2011 16:18:55 +0000 (12:18 -0400)
committerrwldrn <waldron.rick@gmail.com>
Thu, 23 Jun 2011 16:18:55 +0000 (12:18 -0400)
popcorn.js
test/popcorn.unit.js

index a5968c19889c0f221a6ad0ce5e984b80c16e48c3..b2d55125df81e8c52c2d8ec607060c40115f4489 100644 (file)
 
       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;
     }
   });
 
   // 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;
 
       //  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 );
 
         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
index d9033c431040c8e7600b0e02e5d1e069f0ee4e1f..a0ca5a20b11f30126d8aa3a0309622148d165498 100644 (file)
@@ -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();