-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditableTable.js
483 lines (435 loc) · 17.5 KB
/
EditableTable.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* Editable Table Script
*
* This script provides functionality for managing an editable table. It includes:
* - Cell editing with keyboard and mouse interactions.
* - Navigation between cells using arrow keys, (tab and shift-tab also supported).
* - Tracking changes to rows and cells.
* - Adding and deleting rows.
* - Synchronizing changes with the server using efficient (changes only) payload.
* - Reverting all changes to the original state.
* - appropriate field types
* - support for readonly cells
* - unopinionated styles
*
* Global Variables:
* - `table`: The table element (`<table>`) id.
* - `editingCell`: Tracks the currently editable cell.
* - `originalValue`: Stores the original value of a cell before editing.
* - `deletedRows`: A `Set` that tracks rows marked for deletion.
* - `modifiedRows`: An object that tracks changes to rows, indexed by `rowId`.
* - `originalData`: An object that stores the original values of rows for reverting changes.
* - `text`: strings for various prompts/interface elements.
*
* Constructor options:
* - `nextRowId`: Counter for generating unique IDs for new rows.
* - `ajaxEndpoint`: endpoint for ajax postbacks during save.
* - `saveButton`: Reference to the save button dom object.
* - `revertButton`: Reference to the revert button dom object.
* - `addRowButton`: Reference to the add button dom object.
* - `keys`: array of column names for postback.
* - `actionHeader`: text label for actions column.
* - `source`: array of names of data sources for select columns.
* - `colours`: object containing cell colours
*/
class EditableTable {
constructor(tableId, options = {}) {
this.table = document.getElementById(tableId);
this.editingCell = null;
this.originalValue = '';
this.deletedRows = new Set();
this.modifiedRows = {};
this.originalData = {}; // To revert changes
this.text = {
delete: 'Are you sure you want to remove this row?', // delete confirmation
saved: 'Changes synced!', // json success
failed: 'Error syncing changes.', // json error
remove: '🗑' // delete button
}
// Options
this.nextRowId = options.newRowIndex || 1000; // Arbitrary start for new rows
this.ajaxEndpoint = options.ajaxEndpoint || '/your/api/update-rows'; // Default endpoint
this.saveButton = options.saveButton || null;
this.revertButton = options.revertButton || null;
this.addRowButton = options.addRowButton || null;
this.keys = options.keys || []; // Column keys for changes
this.actionHeader = options.actionHeader || 'Actions';
this.source = options.source || {}; // Data sources for select dropdowns
this.colours = options.colours || {dirty:"#fff3cd7f !important", focus: "#4A90E2"}; // cell selection colours
// Add delete button column to the table
this.addDeleteColumn();
// Extend styles
this.addStyles();
// ensure cells are editable
this.enableEdit();
// Initialize event listeners
this.initEventListeners();
}
/**
* Adds a delete button column to the table.
*/
addDeleteColumn() {
// Add a header cell for the delete column if the table has a <thead>
const thead = this.table.querySelector('thead');
if (thead) {
const headerRow = thead.querySelector('tr');
const deleteHeader = document.createElement('th');
deleteHeader.textContent = this.actionHeader;
headerRow.appendChild(deleteHeader);
}
// Add a delete button cell to each row in the <tbody>
const tbody = this.table.querySelector('tbody');
const rows = tbody.querySelectorAll('tr');
rows.forEach((row) => {
const deleteCell = document.createElement('td');
deleteCell.setAttribute('readonly','');
deleteCell.innerHTML = `<button class="delete-row">${this.text.remove}</button>`;
row.appendChild(deleteCell);
});
}
/**
* Initializes event listeners for the table and external buttons.
*/
initEventListeners() {
// Table cell click listener
this.table.addEventListener('click', (event) => {
const target = event.target;
// Handle delete button clicks
if (target.tagName === 'BUTTON' && target.classList.contains('delete-row')) {
this.deleteRow(target);
return;
}
// Handle cell selection
const cell = target.closest('td');
if (cell && !cell.hasAttribute('readonly')) {
this.selectCell(cell);
}
});
// Table keyboard navigation and editing listener
this.table.addEventListener('keydown', (event) => {
const { key, shiftKey, target } = event;
const cell = target.closest('td');
if (!cell) return;
if (this.editingCell) {
// Handle keys during editing
switch (key) {
case 'Enter':
if (shiftKey) {
this.insertLineBreak(this.editingCell);
} else {
this.saveEdit(this.editingCell);
}
event.preventDefault();
break;
case 'Escape':
this.cancelEdit(this.editingCell);
event.preventDefault();
break;
}
} else {
// Handle keys for navigation and editing initiation
switch (key) {
case 'Enter':
this.makeCellEditable(cell);
event.preventDefault();
break;
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
this.navigateCells(cell, key);
event.preventDefault();
break;
}
}
});
// External button listeners
this.saveButton?.addEventListener('click', () => this.sendUpdatedRows());
this.revertButton?.addEventListener('click', () => this.revertAllChanges());
this.addRowButton?.addEventListener('click', () => this.addRow());
}
/**
* Highlights and focuses on a specific table cell.
*/
selectCell(cell) {
if (this.editingCell) return;
const previouslySelected = this.table.querySelector('td[tabindex="0"][aria-selected="true"]');
previouslySelected?.removeAttribute('aria-selected');
cell.setAttribute('aria-selected', 'true');
cell.focus();
}
/**
* Converts a table cell into an editable state using the appropriate input control.
* @param {HTMLElement} cell - The table cell (`<td>`) element to make editable.
*/
makeCellEditable(cell) {
if (this.editingCell) return;
this.editingCell = cell;
this.originalValue = cell.textContent.trim();
// Determine the input type based on the `data-type` attribute
const dataType = cell.dataset.type || 'text'; // Default to "text"
let input;
if (dataType === 'select') {
// Create a <select> dropdown for select-type cells
const sourceName = cell.dataset.sourceName;
const source = this.source[sourceName] || [];
input = document.createElement('select');
// Populate the dropdown with options
source.forEach(({ key, value }) => {
const option = document.createElement('option');
option.value = key;
option.textContent = value;
if (value === this.originalValue) {
option.selected = true;
}
input.appendChild(option);
});
// Add an event listener to handle Enter key for saving
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
this.saveEdit(cell);
event.preventDefault();
}
});
} else if (dataType === 'longtext') {
// Use a <textarea> for longtext
input = document.createElement('textarea');
input.value = this.originalValue;
} else {
// Use an <input> for other types
input = document.createElement('input');
input.type = dataType; // Set the input type (e.g., text, number, date, etc.)
input.value = this.originalValue;
}
// Replace cell content with the input control
cell.innerHTML = '';
cell.appendChild(input);
input.focus();
}
/**
* Cancels editing of a cell and restores its original value.
* @param {HTMLElement} cell - The table cell (`<td>`) element being edited.
*/
cancelEdit(cell) {
cell.textContent = this.originalValue;
this.editingCell = null;
this.selectCell(cell);
}
/**
* Inserts a line break (`\n`) into the `<textarea>` of an editable cell.
* @param {HTMLElement} cell - The table cell (`<td>`) element containing the `<textarea>`.
*/
insertLineBreak(cell) {
const textarea = cell.querySelector('textarea');
if (textarea) {
textarea.value += '\n';
textarea.scrollTop = textarea.scrollHeight;
}
}
/**
* Moves focus to a neighboring cell based on the arrow key direction.
*/
navigateCells(currentCell, direction) {
if (this.editingCell) return;
const currentRow = currentCell.parentElement;
let targetCell;
switch (direction) {
case 'ArrowUp':
targetCell = currentRow.previousElementSibling?.cells[currentCell.cellIndex];
break;
case 'ArrowDown':
targetCell = currentRow.nextElementSibling?.cells[currentCell.cellIndex];
break;
case 'ArrowLeft':
targetCell = currentCell.previousElementSibling;
break;
case 'ArrowRight':
targetCell = currentCell.nextElementSibling;
break;
}
if (targetCell && !targetCell.classList.contains('hidden')) {
this.selectCell(targetCell);
}
}
/**
* Saves the edited value of a cell and tracks changes.
* @param {HTMLElement} cell - The table cell (`<td>`) element being edited.
*/
saveEdit(cell) {
const input = cell.querySelector('input, textarea, select');
if (!input) return;
const newValue = input.tagName === 'SELECT' ? input.options[input.selectedIndex].text : input.value.trim();
const newKey = input.tagName === 'SELECT' ? input.value : null;
const row = cell.parentElement;
const rowId = row.dataset.rowId;
const colIndex = cell.cellIndex;
if (!this.originalData[rowId]) {
// Save original row values on first edit
const cells = row.querySelectorAll('td');
this.originalData[rowId] = Array.from(cells).map(c => c.textContent.trim());
}
const originalValue = this.originalData[rowId][colIndex];
cell.textContent = newValue; // Revert to plain text with the selected value
this.editingCell = null;
this.selectCell(cell);
if (newValue !== originalValue) {
cell.classList.add('dirty');
if (!this.modifiedRows[rowId]) {
this.modifiedRows[rowId] = { id: rowId, changes: {} };
}
// Use the key name if available, otherwise fallback to column index
const key = this.keys[colIndex] || colIndex;
this.modifiedRows[rowId].changes[key] = newKey || newValue;
} else {
// Clean if reverted to original
cell.classList.remove('dirty');
if (this.modifiedRows[rowId]) {
const key = this.keys[colIndex] || colIndex;
delete this.modifiedRows[rowId].changes[key];
if (Object.keys(this.modifiedRows[rowId].changes).length === 0) {
delete this.modifiedRows[rowId];
}
}
}
}
/**
* Sends all modified and deleted rows to the server for synchronization.
*/
sendUpdatedRows() {
const updates = Object.values(this.modifiedRows);
const deletions = Array.from(this.deletedRows);
if (updates.length === 0 && deletions.length === 0) {
alert('No changes to sync.');
return;
}
fetch(this.ajaxEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ updates, deletions })
})
.then(res => res.json())
.then(() => {
alert(this.text.saved);
// Clear state
Object.keys(this.modifiedRows).forEach(k => delete this.modifiedRows[k]);
Object.keys(this.originalData).forEach(k => delete this.originalData[k]);
this.deletedRows.clear();
})
.catch(err => {
console.error('Sync failed:', err);
alert(this.text.failed);
});
}
/**
* Reverts all changes made to the table and restores the original data.
*/
revertAllChanges() {
for (const rowId in this.originalData) {
const row = this.table.querySelector(`tr[data-row-id="${rowId}"]`);
if (!row) continue;
const cells = row.querySelectorAll('td:not([readonly])');
this.originalData[rowId].forEach((val, i) => {
cells[i].textContent = val;
cells[i].classList.remove('dirty');
});
}
Object.keys(this.modifiedRows).forEach(k => delete this.modifiedRows[k]);
Object.keys(this.originalData).forEach(k => delete this.originalData[k]);
}
/**
* Adds a new row to the table with editable cells.
*/
addRow() {
const tbody = this.table.querySelector('tbody');
const lastRow = tbody.querySelector('tr:last-child');
const newRow = lastRow ? lastRow.cloneNode(true) : document.createElement('tr');
const rowId = `new-${this.nextRowId++}`;
newRow.setAttribute('data-row-id', rowId);
// Clear the values of all cells except the last one
// TODO: decide how to handle readonly cells
const cells = newRow.querySelectorAll('td:not(:last-of-type)');
cells.forEach((cell) => {
if (cell.hasAttribute('data-type')) {
// Handle select cells
if (cell.dataset.type === 'select') {
const sourceName = cell.dataset.sourceName;
const source = this.source[sourceName] || [];
const firstOption = source.length > 0 ? source[0].value : '';
cell.textContent = firstOption;
} else {
// Handle other input types
cell.textContent = '';
}
} else {
// For cells without a data-type, just clear the content
cell.textContent = '';
}
});
tbody.appendChild(newRow);
// Track the new row in modifiedRows
this.modifiedRows[rowId] = {
id: rowId,
changes: {}
};
}
/**
* Deletes a row from the table.
* @param {HTMLElement} button - The delete button (`<button>`) inside the row to be deleted.
*/
deleteRow(button) {
if (!window.confirm(this.text.delete)) return;
const row = button.closest('tr');
const rowId = row.dataset.rowId;
// If it's a new row, just remove it entirely
if (rowId.startsWith('new-')) {
delete this.modifiedRows[rowId];
} else {
this.deletedRows.add(rowId);
}
row.remove();
}
/**
* Extend page css to support table cell highlighting
*/
addStyles() {
this.table.setAttribute('data-tabler','');
if (!document.querySelector('tabler-css')) {
const style = document.createElement('style');
style.textContent = `
table[data-tabler] {
td:focus {
outline: 2px solid ${this.colours.focus}
}
.dirty {
background-color: ${this.colours.dirty}; /* light yellow */
}
}`;
style.setAttribute('id','tabler-css');
document.head.appendChild(style);
}
}
/**
* Editable cells are identified by having a tabindex
* run this last.
*/
enableEdit() {
for (let td of [...this.table.querySelectorAll('tbody td:not([readonly])')]) {
td.setAttribute("tabindex",0);
}
}
}
// Example usage
// const table1 = new EditableTable('editableTable', {
// ajaxEndpoint: '/api/subjects/save',
// saveButton: document.getElementById('savebutton'),
// revertButton: document.getElementById('revertbutton'),
// addRowButton: document.getElementById('addrow'),
// keys: ['stage_id', 'name', 'focusarea', 'outcome', 'code'], // Column keys
// source: {
// "stage": [
// { key: 1, value: `Stage 3` },
// { key: 2, value: `Stage 4` },
// { key: 3, value: `Stage 5 (unused)` },
// ]
// }
// });