-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
182 lines (145 loc) · 5.19 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const CLOCK_ELEMENT = document.getElementById('clock');
const DAY_PROGRESS_ELEMENT = document.getElementById('day-progress');
const SEARCH_BUTTON_ELEMENT = document.getElementById('search-btn');
const SEARCH_BAR_ELEMENT = document.getElementById('search-bar');
const SEARCH_ENGINES_ELEMENT = document.getElementById('search-engines-wrapper');
const SHORTCUT_BUTTON_ELEMENT = document.getElementById('shortcuts-btn');
const CLEAR_SEARCH_BUTTON_ELEMENT = document.getElementById('clear-search-btn');
const BOOKMARKS_WRAPPER_ELEMENT = document.getElementById('bookmarks');
function updateClock() {
let date = new Date();
CLOCK_ELEMENT.innerHTML = date.toLocaleString().replace(',', '');
let progress = ((date.getHours() * 3600) + (date.getMinutes() * 60) + date.getSeconds()) / 864;
DAY_PROGRESS_ELEMENT.style.width = '' + progress + '%';
}
function redirect(newLocation) {
window.location.href = newLocation;
}
function search() {
let searchQuery = SEARCH_BAR_ELEMENT.value;
if (!searchQuery) return;
let isUrl = true;
let url;
try {
url = new URL(searchQuery);
} catch (e) {
isUrl = false;
}
if (isUrl) {
redirect(url.href);
return;
}
let searchEngineShortcut = searchQuery.substr(0, searchQuery.indexOf(' '));
let searchEngine = SEARCH_ENGINES[searchEngineShortcut];
searchQuery = searchQuery.trim();
if (!searchEngine) {
searchEngine = {};
searchEngine.url = DEFAULT_SEARCH_ENGINE;
} else {
searchQuery = searchQuery.substr(searchEngineShortcut.length + 1);
}
redirect(searchEngine.url + encodeURIComponent(searchQuery));
}
function unfocus() {
document.documentElement.focus();
document.activeElement.blur();
}
function addSearchShortcut(shortcut) {
SEARCH_BAR_ELEMENT.value += shortcut + ' ';
SEARCH_BAR_ELEMENT.focus();
}
function showHideShortcuts() {
let show = SEARCH_ENGINES_ELEMENT.style.display == 'none';
SEARCH_ENGINES_ELEMENT.style.display = show ? 'flex' : 'none';
if (show) {
SEARCH_ENGINES_ELEMENT.firstChild.focus();
}
}
function handleGlobalKeydown(ev) {
switch (ev.key) {
case 'Escape':
unfocus();
break;
case 'F1':
showHideShortcuts();
ev.preventDefault();
break;
case 'F2':
CLEAR_SEARCH_BUTTON_ELEMENT.click();
ev.preventDefault();
break;
case 'F3':
SEARCH_BAR_ELEMENT.focus();
ev.preventDefault();
break;
case 'F4':
BOOKMARKS_WRAPPER_ELEMENT.firstChild.lastChild.firstChild.focus();
ev.preventDefault();
break;
}
}
BOOKMARK_CATEGORIES.forEach(bookmarkCategory => {
let categoryContainer = document.createElement('div');
categoryContainer.className = 'bookmark-category';
categoryContainer.style.color = bookmarkCategory.color;
let categoryTitle = document.createElement('div');
categoryTitle.className = 'bookmark-category-title';
categoryTitle.style.color = bookmarkCategory.color;
categoryTitle.innerText = bookmarkCategory.name;
categoryContainer.appendChild(categoryTitle);
let linksWrapper = document.createElement('div');
linksWrapper.className = 'bookmark-links-wrapper';
bookmarkCategory.bookmarks.forEach(bookmark => {
let bookmarkLink = document.createElement('a');
bookmarkLink.className = 'bookmark-link';
bookmarkLink.href = bookmark.url;
bookmarkLink.innerText = bookmark.title;
linksWrapper.appendChild(bookmarkLink);
});
categoryContainer.appendChild(linksWrapper);
BOOKMARKS_WRAPPER_ELEMENT.appendChild(categoryContainer);
});
// Creates buttons for each search engine in SEARCH_ENGINES (declared in search_engines.js)
Object.keys(SEARCH_ENGINES).forEach(searchEngine => {
let searchEngineButton = document.createElement('button');
searchEngineButton.className = 'neon-button';
searchEngineButton.innerText = searchEngine + ': ' + SEARCH_ENGINES[searchEngine].description;
searchEngineButton.onclick = () => {
addSearchShortcut(searchEngine)
};
SEARCH_ENGINES_ELEMENT.appendChild(searchEngineButton);
});
SEARCH_BAR_ELEMENT.addEventListener('keydown', ev => {
if (ev.key == 'Enter') {
search();
}
});
SEARCH_BUTTON_ELEMENT.addEventListener('click', () => {
search();
});
SHORTCUT_BUTTON_ELEMENT.addEventListener('click', () => {
showHideShortcuts();
});
CLEAR_SEARCH_BUTTON_ELEMENT.addEventListener('click', () => {
SEARCH_BAR_ELEMENT.value = '';
});
document.addEventListener('keydown', ev => {
handleGlobalKeydown(ev);
});
updateClock();
window.setInterval(() => {
updateClock();
}, 200);
updateWeather();
window.setInterval(() => {
updateWeather();
}, WEATHER_UPDATE_DELAY_MS);
updateCurrency();
window.setInterval(() => {
updateCurrency();
}, CURRENCY_UPDATE_DELAY_MS);
updateIp();
window.setInterval(() => {
updateIp();
}, IPIFY_UPDATE_DELAY_MS);
SEARCH_BAR_ELEMENT.focus();