-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathjsonEditor.js
More file actions
175 lines (160 loc) · 5 KB
/
Copy pathjsonEditor.js
File metadata and controls
175 lines (160 loc) · 5 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
import {json} from '@codemirror/lang-json';
import {EditorState} from '@codemirror/state';
import {EditorView} from '@codemirror/view';
import $ from 'jquery';
import {editorConfig} from '@cdo/apps/codemirror/editorConfig';
const jsonEditorTheme = EditorView.theme({
'&': {
border: '1px solid #eee',
},
});
/**
* "Factory" function to transform a container into an interface for
* editing lists of JSON values.
*
* @param {string} container - jQuery selector for the container
* @param {Object} options
* @param {string} options.json_textarea
* @param {string} options.add_button
* @param {string} options.remove_button
* @param {string} options.value_space
* @param {string} options.template
* @param {string} options.form_container
* @param {string} options.wrapper
* @param {Object} options.model
*/
module.exports = function (container, options) {
container = $(container);
const textarea = container.find(options.json_textarea).get(0);
const editorContainer = document.createElement('div');
textarea.style.display = 'none';
textarea.insertAdjacentElement('afterend', editorContainer);
const editorView = new EditorView({
state: EditorState.create({
doc: textarea.value || '',
extensions: [
...editorConfig,
json(),
jsonEditorTheme,
EditorView.lineWrapping,
EditorView.updateListener.of(update => {
if (update.docChanged) {
textarea.value = update.state.doc.toString();
}
}),
],
}),
parent: editorContainer,
});
const jsonEditor = {
getValue() {
return editorView.state.doc.toString();
},
setValue(value) {
editorView.dispatch({
changes: {
from: 0,
to: editorView.state.doc.length,
insert: value,
},
});
},
};
// Create spaces for each element in the original JSON
var jsonContent = jsonEditor.getValue();
if (jsonContent.length > 0) {
var valuesToUpdate = JSON.parse(jsonEditor.getValue());
$.each(valuesToUpdate, function (index, value) {
updateTemplate(value, $createNewSpace());
});
}
/**
* For each key in the given model, set the <input> with a matching class name to the key's value.
* @param {model} The model to use when updating the DOM.
* @param {$template} The jQuery element to search for <input> elements.
*/
function updateTemplate(model, $template) {
$.each(model, function (key, value) {
if (value && typeof value === 'object') {
updateTemplate(value, $template);
} else {
var item = $template.find('.' + key);
if (item.prop('type') === 'checkbox') {
item.prop('checked', model[key]);
} else {
item.val(model[key]);
}
}
});
if (options.onNewSpace) {
options.onNewSpace($template);
}
}
/**
* For each key in the given model, set the key's value to the value of the <input> with a matching class name.
* @param {model} The model to update from the DOM.
* @param {$template} The jQuery element to search for <input> elements.
*/
function updateModel(model, $template) {
$.each(model, function (key, value) {
if (typeof value === 'object') {
updateModel(value, $template);
} else {
var item = $template.find('.' + key);
if (item.prop('type') === 'checkbox') {
value = item.prop('checked');
} else {
value = item.val();
}
model[key] = typeof model[key] === 'number' ? +value : value;
}
});
}
function updateJSON() {
var updatedValues = [];
container
.find(options.form_container)
.find(options.value_space)
.each(function () {
var model = $.extend(true, {}, options.model);
updateModel(model, $(this));
updatedValues.push(model);
});
jsonEditor.setValue(JSON.stringify(updatedValues, null, ' '));
}
function $createNewSpace() {
var $newValue = container.find(options.template).children(':first').clone();
container.find(options.form_container).append($newValue);
return $newValue;
}
if (options.up_button) {
container.on('click', options.up_button, function () {
var wrapper = $(this).closest(options.value_space);
if (wrapper.prev().length) {
wrapper.insertBefore(wrapper.prev());
updateJSON();
}
});
}
if (options.down_button) {
container.on('click', options.down_button, function () {
var wrapper = $(this).closest(options.value_space);
if (wrapper.next().length) {
wrapper.insertAfter(wrapper.next());
updateJSON();
}
});
}
container.on('click', options.add_button, function () {
var model = $.extend(true, {}, options.model);
updateTemplate(model, $createNewSpace());
updateJSON();
});
container.on('click', options.remove_button, function () {
$(this).closest(options.value_space).remove();
updateJSON();
});
container.on('change', options.wrapper, function () {
updateJSON();
});
};