File size: 10,588 Bytes
f515bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Initial data structure with enhanced features
const todos = [
    {
        id: 1,
        text: 'Beispielaufgabe erstellen',
        completed: true,
        priority: 'medium',
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString()
    },
    {
        id: 2,
        text: 'Todo-App mit neuen Features verbessern',
        completed: false,
        priority: 'high',
        dueDate: new Date(Date.now() + 86400000).toISOString().split('T')[0], // Morgen
        createdAt: new Date(Date.now() - 86400000).toISOString(),
        updatedAt: new Date(Date.now() - 86400000).toISOString()
    },
    {
        id: 3,
        text: 'Dokumentation für das Team erstellen',
        completed: false,
        priority: 'low',
        createdAt: new Date(Date.now() - 172800000).toISOString(), // Vorgestern
        updatedAt: new Date(Date.now() - 172800000).toISOString()
    }
];

function todoApp() {
    return {
        todos: [],
        newTodo: '',
        newPriority: 'medium',
        newDueDate: '',
        filter: 'all',
        sortBy: 'priority',
        searchQuery: '',
        showToast: false,
        toastMessage: '',
        toastType: 'success',
        editingId: null,
        editingText: '',
        editingPriority: 'medium',
        editingDueDate: '',
        dragStartIndex: null,
        dragOverIndex: null,
        
        // Initialize from localStorage or default
        init() {
            const savedTodos = localStorage.getItem('todos');
            this.todos = savedTodos ? JSON.parse(savedTodos) : todos;
            this.updateLocalStorage();
            
            // Initialize Feather icons after content loads
            setTimeout(() => {
                if (typeof feather !== 'undefined') {
                    feather.replace();
                }
            }, 100);
        },
        
        // Filtered and sorted todos based on current settings
        get filteredTodos() {
            let filtered = this.todos;
            
            // Apply search filter
            if (this.searchQuery.trim() !== '') {
                const query = this.searchQuery.toLowerCase();
                filtered = filtered.filter(todo => 
                    todo.text.toLowerCase().includes(query)
                );
            }
            
            // Apply status filter
            if (this.filter === 'active') {
                filtered = filtered.filter(todo => !todo.completed);
            } else if (this.filter === 'completed') {
                filtered = filtered.filter(todo => todo.completed);
            }
            
            // Apply sorting
            filtered = [...filtered]; // Create a copy
            if (this.sortBy === 'priority') {
                const priorityOrder = { high: 0, medium: 1, low: 2, undefined: 3 };
                filtered.sort((a, b) => {
                    const aPriority = priorityOrder[a.priority || 'undefined'];
                    const bPriority = priorityOrder[b.priority || 'undefined'];
                    return aPriority - bPriority;
                });
            } else {
                filtered.sort((a, b) => {
                    const aDate = new Date(a.createdAt);
                    const bDate = new Date(b.createdAt);
                    return bDate - aDate; // Newest first
                });
            }
            
            return filtered;
        },
        
        // Enhanced statistics
        get completedCount() {
            return this.todos.filter(todo => todo.completed).length;
        },
        
        get activeCount() {
            return this.todos.filter(todo => !todo.completed).length;
        },
        
        get totalCount() {
            return this.todos.length;
        },
        
        get overdueCount() {
            const today = new Date().toISOString().split('T')[0];
            return this.todos.filter(todo => 
                !todo.completed && 
                todo.dueDate && 
                todo.dueDate < today
            ).length;
        },
        
        // Check if a todo is overdue
        isOverdue(dueDate) {
            if (!dueDate) return false;
            const today = new Date().toISOString().split('T')[0];
            return dueDate < today;
        },
        
        // Add new todo with enhanced features
        addTodo() {
            if (this.newTodo.trim() === '') {
                this.showNotification('Bitte gib eine Aufgabe ein', 'error');
                return;
            }
            
            const newTodo = {
                id: Date.now(),
                text: this.newTodo.trim(),
                completed: false,
                priority: this.newPriority,
                dueDate: this.newDueDate || null,
                overdue: this.isOverdue(this.newDueDate),
                createdAt: new Date().toISOString(),
                updatedAt: new Date().toISOString()
            };
            
            this.todos.unshift(newTodo);
            this.newTodo = '';
            this.newDueDate = '';
            this.newPriority = 'medium';
            this.updateLocalStorage();
            this.showNotification('Aufgabe hinzugefügt', 'success');
        },
        
        // Toggle todo completion
        toggleTodo(id) {
            const todo = this.todos.find(t => t.id === id);
            if (todo) {
                todo.completed = !todo.completed;
                todo.updatedAt = new Date().toISOString();
                this.updateLocalStorage();
                const message = todo.completed ? 'Aufgabe erledigt!' : 'Aufgabe wieder geöffnet';
                this.showNotification(message, 'success');
            }
        },
        
        // Delete todo
        deleteTodo(id) {
            this.todos = this.todos.filter(t => t.id !== id);
            this.updateLocalStorage();
            this.showNotification('Aufgabe gelöscht', 'success');
        },
        
        // Clear completed todos
        clearCompleted() {
            const completedCount = this.completedCount;
            this.todos = this.todos.filter(t => !t.completed);
            this.updateLocalStorage();
            this.showNotification(`${completedCount} erledigte Aufgabe(n) gelöscht`, 'success');
        },
        
        // Edit todo with enhanced features
        startEdit(id, text, priority = 'medium', dueDate = '') {
            this.editingId = id;
            this.editingText = text;
            this.editingPriority = priority || 'medium';
            this.editingDueDate = dueDate || '';
        },
        
        saveEdit() {
            if (this.editingText.trim() === '') {
                this.showNotification('Aufgabe darf nicht leer sein', 'error');
                return;
            }
            
            const todo = this.todos.find(t => t.id === this.editingId);
            if (todo) {
                todo.text = this.editingText.trim();
                todo.priority = this.editingPriority;
                todo.dueDate = this.editingDueDate || null;
                todo.overdue = this.isOverdue(this.editingDueDate);
                todo.updatedAt = new Date().toISOString();
                this.updateLocalStorage();
                this.cancelEdit();
                this.showNotification('Aufgabe aktualisiert', 'success');
            }
        },
        
        cancelEdit() {
            this.editingId = null;
            this.editingText = '';
            this.editingPriority = 'medium';
            this.editingDueDate = '';
        },
        
        // Drag and drop functionality
        dragStart(index) {
            this.dragStartIndex = index;
            this.dragOverIndex = null;
        },
        
        dragOver(event) {
            event.preventDefault();
        },
        
        setDragOver(index) {
            if (index !== this.dragStartIndex) {
                this.dragOverIndex = index;
            }
        },
        
        clearDragOver() {
            this.dragOverIndex = null;
        },
        
        dropOn(targetIndex) {
            if (this.dragStartIndex === null || this.dragStartIndex === targetIndex) {
                this.clearDragOver();
                return;
            }
            
            const draggedTodo = this.todos[this.dragStartIndex];
            this.todos.splice(this.dragStartIndex, 1);
            this.todos.splice(targetIndex, 0, draggedTodo);
            
            this.dragStartIndex = null;
            this.dragOverIndex = null;
            this.updateLocalStorage();
            this.showNotification('Aufgabe verschoben', 'success');
        },
        
        drop() {
            this.dragStartIndex = null;
            this.dragOverIndex = null;
        },
        
        // Format date for display
        formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleDateString('de-DE', {
                day: '2-digit',
                month: '2-digit',
                year: 'numeric',
                hour: '2-digit',
                minute: '2-digit'
            });
        },
        
        // Format due date
        formatDueDate(dateString) {
            if (!dateString) return '';
            const date = new Date(dateString);
            const today = new Date();
            const tomorrow = new Date(today);
            tomorrow.setDate(tomorrow.getDate() + 1);
            
            if (date.toDateString() === today.toDateString()) {
                return 'Heute';
            } else if (date.toDateString() === tomorrow.toDateString()) {
                return 'Morgen';
            } else {
                return date.toLocaleDateString('de-DE', {
                    day: '2-digit',
                    month: '2-digit',
                    year: 'numeric'
                });
            }
        },
        
        // Show notification toast
        showNotification(message, type = 'success') {
            this.toastMessage = message;
            this.toastType = type;
            this.showToast = true;
            
            setTimeout(() => {
                this.showToast = false;
            }, 3000);
        },
        
        // Update localStorage
        updateLocalStorage() {
            localStorage.setItem('todos', JSON.stringify(this.todos));
        },
        
        // Update Feather icons when needed
        updateIcons() {
            if (typeof feather !== 'undefined') {
                feather.replace();
            }
        }
    };
}