if ( !document.addEventListener ) {
global.Popcorn = {};
- var methods = ( "removeInstance addInstance getInstanceById removeInstanceById " +
+ var methods = ( "removeInstance destroy addInstance getInstanceById removeInstanceById " +
"forEach extend effects error guid sizeOf isArray nop position disable enable " +
"addTrackEvent removeTrackEvent getTrackEvents getTrackEvent getLastTrackEventId " +
"timeUpdate plugin removePlugin compose effect parser xhr getJSONP getScript" ).split(/\s+/);
return Popcorn.instances;
};
+ Popcorn.destroy = function( instance ) {
+ var events = instance.data.events,
+ singleEvent;
+
+ // Iterate through all events and remove them
+ for( item in events ) {
+ singleEvent = events[ item ];
+ for( fn in singleEvent ) {
+ delete singleEvent[ fn ];
+ }
+ events[ item ] = null;
+ }
+
+ //instance.media.removeEventListener( "timeupdate", function( event ) {
+ //console.log("HERE");
+ //Popcorn.timeUpdate( that, event );
+ //}, false );
+
+ instance.media.removeEventListener( "timeupdate", instance.data.timeUpdateFunction, false );
+
+ Popcorn.removeInstance( instance );
+ };
+
// Addes a Popcorn instance to the Popcorn instance array
Popcorn.addInstance = function( instance ) {
} else {
- that.media.addEventListener( "timeupdate", function( event ) {
+ that.data.timeUpdateFunction = function( event ) {
+ console.log( "here" );
+ Popcorn.timeUpdate( that, event );
+ }
- Popcorn.timeUpdate( that, event );
- }, false );
+ // that.media.addEventListener( "timeupdate", function( event ) {
+ // console.log("HERE");
+ // Popcorn.timeUpdate( that, event );
+ //}, false );
+ //
+ that.media.addEventListener( "timeupdate", that.data.timeUpdateFunction, false );
}
} else {
global.setTimeout(function() {
timeUpdate: function( event ) {
Popcorn.timeUpdate.call( null, this, event );
return this;
+ },
+
+ destroy: function() {
+ Popcorn.destroy.call( null, this );
+ return this;
}
});
plus();
});
+test( "Popcorn.destroy", function() {
+ var popcorn = Popcorn( "#video" ),
+ expects = 8,
+ count = 0,
+ playCounter = 0,
+ timeUpdateCounter = 0;
+
+ expect( expects );
+
+ function plus() {
+ if( ++count === expects ) {
+ start();
+ }
+ }
+
+ function howMany() {
+ var counter = 0;
+
+ for( things in popcorn.data.events ) {
+ counter++;
+ }
+
+ return counter;
+ }
+
+ stop();
+
+ // initially no listeners
+ equals( howMany(), 0, "Initially no events have been added" );
+ plus();
+
+ equals( playCounter, 0, "playCounter is intially 0" );
+ plus();
+
+ equals( timeUpdateCounter, 0, "timeUpdateCounter is intially 0" );
+ plus();
+
+ // add some event listeners for testing
+ popcorn.listen( "timeupdate", function( event ) { timeUpdateCounter++; }, false );
+ popcorn.listen( "play", function( event ) { playCounter++; }, false );
+
+ popcorn.play( 0 );
+
+ popcorn.exec( 1, function() {
+ popcorn.pause();
+
+ equals( howMany(), 2, "popcorn.data.events has correct number of events - before Popcorn.destroy" );
+ plus();
+
+ equals( playCounter, 1, "playCounter triggered exactly 1 time" );
+ plus();
+
+ equals( timeUpdateCounter, 4, "timeUpdateCounter triggered exactly 4 times" );
+ plus();
+
+ playCounter = timeUpdateCounter = 0;
+
+ popcorn.destroy();
+ popcorn.currentTime( 0 ).play();
+
+ popcorn.exec( 0.25, function() {
+ popcorn.pause();
+
+ equals( playCounter, 0, "playCounter was not triggered - after destroy called" );
+ plus();
+
+ equals( timeUpdateCounter, 0, "timeUpdateCounter was not triggered - after destroy called" );
+ plus();
+ });
+
+ });
+
+});
+
test("guid", function() {
expect(6);
--- /dev/null
+<html>
+ <head>
+ <script src="popcorn.js"></script>
+ <script>
+ var popcorn;
+
+ function destroy() {
+ Popcorn.destroy( popcorn );
+ }
+
+
+ document.addEventListener( "DOMContentLoaded", function( event ) {
+ popcorn = Popcorn( "#video" );
+
+ popcorn.listen( "timeupdate", function() { });
+ popcorn.listen( "play", function() {});
+
+ for( item in popcorn.data.events ) {
+ console.log( "EVENT: ", popcorn.data.events[ item ] );
+ }
+
+ }, false );
+ </script>
+ </head>
+ <body>
+ <button onclick="destroy()">DESTROY</button>
+
+ <video id='video'
+ style="display:;width:300px"
+ controls
+ preload="auto">
+
+ <source id='mp4'
+ src="test/trailer.mp4"
+ type='video/mp4; codecs="avc1, mp4a"'>
+
+ <source id='ogv'
+ src="test/trailer.ogv"
+ type='video/ogg; codecs="theora, vorbis"'>
+
+ <source id='webm'
+ src="test/trailer.webm"
+ type='video/webm; codecs="vp8, vorbis"'>
+
+ <p>Your user agent does not support the HTML5 Video element.</p>
+ </video>
+ </body>
+</html>