Skip to content

Commit

Permalink
feat(player): modify SpatialNavigation class & Component class
Browse files Browse the repository at this point in the history
Modify SpatialNavigation class:
-Add function ‘handlePlayerBlur’
-Add function ‘handlePlayerFocus’
Modify Component class:
-Modify ‘handleBlur’
-Modify ‘handleFocus’
  • Loading branch information
CarlosVillasenor committed Dec 20, 2023
1 parent 1966c3e commit 2289953
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,20 +1331,51 @@ class Component {
}

/**
* Abstract method to handle the focus event. This method should be overridden
* Handles the focus event. This method could be overridden
* in subclasses to provide specific focus event handling.
*
* @abstract
* Calls for handling of the Player Focus if:
* * SpatialNavigation is enabled, not paused & element is focusable.
*/
handleFocus() {}
handleFocus() {
// eslint-disable-next-line
const SpatialNavigation = window.SpatialNavigation;

if (SpatialNavigation && SpatialNavigation.isPaused && this.getIsFocusable()) {
SpatialNavigation.handlePlayerFocus();
}
}

/**
* Abstract method to handle the blur event. This method should be overridden
* in subclasses to provide specific blur event handling.
* Handles the blur event. This method could be overridden
* in subclasses to provide specific focus event handling.
*
* @abstract
* @param {string|Event|Object} event
* The name of the event, an `Event`, or an object with a key of type set to
* an event name.
*
* Calls for handling of the Player Blur if:
* * Next focused element is not a children of current focused element &
* next focused element is not a children of Player.
* * There is no next focused element
*/
handleBlur() {}
handleBlur(event) {
// eslint-disable-next-line
const SpatialNavigation = window.SpatialNavigation;

if (SpatialNavigation && this.getIsFocusable()) {
const nextFocusedElement = event.relatedTarget;
let isChildrenOfPlayer = null;

if (nextFocusedElement) {
isChildrenOfPlayer = Boolean(nextFocusedElement.closest('.video-js'));
}

if (!(event.currentTarget.contains(event.relatedTarget)) && !isChildrenOfPlayer || !nextFocusedElement) {
SpatialNavigation.handlePlayerBlur();
}
}
}

/**
* Set the focus to this component
Expand Down
16 changes: 16 additions & 0 deletions src/js/spatial-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ class SpatialNavigation {
this.isPaused = false;
}

/**
* Handles Player Blur.
*
*/
handlePlayerBlur() {
this.pause();
}

/**
* Handles Player Focus.
*
*/
handlePlayerFocus() {
this.resume();
}

/**
* Gets a set of focusable components.
*
Expand Down

0 comments on commit 2289953

Please sign in to comment.