-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
361 lines (315 loc) · 13.3 KB
/
script.js
File metadata and controls
361 lines (315 loc) · 13.3 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Settings management
const defaultSettings = {
theme: 'light',
searchEngine: 'duckduckgo',
weatherUnit: 'fahrenheit',
timeFormat: '12',
categoriesPerRow: 'auto', // Can be 'auto' or number 1-10
displayMode: 'icon' // Can be 'icon', 'text', or 'balanced'
};
// Search engines configuration
const searchEngines = {
google: {
name: 'Google',
url: 'https://www.google.com/search?q=',
placeholder: 'Search Google or type a URL...'
},
bing: {
name: 'Bing',
url: 'https://www.bing.com/search?q=',
placeholder: 'Search Bing or type a URL...'
},
duckduckgo: {
name: 'DuckDuckGo',
url: 'https://duckduckgo.com/?q=',
placeholder: 'Search DuckDuckGo or type a URL...'
}
};
// Load links from JSON file
async function loadLinksFromFile() {
try {
const response = await fetch('./links.json?t=' + Date.now());
const links = await response.json();
return links;
} catch (error) {
console.error('Error loading links.json:', error);
return {};
}
}
// Load and save settings
function loadSettings() {
const saved = localStorage.getItem('homePageSettings');
return saved ? { ...defaultSettings, ...JSON.parse(saved) } : defaultSettings;
}
function saveSettings(settings) {
localStorage.setItem('homePageSettings', JSON.stringify(settings));
applySettings(settings);
}
function applySettings(settings) {
// Apply theme
document.documentElement.setAttribute('data-theme', settings.theme);
// Apply display mode
document.documentElement.setAttribute('data-display-mode', settings.displayMode);
// Apply categories per row - handle auto sizing
if (settings.categoriesPerRow === 'auto') {
// Auto sizing based on screen width
const screenWidth = window.innerWidth;
let columns;
if (screenWidth < 768) {
columns = 1; // Mobile: always 1 column
} else if (screenWidth < 1024) {
columns = 2; // Tablet: 2 columns
} else if (screenWidth < 1440) {
columns = 3; // Desktop: 3 columns
} else {
columns = 4; // Large desktop: 4 columns
}
document.documentElement.style.setProperty('--categories-per-row', columns);
} else {
document.documentElement.style.setProperty('--categories-per-row', settings.categoriesPerRow);
}
// Update search bar placeholder
const searchInput = document.getElementById('searchInput');
const engine = searchEngines[settings.searchEngine];
if (searchInput && engine) {
searchInput.placeholder = engine.placeholder;
}
}
// Load bookmark sections from JSON file
async function loadBookmarkSections() {
try {
// Show loading state
const grids = document.querySelectorAll('[id$="Grid"]');
grids.forEach(grid => {
grid.innerHTML = '<div class="loading-placeholder">Loading...</div>';
});
const bookmarkSections = await loadLinksFromFile();
Object.keys(bookmarkSections).forEach(sectionKey => {
const gridElement = document.getElementById(sectionKey + 'Grid');
if (gridElement && bookmarkSections[sectionKey]) {
const links = bookmarkSections[sectionKey];
const htmlContent = links.map(link => `
<a href="${link.url}" target="_blank" rel="noopener noreferrer" class="bookmark-tile" data-description="${link.description || link.name}">
<i class="${link.icon}"></i>
<span>${link.name}</span>
</a>
`).join('');
gridElement.innerHTML = htmlContent;
} else if (gridElement) {
gridElement.innerHTML = '<div class="error-placeholder">Failed to load links</div>';
}
});
} catch (error) {
console.error('Error loading bookmark sections:', error);
// Show error state
const grids = document.querySelectorAll('[id$="Grid"]');
grids.forEach(grid => {
grid.innerHTML = '<div class="error-placeholder">Failed to load links</div>';
});
}
}
// Settings sidebar functions
function toggleSettings() {
const sidebar = document.getElementById('settingsSidebar');
const arrow = document.getElementById('settingsArrow');
const toggle = document.querySelector('.settings-toggle');
sidebar.classList.toggle('open');
const isOpen = sidebar.classList.contains('open');
// Update ARIA attributes
toggle.setAttribute('aria-expanded', isOpen);
sidebar.setAttribute('aria-hidden', !isOpen);
if (isOpen) {
arrow.className = 'fas fa-chevron-right';
loadSettingsIntoSidebar();
// Focus management
setTimeout(() => {
const firstFocusable = sidebar.querySelector('select, button');
if (firstFocusable) firstFocusable.focus();
}, 150);
} else {
arrow.className = 'fas fa-chevron-left';
toggle.focus();
}
}
function loadSettingsIntoSidebar() {
const settings = loadSettings();
// Populate current settings
document.getElementById('themeSelect').value = settings.theme;
document.getElementById('searchEngineSelect').value = settings.searchEngine;
document.getElementById('weatherUnitSelect').value = settings.weatherUnit;
document.getElementById('timeFormatSelect').value = settings.timeFormat;
document.getElementById('categoriesPerRowSelect').value = settings.categoriesPerRow;
document.getElementById('displayModeSelect').value = settings.displayMode;
}
// Search functionality
function handleSearch(event) {
if (event.key === 'Enter') {
const query = event.target.value.trim();
if (query) {
const settings = loadSettings();
const engine = searchEngines[settings.searchEngine];
// Improved URL detection
const urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i;
const isUrl = urlRegex.test(query) || query.includes('localhost') || query.includes('127.0.0.1');
if (isUrl) {
const url = query.startsWith('http') ? query : 'https://' + query;
window.open(url, '_blank', 'noopener,noreferrer');
} else {
// Search using selected engine
window.open(engine.url + encodeURIComponent(query), '_blank', 'noopener,noreferrer');
}
event.target.value = '';
}
}
}
// Time display
function updateTime() {
const now = new Date();
const settings = loadSettings();
const timeElement = document.getElementById('time');
const dateElement = document.getElementById('date');
if (timeElement) {
const options = {
hour: '2-digit',
minute: '2-digit',
hour12: settings.timeFormat === '12'
};
timeElement.textContent = now.toLocaleTimeString('en-US', options);
}
if (dateElement) {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
dateElement.textContent = now.toLocaleDateString('en-US', options);
}
}
// Weather functionality
async function updateWeather() {
const settings = loadSettings();
const temperatureElement = document.getElementById('temperature');
const locationElement = document.getElementById('location');
const weatherIcon = document.getElementById('weatherIcon');
if (!temperatureElement || !locationElement || !weatherIcon) return;
try {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(async (position) => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
try {
// Using mock weather data for demo
const mockWeather = {
temperature: 22,
location: 'Your Location',
condition: 'sunny'
};
const temp = settings.weatherUnit === 'fahrenheit'
? Math.round(mockWeather.temperature * 9/5 + 32)
: mockWeather.temperature;
const unit = settings.weatherUnit === 'fahrenheit' ? '°F' : '°C';
temperatureElement.textContent = `${temp}${unit}`;
locationElement.textContent = mockWeather.location;
// Update weather icon
switch (mockWeather.condition) {
case 'sunny':
weatherIcon.className = 'fas fa-sun';
break;
case 'cloudy':
weatherIcon.className = 'fas fa-cloud';
break;
case 'rain':
weatherIcon.className = 'fas fa-cloud-rain';
break;
case 'snow':
weatherIcon.className = 'fas fa-snowflake';
break;
default:
weatherIcon.className = 'fas fa-cloud-sun';
}
} catch (error) {
console.error('Weather API error:', error);
showWeatherError(temperatureElement, locationElement, weatherIcon, 'Weather unavailable');
}
}, (error) => {
// Location access denied or error
console.error('Geolocation error:', error);
showWeatherError(temperatureElement, locationElement, weatherIcon,
error.code === 1 ? 'Location access denied' : 'Location unavailable');
});
} else {
showWeatherError(temperatureElement, locationElement, weatherIcon, 'Geolocation not supported');
}
} catch (error) {
console.error('Weather error:', error);
showWeatherError(temperatureElement, locationElement, weatherIcon, 'Weather service unavailable');
}
}
function showWeatherError(tempEl, locEl, iconEl, message) {
if (tempEl) tempEl.textContent = '--°';
if (locEl) locEl.textContent = message;
if (iconEl) iconEl.className = 'fas fa-exclamation-triangle';
}
// Save settings from sidebar (called on change)
function saveSettingsFromSidebar() {
const settings = {
theme: document.getElementById('themeSelect').value,
searchEngine: document.getElementById('searchEngineSelect').value,
weatherUnit: document.getElementById('weatherUnitSelect').value,
timeFormat: document.getElementById('timeFormatSelect').value,
categoriesPerRow: document.getElementById('categoriesPerRowSelect').value,
displayMode: document.getElementById('displayModeSelect').value
};
localStorage.setItem('homePageSettings', JSON.stringify(settings));
applySettings(settings);
// Refresh displays that depend on settings
updateTime();
updateWeather();
}
// Initialize everything when page loads
document.addEventListener('DOMContentLoaded', async function() {
const settings = loadSettings();
applySettings(settings);
await loadBookmarkSections();
updateTime();
updateWeather();
// Update time every minute
setInterval(updateTime, 60000);
// Update weather every 30 minutes
setInterval(updateWeather, 1800000);
// Add search functionality
const searchInput = document.getElementById('searchInput');
const searchBtn = document.getElementById('searchBtn');
if (searchInput) {
searchInput.addEventListener('keypress', handleSearch);
}
if (searchBtn) {
searchBtn.addEventListener('click', function() {
const event = { key: 'Enter', target: searchInput };
handleSearch(event);
});
}
// Keyboard shortcuts
document.addEventListener('keydown', function(event) {
// Ctrl/Cmd + K to focus search
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault();
if (searchInput) searchInput.focus();
}
// Escape to close settings
if (event.key === 'Escape') {
const sidebar = document.getElementById('settingsSidebar');
if (sidebar && sidebar.classList.contains('open')) {
toggleSettings();
}
}
});
// Handle window resize for auto layout
window.addEventListener('resize', function() {
const currentSettings = loadSettings();
if (currentSettings.categoriesPerRow === 'auto') {
applySettings(currentSettings);
}
});
});