-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbujo.js
169 lines (162 loc) · 6.13 KB
/
bujo.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
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,
initAlpine() {
this.calculateDaysInMonth();
Alpine.store('habitData').load();
if (!Alpine.store('habitData').getRowsForMonth(this.currentYear, this.currentMonth).length) {
this.initializeDefaultData();
}
},
initializeDefaultData() {
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, 'Eating Junk', 'Bad Habits');
this.addRow(true, 'Mood');
},
calculateDaysInMonth() {
this.daysInMonth = Array.from({ length: new Date(this.currentYear, this.currentMonth + 1, 0).getDate() }, (_, i) => i + 1);
},
isWeekend(day) {
const date = new Date(this.currentYear, this.currentMonth, day);
const dayOfWeek = date.getDay();
return dayOfWeek === 0 || dayOfWeek === 6; // Sunday or Saturday
},
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();
},
// This exports the data
exportData() {
const data = {
currentYear: this.currentYear,
currentMonth: this.currentMonth,
rows: this.getCurrentMonthRows()
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `habit_data_${this.currentYear}_${this.currentMonth + 1}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
// Import data
importData(event) {
const file = event.target.files[0];
if (!file) {
alert('No file selected for import.');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const importedData = JSON.parse(e.target.result);
this.currentYear = importedData.currentYear;
this.currentMonth = importedData.currentMonth;
this.saveCurrentMonthRows(importedData.rows);
alert('Your habits have been imported successfully!');
} catch (error) {
alert('Error importing habits. Verify that it is a HabitTracker® approved JSON file.');
}
};
reader.readAsText(file);
},
}));
});