-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfli-editable.js
More file actions
371 lines (320 loc) · 11.9 KB
/
Copy pathfli-editable.js
File metadata and controls
371 lines (320 loc) · 11.9 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
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath)
//
// fli-editable.js — FLI module: inline editing, keyboard navigation, cell editing
//
// Provides inline-edit capabilities for panels that inherit fli-editable.
// Used by: gsa-config (field editing), gsa-history (rollback interactions)
//
// Provisioning: this module is loaded on-demand when a panel with the
// fli-editable trait is activated. Panels start bare and FLI modules
// layer capabilities on top, following the same pattern as Groove
// (Burble/Vext) integration on base panels.
//
// API:
// FLI.editable.init(container, options) — enable inline editing
// FLI.editable.makeEditable(element, options) — make a single element editable
// FLI.editable.onSave(callback) — register save callback
// FLI.editable.destroy(container) — tear down inline editing
(function() {
'use strict';
window.FLI = window.FLI || {};
window.FLI.editable = {};
// =========================================================================
// State
// =========================================================================
/** Active editors keyed by container ID. */
var editors = {};
/** Global save callback. */
var saveCallback = null;
/** Currently active inline editor element (only one at a time). */
var activeEditor = null;
// =========================================================================
// Core: make elements inline-editable
// =========================================================================
/**
* Initialise inline editing on all [data-editable] elements within a
* container. Double-click to enter edit mode, Tab/Enter to commit,
* Escape to cancel.
*
* @param {HTMLElement|string} container - Container element or ID
* @param {object} options - Configuration:
* selector: {string} CSS selector for editable cells (default '[data-editable]')
* onSave: {function} Called with (key, value, oldValue) when a field is saved
* onCancel: {function} Called with (key, oldValue) when editing is cancelled
* validate: {function} Called with (key, value) → true/false or error string
* commitOn: {string} 'blur' | 'enter' | 'both' (default 'both')
*/
FLI.editable.init = function(container, options) {
var el = typeof container === 'string' ? document.getElementById(container) : container;
if (!el) return;
var opts = options || {};
var selector = opts.selector || '[data-editable]';
var commitOn = opts.commitOn || 'both';
var id = el.id || 'fli-edit-' + Math.random().toString(36).substr(2, 6);
el.id = id;
editors[id] = {
element: el,
options: opts,
selector: selector
};
// Attach double-click listener via delegation
el.addEventListener('dblclick', function(e) {
var target = e.target.closest(selector);
if (target) {
e.preventDefault();
e.stopPropagation();
enterEditMode(target, opts);
}
});
// Also support single-click on elements with data-editable="click"
el.addEventListener('click', function(e) {
var target = e.target.closest(selector + '[data-editable="click"]');
if (target) {
e.preventDefault();
enterEditMode(target, opts);
}
});
};
/**
* Make a single element inline-editable.
*
* @param {HTMLElement} element - The element to make editable
* @param {object} options - Same as init() options
*/
FLI.editable.makeEditable = function(element, options) {
if (!element) return;
element.setAttribute('data-editable', 'true');
element.style.cursor = 'pointer';
element.title = element.title || 'Double-click to edit';
element.addEventListener('dblclick', function(e) {
e.preventDefault();
e.stopPropagation();
enterEditMode(element, options || {});
});
};
/**
* Register a global save callback.
*
* @param {function} callback - Called with (key, newValue, oldValue, element)
*/
FLI.editable.onSave = function(callback) {
saveCallback = callback;
};
/**
* Destroy inline editing on a container.
*
* @param {HTMLElement|string} container - Container element or ID
*/
FLI.editable.destroy = function(container) {
var el = typeof container === 'string' ? document.getElementById(container) : container;
if (!el) return;
delete editors[el.id];
// Cancel any active editor in this container
if (activeEditor && el.contains(activeEditor.element)) {
cancelEdit();
}
};
// =========================================================================
// Edit mode lifecycle
// =========================================================================
/**
* Enter inline edit mode on an element. Replaces the element's content
* with an appropriate input control and manages focus, commit, and cancel.
*
* @param {HTMLElement} element - The element to enter edit mode on
* @param {object} opts - Configuration options
*/
function enterEditMode(element, opts) {
// Cancel any existing active editor first
if (activeEditor) {
commitEdit();
}
var key = element.dataset.editableKey || element.dataset.key || '';
var type = element.dataset.editableType || 'text';
var oldValue = element.dataset.editableValue !== undefined
? element.dataset.editableValue
: element.textContent.trim();
var enumOptions = element.dataset.editableOptions
? element.dataset.editableOptions.split(',')
: null;
var min = element.dataset.editableMin;
var max = element.dataset.editableMax;
// Store state — capture child nodes via deep clone so cancelEdit restores
// the DOM directly (no innerHTML re-parse, no injection risk).
activeEditor = {
element: element,
key: key,
oldValue: oldValue,
oldNodes: Array.from(element.childNodes).map(function(n) { return n.cloneNode(true); }),
opts: opts
};
// Add editing class
element.classList.add('fli-editing');
// Create the appropriate input control
var input;
if (type === 'bool') {
// Toggle immediately without an input
var newVal = oldValue === 'true' ? 'false' : 'true';
element.dataset.editableValue = newVal;
commitEditDirect(key, newVal, oldValue, element, opts);
activeEditor = null;
return;
}
if (enumOptions) {
input = document.createElement('select');
input.className = 'fli-edit-input';
enumOptions.forEach(function(opt) {
var o = document.createElement('option');
o.value = opt.trim();
o.textContent = opt.trim();
if (opt.trim() === oldValue) o.selected = true;
input.appendChild(o);
});
} else if (type === 'number') {
input = document.createElement('input');
input.type = 'number';
input.className = 'fli-edit-input';
input.value = oldValue;
if (min !== undefined) input.min = min;
if (max !== undefined) input.max = max;
} else {
input = document.createElement('input');
input.type = type === 'secret' ? 'password' : 'text';
input.className = 'fli-edit-input';
input.value = oldValue;
}
// Style the input to match the cell
input.style.cssText = 'width:100%;padding:2px 6px;font-size:inherit;font-family:inherit;'
+ 'background:var(--bg-primary,#0d1117);color:var(--text-primary,#c9d1d9);'
+ 'border:1px solid var(--link-color,#58a6ff);border-radius:4px;outline:none;'
+ 'box-shadow:0 0 0 2px rgba(88,166,255,0.2);';
// Replace content with input (replaceChildren clears without an
// innerHTML re-parse — no markup interpretation, no injection surface).
element.replaceChildren();
element.appendChild(input);
input.focus();
if (input.select) input.select();
// Keyboard handlers
input.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
commitEdit();
} else if (e.key === 'Escape') {
e.preventDefault();
cancelEdit();
} else if (e.key === 'Tab') {
e.preventDefault();
commitEdit();
// Move to next editable element
tabToNext(element, e.shiftKey);
}
});
// Blur handler (commit on blur unless commitOn is 'enter')
input.addEventListener('blur', function() {
// Small delay to allow Tab to fire first
setTimeout(function() {
if (activeEditor && activeEditor.element === element) {
if (opts.commitOn === 'enter') {
cancelEdit();
} else {
commitEdit();
}
}
}, 100);
});
}
/**
* Commit the current inline edit. Reads the input value, validates it,
* and triggers the save callback.
*/
function commitEdit() {
if (!activeEditor) return;
var element = activeEditor.element;
var key = activeEditor.key;
var oldValue = activeEditor.oldValue;
var opts = activeEditor.opts;
var input = element.querySelector('.fli-edit-input');
var newValue = input ? input.value : oldValue;
// Validate
if (opts.validate) {
var result = opts.validate(key, newValue);
if (result !== true && result !== undefined) {
// Validation failed — show error state briefly
if (input) {
input.style.borderColor = 'var(--danger-fg,#f85149)';
input.style.boxShadow = '0 0 0 2px rgba(248,81,73,0.2)';
}
return;
}
}
// Exit edit mode
element.classList.remove('fli-editing');
element.textContent = newValue;
element.dataset.editableValue = newValue;
// Notify
commitEditDirect(key, newValue, oldValue, element, opts);
activeEditor = null;
}
/**
* Directly commit an edit without going through the input UI.
* Used for immediate toggles (bools) and programmatic edits.
*/
function commitEditDirect(key, newValue, oldValue, element, opts) {
if (String(newValue) !== String(oldValue)) {
if (opts.onSave) opts.onSave(key, newValue, oldValue, element);
if (saveCallback) saveCallback(key, newValue, oldValue, element);
// Visual feedback: brief highlight
element.style.transition = 'background 0.3s ease';
element.style.background = 'rgba(88,166,255,0.1)';
setTimeout(function() {
element.style.background = '';
}, 600);
}
}
/**
* Cancel the current inline edit. Restores the original content.
*/
function cancelEdit() {
if (!activeEditor) return;
var element = activeEditor.element;
var opts = activeEditor.opts;
element.classList.remove('fli-editing');
element.replaceChildren.apply(element, activeEditor.oldNodes);
if (opts.onCancel) opts.onCancel(activeEditor.key, activeEditor.oldValue);
activeEditor = null;
}
/**
* Tab to the next (or previous) editable element in the container.
*
* @param {HTMLElement} current - The current editable element
* @param {boolean} reverse - Tab backwards if true
*/
function tabToNext(current, reverse) {
var container = current.closest('[id]');
if (!container || !editors[container.id]) return;
var selector = editors[container.id].selector;
var all = Array.from(container.querySelectorAll(selector));
var idx = all.indexOf(current);
if (idx === -1) return;
var nextIdx = reverse
? (idx - 1 + all.length) % all.length
: (idx + 1) % all.length;
var next = all[nextIdx];
if (next) {
enterEditMode(next, editors[container.id].options);
}
}
// =========================================================================
// Global keyboard shortcuts
// =========================================================================
// Escape to cancel any active editor
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape' && activeEditor) {
cancelEdit();
}
});
// Mark module as loaded
FLI.editable._loaded = true;
console.log('[FLI] editable module loaded');
})();