-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
400 lines (367 loc) · 15.4 KB
/
script.js
File metadata and controls
400 lines (367 loc) · 15.4 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
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
document.addEventListener('DOMContentLoaded', () => {
const fileInput = document.getElementById('file-input');
const saveBtn = document.getElementById('save-btn');
const copyBtn = document.getElementById('copy-btn');
const jsonEditor = document.getElementById('json-editor');
const modal = document.getElementById('add-modal');
const modalHeader = document.getElementById('modal-header');
const modalFormContent = document.getElementById('modal-form-content');
let currentData = null;
let currentFileName = 'edited.json';
const renderJSON = (data, parentElement) => {
parentElement.innerHTML = '';
if (typeof data === 'object' && data !== null) {
parentElement.appendChild(createObjectBox(data, 'Root', 0, () => {}));
} else {
parentElement.textContent = 'JSON data is not a valid object or array.';
}
};
const createObjectBox = (data, key, level, deleteCallback) => {
const box = document.createElement('div');
box.className = 'object-box';
const header = document.createElement('div');
header.className = 'object-header';
const title = document.createElement('span');
title.className = `object-title level-${level}`;
title.textContent = key;
header.appendChild(title);
const actions = document.createElement('div');
actions.className = 'action-buttons';
const addButton = createButton(Array.isArray(data) ? 'Add Item' : 'Add Field', 'add-btn', () => {
if (Array.isArray(data)) addItem(data);
else addField(data);
});
actions.appendChild(addButton);
if (level > 0) {
actions.appendChild(createButton('Delete', 'delete-btn', deleteCallback));
}
header.appendChild(actions);
box.appendChild(header);
const content = document.createElement('div');
content.className = 'object-content';
renderNodeContent(data, content, level + 1);
box.appendChild(content);
return box;
};
const renderNodeContent = (data, parentElement, level) => {
parentElement.innerHTML = '';
const schema = getObjectSchema(data);
if (schema && schema.length > 3) {
if (Array.isArray(data)) {
parentElement.appendChild(renderArrayAsTable(data, schema, null, data));
} else {
parentElement.appendChild(renderArrayAsTable(Object.values(data), schema, Object.keys(data), data));
}
return;
}
const propertyGrid = document.createElement('div');
propertyGrid.className = 'property-grid';
const nestedObjects = [];
const processEntry = (value, key) => {
if (typeof value === 'object' && value !== null) {
nestedObjects.push({ key, value });
} else {
renderPrimitive(key, value, data, propertyGrid);
}
};
if (Array.isArray(data)) {
data.forEach((item, index) => processEntry(item, index));
} else {
Object.keys(data).forEach(key => processEntry(data[key], key));
}
if (propertyGrid.hasChildNodes()) {
parentElement.appendChild(propertyGrid);
}
nestedObjects.forEach(({ key, value }) => {
parentElement.appendChild(createObjectBox(value, key, level, () => deleteProperty(data, key)));
});
};
const renderArrayAsTable = (dataArray, schema, keys, parentObject) => {
const table = document.createElement('table');
table.className = 'horizontal-table';
const thead = table.createTHead().insertRow();
if (keys) { // It's an object being displayed as a table
thead.appendChild(document.createElement('th')).textContent = 'Key';
}
schema.forEach(key => thead.appendChild(document.createElement('th')).textContent = key);
thead.appendChild(document.createElement('th')).textContent = 'Actions';
const tbody = table.createTBody();
dataArray.forEach((item, index) => {
const row = tbody.insertRow();
if (keys) {
row.insertCell().textContent = keys[index];
}
schema.forEach(key => {
const input = document.createElement('input');
input.type = 'text';
input.value = item[key] !== undefined ? item[key] : '';
input.onchange = () => updateValue(item, key, input.value);
row.insertCell().appendChild(input);
});
const actionsCell = row.insertCell();
actionsCell.appendChild(createButton('Delete', 'delete-btn', () => {
if (keys && parentObject) {
// For objects displayed as tables, delete the property from the parent object
const keyToDelete = keys[index];
deleteProperty(parentObject, keyToDelete);
} else {
// For arrays, delete the item at the specific index
deleteProperty(dataArray, index);
}
}));
});
return table;
};
const renderPrimitive = (key, value, parentObject, grid) => {
const keyDiv = document.createElement('div');
keyDiv.className = 'property-key';
keyDiv.textContent = key;
const valueDiv = document.createElement('div');
valueDiv.className = 'property-value';
const input = document.createElement('input');
input.type = 'text';
input.value = value;
input.onchange = () => updateValue(parentObject, key, input.value);
valueDiv.appendChild(input);
const actionsDiv = document.createElement('div');
actionsDiv.className = 'property-actions';
actionsDiv.appendChild(createButton('Delete', 'delete-btn', () => deleteProperty(parentObject, key)));
grid.appendChild(keyDiv);
grid.appendChild(valueDiv);
grid.appendChild(actionsDiv);
};
const isFlatObject = (obj) => obj !== null && typeof obj === 'object' && !Array.isArray(obj) && Object.values(obj).every(val => typeof val !== 'object' || val === null);
const getObjectSchema = (obj) => {
if (Array.isArray(obj)) return getArraySchema(obj);
if (!obj || typeof obj !== 'object') return null;
const values = Object.values(obj);
if (values.length === 0) return null;
return getArraySchema(values);
};
const getArraySchema = (arr) => {
if (!Array.isArray(arr) || arr.length === 0) return null;
const first = arr[0];
if (!isFlatObject(first)) return null;
const firstKeys = Object.keys(first);
if (firstKeys.length === 0) return null;
for (let i = 1; i < arr.length; i++) {
const current = arr[i];
if (!isFlatObject(current)) return null;
const currentKeys = Object.keys(current);
if (currentKeys.length !== firstKeys.length || currentKeys.some((k, j) => k !== firstKeys[j])) {
return null;
}
}
return firstKeys;
};
const showModal = (title, fields, callback) => {
modalHeader.textContent = title;
modalFormContent.innerHTML = '';
const formGrid = document.createElement('div');
formGrid.className = 'modal-form-grid';
const inputs = {};
fields.forEach(field => {
const label = document.createElement('label');
label.htmlFor = `modal-field-${field.name}`;
label.textContent = field.label;
const input = document.createElement('input');
input.type = field.type || 'text';
input.id = `modal-field-${field.name}`;
input.name = field.name;
input.value = field.defaultValue || '';
formGrid.appendChild(label);
formGrid.appendChild(input);
inputs[field.name] = input;
});
modalFormContent.appendChild(formGrid);
const actions = document.createElement('div');
actions.className = 'modal-actions';
const saveButton = createButton('Save', 'add-btn', () => {
const result = {};
for (const name in inputs) {
result[name] = inputs[name].value;
}
callback(result);
hideModal();
});
const cancelButton = createButton('Cancel', 'cancel-btn', hideModal);
actions.appendChild(cancelButton);
actions.appendChild(saveButton);
modalFormContent.appendChild(actions);
modal.style.display = 'flex';
};
const hideModal = () => {
modal.style.display = 'none';
};
const addField = (obj) => {
const schema = getObjectSchema(obj);
if (schema) {
const fields = [{ name: '__new_key__', label: 'Key' }, ...schema.map(key => ({ name: key, label: key }))];
showModal('Add New Field', fields, (result) => {
const newKey = result['__new_key__'];
if (newKey && !obj.hasOwnProperty(newKey)) {
const newItem = {};
for (const key in result) {
if (key !== '__new_key__') {
newItem[key] = parseValue(result[key]);
}
}
obj[newKey] = newItem;
renderJSON(currentData, jsonEditor);
} else if (newKey) {
alert(`Key "${newKey}" already exists.`);
}
});
} else {
const fields = [
{ name: 'key', label: 'Key' },
{ name: 'value', label: 'Value', defaultValue: 'null' }
];
showModal('Add New Field', fields, (result) => {
if (result.key && !obj.hasOwnProperty(result.key)) {
obj[result.key] = parseValue(result.value);
renderJSON(currentData, jsonEditor);
} else if (result.key) {
alert(`Key "${result.key}" already exists.`);
}
});
}
};
const generateFormFields = (obj, prefix = '') => {
const fields = [];
for (const key in obj) {
const value = obj[key];
const newPrefix = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
fields.push(...generateFormFields(value, newPrefix));
} else {
fields.push({ name: newPrefix, label: newPrefix, defaultValue: value });
}
}
return fields;
};
const unflatten = (obj) => {
const result = {};
for (const key in obj) {
const keys = key.split('.');
keys.reduce((acc, part, i) => {
if (i === keys.length - 1) {
acc[part] = parseValue(obj[key]);
} else {
acc[part] = acc[part] || (Number.isInteger(Number(keys[i+1])) ? [] : {});
}
return acc[part];
}, result);
}
return result;
};
const addItem = (arr) => {
if (arr.length > 0 && Array.isArray(arr[0])) {
const template = arr[0];
const fields = template.map((_, index) => ({ name: `value-${index}`, label: `Value ${index}` }));
showModal('Add New Array Item', fields, (result) => {
const newItem = [];
for (const key in result) {
newItem.push(parseValue(result[key]));
}
arr.push(newItem);
renderJSON(currentData, jsonEditor);
});
return;
}
const schema = getArraySchema(arr);
if (schema) {
const fields = schema.map(key => ({ name: key, label: key, defaultValue: '' }));
showModal('Add New Item', fields, (result) => {
const newItem = {};
for (const key in result) {
newItem[key] = parseValue(result[key]);
}
arr.push(newItem);
renderJSON(currentData, jsonEditor);
});
} else if (arr.length > 0 && typeof arr[0] === 'object' && arr[0] !== null) {
const template = arr[0];
const fields = generateFormFields(template);
showModal('Add New Item', fields, (result) => {
const newItem = unflatten(result);
arr.push(newItem);
renderJSON(currentData, jsonEditor);
});
} else {
const fields = [{ name: 'value', label: 'Value', defaultValue: 'null' }];
showModal('Add New Item', fields, (result) => {
arr.push(parseValue(result.value));
renderJSON(currentData, jsonEditor);
});
}
};
const parseValue = (str) => {
if (str.toLowerCase() === 'true') return true;
if (str.toLowerCase() === 'false') return false;
if (str.trim() !== '' && !isNaN(Number(str)) && !str.startsWith('0x')) return Number(str);
try {
return JSON.parse(str);
} catch (e) {
return str;
}
};
const createButton = (text, className, onClick) => {
const button = document.createElement('button');
button.textContent = text;
button.className = className;
button.onclick = (e) => {
e.stopPropagation();
onClick();
};
return button;
};
const updateValue = (obj, key, newValueStr) => {
const originalValue = obj[key];
obj[key] = parseValue(newValueStr);
};
const deleteProperty = (parent, key) => {
if (confirm(`Are you sure you want to delete '${key}'?`)) {
if (Array.isArray(parent)) {
parent.splice(Number(key), 1);
} else {
delete parent[key];
}
renderJSON(currentData, jsonEditor);
}
};
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
currentFileName = file.name;
const reader = new FileReader();
reader.onload = (e) => {
try {
currentData = JSON.parse(e.target.result);
renderJSON(currentData, jsonEditor);
} catch (error) { alert(`Error parsing JSON file: ${error.message}`); }
};
reader.readAsText(file);
}
});
saveBtn.addEventListener('click', () => {
if (!currentData) return alert('No data to save.');
const jsonData = JSON.stringify(currentData, null, 2);
const blob = new Blob([jsonData], { type: 'application/json' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = currentFileName;
a.click();
URL.revokeObjectURL(a.href);
});
copyBtn.addEventListener('click', () => {
if (!currentData) return alert('No data to copy.');
const jsonData = JSON.stringify(currentData, null, 2);
navigator.clipboard.writeText(jsonData).then(() => alert('JSON copied!'), () => alert('Copy failed.'));
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
hideModal();
}
});
});