diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b630995f..809cb4526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -108,6 +108,7 @@ CHANGELOG * @forbesjo switched automated testing to BrowserStack ([view](https://github.com/videojs/video.js/pull/2492)) * @gkatsev fixed nativeControlsForTouch handling. Defaults to native controls on iphone and native android browsers. ([view](https://github.com/videojs/video.js/pull/2499)) * @heff fixed cross-platform track tests by switching to a fake tech ([view](https://github.com/videojs/video.js/pull/2496)) +* @gkatsev improved tech controls listener handling. ([view](https://github.com/videojs/video.js/pull/2511)) -------------------- diff --git a/src/js/player.js b/src/js/player.js index b8b2ee155..4eaa475d6 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -609,6 +609,9 @@ class Player extends Component { * @method addTechControlsListeners */ addTechControlsListeners() { + // Make sure to remove all the previous listeners in case we are called multiple times. + this.removeTechControlsListeners(); + // Some browsers (Chrome & IE) don't trigger a click on a flash swf, but do // trigger mousedown/up. // http://stackoverflow.com/questions/1444562/javascript-onclick-event-over-flash-object @@ -1939,7 +1942,6 @@ class Player extends Component { if (this.usingNativeControls_ !== bool) { this.usingNativeControls_ = bool; if (bool) { - this.removeTechControlsListeners(); this.addClass('vjs-using-native-controls'); /** @@ -1952,7 +1954,6 @@ class Player extends Component { */ this.trigger('usingnativecontrols'); } else { - this.addTechControlsListeners(); this.removeClass('vjs-using-native-controls'); /** diff --git a/test/unit/player.test.js b/test/unit/player.test.js index 03ada4db7..1cefa2991 100644 --- a/test/unit/player.test.js +++ b/test/unit/player.test.js @@ -415,6 +415,32 @@ test('should allow for tracking when native controls are used', function(){ player.dispose(); }); +test('make sure that controls listeners do not get added too many times', function(){ + var player = TestHelpers.makePlayer({}); + var listeners = 0; + + player.addTechControlsListeners = function() { + listeners++; + }; + + // Make sure native controls is false before starting test + player.usingNativeControls(false); + + player.usingNativeControls(true); + + player.controls(true); + + equal(listeners, 0, 'addTechControlsListeners should not have gotten called yet'); + + player.usingNativeControls(false); + player.controls(false); + + player.controls(true); + equal(listeners, 1, 'addTechControlsListeners should have gotten called once'); + + player.dispose(); +}); + // test('should use custom message when encountering an unsupported video type', // function() { // videojs.options['notSupportedMessage'] = 'Video no go link';