class CustomTaskItem extends HTMLElement {
constructor() {
super();
this.title = this.getAttribute('title') || 'Untitled Task';
this.project = this.getAttribute('project') || 'No Project';
this.dueDate = this.getAttribute('due-date') || '';
this.priority = this.getAttribute('priority') || 'medium';
this.assignee = this.getAttribute('assignee') || 'unassigned';
this.completed = this.getAttribute('completed') === 'true';
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
}
static get observedAttributes() {
return ['title', 'project', 'due-date', 'priority', 'assignee', 'completed'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
if (name === 'completed') {
this.completed = newValue === 'true';
} else {
this[name] = newValue;
}
this.render();
}
}
render() {
const priorityColors = {
'high': { bg: 'bg-red-100', text: 'text-red-800', border: 'border-red-200' },
'medium': { bg: 'bg-yellow-100', text: 'text-yellow-800', border: 'border-yellow-200' },
'low': { bg: 'bg-green-100', text: 'text-green-800', border: 'border-green-200' }
};
const priorityLabels = {
'high': 'High',
'medium': 'Medium',
'low': 'Low'
};
const colors = priorityColors[this.priority] || priorityColors.medium;
const formattedDate = this.dueDate ? Utils.formatDate(this.dueDate) : 'No due date';
const isOverdue = this.dueDate && new Date(this.dueDate) < new Date() && !this.completed;
const assigneeInitials = this.getAssigneeInitials();
const assigneeColor = this.getAssigneeColor();
this.shadowRoot.innerHTML = `
${this.title}
${this.hasSubtasks() ? `
${this.getSubtaskCount()} subtasks
` : ''}
${priorityLabels[this.priority]}
${this.project}
${assigneeInitials}
`;
setTimeout(() => {
feather.replace();
// Add checkbox click handler
const checkbox = this.shadowRoot.querySelector('.task-checkbox');
if (checkbox) {
checkbox.addEventListener('click', (e) => {
e.stopPropagation();
this.toggleCompletion();
});
}
// Add click handler for task item
const taskItem = this.shadowRoot.querySelector('.task-item');
if (taskItem) {
taskItem.addEventListener('click', (e) => {
if (!e.target.closest('.task-checkbox') && !e.target.closest('.task-action-btn')) {
window.location.href = `task-detail.html?id=${encodeURIComponent(this.title.toLowerCase().replace(/\s+/g, '-'))}`;
}
});
}
// Add action button handlers
const actionBtns = this.shadowRoot.querySelectorAll('.task-action-btn');
actionBtns.forEach((btn, index) => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
this.handleAction(index);
});
});
}, 100);
}
toggleCompletion() {
this.completed = !this.completed;
this.setAttribute('completed', this.completed);
// Dispatch custom event
this.dispatchEvent(new CustomEvent('task-toggled', {
detail: {
taskId: this.getAttribute('data-task-id'),
completed: this.completed
},
bubbles: true,
composed: true
}));
}
getAssigneeInitials() {
const nameMap = {
'alex': 'AJ',
'sarah': 'SC',
'mike': 'MD',
'jane': 'JD',
'tom': 'TB'
};
return nameMap[this.assignee] || this.assignee.slice(0, 2).toUpperCase();
}
getAssigneeName() {
const nameMap = {
'alex': 'Alex Johnson',
'sarah': 'Sarah Chen',
'mike': 'Mike Davis',
'jane': 'Jane Doe',
'tom': 'Tom Brown'
};
return nameMap[this.assignee] || this.assignee;
}
getAssigneeColor() {
const colors = ['#ef4444', '#3b82f6', '#10b981', '#8b5cf6', '#f59e0b', '#ec4899'];
const index = ['alex', 'sarah', 'mike', 'jane', 'tom'].indexOf(this.assignee);
return index >= 0 ? colors[index] : colors[Math.floor(Math.random() * colors.length)];
}
hasSubtasks() {
// In a real app, this would check actual subtask data
return Math.random() > 0.5;
}
getSubtaskCount() {
return Math.floor(Math.random() * 5) + 1;
}
handleAction(index) {
const actions = ['dependency', 'subtask', 'more'];
const action = actions[index];
switch(action) {
case 'dependency':
this.showDependencyModal();
break;
case 'subtask':
this.showSubtaskModal();
break;
case 'more':
this.showMoreOptions();
break;
}
}
showDependencyModal() {
console.log('Show dependency modal for task:', this.title);
// In a real app, this would open a modal
}
showSubtaskModal() {
console.log('Show subtask modal for task:', this.title);
// In a real app, this would open a modal
}
showMoreOptions() {
console.log('Show more options for task:', this.title);
// In a real app, this would show a dropdown menu
}
}
customElements.define('custom-task-item', CustomTaskItem);