-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUtils.js
More file actions
107 lines (94 loc) · 3.37 KB
/
Utils.js
File metadata and controls
107 lines (94 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// @flow
import { isWorldName } from './Worlds';
import type { ThemeName } from './types';
import type { WorldName } from './Worlds';
let idCounter: number = 0;
/* istanbul ignore next */
function generateId(prefix: string): string {
const id = `${prefix}-${idCounter}`;
idCounter += 1;
return id;
}
/* istanbul ignore next */
function makeDelayedPromise(timeMs: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeMs);
});
}
function generateEncodedProgramURL(versionString: string, themeString: string, worldString: string, programString: string, characterStateString: string, allowedActionsString: string): string {
return `?v=${encodeURIComponent(versionString)}&t=${themeString}&w=${worldString}&p=${encodeURIComponent(programString)}&c=${encodeURIComponent(characterStateString)}&a=${encodeURIComponent(allowedActionsString)}`;
}
/*
"mixed" => A mixture of light and dark elements, with colour.
"light" => A light theme, with colour.
"dark" => A dark theme, with colour.
"gray" => A grayscale theme, without colour.
"contrast" => A high-contrast black and white theme.
*/
function getThemeFromString(themeQuery: ?string, defaultThemeName: ThemeName): ThemeName {
switch (themeQuery) {
case('mixed'): return 'mixed';
case('dark'): return 'dark';
case('light'): return 'light';
case('gray'): return 'gray';
case('contrast'): return 'contrast';
default: return defaultThemeName;
}
}
function getWorldFromString(worldQuery: ?string, defaultWorldName: WorldName): WorldName {
switch (worldQuery) {
// Convert old world names to the new world names
case('space'):
return 'Space';
case('forest'):
return 'Jungle';
// If 'worldQuery' is a known world name, use it,
// otherwise return 'defaultWorldName'
default:
if (isWorldName(worldQuery)) {
return ((worldQuery: any): WorldName);
} else {
return defaultWorldName;
}
}
}
/**
* A simplified pure JS equivalent of jQuery.extend that always performs a
* "deep" merge.
*
* @param {...Object} toMerge - One or more objects to be merged together from left to right.
* @returns {Object} - The merged object.
*
*/
function extend(...toMerge:Object) {
const merged = {};
for (const singleEntryToMerge of toMerge) {
for (const [key, value] of Object.entries(singleEntryToMerge)) {
if (typeof value === "object" && !Array.isArray(value) && (typeof merged[key] === "object" && !Array.isArray(merged[key]) && merged[key] !== null)) {
merged[key] = extend(merged[key], value);
}
else {
merged[key] = value;
}
}
}
return merged;
}
function focusByQuerySelector (selectors: string) {
const element = document.querySelector(selectors);
if (element && element.focus) {
element.focus();
}
}
const ApplePlatforms = [
"iPad",
"iPhone",
"iPod",
"MacIntel"
]
function isAppleDevice () :boolean {
return ApplePlatforms.indexOf(window.navigator.platform) !== -1;
}
export { extend, focusByQuerySelector, generateId, makeDelayedPromise, generateEncodedProgramURL, getThemeFromString, getWorldFromString, isAppleDevice };