File size: 10,502 Bytes
ea012db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Main application script for TaskTamer Pro

class TaskTamerApp {
    constructor() {
        this.currentView = 'list';
        this.init();
    }

    init() {
        this.loadProjects();
        this.loadTasks();
        this.setupEventListeners();
        this.setupDragAndDrop();
    }

    loadProjects() {
        // In a real app, this would be an API call
        console.log('Loading projects...');
    }

    loadTasks() {
        // In a real app, this would be an API call
        console.log('Loading tasks...');
    }

    setupEventListeners() {
        // View switcher
        document.addEventListener('click', (e) => {
            if (e.target.closest('[data-view]')) {
                const view = e.target.closest('[data-view]').dataset.view;
                this.switchView(view);
            }
        });

        // Task completion toggle
        document.addEventListener('click', (e) => {
            if (e.target.closest('[data-task-toggle]')) {
                const taskId = e.target.closest('[data-task-toggle]').dataset.taskToggle;
                this.toggleTaskCompletion(taskId);
            }
        });

        // Modal handling
        document.addEventListener('click', (e) => {
            if (e.target.closest('[data-modal-open]')) {
                const modalId = e.target.closest('[data-modal-open]').dataset.modalOpen;
                this.openModal(modalId);
            }
            
            if (e.target.closest('[data-modal-close]') || e.target.classList.contains('modal-overlay')) {
                this.closeModal();
            }
        });
    }

    switchView(view) {
        this.currentView = view;
        
        // Update active view button
        document.querySelectorAll('[data-view]').forEach(btn => {
            btn.classList.toggle('active', btn.dataset.view === view);
        });
        
        // Show/hide view containers
        document.querySelectorAll('.view-container').forEach(container => {
            container.classList.toggle('hidden', container.id !== `${view}-view`);
        });
        
        // Save preference
        localStorage.setItem('preferredView', view);
        
        console.log(`Switched to ${view} view`);
    }

    toggleTaskCompletion(taskId) {
        const taskElement = document.querySelector(`[data-task-id="${taskId}"]`);
        if (taskElement) {
            const isCompleted = taskElement.classList.toggle('completed');
            
            // Update task in database (API call in real app)
            console.log(`Task ${taskId} ${isCompleted ? 'completed' : 'uncompleted'}`);
            
            // Update progress bars
            this.updateProjectProgress(taskElement.dataset.projectId);
        }
    }
    updateProjectProgress(projectId) {
        // Update progress for the specific project
        const progressElements = document.querySelectorAll(`[data-project-progress="${projectId}"]`);
        progressElements.forEach(el => {
            // Calculate new progress
            // This would be an API call in real app
            const newProgress = Math.min(100, parseInt(el.style.width) + 10);
            el.style.width = `${newProgress}%`;
            el.textContent = `${newProgress}%`;
        });
    }

    // Advanced task management methods
    addDependency(parentTaskId, childTaskId) {
        console.log(`Adding dependency: ${parentTaskId} -> ${childTaskId}`);
        return Promise.resolve({ success: true });
    }

    addCustomField(taskId, fieldName, value) {
        console.log(`Adding custom field ${fieldName} to task ${taskId}`);
        return Promise.resolve({ success: true });
    }

    createSubtask(parentTaskId, subtaskData) {
        console.log(`Creating subtask for ${parentTaskId}:`, subtaskData);
        return Promise.resolve({ id: Date.now(), ...subtaskData });
    }

    // Automation methods
    createAutomationRule(ruleData) {
        console.log('Creating automation rule:', ruleData);
        return Promise.resolve({ id: Date.now(), ...ruleData });
    }

    executeWorkflow(workflowId, inputData) {
        console.log(`Executing workflow ${workflowId}:`, inputData);
        return Promise.resolve({ success: true });
    }

    // Collaboration methods
    addComment(taskId, commentData) {
        console.log(`Adding comment to task ${taskId}:`, commentData);
        return Promise.resolve({ id: Date.now(), ...commentData });
    }

    sendStatusUpdate(updateData) {
        console.log('Sending status update:', updateData);
        return Promise.resolve({ success: true });
    }

    // Reporting methods
    generateReport(reportConfig) {
        console.log('Generating report:', reportConfig);
        return Promise.resolve({ id: Date.now(), data: {} });
    }

    createPortfolioView(portfolioData) {
        console.log('Creating portfolio view:', portfolioData);
        return Promise.resolve({ success: true });
    }
setupDragAndDrop() {
        // Setup drag and drop for board view
        const draggables = document.querySelectorAll('.draggable-task');
        const columns = document.querySelectorAll('.board-column');
        
        draggables.forEach(draggable => {
            draggable.addEventListener('dragstart', () => {
                draggable.classList.add('dragging');
            });
            
            draggable.addEventListener('dragend', () => {
                draggable.classList.remove('dragging');
            });
        });
        
        columns.forEach(column => {
            column.addEventListener('dragover', e => {
                e.preventDefault();
                const afterElement = this.getDragAfterElement(column, e.clientY);
                const draggable = document.querySelector('.dragging');
                
                if (afterElement == null) {
                    column.appendChild(draggable);
                } else {
                    column.insertBefore(draggable, afterElement);
                }
            });
        });
    }

    getDragAfterElement(container, y) {
        const draggableElements = [...container.querySelectorAll('.draggable-task:not(.dragging)')];
        
        return draggableElements.reduce((closest, child) => {
            const box = child.getBoundingClientRect();
            const offset = y - box.top - box.height / 2;
            
            if (offset < 0 && offset > closest.offset) {
                return { offset: offset, element: child };
            } else {
                return closest;
            }
        }, { offset: Number.NEGATIVE_INFINITY }).element;
    }

    openModal(modalId) {
        const modal = document.getElementById(modalId);
        if (modal) {
            modal.classList.remove('hidden');
            document.body.style.overflow = 'hidden';
        }
    }

    closeModal() {
        document.querySelectorAll('.modal').forEach(modal => {
            modal.classList.add('hidden');
        });
        document.body.style.overflow = 'auto';
    }

    // Task management methods
    createTask(taskData) {
        // API call to create task
        console.log('Creating task:', taskData);
        return Promise.resolve({ id: Date.now(), ...taskData });
    }

    updateTask(taskId, updates) {
        // API call to update task
        console.log(`Updating task ${taskId}:`, updates);
        return Promise.resolve({ success: true });
    }

    deleteTask(taskId) {
        // API call to delete task
        console.log(`Deleting task ${taskId}`);
        return Promise.resolve({ success: true });
    }

    // Project management methods
    createProject(projectData) {
        // API call to create project
        console.log('Creating project:', projectData);
        return Promise.resolve({ id: Date.now(), ...projectData });
    }

    updateProject(projectId, updates) {
        // API call to update project
        console.log(`Updating project ${projectId}:`, updates);
        return Promise.resolve({ success: true });
    }
}

// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    window.taskTamer = new TaskTamerApp();
    
    // Load preferred view
    const preferredView = localStorage.getItem('preferredView') || 'list';
    window.taskTamer.switchView(preferredView);
    
    // Initialize tooltips
    const tooltipTriggers = document.querySelectorAll('[data-tooltip]');
    tooltipTriggers.forEach(trigger => {
        trigger.addEventListener('mouseenter', (e) => {
            const tooltip = document.createElement('div');
            tooltip.className = 'absolute z-50 px-3 py-2 text-sm font-medium text-white bg-gray-900 rounded-lg shadow-sm tooltip';
            tooltip.textContent = e.target.dataset.tooltip;
            
            const rect = e.target.getBoundingClientRect();
            tooltip.style.top = `${rect.top - 40}px`;
            tooltip.style.left = `${rect.left + rect.width / 2 - tooltip.offsetWidth / 2}px`;
            
            document.body.appendChild(tooltip);
            e.target._tooltip = tooltip;
        });
        
        trigger.addEventListener('mouseleave', (e) => {
            if (e.target._tooltip) {
                e.target._tooltip.remove();
                delete e.target._tooltip;
            }
        });
    });
});

// Utility functions
const Utils = {
    formatDate(dateString) {
        const date = new Date(dateString);
        return date.toLocaleDateString('en-US', {
            weekday: 'short',
            month: 'short',
            day: 'numeric'
        });
    },

    formatRelativeDate(dateString) {
        const date = new Date(dateString);
        const now = new Date();
        const diffTime = Math.abs(date - now);
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        
        if (diffDays === 0) return 'Today';
        if (diffDays === 1) return 'Tomorrow';
        if (diffDays < 7) return `In ${diffDays} days`;
        if (diffDays < 30) return `In ${Math.floor(diffDays / 7)} weeks`;
        return `In ${Math.floor(diffDays / 30)} months`;
    },

    debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }
};

// Make utilities globally available
window.Utils = Utils;