-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
442 lines (366 loc) · 16.5 KB
/
script.js
File metadata and controls
442 lines (366 loc) · 16.5 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
class SchedulerApp {
constructor() {
this.date = new Date();
this.selectedDate = new Date(); // Currently selected date in UI
this.events = JSON.parse(localStorage.getItem('ghibli_events')) || {};
this.healthData = JSON.parse(localStorage.getItem('ghibli_health')) || {};
// --- DOM Elements ---
this.grid = document.getElementById('calendar-grid');
this.monthLabel = document.getElementById('current-month');
this.prevBtn = document.getElementById('prev-month');
this.nextBtn = document.getElementById('next-month');
this.yearSelect = document.getElementById('year-select');
this.headerWareki = document.getElementById('header-wareki');
this.eventList = document.getElementById('event-list');
this.selectedDateLabel = document.getElementById('selected-date-label');
this.addEventBtn = document.getElementById('add-event-btn');
this.modal = document.getElementById('event-modal');
this.modalForm = document.getElementById('event-form');
this.closeModalBtn = document.getElementById('close-modal');
this.voiceBtn = document.getElementById('voice-btn');
this.voiceStatus = document.getElementById('voice-status');
// Stamp Buttons
this.stampBtns = document.querySelectorAll('.stamp-btn');
// Hanko Buttons
this.hankoMedicine = document.getElementById('hanko-medicine');
this.hankoTaiso = document.getElementById('hanko-taiso');
// Tools
this.zoomBtn = document.getElementById('zoom-btn');
this.printBtn = document.getElementById('print-btn');
// Guide
this.guideText = document.getElementById('guide-text');
this.init();
}
init() {
this.initYearSelect();
this.renderCalendar();
this.renderEventList();
this.updateGuide();
this.initLocationGuide();
// Listeners
this.prevBtn.addEventListener('click', () => this.changeMonth(-1));
this.nextBtn.addEventListener('click', () => this.changeMonth(1));
// Header Controls
this.yearSelect.addEventListener('change', (e) => this.changeYear(e.target.value));
this.zoomBtn.addEventListener('click', () => document.body.classList.toggle('large-text'));
this.printBtn.addEventListener('click', () => window.print());
// Modal
this.addEventBtn.addEventListener('click', () => this.openModal());
this.closeModalBtn.addEventListener('click', () => this.closeModal());
this.modalForm.addEventListener('submit', (e) => this.handleFormSubmit(e));
this.modal.addEventListener('click', (e) => {
if (e.target === this.modal) this.closeModal();
});
// Stamp Actions
this.stampBtns.forEach(btn => {
btn.addEventListener('click', () => {
const title = btn.dataset.label;
const icon = btn.dataset.stamp;
this.quickAddEvent(`${icon} ${title}`);
});
});
// Hanko Actions
this.hankoMedicine.addEventListener('click', () => this.toggleHealth('medicine'));
this.hankoTaiso.addEventListener('click', () => this.toggleHealth('taiso'));
// Voice Input
if ('webkitSpeechRecognition' in window) {
this.recognition = new webkitSpeechRecognition();
this.recognition.lang = 'ja-JP';
this.recognition.onstart = () => {
this.voiceStatus.classList.remove('hidden');
this.voiceBtn.classList.add('bg-red-500', 'animate-pulse');
};
this.recognition.onend = () => {
this.voiceStatus.classList.add('hidden');
this.voiceBtn.classList.remove('bg-red-500', 'animate-pulse');
};
this.recognition.onresult = (event) => {
const text = event.results[0][0].transcript;
document.getElementById('event-title').value = text;
};
this.voiceBtn.addEventListener('click', () => {
this.recognition.start();
});
} else {
this.voiceBtn.style.display = 'none'; // Hide if not supported
}
}
initYearSelect() {
const currentYear = new Date().getFullYear();
// Range: -5 to +5 years
for (let y = currentYear - 5; y <= currentYear + 5; y++) {
const opt = document.createElement('option');
opt.value = y;
opt.text = `${y}年`;
if (y === currentYear) opt.selected = true;
this.yearSelect.appendChild(opt);
}
}
getDateKey(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
// --- Japanese Calendar Logic ---
getWareki(year) {
// Simple Reiwa check
const reiwaStart = 2019;
if (year >= reiwaStart) {
const ry = year - reiwaStart + 1;
return ry === 1 ? '令和元年' : `令和${ry}年`;
}
return ''; // Keep simple for now
}
// Rokuyo removed as per request
// Solar Term removed as per request
// ----------------------------
renderCalendar() {
this.grid.innerHTML = '';
const year = this.date.getFullYear();
const month = this.date.getMonth();
// Update Headers
this.monthLabel.innerText = `${year}年 ${month + 1}月`;
// Print title
document.getElementById('print-title').innerText = `${year}年 ${month + 1}月`;
this.headerWareki.innerText = this.getWareki(year);
this.yearSelect.value = year;
// Days logic
const firstDayIndex = new Date(year, month, 1).getDay();
const lastDay = new Date(year, month + 1, 0).getDate();
const prevLastDay = new Date(year, month, 0).getDate();
const todayKey = this.getDateKey(new Date());
const selectedKey = this.getDateKey(this.selectedDate);
// Previous Month Padding
for (let i = firstDayIndex; i > 0; i--) {
const dayDiv = document.createElement('div');
dayDiv.classList.add('calendar-day', 'other-month');
dayDiv.innerText = prevLastDay - i + 1;
this.grid.appendChild(dayDiv);
}
// Current Days
for (let i = 1; i <= lastDay; i++) {
const currentLoopDate = new Date(year, month, i);
const key = this.getDateKey(currentLoopDate);
const dayDiv = document.createElement('div');
dayDiv.classList.add('calendar-day');
// Inner Layout
// Date Number
const num = document.createElement('div');
num.innerText = i;
num.className = "text-lg font-bold leading-none";
dayDiv.appendChild(num);
// Info Row (Removed Rokuyo / Solar Terms)
// Events Dots
const events = this.events[key] || [];
if (events.length > 0) {
const dotContainer = document.createElement('div');
dotContainer.className = "event-dot-container";
// Max 3 dots
for (let k = 0; k < Math.min(events.length, 3); k++) {
const dot = document.createElement('div');
dot.className = "event-dot";
dotContainer.appendChild(dot);
}
dayDiv.appendChild(dotContainer);
// For Print: Add Text
events.forEach(ev => {
const printText = document.createElement('div');
printText.className = "print-event-text";
printText.innerText = `• ${ev.title}`;
dayDiv.appendChild(printText);
});
}
if (key === todayKey) dayDiv.classList.add('today');
if (key === selectedKey) dayDiv.classList.add('selected');
dayDiv.addEventListener('click', () => {
this.selectedDate = new Date(year, month, i);
this.renderCalendar();
this.renderEventList();
this.updateGuide();
});
this.grid.appendChild(dayDiv);
}
// Fill remaining
const totalCells = firstDayIndex + lastDay;
const nextDays = (totalCells > 35) ? 42 - totalCells : 35 - totalCells;
for (let i = 1; i <= nextDays; i++) {
const dayDiv = document.createElement('div');
dayDiv.classList.add('calendar-day', 'other-month');
dayDiv.innerText = i;
this.grid.appendChild(dayDiv);
}
}
renderEventList() {
const days = ['日', '月', '火', '水', '木', '金', '土'];
const m = this.selectedDate.getMonth() + 1;
const d = this.selectedDate.getDate();
const w = days[this.selectedDate.getDay()];
this.selectedDateLabel.innerText = `${m}月${d}日 (${w})`;
// Update Hanko State
const key = this.getDateKey(this.selectedDate);
const health = this.healthData[key] || {};
this.updateHankoUI(this.hankoMedicine, health.medicine, '済');
this.updateHankoUI(this.hankoTaiso, health.taiso, '完');
// Render List
this.eventList.innerHTML = '';
const dayEvents = this.events[key] || [];
if (dayEvents.length === 0) {
this.eventList.innerHTML = `
<div class="text-center text-gray-400 py-8 font-bold opacity-50">
<i class="fas fa-mug-hot text-4xl mb-2"></i><br>
予定はありません・ゆっくりしましょう
</div>
`;
return;
}
dayEvents.sort((a, b) => a.time.localeCompare(b.time));
dayEvents.forEach((ev, index) => {
const el = document.createElement('div');
el.className = 'bg-white p-3 rounded-xl shadow-paper border-l-8 border-forest-green flex justify-between items-center transform transition hover:scale-[1.02]';
el.innerHTML = `
<div class="flex items-center gap-3">
<div class="text-lg font-bold text-forest-green bg-forest-cream px-2 rounded">${ev.time}</div>
<div class="font-bold text-lg text-forest-dark">${ev.title}</div>
</div>
<button class="text-red-300 hover:text-red-500 p-2 transition" data-index="${index}">
<i class="fas fa-trash"></i>
</button>
`;
el.querySelector('button').addEventListener('click', (e) => {
e.stopPropagation();
if (confirm('けしますか?')) {
this.deleteEvent(key, index);
}
});
this.eventList.appendChild(el);
});
}
updateHankoUI(el, isChecked, label) {
// Clear old stamp
const old = el.querySelector('.hanko-stamp');
if (old) old.remove();
if (isChecked) {
el.classList.add('checked');
const stamp = document.createElement('div');
stamp.className = 'hanko-stamp';
stamp.innerText = label;
el.querySelector('div').appendChild(stamp);
} else {
el.classList.remove('checked');
}
}
// --- Actions ---
initLocationGuide() {
const btn = document.getElementById('guide-location-btn');
if (!btn) return;
btn.addEventListener('click', () => {
if (!navigator.geolocation) {
alert("お使いのブラウザでは位置機能が使えません。");
return;
}
this.guideText.innerText = "ふむふむ... あなたの場所を確認しています...";
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
this.showLocationRecommendation(latitude, longitude);
},
(error) => {
console.error(error);
this.guideText.innerText = "場所がわかりませんでした。森が深いようです。";
}
);
});
}
showLocationRecommendation(lat, lon) {
// Mock Recommendation Logic
// In a real app, this would call the Google Places API or similar.
// Here, we provide "Atmospheric" recommendations based on pseudo-random logic.
const spots = [
"近くに「木漏れ日カフェ」という素敵な場所がありそうです。",
"少し歩くと、静かな公園が見つかるかもしれません。",
"この辺りには、美味しいパン屋さんがある予感がします。",
"風が気持ちいいですね。近くの川沿いを散歩してみては?",
"「星屑堂」という雑貨屋さんが近くにあるみたいですよ。",
"古い図書館で本を読むのも良い午後になりそうです。"
];
const randomSpot = spots[Math.floor(Math.random() * spots.length)];
this.guideText.innerText = `ふぉっふぉ。${randomSpot}`;
}
quickAddEvent(title) {
const time = "10:00";
const key = this.getDateKey(this.selectedDate);
if (!this.events[key]) this.events[key] = [];
this.events[key].push({ title, time });
this.saveEvents();
this.renderCalendar();
this.renderEventList();
alert(`「${title}」を追加しました`);
}
toggleHealth(type) {
const key = this.getDateKey(this.selectedDate);
if (!this.healthData[key]) this.healthData[key] = {};
this.healthData[key][type] = !this.healthData[key][type];
localStorage.setItem('ghibli_health', JSON.stringify(this.healthData));
this.renderEventList(); // Re-render ui
}
changeMonth(delta) {
this.date.setMonth(this.date.getMonth() + delta);
this.renderCalendar();
}
changeYear(year) {
this.date.setFullYear(parseInt(year));
this.renderCalendar();
}
updateGuide() {
// Guide Logic (Updated: Removed Rokuyo and Solar Terms)
const d = this.selectedDate.getDate();
const msgs = [
"お茶でも飲んで一息入れましょう。",
"今日の体調はいかがですか?",
"散歩日和かもしれません。",
"忘れた予定はありませんか?",
"深呼吸して、リラックス。",
"ゆっくり歩くのも、いい運動です。",
"夜は暖かくして寝ましょうね。"
];
// Simple random-ish message based on date
const msg = msgs[d % msgs.length];
this.guideText.innerText = msg;
}
// Modal & CRUD
openModal() {
this.modal.classList.remove('hidden');
const now = new Date();
const nextHour = new Date(now.setHours(now.getHours() + 1, 0));
document.getElementById('event-time').value = nextHour.toTimeString().substring(0, 5);
document.getElementById('event-title').focus();
}
closeModal() {
this.modal.classList.add('hidden');
this.modalForm.reset();
}
handleFormSubmit(e) {
e.preventDefault();
const title = document.getElementById('event-title').value;
const time = document.getElementById('event-time').value;
const key = this.getDateKey(this.selectedDate);
if (!this.events[key]) this.events[key] = [];
this.events[key].push({ title, time });
this.saveEvents();
this.closeModal();
this.renderCalendar();
this.renderEventList();
}
deleteEvent(key, index) {
this.events[key].splice(index, 1);
if (this.events[key].length === 0) delete this.events[key];
this.saveEvents();
this.renderCalendar();
this.renderEventList();
}
saveEvents() {
localStorage.setItem('ghibli_events', JSON.stringify(this.events));
}
}
// Init
document.addEventListener('DOMContentLoaded', () => {
new SchedulerApp();
});