-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtab.js
148 lines (122 loc) · 5.27 KB
/
newtab.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Clock } from "./modules/clock.js";
import { WeatherApi } from "./modules/owm-weather.js";
import { Autocomplete } from "./modules/autocomplete.js";
import { Search } from "./widgets/search/search.js";
import { CustomWallpapers } from "./widgets/customwallpapers/customwallpapers.js";
import { BibleVerses } from "./widgets/bibleverses/bibleverses.js";
import { QuickLinks } from "./widgets/quicklinks/quicklinks.js";
if (!localStorage.hasOwnProperty("settings")) {
localStorage.setItem("settings", JSON.stringify({ widgets: {} }));
}
async function main() {
const clock = new Clock();
clock.setTimeFormat(JSON.parse(localStorage.getItem("settings"))?.timeFormat)
.onUpdateMinute(t => {
document.getElementById("time").innerText = clock.getDisplayTime(t);
})
.onUpdateDay(t => {
document.getElementById("date").innerText = clock.getDisplayDate(t);
})
.start();
let backgrounds = await getBackgrounds(JSON.parse(localStorage.getItem("settings"))["theme"] ?? "summer");
let randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)];
setBackground(randomBackground);
let latlng = getLocation();
const weatherApi = new WeatherApi();
weatherApi.setLocation(latlng.lat, latlng.lng).autoUpdate((temp, icon) => {
document.getElementById("currentWeatherTemperature").innerText = temp;
document.getElementById("currentWeatherIcon").classList = icon;
}, 60 * 60 * 1000);
document
.querySelector(".currentWeather")
.addEventListener(
"click",
() =>
(location.href = `https://forecast.weather.gov/MapClick.php?lon=${latlng.lng}&lat=${latlng.lat}`)
);
document
.getElementById("settingsBtn")
.addEventListener("click", () => openSettings(clock, weatherApi));
document
.querySelector(".settings.header button")
.addEventListener("click", () => closeSettings());
document.getElementById("springTheme").onclick = () => setTheme("spring");
document.getElementById("summerTheme").onclick = () => setTheme("summer");
document.getElementById("fallTheme").onclick = () => setTheme("fall");
document.getElementById("winterTheme").onclick = () => setTheme("winter");
try { BibleVerses.load(); } catch (e) { console.error(e); }
try { Search.load(); } catch (e) { console.error(e); }
try { CustomWallpapers.load(); } catch (e) { console.error(e); }
try { QuickLinks.load(); } catch (e) { console.error(e); }
}
main();
function getBackgrounds(theme) {
return new Promise(resolve => {
const backgrounds = ["---BACKGROUNDS---"];
resolve(backgrounds.filter(f => f.startsWith(theme)).map(f => f));
});
}
function setBackground(filename) {
let url = "backgrounds/" + encodeURIComponent(filename);
document.body.style.background =
"linear-gradient(0deg, rgb(2 0 36 / 40%) 0%, rgb(0 0 0 / 20%) 20%, rgba(255,255,255,0) 40%, rgb(0 0 0 / 20%) 80%, rgb(2 0 36 / 40%) 100%), url('" +
url +
"') no-repeat center";
document.body.style.backgroundSize = "cover";
}
function getLocation() {
return (
JSON.parse(localStorage.getItem("settings"))["location"] ?? {
lat: 32,
lng: 96
}
);
}
function openSettings(clock, weatherApi) {
let autoComplete = new Autocomplete();
document.getElementById("timeFormat").checked = clock.format == 12;
let menu = document.getElementById("menu")
menu.classList.add("show");
menu.classList.remove("hide");
let loc = getLocation();
let input = document.getElementById("settingsAddressInput");
if (!!loc.name) input.value = loc.name;
autoComplete.bind(input);
autoComplete.setOnCompleteListener((city, lat, lng) => {
var settings = JSON.parse(localStorage.getItem("settings"));
input.value = city;
settings.location = {
name: city,
lat: lat,
lng: lng
};
localStorage.setItem("settings", JSON.stringify(settings));
let latlng = getLocation();
weatherApi.setLocation(latlng.lat, latlng.lng).update((temp, icon) => {
document.getElementById("currentWeatherTemperature").innerText = temp;
document.getElementById("currentWeatherIcon").classList = icon;
});
});
document.getElementById("timeFormat").addEventListener("change", () => updateTimeFormat(clock));
}
function closeSettings() {
let menu = document.getElementById("menu")
menu.classList.remove("show");
menu.classList.add("hide");
}
function updateTimeFormat(clock) {
var settings = JSON.parse(localStorage.getItem("settings"));
let checked = document.getElementById("timeFormat").checked;
settings.timeFormat = checked ? 12 : 24;
clock.setTimeFormat(settings.timeFormat);
clock.update(true);
localStorage.setItem("settings", JSON.stringify(settings));
}
async function setTheme(theme) {
var settings = JSON.parse(localStorage.getItem("settings"));
settings.theme = theme;
let backgrounds = await getBackgrounds(theme);
let randomBackground = backgrounds[Math.floor(Math.random() * backgrounds.length)];
setBackground(randomBackground);
localStorage.setItem("settings", JSON.stringify(settings));
}