-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.html
205 lines (204 loc) · 10.3 KB
/
control.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Habit Tracker</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('habitData', {
monthlyRows: {},
load() {
let data = JSON.parse(localStorage.getItem('habitData')) || {};
this.monthlyRows = data.monthlyRows || {};
},
save() {
localStorage.setItem('habitData', JSON.stringify({
monthlyRows: this.monthlyRows
}));
},
getRowsForMonth(year, month) {
const key = `${year}-${month}`;
return this.monthlyRows[key] || [];
},
setRowsForMonth(year, month, rows) {
const key = `${year}-${month}`;
this.monthlyRows[key] = rows;
this.save();
},
});
Alpine.data('habitTracker', () => ({
nextId: 1,
daysInMonth: [],
currentMonth: new Date().getMonth(),
currentYear: new Date().getFullYear(),
draggedRow: null,
init() {
this.calculateDaysInMonth();
Alpine.store('habitData').load();
if (!Alpine.store('habitData').getRowsForMonth(this.currentYear, this.currentMonth).length) {
this.addRow(true, 'Good Habits');
this.addRow(false, 'Read', 'Good Habits');
this.addRow(false, 'Workout', 'Good Habits');
this.addRow(true, 'Bad Habits');
this.addRow(false, 'Smoking', 'Bad Habits');
this.addRow(true, 'Mood');
}
},
calculateDaysInMonth() {
this.daysInMonth = Array.from({ length: new Date(this.currentYear, this.currentMonth + 1, 0).getDate() }, (_, i) => i + 1);
},
addRow(isCategory = false, name = '', category = null) {
const rows = this.getCurrentMonthRows();
const newRow = {
id: this.nextId++,
name,
checkedDays: [],
isCategory,
category,
emoji: isCategory && name === 'Mood' ? Object.fromEntries(this.daysInMonth.map(day => [day, ''])) : null
};
rows.push(newRow);
this.saveCurrentMonthRows(rows);
},
toggleHabit(row, day) {
if (row.checkedDays.includes(day)) {
row.checkedDays = row.checkedDays.filter(d => d !== day);
} else {
row.checkedDays.push(day);
}
this.saveCurrentMonthRows(this.getCurrentMonthRows());
},
isHabitChecked(row, day) {
return row.checkedDays.includes(day);
},
removeRow(id) {
const rows = this.getCurrentMonthRows().filter(row => row.id !== id);
this.saveCurrentMonthRows(rows);
},
prevMonth() {
if (this.currentMonth === 0) {
this.currentMonth = 11;
this.currentYear--;
} else {
this.currentMonth--;
}
this.calculateDaysInMonth();
},
nextMonth() {
if (this.currentMonth === 11) {
this.currentMonth = 0;
this.currentYear++;
} else {
this.currentMonth++;
}
this.calculateDaysInMonth();
},
getMonthName() {
return new Date(this.currentYear, this.currentMonth).toLocaleString('default', { month: 'long' });
},
getCurrentMonthRows() {
return Alpine.store('habitData').getRowsForMonth(this.currentYear, this.currentMonth);
},
saveCurrentMonthRows(rows) {
Alpine.store('habitData').setRowsForMonth(this.currentYear, this.currentMonth, rows);
},
dragStart(row) {
this.draggedRow = row;
},
dragOver(event) {
event.preventDefault();
},
drop(targetRow) {
const rows = this.getCurrentMonthRows();
const draggedRowIndex = rows.findIndex(row => row.id === this.draggedRow.id);
const targetRowIndex = rows.findIndex(row => row.id === targetRow.id);
rows.splice(targetRowIndex, 0, rows.splice(draggedRowIndex, 1)[0]);
this.saveCurrentMonthRows(rows);
this.draggedRow = null;
},
saveState() {
Alpine.store('habitData').save();
},
}));
});
</script>
<script src="https://unpkg.com/alpinejs" defer></script>
<link rel="stylesheet" type="text/css" href="webfonts/ywft-hannah-wide.css">
<link rel="stylesheet" type="text/css" href="webfonts/ywft-hannah-regular.css">
<link rel="stylesheet" type="text/css" href="webfonts/ywft-hannah-narrow.css">
<link rel="stylesheet" type="text/css" href="bujo.css">
</head>
<body class="bg-gray-100 bujo-bg" x-data="habitTracker" x-init="init()">
<div class="container mx-auto p-4">
<div class="bg-white p-6 rounded-lg shadow-lg bujo-bg">
<h1 class="text-2xl font-bold mb-4 bujo-text">Habit Tracker</h1>
<div class="flex justify-between mb-4">
<div>
<button class="bujo-btn" @click="addRow()">Add Row</button>
<button class="bujo-btn" @click="addRow(true)">Add Category</button>
<!-- Button to add Mood Category Row -->
<button class="bujo-btn" @click="addRow(true, 'Mood')">Add Mood</button>
</div>
<div>
<button class="bujo-btn" @click="prevMonth()">Prev</button>
<span x-text="getMonthName() + ' ' + currentYear" class="font-bold bujo-text"></span>
<button class="bujo-btn" @click="nextMonth()">Next</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="min-w-full bg-white bujo-table">
<thead class="bg-gray-800 text-white">
<tr>
<th class="w-1/6 py-3 px-4 uppercase font-semibold text-sm bujo-text">Row</th>
<template x-for="day in daysInMonth" :key="day">
<th class="py-3 px-4 uppercase font-semibold text-sm bujo-text" x-text="day"></th>
</template>
<th class="py-3 px-4 uppercase font-semibold text-sm bujo-text">Remove</th>
</tr>
</thead>
<tbody class="text-gray-700">
<template x-for="row in getCurrentMonthRows()" :key="row.id">
<tr :class="{'bg-gray-200': row.isCategory}" draggable="true" @dragstart="dragStart(row)" @dragover="dragOver" @drop="drop(row)" class="draggable">
<td class="w-1/6 py-3 px-4">
<input type="text" class="border-none bujo-input" x-model="row.name">
</td>
<template x-if="!row.isCategory">
<template x-for="day in daysInMonth" :key="day">
<td class="py-3 px-4">
<input type="checkbox" class="checkbox" :checked="isHabitChecked(row, day)" @click="toggleHabit(row, day)">
</td>
</template>
</template>
<template x-if="row.isCategory && row.name === 'Mood'">
<template x-for="day in daysInMonth" :key="day">
<td class="py-3 px-4">
<select class="emoji-select bujo-input" x-model="row.emoji[day]">
<option value="">Select</option>
<option value="😊">😊 Happy</option>
<option value="😢">😢 Sad</option>
<option value="😠">😠 Angry</option>
<option value="😐">😐 Neutral</option>
<!-- Add more emoji options as needed -->
</select>
</td>
</template>
</template>
<td class="py-3 px-4">
<button class="text-red-500" @click="removeRow(row.id)">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</td>
</tr>
</template>
</tbody>
</table>
</div>
<button class="bujo-btn" @click="saveState()">Save</button>
</div>
</div>
</body>
</html>