Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(player): modify SpatialNavigation class & Component class #2

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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