-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicWebAdapter.js
More file actions
115 lines (96 loc) · 3.41 KB
/
Copy pathDynamicWebAdapter.js
File metadata and controls
115 lines (96 loc) · 3.41 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
108
109
110
111
112
113
114
115
const jsonUrl = 'http://localhost:8192/'; // DO NOT CHANGE THIS Defines URL from host app
const configFile = 'DWAconfig.json'; // Define your config file here
const basePath = window.location.origin;
let themePref;
let localFilePath;
fetch(`${basePath}/${configFile}`) // gathers data from config file
.then(response => {
if (!response.ok) {
throw new Error('Config file not found');
}
return response.json();
})
.then(data => {
localFilePath = data.colours_path;
themePref = data.theme_pref;
// console.log(localFilePath);
// console.log(themePref);
fetchColorsAndSetVariables(jsonUrl, localFilePath);
})
.catch(error => {
console.error('Error fetching the JSON:', error);
localFilePath = '/colours.json'; // default value
themePref = 'auto'; // default value
fetchColorsAndSetVariables(jsonUrl, localFilePath);
});
async function fetchColorsAndSetVariables(url, localFile) {
const setCSSVariables = (colors) => {
const root = document.documentElement;
for (const [key, value] of Object.entries(colors)) {
root.style.setProperty(`--${key}`, value);
}
};
const fetchData = async (url) => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const json = await response.json();
return json.colors;
} catch (error) {
console.error('Failed to fetch from URL:', error);
return null;
}
};
const loadLocalFile = async (filePath) => {
try {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error('Failed to load local file');
}
const json = await response.json();
return json.schemes;
} catch (error) {
console.error('Failed to load local file:', error);
return null;
}
};
const getColorsForScheme = (schemes) => { // Sets colour scheme from colours and from defined in config file
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (themePref === "auto") {
console.log('auto selected');
return prefersDarkScheme ? schemes.dark : schemes.light;
}
if (themePref === "dark") {
console.log('dark selected');
return schemes.dark;
}
if (themePref === "light") {
console.log('light selected');
return schemes.light;
}
else {
return prefersDarkScheme ? schemes.dark : schemes.light; // default to auto if not defined
}
};
const updateColors = async () => {
let colors = await fetchData(url);
if (colors) {
setCSSVariables(colors);
console.log('Colors updated from URL:', colors);
}
};
const initialLoad = async () => {
const localSchemes = await loadLocalFile(localFile);
if (localSchemes) {
const colors = getColorsForScheme(localSchemes);
setCSSVariables(colors);
console.log('Colors loaded from local file:', colors);
}
await updateColors();
setInterval(updateColors, 5000);
};
initialLoad();
}
fetchColorsAndSetVariables(jsonUrl, localFilePath);