-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
155 lines (141 loc) · 5.84 KB
/
script.js
File metadata and controls
155 lines (141 loc) · 5.84 KB
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
console.log("Running script.js");
document.addEventListener('DOMContentLoaded', function() {
const tableBody = document.getElementById('tableBody');
const addRowBtn = document.getElementById('addRow');
const removeRowBtn = document.getElementById('removeRow');
// Helper: Save table data to localStorage
function saveTableToLS() {
const data = [];
for (const row of tableBody.rows) {
const date = row.cells[0].textContent;
const areeb = {
value: row.cells[1].querySelector('span').textContent,
done: row.cells[1].querySelector('button').classList.contains('done')
};
const hania = {
value: row.cells[2].querySelector('span').textContent,
done: row.cells[2].querySelector('button').classList.contains('done')
};
data.push({ date, areeb, hania });
}
localStorage.setItem('tableData', JSON.stringify(data));
console.log('Table data saved to localStorage:', data);
}
// Helper: Load table data from localStorage
function loadTableFromLS() {
const data = JSON.parse(localStorage.getItem('tableData') || '[]');
tableBody.innerHTML = '';
data.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.date}</td>
<td>
<span class="${row.areeb.done ? 'green' : 'red'}">${row.areeb.value}</span>
<button class="done-btn${row.areeb.done ? ' done' : ''}">${row.areeb.done ? 'Completed' : 'Done'}</button>
</td>
<td>
<span class="${row.hania.done ? 'green' : 'red'}">${row.hania.value}</span>
<button class="done-btn${row.hania.done ? ' done' : ''}">${row.hania.done ? 'Completed' : 'Done'}</button>
</td>
`;
tableBody.appendChild(tr);
});
console.log('Table data loaded from localStorage:', data);
}
// Initial load
loadTableFromLS();
// Add Row
addRowBtn.addEventListener('click', function() {
// Create a visible date input for calendar selection
let dateInput = document.createElement('input');
dateInput.type = 'date';
dateInput.style.position = 'fixed';
dateInput.style.top = '50%';
dateInput.style.left = '50%';
dateInput.style.transform = 'translate(-50%, -50%)';
dateInput.style.zIndex = '9999';
dateInput.style.background = '#fff';
dateInput.style.padding = '12px 18px';
dateInput.style.border = '1.5px solid #6a82fb';
dateInput.style.borderRadius = '12px';
dateInput.style.boxShadow = '0 4px 24px rgba(31,38,135,0.13)';
dateInput.style.fontSize = '1.1em';
document.body.appendChild(dateInput);
dateInput.focus();
function cleanup() {
if (dateInput && dateInput.parentNode) {
document.body.removeChild(dateInput);
}
}
function addRowWithDate(dateStr) {
const selectedDate = dateStr
? new Date(dateStr).toLocaleDateString('en-GB')
: new Date().toLocaleDateString('en-GB');
const basicValue = localStorage.getItem('basicValue') || '0';
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${selectedDate}</td>
<td>
<span class="red">${basicValue}</span>
<button class="done-btn">Done</button>
</td>
<td>
<span class="red">${basicValue}</span>
<button class="done-btn">Done</button>
</td>
`;
tableBody.appendChild(newRow);
saveTableToLS();
doTotal();
}
dateInput.addEventListener('change', function handler() {
addRowWithDate(dateInput.value);
cleanup();
});
dateInput.addEventListener('blur', function handler() {
// If no date picked, fallback to today
if (!dateInput.value) {
addRowWithDate('');
}
cleanup();
});
// Open the calendar
dateInput.showPicker && dateInput.showPicker(); // for browsers that support showPicker
dateInput.focus();
});
// Remove Row
removeRowBtn.addEventListener('click', function() {
if (tableBody.rows.length > 0) {
tableBody.deleteRow(tableBody.rows.length - 1);
saveTableToLS();
doTotal();
}
});
// Mark as Done
tableBody.addEventListener('click', function(e) {
if (e.target.classList.contains('done-btn')) {
const btn = e.target;
// Support both <p> and <span>
let valueElem = btn.parentElement.querySelector('span') || btn.parentElement.querySelector('p');
if (!btn.classList.contains('done')) {
btn.classList.add('done');
btn.textContent = 'Completed';
if (valueElem) {
valueElem.classList.remove('red');
valueElem.classList.add('green');
}
console.log('Marked as completed:', btn.parentElement.parentElement.rowIndex, btn.parentElement.cellIndex);
} else {
btn.classList.remove('done');
btn.textContent = 'Done';
if (valueElem) {
valueElem.classList.remove('green');
valueElem.classList.add('red');
}
console.log('Marked as not completed:', btn.parentElement.parentElement.rowIndex, btn.parentElement.cellIndex);
}
saveTableToLS();
doTotal();
}
});
});