-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.js
More file actions
320 lines (271 loc) · 9.89 KB
/
widgets.js
File metadata and controls
320 lines (271 loc) · 9.89 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
// 待办清单管理
let currentTodoDate = new Date().toISOString().split('T')[0];
// 打开待办弹窗
function openTodoModal() {
const modal = document.getElementById('todoModal');
modal.classList.add('active');
document.getElementById('todoDate').value = currentTodoDate;
loadTodos();
}
// 关闭待办弹窗
function closeTodoModal() {
const modal = document.getElementById('todoModal');
modal.classList.remove('active');
}
// 加载待办事项
function loadTodos() {
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
const dateTodos = todos[currentTodoDate] || [];
const todoList = document.getElementById('todoList');
todoList.innerHTML = '';
dateTodos.forEach((todo, index) => {
const item = document.createElement('div');
item.className = `todo-list-item ${todo.completed ? 'completed' : ''}`;
item.innerHTML = `
<input type="checkbox" ${todo.completed ? 'checked' : ''}
onchange="toggleTodo(${index})">
<span>${todo.text}</span>
<button onclick="deleteTodo(${index})">删除</button>
`;
todoList.appendChild(item);
});
}
// 添加待办
function addTodo() {
const input = document.getElementById('todoInput');
const text = input.value.trim();
if (!text) return;
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
if (!todos[currentTodoDate]) {
todos[currentTodoDate] = [];
}
todos[currentTodoDate].push({
text: text,
completed: false
});
localStorage.setItem('todos', JSON.stringify(todos));
input.value = '';
loadTodos();
updateAllWidgets();
}
// 切换待办状态
function toggleTodo(index) {
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
if (todos[currentTodoDate] && todos[currentTodoDate][index]) {
todos[currentTodoDate][index].completed = !todos[currentTodoDate][index].completed;
localStorage.setItem('todos', JSON.stringify(todos));
loadTodos();
updateAllWidgets();
}
}
// 删除待办
function deleteTodo(index) {
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
if (todos[currentTodoDate]) {
todos[currentTodoDate].splice(index, 1);
localStorage.setItem('todos', JSON.stringify(todos));
loadTodos();
updateAllWidgets();
}
}
// 日期选择器变化
document.addEventListener('DOMContentLoaded', () => {
const dateInput = document.getElementById('todoDate');
if (dateInput) {
dateInput.addEventListener('change', (e) => {
currentTodoDate = e.target.value;
loadTodos();
});
}
});
// 创建时钟小组件
function createClockWidget() {
const widget = document.createElement('div');
widget.className = 'widget clock-widget';
const now = new Date();
const time = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
const date = now.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' });
widget.innerHTML = `
<button class="widget-close" onclick="removeWidget(this)">×</button>
<div class="clock-time">${time}</div>
<div class="clock-date">${date}</div>
`;
return widget;
}
// 创建待办小组件
function createTodoWidget() {
const widget = document.createElement('div');
widget.className = 'widget todo-widget';
widget.style.cursor = 'pointer';
const now = new Date();
const day = now.getDate();
const month = now.toLocaleDateString('zh-CN', { month: 'short' });
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
const today = new Date().toISOString().split('T')[0];
const todayTodos = todos[today] || [];
const displayTodos = todayTodos.slice(0, 4);
const todoItems = displayTodos.map(todo =>
`<div class="todo-item ${todo.completed ? 'completed' : ''}">${todo.text}</div>`
).join('');
widget.innerHTML = `
<button class="widget-close" onclick="event.stopPropagation(); removeWidget(this)">×</button>
<div class="todo-left">
<div class="todo-day">${day}</div>
<div class="todo-month">${month}</div>
</div>
<div class="todo-right">
${todoItems || '<div class="todo-item" style="color: #999;">暂无任务</div>'}
</div>
`;
// 添加点击事件
widget.addEventListener('click', (e) => {
if (!e.target.classList.contains('widget-close')) {
openTodoModal();
}
});
return widget;
}
// 创建日历小组件
function createCalendarWidget() {
const widget = document.createElement('div');
widget.className = 'widget calendar-widget';
const now = new Date();
const weekday = now.toLocaleDateString('zh-CN', { weekday: 'short' });
const day = now.getDate();
const monthName = now.toLocaleDateString('zh-CN', { month: 'long' });
// 获取当月日历
const year = now.getFullYear();
const month = now.getMonth();
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const daysInPrevMonth = new Date(year, month, 0).getDate();
let calendarDays = '';
// 上个月的日期
for (let i = firstDay - 1; i >= 0; i--) {
calendarDays += `<span class="other-month">${daysInPrevMonth - i}</span>`;
}
// 当月日期
for (let i = 1; i <= daysInMonth; i++) {
const isToday = i === day ? 'today' : '';
calendarDays += `<span class="${isToday}">${i}</span>`;
}
// 下个月的日期(补齐)
const totalCells = firstDay + daysInMonth;
const remainingCells = totalCells % 7 === 0 ? 0 : 7 - (totalCells % 7);
for (let i = 1; i <= remainingCells; i++) {
calendarDays += `<span class="other-month">${i}</span>`;
}
widget.innerHTML = `
<button class="widget-close" onclick="removeWidget(this)">×</button>
<div class="calendar-left">
<div class="calendar-weekday">${weekday}</div>
<div class="calendar-day">${day}</div>
</div>
<div class="calendar-right">
<div class="calendar-header">${monthName}</div>
<div class="calendar-weekdays">
<span>日</span><span>一</span><span>二</span><span>三</span>
<span>四</span><span>五</span><span>六</span>
</div>
<div class="calendar-days">
${calendarDays}
</div>
</div>
`;
return widget;
}
// 移除小组件
function removeWidget(btn) {
const slot = btn.closest('.icon-slot');
if (!slot) return;
const location = slot.dataset.location;
const position = parseInt(slot.dataset.position);
const widgets = JSON.parse(localStorage.getItem('widgets') || '[]');
const index = widgets.findIndex(w => w.location === location && w.position === position);
if (index !== -1) {
widgets.splice(index, 1);
localStorage.setItem('widgets', JSON.stringify(widgets));
// 重新渲染
if (window.renderApps) {
window.renderApps();
}
}
}
// 更新所有小组件
function updateAllWidgets() {
document.querySelectorAll('.todo-widget').forEach(widget => {
const now = new Date();
const day = now.getDate();
const month = now.toLocaleDateString('zh-CN', { month: 'short' });
const todos = JSON.parse(localStorage.getItem('todos') || '{}');
const today = new Date().toISOString().split('T')[0];
const todayTodos = todos[today] || [];
const displayTodos = todayTodos.slice(0, 4);
const todoItems = displayTodos.map(todo =>
`<div class="todo-item ${todo.completed ? 'completed' : ''}">${todo.text}</div>`
).join('');
const closeBtn = widget.querySelector('.widget-close');
widget.innerHTML = `
<div class="todo-left">
<div class="todo-day">${day}</div>
<div class="todo-month">${month}</div>
</div>
<div class="todo-right">
${todoItems || '<div class="todo-item" style="color: #999;">暂无任务</div>'}
</div>
`;
widget.appendChild(closeBtn);
// 重新添加点击事件
widget.addEventListener('click', (e) => {
if (!e.target.classList.contains('widget-close')) {
openTodoModal();
}
});
});
}
// 内置预设小组件方案(供 app.js 和 appearance.js 共用)
const builtinWidgetSchemes = {
clock: {
'透明居中': `/* 透明居中时钟样式 */
.clock-widget {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
padding: 10px 20px;
background: transparent;
border: none;
box-shadow: none;
backdrop-filter: none;
-webkit-backdrop-filter: none;
}
.clock-time {
font-size: 52px;
font-weight: 700;
color: #fff;
line-height: 1;
text-shadow: 0 2px 8px rgba(0,0,0,0.3);
letter-spacing: -1px;
}
.clock-date {
font-size: 13px;
color: rgba(255,255,255,0.85);
margin-top: 6px;
font-weight: 500;
text-shadow: 0 1px 4px rgba(0,0,0,0.3);
}`
}
};
// 每分钟更新时钟
setInterval(() => {
document.querySelectorAll('.clock-widget').forEach(widget => {
const now = new Date();
const time = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
const date = now.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' });
const timeEl = widget.querySelector('.clock-time');
const dateEl = widget.querySelector('.clock-date');
if (timeEl) timeEl.textContent = time;
if (dateEl) dateEl.textContent = date;
});
}, 60000);