2019-11-14 18:33:31 +03:00
|
|
|
define(["focusManager", "layoutManager"], function (focusManager, layoutManager) {
|
2019-11-02 20:38:58 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Previously selected element.
|
|
|
|
*/
|
|
|
|
var activeElement;
|
|
|
|
|
2019-11-14 18:33:31 +03:00
|
|
|
/**
|
|
|
|
* Returns true if AutoFocuser is enabled.
|
|
|
|
*/
|
|
|
|
function isEnabled() {
|
|
|
|
return layoutManager.tv;
|
|
|
|
};
|
|
|
|
|
2019-11-02 20:38:58 +03:00
|
|
|
/**
|
|
|
|
* Start AutoFocuser
|
|
|
|
*/
|
|
|
|
function enable() {
|
2019-11-14 18:33:31 +03:00
|
|
|
if (!isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:38:58 +03:00
|
|
|
window.addEventListener("focusin", function (e) {
|
|
|
|
activeElement = e.target;
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("AutoFocuser enabled");
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:55:44 +03:00
|
|
|
/**
|
|
|
|
* Create an array from some source.
|
|
|
|
*/
|
|
|
|
var arrayFrom = Array.prototype.from || function (src) {
|
|
|
|
return Array.prototype.slice.call(src);
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:38:58 +03:00
|
|
|
/**
|
|
|
|
* Set focus on a suitable element, taking into account the previously selected.
|
|
|
|
*/
|
|
|
|
function autoFocus(container) {
|
2019-11-14 18:33:31 +03:00
|
|
|
if (!isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:38:58 +03:00
|
|
|
container = container || document.body;
|
|
|
|
|
|
|
|
var candidates = [];
|
|
|
|
|
|
|
|
if (activeElement) {
|
|
|
|
// These elements are recreated
|
|
|
|
if (activeElement.classList.contains("btnPreviousPage")) {
|
|
|
|
candidates.push(container.querySelector(".btnPreviousPage"));
|
|
|
|
candidates.push(container.querySelector(".btnNextPage"));
|
|
|
|
} else if (activeElement.classList.contains("btnNextPage")) {
|
|
|
|
candidates.push(container.querySelector(".btnNextPage"));
|
|
|
|
candidates.push(container.querySelector(".btnPreviousPage"));
|
|
|
|
} else if (activeElement.classList.contains("btnSelectView")) {
|
|
|
|
candidates.push(container.querySelector(".btnSelectView"));
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates.push(activeElement);
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:55:44 +03:00
|
|
|
candidates = candidates.concat(arrayFrom(container.querySelectorAll(".btnResume")));
|
|
|
|
candidates = candidates.concat(arrayFrom(container.querySelectorAll(".btnPlay")));
|
2019-11-02 20:38:58 +03:00
|
|
|
|
|
|
|
var notFound = candidates.every(function (element) {
|
|
|
|
if (focusManager.isCurrentlyFocusable(element)) {
|
|
|
|
focusManager.focus(element);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-11-14 16:41:50 +03:00
|
|
|
if (notFound) {
|
|
|
|
// FIXME: Multiple itemsContainers
|
|
|
|
var itemsContainer = container.querySelector(".itemsContainer");
|
|
|
|
|
|
|
|
if (itemsContainer) {
|
|
|
|
notFound = !focusManager.autoFocus(itemsContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:38:58 +03:00
|
|
|
if (notFound) {
|
|
|
|
focusManager.autoFocus(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2019-11-14 18:33:31 +03:00
|
|
|
isEnabled: isEnabled,
|
2019-11-02 20:38:58 +03:00
|
|
|
enable: enable,
|
|
|
|
autoFocus: autoFocus
|
|
|
|
};
|
|
|
|
});
|