Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
142 lines (130 loc) · 4.04 KB

File metadata and controls

142 lines (130 loc) · 4.04 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import $ from 'jquery';
var CodeMirror = require('codemirror');
/**
* "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);
var jsonEditor = CodeMirror.fromTextArea(
container.find(options.json_textarea).get(0),
{
mode: 'javascript',
viewportMargin: Infinity,
matchBrackets: true
}
);
// 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();
});
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.