From 36af94ecf6802df5169f270588297dc09abb7c3b Mon Sep 17 00:00:00 2001 From: rwldrn Date: Fri, 3 Dec 2010 16:54:50 -0500 Subject: [PATCH] demo and example plugin --- popcorn-api-draft.js | 13 ++-- test/demo.html | 48 +++++++++++++ test/demo.js | 126 +++++++++++++++++++++++++++++++++ test/popcorn-api-draft-unit.js | 2 +- 4 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 test/demo.html create mode 100644 test/demo.js diff --git a/popcorn-api-draft.js b/popcorn-api-draft.js index d849637..9876d38 100644 --- a/popcorn-api-draft.js +++ b/popcorn-api-draft.js @@ -88,7 +88,7 @@ // A Few reusable utils, memoized onto Popcorn Popcorn.extend( Popcorn, { guid: function() { - return +new Date(); + return +new Date() + Math.floor(Math.random()*11); }, sizeOf: function ( obj ) { var size = 0; @@ -181,17 +181,14 @@ } // setup for custom event system - this.data.events[type][ fn.toString() ] = fn; + this.data.events[type][ fn.toString() + Popcorn.guid() ] = fn; if ( Popcorn.events.all.indexOf( type ) > -1 ) { - this.video.addEventListener( type, function( event ) { - + Popcorn.forEach( self.data.events[type], function ( obj, key ) { - obj.call(self, event); - }); }, false); @@ -222,6 +219,10 @@ Popcorn.extend( Popcorn.p, plugin ); + + + + // within the context of a plugin, the `timeupdate` or any of the events can be listened to }; diff --git a/test/demo.html b/test/demo.html new file mode 100644 index 0000000..c96fe02 --- /dev/null +++ b/test/demo.html @@ -0,0 +1,48 @@ + + + + Popcorn API DEMO + + + + + + +

Popcorn API

+ + + +
+ +
+ + + + + + + + diff --git a/test/demo.js b/test/demo.js new file mode 100644 index 0000000..69b5f51 --- /dev/null +++ b/test/demo.js @@ -0,0 +1,126 @@ + +// EXAMPLE PLUGIN: SUBTITLER + +(function (Popcorn) { + + + + + Popcorn.plugin( "subtitler" , function ( options ) { + + var subtitles = [], + context = this.video, + div = document.getElementById("subtitle-container"); + + + // Check for existing frame + if ( !div ) { + // if no existing frame, create it. + div = document.createElement("div"); + div.id = "subtitle-container"; + + Popcorn.extend(div.style, { + //border: "1px solid red", + width: context.clientWidth + "px", + height: context.clientHeight + "px", + top: context.offsetTop + "px", + left: context.offsetLeft + "px", + position: "absolute", + color: "white", + textShadow: "black 2px 2px 6px", + fontSize: "18px", + fontWeight: "bold", + textAlign: "center" + }); + + context.parentNode.appendChild(div); + } + + if ( typeof options === "object" && "join" in options ) { + subtitles = options; + } else { + subtitles.push(options); + } + + this.listen("timeupdate", function (event) { + + + Popcorn.forEach(subtitles, function ( subtitle ) { + + var temp = div.querySelectorAll( subtitle.id ); + + if ( this.currentTime() >= subtitle.start && !temp.length ) { + div.innerHTML = div.innerHTML + subtitle.html; + } + + if ( this.currentTime() >= subtitle.end ) { + + if ( temp.length ) { + Popcorn.forEach(temp, function ( title ) { + title.style.display = "none"; + }); + } + } + + }, this); + }); + }); + +})(Popcorn); + + +// END EXAMPLE + + +$(function () { + + + + var p = Popcorn('#video'); + + + p.play(); + + + p.subtitler({ + id: "#subtitle-a", + start: 2, // seconds + end: 5, // seconds + html: '

Appear at 2 second mark, dissappear at 5 second mark

' + }); + + + + // Butter would generate the data to populate this api function call's argument + + p.subtitler([ + { + id: "#stays-forever", + start: 5, + html: '

Stays forever!

' + }, + { + id: "#comes-and-goes-1", + start: 10, + end: 12, + html: '

Comes and goes

' + }, + { + id: "#comes-and-goes-2", + start: 12, + end: 14, + html: '

BACK! For the second time...

' + }, + { + id: "#comes-and-goes-3", + start: 14, + end: 16, + html: '

This is the last time. I promise.

' + } + + ]); + + + + +}); \ No newline at end of file diff --git a/test/popcorn-api-draft-unit.js b/test/popcorn-api-draft-unit.js index 8adf688..6d205fb 100644 --- a/test/popcorn-api-draft-unit.js +++ b/test/popcorn-api-draft-unit.js @@ -83,7 +83,7 @@ test("Popcorn Plugin", function () { Popcorn.plugin("subtitles", function ( ) { - //console.log(this); + console.log(this); -- 2.43.0