1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Migrate some easy components to ES6

This commit is contained in:
Cameron Cordes 2020-03-19 21:20:47 -04:00
parent 37ebb772e8
commit f3b5c804a3
5 changed files with 256 additions and 267 deletions

View file

@ -70,11 +70,23 @@
"whatwg-fetch": "^3.0.0" "whatwg-fetch": "^3.0.0"
}, },
"babel": { "babel": {
"presets": ["@babel/preset-env"], "presets": [
"overrides": [{ "@babel/preset-env"
"test": ["src/components/cardbuilder/cardBuilder.js"], ],
"plugins": ["@babel/plugin-transform-modules-amd"] "overrides": [
}] {
"test": [
"src/components/cardbuilder/cardBuilder.js",
"src/components/filedownloader.js",
"src/components/filesystem.js",
"src/components/input/keyboardnavigation.js",
"src/components/sanatizefilename.js"
],
"plugins": [
"@babel/plugin-transform-modules-amd"
]
}
]
}, },
"browserslist": [ "browserslist": [
"last 2 Firefox versions", "last 2 Firefox versions",

View file

@ -1,18 +1,14 @@
define(['multi-download'], function (multiDownload) { import multiDownload from "multi-download"
'use strict';
return { export function download(items) {
download: function (items) {
if (window.NativeShell) { if (window.NativeShell) {
items.map(function (item) { items.map(function (item) {
window.NativeShell.downloadFile(item.url); window.NativeShell.downloadFile(item.url);
}); });
} else { } else {
multiDownload(items.map(function (item) { multiDownload(items.map(function (item) {
return item.url; return item.url;
})); }));
} }
} }
};
});

View file

@ -1,18 +1,13 @@
define([], function () { export function fileExists(path) {
'use strict'; if (window.NativeShell && window.NativeShell.FileSystem) {
return window.NativeShell.FileSystem.fileExists(path);
}
return Promise.reject();
}
return { export function directoryExists(path) {
fileExists: function (path) { if (window.NativeShell && window.NativeShell.FileSystem) {
if (window.NativeShell && window.NativeShell.FileSystem) { return window.NativeShell.FileSystem.directoryExists(path);
return window.NativeShell.FileSystem.fileExists(path); }
} return Promise.reject();
return Promise.reject(); }
},
directoryExists: function (path) {
if (window.NativeShell && window.NativeShell.FileSystem) {
return window.NativeShell.FileSystem.directoryExists(path);
}
return Promise.reject();
}
};
});

View file

@ -1,165 +1,157 @@
define(["inputManager", "layoutManager"], function (inputManager, layoutManager) { import inputManager from "inputManager";
"use strict"; import layoutManager from "layoutManager";
/** /**
* Key name mapping. * Key name mapping.
*/ */
// Add more to support old browsers const KeyNames = {
var KeyNames = { 13: "Enter",
13: "Enter", 19: "Pause",
19: "Pause", 27: "Escape",
27: "Escape", 32: "Space",
32: "Space", 37: "ArrowLeft",
37: "ArrowLeft", 38: "ArrowUp",
38: "ArrowUp", 39: "ArrowRight",
39: "ArrowRight", 40: "ArrowDown",
40: "ArrowDown", // MediaRewind (Tizen/WebOS)
// MediaRewind (Tizen/WebOS) 412: "MediaRewind",
412: "MediaRewind", // MediaStop (Tizen/WebOS)
// MediaStop (Tizen/WebOS) 413: "MediaStop",
413: "MediaStop", // MediaPlay (Tizen/WebOS)
// MediaPlay (Tizen/WebOS) 415: "MediaPlay",
415: "MediaPlay", // MediaFastForward (Tizen/WebOS)
// MediaFastForward (Tizen/WebOS) 417: "MediaFastForward",
417: "MediaFastForward", // Back (WebOS)
// Back (WebOS) 461: "Back",
461: "Back", // Back (Tizen)
// Back (Tizen) 10009: "Back",
10009: "Back", // MediaTrackPrevious (Tizen)
// MediaTrackPrevious (Tizen) 10232: "MediaTrackPrevious",
10232: "MediaTrackPrevious", // MediaTrackNext (Tizen)
// MediaTrackNext (Tizen) 10233: "MediaTrackNext",
10233: "MediaTrackNext", // MediaPlayPause (Tizen)
// MediaPlayPause (Tizen) 10252: "MediaPlayPause"
10252: "MediaPlayPause" };
};
/** /**
* Keys used for keyboard navigation. * Keys used for keyboard navigation.
*/ */
var NavigationKeys = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"]; const NavigationKeys = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"];
var hasFieldKey = false; let hasFieldKey = false;
try { try {
hasFieldKey = "key" in new KeyboardEvent("keydown"); hasFieldKey = "key" in new KeyboardEvent("keydown");
} catch (e) { } catch (e) {
console.error("error checking 'key' field"); console.error("error checking 'key' field");
}
if (!hasFieldKey) {
// Add [a..z]
for (let i = 65; i <= 90; i++) {
KeyNames[i] = String.fromCharCode(i).toLowerCase();
} }
}
if (!hasFieldKey) { /**
// Add [a..z] * Returns key name from event.
for (var i = 65; i <= 90; i++) { *
KeyNames[i] = String.fromCharCode(i).toLowerCase(); * @param {KeyboardEvent} event keyboard event
* @return {string} key name
*/
export function getKeyName(event) {
return KeyNames[event.keyCode] || event.key;
}
/**
* Returns _true_ if key is used for navigation.
*
* @param {string} key name
* @return {boolean} _true_ if key is used for navigation
*/
export function isNavigationKey(key) {
return NavigationKeys.indexOf(key) != -1;
}
export function enable() {
document.addEventListener("keydown", function (e) {
const key = getKeyName(e);
// Ignore navigation keys for non-TV
if (!layoutManager.tv && isNavigationKey(key)) {
return;
} }
}
/** let capture = true;
* Returns key name from event.
*
* @param {KeyboardEvent} keyboard event
* @return {string} key name
*/
function getKeyName(event) {
return KeyNames[event.keyCode] || event.key;
}
/** switch (key) {
* Returns _true_ if key is used for navigation. case "ArrowLeft":
* inputManager.handle("left");
* @param {string} key name break;
* @return {boolean} _true_ if key is used for navigation case "ArrowUp":
*/ inputManager.handle("up");
function isNavigationKey(key) { break;
return NavigationKeys.indexOf(key) != -1; case "ArrowRight":
} inputManager.handle("right");
break;
case "ArrowDown":
inputManager.handle("down");
break;
function enable() { case "Back":
document.addEventListener("keydown", function (e) { inputManager.handle("back");
var key = getKeyName(e); break;
// Ignore navigation keys for non-TV case "Escape":
if (!layoutManager.tv && isNavigationKey(key)) { if (layoutManager.tv) {
return;
}
var capture = true;
switch (key) {
case "ArrowLeft":
inputManager.handle("left");
break;
case "ArrowUp":
inputManager.handle("up");
break;
case "ArrowRight":
inputManager.handle("right");
break;
case "ArrowDown":
inputManager.handle("down");
break;
case "Back":
inputManager.handle("back"); inputManager.handle("back");
break; } else {
case "Escape":
if (layoutManager.tv) {
inputManager.handle("back");
} else {
capture = false;
}
break;
case "MediaPlay":
inputManager.handle("play");
break;
case "Pause":
inputManager.handle("pause");
break;
case "MediaPlayPause":
inputManager.handle("playpause");
break;
case "MediaRewind":
inputManager.handle("rewind");
break;
case "MediaFastForward":
inputManager.handle("fastforward");
break;
case "MediaStop":
inputManager.handle("stop");
break;
case "MediaTrackPrevious":
inputManager.handle("previoustrack");
break;
case "MediaTrackNext":
inputManager.handle("nexttrack");
break;
default:
capture = false; capture = false;
} }
break;
if (capture) { case "MediaPlay":
console.debug("disabling default event handling"); inputManager.handle("play");
e.preventDefault(); break;
} case "Pause":
}); inputManager.handle("pause");
} break;
case "MediaPlayPause":
inputManager.handle("playpause");
break;
case "MediaRewind":
inputManager.handle("rewind");
break;
case "MediaFastForward":
inputManager.handle("fastforward");
break;
case "MediaStop":
inputManager.handle("stop");
break;
case "MediaTrackPrevious":
inputManager.handle("previoustrack");
break;
case "MediaTrackNext":
inputManager.handle("nexttrack");
break;
// Gamepad initialisation. No script is required if no gamepads are present at init time, saving a bit of resources. default:
// Whenever the gamepad is connected, we hand all the control of the gamepad to gamepadtokey.js by removing the event handler capture = false;
function attachGamepadScript(e) { }
console.log("Gamepad connected! Attaching gamepadtokey.js script");
window.removeEventListener("gamepadconnected", attachGamepadScript);
require(["components/input/gamepadtokey"]);
}
// No need to check for gamepads manually at load time, the eventhandler will be fired for that if (capture) {
window.addEventListener("gamepadconnected", attachGamepadScript); console.debug("disabling default event handling");
e.preventDefault();
}
});
}
return { // Gamepad initialisation. No script is required if no gamepads are present at init time, saving a bit of resources.
enable: enable, // Whenever the gamepad is connected, we hand all the control of the gamepad to gamepadtokey.js by removing the event handler
getKeyName: getKeyName, function attachGamepadScript(e) {
isNavigationKey: isNavigationKey console.log("Gamepad connected! Attaching gamepadtokey.js script");
}; window.removeEventListener("gamepadconnected", attachGamepadScript);
}); require(["components/input/gamepadtokey"]);
}
// No need to check for gamepads manually at load time, the eventhandler will be fired for that
window.addEventListener("gamepadconnected", attachGamepadScript);

View file

@ -1,96 +1,90 @@
// From https://github.com/parshap/node-sanitize-filename // From https://github.com/parshap/node-sanitize-filename
define([], function () { const illegalRe = /[\/\?<>\\:\*\|":]/g;
'use strict'; // eslint-disable-next-line no-control-regex
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
const reservedRe = /^\.+$/;
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
const windowsTrailingRe = /[\. ]+$/;
var illegalRe = /[\/\?<>\\:\*\|":]/g; function isHighSurrogate(codePoint) {
// eslint-disable-next-line no-control-regex return codePoint >= 0xd800 && codePoint <= 0xdbff;
var controlRe = /[\x00-\x1f\x80-\x9f]/g; }
var reservedRe = /^\.+$/;
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
var windowsTrailingRe = /[\. ]+$/;
function isHighSurrogate(codePoint) { function isLowSurrogate(codePoint) {
return codePoint >= 0xd800 && codePoint <= 0xdbff; return codePoint >= 0xdc00 && codePoint <= 0xdfff;
}
function getByteLength(string) {
if (typeof string !== "string") {
throw new Error("Input must be string");
} }
function isLowSurrogate(codePoint) { const charLength = string.length;
return codePoint >= 0xdc00 && codePoint <= 0xdfff; let byteLength = 0;
} let codePoint = null;
let prevCodePoint = null;
function getByteLength(string) { for (let i = 0; i < charLength; i++) {
if (typeof string !== "string") { codePoint = string.charCodeAt(i);
throw new Error("Input must be string"); // handle 4-byte non-BMP chars
} // low surrogate
if (isLowSurrogate(codePoint)) {
var charLength = string.length; // when parsing previous hi-surrogate, 3 is added to byteLength
var byteLength = 0; if (prevCodePoint != null && isHighSurrogate(prevCodePoint)) {
var codePoint = null;
var prevCodePoint = null;
for (var i = 0; i < charLength; i++) {
codePoint = string.charCodeAt(i);
// handle 4-byte non-BMP chars
// low surrogate
if (isLowSurrogate(codePoint)) {
// when parsing previous hi-surrogate, 3 is added to byteLength
if (prevCodePoint != null && isHighSurrogate(prevCodePoint)) {
byteLength += 1;
} else {
byteLength += 3;
}
} else if (codePoint <= 0x7f) {
byteLength += 1; byteLength += 1;
} else if (codePoint >= 0x80 && codePoint <= 0x7ff) { } else {
byteLength += 2;
} else if (codePoint >= 0x800 && codePoint <= 0xffff) {
byteLength += 3; byteLength += 3;
} }
prevCodePoint = codePoint; } else if (codePoint <= 0x7f) {
byteLength += 1;
} else if (codePoint >= 0x80 && codePoint <= 0x7ff) {
byteLength += 2;
} else if (codePoint >= 0x800 && codePoint <= 0xffff) {
byteLength += 3;
} }
prevCodePoint = codePoint;
return byteLength;
} }
function truncate(string, byteLength) { return byteLength;
if (typeof string !== "string") { }
throw new Error("Input must be string");
}
var charLength = string.length; function truncate(string, byteLength) {
var curByteLength = 0; if (typeof string !== "string") {
var codePoint; throw new Error("Input must be string");
var segment;
for (var i = 0; i < charLength; i += 1) {
codePoint = string.charCodeAt(i);
segment = string[i];
if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) {
i += 1;
segment += string[i];
}
curByteLength += getByteLength(segment);
if (curByteLength === byteLength) {
return string.slice(0, i + 1);
} else if (curByteLength > byteLength) {
return string.slice(0, i - segment.length + 1);
}
}
return string;
} }
return { const charLength = string.length;
sanitize: function (input, replacement) { let curByteLength = 0;
var sanitized = input let codePoint;
.replace(illegalRe, replacement) let segment;
.replace(controlRe, replacement)
.replace(reservedRe, replacement) for (let i = 0; i < charLength; i += 1) {
.replace(windowsReservedRe, replacement) codePoint = string.charCodeAt(i);
.replace(windowsTrailingRe, replacement); segment = string[i];
return truncate(sanitized, 255);
if (isHighSurrogate(codePoint) && isLowSurrogate(string.charCodeAt(i + 1))) {
i += 1;
segment += string[i];
} }
};
}); curByteLength += getByteLength(segment);
if (curByteLength === byteLength) {
return string.slice(0, i + 1);
} else if (curByteLength > byteLength) {
return string.slice(0, i - segment.length + 1);
}
}
return string;
}
export function sanitize(input, replacement) {
const sanitized = input
.replace(illegalRe, replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)
.replace(windowsReservedRe, replacement)
.replace(windowsTrailingRe, replacement);
return truncate(sanitized, 255);
}