From 6cbe3edae42e5e902a584d267819c32c6fc319f3 Mon Sep 17 00:00:00 2001 From: Chuong Date: Thu, 16 Nov 2017 23:49:54 +0700 Subject: [PATCH] refactor: player.userActive() (#4716) --- src/js/player.js | 91 +++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/src/js/player.js b/src/js/player.js index 018c9e667e..20c4ed9a6e 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -321,6 +321,9 @@ class Player extends Component { // Init state hasStarted_ this.hasStarted_ = false; + // Init state userActive_ + this.userActive_ = false; + // if the global option object was accidentally blown away by // someone, bail early with an informative error if (!this.options_ || @@ -2816,53 +2819,53 @@ class Player extends Component { * The current value of userActive when getting */ userActive(bool) { - if (bool !== undefined) { - bool = !!bool; - if (bool !== this.userActive_) { - this.userActive_ = bool; - if (bool) { - // If the user was inactive and is now active we want to reset the - // inactivity timer - this.userActivity_ = true; - this.removeClass('vjs-user-inactive'); - this.addClass('vjs-user-active'); - /** - * @event Player#useractive - * @type {EventTarget~Event} - */ - this.trigger('useractive'); - } else { - // We're switching the state to inactive manually, so erase any other - // activity - this.userActivity_ = false; - - // Chrome/Safari/IE have bugs where when you change the cursor it can - // trigger a mousemove event. This causes an issue when you're hiding - // the cursor when the user is inactive, and a mousemove signals user - // activity. Making it impossible to go into inactive mode. Specifically - // this happens in fullscreen when we really need to hide the cursor. - // - // When this gets resolved in ALL browsers it can be removed - // https://code.google.com/p/chromium/issues/detail?id=103041 - if (this.tech_) { - this.tech_.one('mousemove', function(e) { - e.stopPropagation(); - e.preventDefault(); - }); - } + if (bool === undefined) { + return this.userActive_; + } - this.removeClass('vjs-user-active'); - this.addClass('vjs-user-inactive'); - /** - * @event Player#userinactive - * @type {EventTarget~Event} - */ - this.trigger('userinactive'); - } - } + bool = !!bool; + + if (bool === this.userActive_) { + return; + } + + this.userActive_ = bool; + + if (this.userActive_) { + this.userActivity_ = true; + this.removeClass('vjs-user-inactive'); + this.addClass('vjs-user-active'); + /** + * @event Player#useractive + * @type {EventTarget~Event} + */ + this.trigger('useractive'); return; } - return this.userActive_; + + // Chrome/Safari/IE have bugs where when you change the cursor it can + // trigger a mousemove event. This causes an issue when you're hiding + // the cursor when the user is inactive, and a mousemove signals user + // activity. Making it impossible to go into inactive mode. Specifically + // this happens in fullscreen when we really need to hide the cursor. + // + // When this gets resolved in ALL browsers it can be removed + // https://code.google.com/p/chromium/issues/detail?id=103041 + if (this.tech_) { + this.tech_.one('mousemove', function(e) { + e.stopPropagation(); + e.preventDefault(); + }); + } + + this.userActivity_ = false; + this.removeClass('vjs-user-active'); + this.addClass('vjs-user-inactive'); + /** + * @event Player#userinactive + * @type {EventTarget~Event} + */ + this.trigger('userinactive'); } /**