-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
245 lines (202 loc) · 6.7 KB
/
script.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
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
window.onload = function () {
displayClock();
displayDate();
quoteLiveTile();
}
document.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
const tiles = document.querySelectorAll('.tile');
const customMenu = document.getElementById('custom-menu');
tiles.forEach(tile => {
tile.addEventListener('contextmenu', (e) => {
e.preventDefault();
const menuX = e.pageX + 'px';
const menuY = e.pageY + 'px';
customMenu.style.left = menuX;
customMenu.style.top = menuY;
customMenu.style.display = 'block';
});
document.addEventListener('click', () => {
customMenu.style.display = 'none';
});
});
// For touch devices (long-press detection)
tiles.forEach(tile => {
let pressTimer;
tile.addEventListener('touchstart', (e) => {
pressTimer = setTimeout(() => {
const menuX = e.touches[0].pageX + 'px';
const menuY = e.touches[0].pageY + 'px';
customMenu.style.left = menuX;
customMenu.style.top = menuY;
customMenu.style.display = 'block';
}, 2000);
});
tile.addEventListener('touchend', () => {
clearTimeout(pressTimer);
});
});
function displayClock(){
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
if(hours < 10) {
hours = '0' + hours;
}
if(minutes < 10) {
minutes = '0' + minutes;
}
var display = hours + ':' + minutes;
var clock = document.getElementById('clock');
clock.textContent = display;
setTimeout(displayClock, 1000);
}
function displayDate() {
var now = new Date();
var date = now.getDate();
var calendar = document.getElementById('calendar');
calendar.textContent = date;
}
function toggleAllApps() {
const allApps = document.getElementById('app-center');
const startScreen = document.getElementById('start-screen');
startScreen.classList.toggle('hidden');
allApps.classList.toggle('open');
}
let touchstartX = 0;
let touchendX = 0;
const swipeThreshold = 50; // Minimum swipe distance in pixels
const startScreen = document.getElementById('start-screen');
const allApps = document.getElementById('app-center');
startScreen.addEventListener('touchstart', (e) => {
touchstartX = e.changedTouches[0].screenX;
});
startScreen.addEventListener('touchend', (e) => {
touchendX = e.changedTouches[0].screenX;
handleGesture(0);
});
allApps.addEventListener('touchstart', (e) => {
touchstartX = e.changedTouches[0].screenX;
});
allApps.addEventListener('touchend', (e) => {
touchendX = e.changedTouches[0].screenX;
handleGesture(1);
});
function handleGesture(pointer) {
const swipeDistance = touchendX - touchstartX;
if (pointer === 1) {
if (swipeDistance > swipeThreshold) {
toggleAllApps();
}
} else if (pointer === 0) {
if (swipeDistance < -swipeThreshold) {
toggleAllApps();
}
}
}
async function quoteLiveTile() {
try {
const response = await fetch('https://yurippe.vercel.app/api/quotes?show=violet%20evergarden&random=1');
if (!response.ok) {
throw new Error(`Error status: ${response.status}`);
}
const quote = await response.json();
// console.log(quote);
let liveTile = document.getElementById('live-tile');
liveTile.textContent = quote[0].quote;
} catch (error) {
console.error(error.message);
}
}
function flipTile() {
const tile = document.getElementById('quote-tile');
tile.classList.toggle('flipped');
setTimeout(() => {
quoteLiveTile();
}, 1000);
}
function applyTileColor(tileColor) {
const defaultTileColor = '#1BA1E2';
if (tileColor === '#000000') {
alert("Can't use pure black as accent colour");
return false; // Indicate failure
} else {
const isValidColor = /^#([0-9A-F]{3}){1,2}$/i.test(tileColor) || /^rgba?\(\s*(\d{1,3}\s*,\s*){2,3}\d{1,3}\s*(,\s*(0(\.\d+)?|1(\.0+)?))?\s*\)$/i.test(tileColor);
if (isValidColor) {
document.documentElement.style.setProperty('--accent', tileColor);
localStorage.setItem('Windows-Phone-Accent-Colour', tileColor);
return true;
} else {
alert('Invalid color value. Please enter a valid hex (#rrggbb format) or rgba color (rr,gg,bb,aa format)');
return false;
}
}
}
function applyBackgroundImage(backgroundImageUrl) {
const defaultBackground = '#000000';
const canvas = document.getElementById('blur-canvas');
const ctx = canvas.getContext('2d');
if (backgroundImageUrl) {
const img = new Image();
img.crossOrigin = "anonymous"; // Allow cross-origin image loading if needed
img.src = backgroundImageUrl;
img.onload = function() {
resizeCanvas();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.filter = 'blur(12px)';
for (let y = 0; y < canvas.height; y += img.height) {
ctx.drawImage(img, 0, y, canvas.width, img.height);
}
localStorage.setItem('Windows-Phone-Background', backgroundImageUrl);
};
img.onerror = function() {
alert('Failed to load image. Please check the URL and try again.');
document.body.style.backgroundColor = defaultBackground;
};
} else {
document.body.style.backgroundColor = defaultBackground;
}
}
function applySettings() {
const selectedColor = document.querySelector('input[name="tile-color"]:checked').value;
const isTileColorValid = applyTileColor(selectedColor);
if (isTileColorValid) {
const backgroundImageUrlInput = document.getElementById('background-image-url').value;
applyBackgroundImage(backgroundImageUrlInput);
}
}
function loadSettings() {
const savedTileColor = localStorage.getItem('Windows-Phone-Accent-Colour') || '#1BA1E2';
const savedBackgroundImage = localStorage.getItem('Windows-Phone-Background') || '#000000';
document.documentElement.style.setProperty('--accent', savedTileColor);
if (savedBackgroundImage.startsWith('http')) {
applyBackgroundImage(savedBackgroundImage); // Use the applyBackgroundImage function to load the saved image
} else {
document.body.style.backgroundColor = savedBackgroundImage;
}
}
document.getElementById('apply-settings-button').addEventListener('click', applySettings);
document.addEventListener('DOMContentLoaded', () => {
loadSettings();
resizeCanvas();
});
document.getElementById('background-image-url').addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
applySettings();
}
});
function clearStorage() {
localStorage.removeItem('Windows-Phone-Accent-Colour');
localStorage.removeItem('Windows-Phone-Background');
loadSettings();
}
function resizeCanvas() {
const canvas = document.getElementById('blur-canvas');
if (canvas) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight + window.innerHeight;
}
}
window.addEventListener('resize', resizeCanvas);
setInterval(flipTile, 10000);