From: Edsfault Date: Sun, 1 May 2011 00:19:29 +0000 (-0400) Subject: Adding a plugin: Video(s) can be set to stop when a link is clicked X-Git-Tag: v0.6~5^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a13a19d42a3545c6878a659d5ccf6803829519f9;p=popcorn-js.git Adding a plugin: Video(s) can be set to stop when a link is clicked --- diff --git a/plugins/pause/popcorn.pause.html b/plugins/pause/popcorn.pause.html new file mode 100644 index 0000000..e15d087 --- /dev/null +++ b/plugins/pause/popcorn.pause.html @@ -0,0 +1,53 @@ + + + + Popcorn Pause Demo + + + + + + +

Popcorn Code Plugin Demo

+

The video pauses when links (anchor elements) are clicked

+
+ +
+
+ Home page
+ Mozila
+ + Popcorn.js on Lighthouse +
+ + An another in this page + (check the url after you click) +
+ +
+ + diff --git a/plugins/pause/popcorn.pause.js b/plugins/pause/popcorn.pause.js new file mode 100644 index 0000000..cb1cc11 --- /dev/null +++ b/plugins/pause/popcorn.pause.js @@ -0,0 +1,32 @@ +/** +* Pause Popcorn Plug-in +* +* When this plugin is used, links on the webpage, when clicked, will pause +* popcorn videos that especified 'pauseOnLinkClicked' as an option. Links may +* cause a new page to display on a new window, or may cause a new page to +* display in the current window, in which case the videos won't be available +* anymore. It only affects anchor tags. It does not affect objects with click +* events that act as anchors. +* +* Example: + var p = Popcorn('#video', { pauseOnLinkClicked : true } ) + .play(); +* +*/ + +document.addEventListener( "click", function( event ) { + //Consider I.E. + var targetElement = event.target; + if ( window.event ) { + targetElement = window.event.srcElement; + } + + //Some browsers use an element as the target, some use the text node inside + if ( targetElement.tagName === "A" || targetElement.parentNode.tagName === "A" ) { + Popcorn.instances.forEach( function( video ) { + if ( video.options.pauseOnLinkClicked ) { + video.pause(); + } + }); + } +}, false ); diff --git a/plugins/pause/popcorn.pause.unit.html b/plugins/pause/popcorn.pause.unit.html new file mode 100644 index 0000000..e597244 --- /dev/null +++ b/plugins/pause/popcorn.pause.unit.html @@ -0,0 +1,66 @@ + + + + Popcorn API + + + + + + + + + + + +

Popcorn Pause Plugin

+

+
+

+
    +
    + + + +

    + Anchor A + Anchor B + +

    + + diff --git a/plugins/pause/popcorn.pause.unit.js b/plugins/pause/popcorn.pause.unit.js new file mode 100644 index 0000000..0a9937e --- /dev/null +++ b/plugins/pause/popcorn.pause.unit.js @@ -0,0 +1,63 @@ +test('Popcorn Pause Plugin', function () { + + var expectedTests = 3 , + anchorA = document.getElementById( 'anchorA' ), + anchorB = document.getElementById( 'anchorB' ), + tolerance = 0.250 ; + + var simulateClickOn = function ( paramElement ) { + + if ( typeof paramElement.click == "Function" ) { + paramElement.click() ; + return ; + } + + var evt = document.createEvent("MouseEvents"); + + evt.initMouseEvent( + "click", true, true, window, + 0, 0, 0, 0, 0, false, false, + false, false, 0, null + ); + + paramElement.dispatchEvent(evt); + }; + + var otherVideo = Popcorn('#video2' , { pauseOnLinkClicked : true } ) + .play() ; + + var popped = Popcorn('#video' , { pauseOnLinkClicked : true } ) + .code ({ + start: 2.000, + end: 4, + onStart: function ( options ) { + var currentTime = popped.currentTime() ; + simulateClickOn( anchorA ) ; + ok ( + ( currentTime + tolerance >= 2 ) && ( currentTime - tolerance <= 2 ) , + 'Video successfully stopped with a click on an anchor at ' + + 'second 2 approximately (' + currentTime + ')' + ) ; + //Continue playing + popped.play() ; + } + }) + .code ({ + start: 5.561, + end : 7, + onStart: function ( options ) { + var currentTime = popped.currentTime() ; + simulateClickOn( anchorB ) ; + ok ( + ( currentTime + tolerance >= 5.561 ) && ( currentTime - tolerance <= 5.561 ) , + 'Video successfully stopped with a click on an anchor at ' + + 'second 5.561 approximately (' + currentTime + ')' + ) ; + start() ; + } + }); + + popped.volume(0); + popped.play(); + stop() ; +});