Skip to content

Commit

Permalink
Merge branch 'main' into ts-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mister-ben authored Jul 30, 2024
2 parents 9e4d7b5 + 45570d9 commit abe270d
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/js/modal-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ class ModalDialog extends Component {
if (closeButton) {
parentEl.appendChild(closeButton.el_);
}

/**
* Fired after `ModalDialog` is re-filled with content & close button is appended.
*
* @event ModalDialog#aftermodalfill
* @type {Event}
*/
this.trigger('aftermodalfill');
}

/**
Expand Down
89 changes: 86 additions & 3 deletions src/js/spatial-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ class SpatialNavigation extends EventTarget {
this.player_.on('focusin', this.handlePlayerFocus_.bind(this));
this.player_.on('focusout', this.handlePlayerBlur_.bind(this));
this.isListening_ = true;
this.player_.errorDisplay.on('aftermodalfill', () => {
this.updateFocusableComponents();

if (this.focusableComponents.length) {
// The modal has focusable components:

if (this.focusableComponents.length > 1) {
// The modal has close button + some additional buttons.
// Focusing first additional button:

this.focusableComponents[1].focus();
} else {
// The modal has only close button,
// Focusing it:

this.focusableComponents[0].focus();
}
}
});
}

/**
Expand Down Expand Up @@ -196,7 +215,7 @@ class SpatialNavigation extends EventTarget {
}

if (!(event.currentTarget.contains(event.relatedTarget)) && !isChildrenOfPlayer || !nextFocusedElement) {
if (currentComponent.name() === 'CloseButton') {
if (currentComponent && currentComponent.name() === 'CloseButton') {
this.refocusComponent();
} else {
this.pause();
Expand Down Expand Up @@ -267,9 +286,65 @@ class SpatialNavigation extends EventTarget {
focusableComponents.push(value);
}
}

// TODO - Refactor the following logic after refactor of videojs-errors elements to be components is done.
if (value.name_ === 'ErrorDisplay' && value.opened_) {
const buttonContainer = value.el_.querySelector('.vjs-errors-ok-button-container');

if (buttonContainer) {
const modalButtons = buttonContainer.querySelectorAll('button');

modalButtons.forEach((element, index) => {
// Add elements as objects to be handled by the spatial navigation
focusableComponents.push({
name: () => {
return 'ModalButton' + (index + 1);
},
el: () => element,
getPositions: () => {
const rect = element.getBoundingClientRect();

// Creating objects that mirror DOMRectReadOnly for boundingClientRect and center
const boundingClientRect = {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
left: rect.left
};

// Calculating the center position
const center = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2,
width: 0,
height: 0,
top: rect.top + rect.height / 2,
right: rect.left + rect.width / 2,
bottom: rect.top + rect.height / 2,
left: rect.left + rect.width / 2
};

return {
boundingClientRect,
center
};
},
// Asume that the following are always focusable
getIsAvailableToBeFocused: () => true,
getIsFocusable: (el) => true,
focus: () => element.focus()
});
});
}
}
});

this.focusableComponents = focusableComponents;

return this.focusableComponents;
}

Expand Down Expand Up @@ -307,7 +382,11 @@ class SpatialNavigation extends EventTarget {
return null;
}

return searchForSuitableChild(component.el());
if (component.el()) {
return searchForSuitableChild(component.el());
}
return null;

}

/**
Expand Down Expand Up @@ -464,7 +543,7 @@ class SpatialNavigation extends EventTarget {
*/
refocusComponent() {
if (this.lastFocusedComponent_) {
// If use is not active, set it to active.
// If user is not active, set it to active.
if (!this.player_.userActive()) {
this.player_.userActive(true);
}
Expand Down Expand Up @@ -492,6 +571,10 @@ class SpatialNavigation extends EventTarget {
* @param {Component} component - The component to be focused.
*/
focus(component) {
if (typeof component !== 'object') {
return;
}

if (component.getIsAvailableToBeFocused(component.el())) {
component.focus();
} else if (this.findSuitableDOMChild(component)) {
Expand Down
121 changes: 121 additions & 0 deletions test/unit/spatial-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TestHelpers from './test-helpers.js';
import sinon from 'sinon';
import document from 'global/document';
import TextTrackSelect from '../../src/js/tracks/text-track-select';
import * as Dom from '../../src/js/utils/dom.js';

QUnit.module('SpatialNavigation', {
beforeEach() {
Expand Down Expand Up @@ -491,3 +492,123 @@ QUnit.test('should call `searchForTrackSelect()` if spatial navigation is enable

assert.ok(trackSelectSpy.calledOnce);
});

QUnit.test('error on player calls updateFocusableComponents', function(assert) {
const updateFocusableComponentsSpy = sinon.spy(this.spatialNav, 'updateFocusableComponents');

this.spatialNav.start();

this.player.error('Error 1');

assert.ok(updateFocusableComponentsSpy.calledOnce, 'on error event spatial navigation should call "updateFocusableComponents"');
});

QUnit.test('error on player focus the second focusable element of error modal', function(assert) {
this.spatialNav.start();

const firstComponent = {
name: () => 'firstComponent',
el: () => document.createElement('div'),
focus: sinon.spy(),
getIsAvailableToBeFocused: () => true
};

const secondComponent = {
name: () => 'secondComponent',
el: () => document.createElement('div'),
focus: sinon.spy(),
getIsAvailableToBeFocused: () => true
};

this.spatialNav.focusableComponents = [firstComponent, secondComponent];
this.spatialNav.getCurrentComponent = () => firstComponent;
this.spatialNav.updateFocusableComponents = () => [firstComponent, secondComponent];

this.player.error({
code: 1,
dismiss: true
});

assert.ok(secondComponent.focus.calledOnce, 'Focus should move to the second component');
assert.notOk(firstComponent.focus.called, 'Focus should not remain on the first component');
});

QUnit.test('on error, modalButtons should get the buttons if those are available', function(assert) {
this.spatialNav.start();

const buttonContainer = Dom.createEl('div', {}, {class: 'vjs-errors-ok-button-container'});

const testEl1 = Dom.createEl('button', {}, {class: 'c1'});

// Add first element to error modal
buttonContainer.appendChild(testEl1);

const testEl2 = Dom.createEl('button', {}, {class: 'c1'});

// Add second element to error modal
buttonContainer.appendChild(testEl2);
this.player.errorDisplay.el().appendChild(buttonContainer);

this.player.error({
code: 1,
dismiss: true
});

this.spatialNav.getCurrentComponent = () => this.spatialNav.focusableComponents[0];

const getPositionsEl1Spy = sinon.spy(this.spatialNav.focusableComponents[0], 'getPositions');
const getPositionsEl2Spy = sinon.spy(this.spatialNav.focusableComponents[1], 'getPositions');

this.spatialNav.move('left');

assert.strictEqual(this.spatialNav.focusableComponents.length, 2, 'button elements are now part of the array of focusableComponents');
assert.ok(getPositionsEl1Spy.calledOnce, 'getPositions method called on button');
assert.ok(getPositionsEl2Spy.calledOnce, 'getPositions method called on button');
assert.strictEqual(this.spatialNav.focusableComponents[0].name(), 'ModalButton1', 'testEl1 name should be ModalButton1');
assert.strictEqual(this.spatialNav.focusableComponents[1].name(), 'ModalButton2', 'testEl2 name should be ModalButton2');

getPositionsEl1Spy.restore();
getPositionsEl2Spy.restore();
});

QUnit.test('on error, modalButtons added functions should work properly', function(assert) {
this.spatialNav.start();

const buttonContainer = Dom.createEl('div', {}, {class: 'vjs-errors-ok-button-container'});

const testEl1 = Dom.createEl('button', {}, {class: 'c1'});

// Add first element to error modal
buttonContainer.appendChild(testEl1);

this.player.errorDisplay.el().appendChild(buttonContainer);

this.player.error({ code: 1, dismiss: true });

assert.strictEqual(this.spatialNav.focusableComponents[0].el() instanceof Element, true, 'el function from modal buttons should return a DOM element'); // eslint-disable-line no-undef
assert.strictEqual(this.spatialNav.focusableComponents[0].getIsFocusable(), true, 'getIsFocusable function from modal buttons is always true');
assert.strictEqual(this.spatialNav.focusableComponents[0].getIsAvailableToBeFocused(), true, 'getIsAvailableToBeFocused function from modal buttons is always true');
assert.strictEqual(this.spatialNav.focusableComponents[0].getIsAvailableToBeFocused(), true, 'getIsAvailableToBeFocused function from modal buttons is always true');
assert.strictEqual(typeof this.spatialNav.focusableComponents[0].getPositions(), 'object', 'focusableComponents function from modal buttons should return an object');
});

QUnit.test('If component passes the required functions it should be added to focusableComponents', function(assert) {
this.spatialNav.start();

const firstComponent = {
name_: 'firstComponent',
name: () => this.name,
el_: document.createElement('div'),
el: () => this.el_,
focus: sinon.spy(),
getIsAvailableToBeFocused: () => true,
getIsFocusable: () => true
};

this.player.children_.push(firstComponent);
this.spatialNav.getCurrentComponent = () => firstComponent;
this.spatialNav.updateFocusableComponents();

assert.strictEqual(this.spatialNav.focusableComponents.length, 1, 'focusableComponents array should have 1 component');
assert.strictEqual(this.spatialNav.focusableComponents[0].name_, 'firstComponent', 'the name of the component in focusableComponents array should be "firstComponent"');
});

0 comments on commit abe270d

Please sign in to comment.