forked from contentful/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
303 lines (279 loc) · 11.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>JSON Form Editor Widget</title>
<script src="https://unpkg.com/contentful-ui-extensions-sdk@3/dist/cf-extension-api.js"></script>
<script src="https://unpkg.com/[email protected]/dist/jsoneditor.js"></script>
<link rel="stylesheet" href="https://unpkg.com/contentful-ui-extensions-sdk@3/dist/cf-extension.css" />
</head>
<body>
<script>
JSONEditor.defaults.themes.contentful = (function (JSONEditor) {
var PARENT = JSONEditor.AbstractTheme;
var definition = {
getFormControl: 'cf-form-field',
getIndentedPanel: function () {
// No need for _super's .style attributes.
return document.createElement('div');
},
getFormInputField: 'cf-form-input',
getFormInputDescription: 'cf-form-hint',
getFormInputLabel: 'jfe-label',
afterInputReady: function (input) {
input.cfFormField = input.cfFormField || this.closest(input, '.cf-form-field');
},
getButton: el(function (el, text, icon, title) {
el.className = text.match(/save/i) ? 'cf-btn-primary' : 'cf-btn-secondary';
}),
getHeader: el(function (el, text) {
if (!(text instanceof HTMLElement)) {
var label = document.createElement('span');
label.appendChild(document.createTextNode(text));
el.innerHTML = '';
el.appendChild(label);
text = label;
}
text.className = 'jfe-label jfe-header-label';
var el2 = document.createElement('div');
el2.innerHTML = el.innerHTML;
return el2;
}),
getHeaderButtonHolder: 'jfe-button-holder',
getSelectInput: 'cf-form-input',
getTextareaInput: 'cf-form-input',
getSwitcher: function (options) {
var el = this.getSelectInput(options);
el.className += ' jfe-switcher';
return el;
},
getErrorMessage: function (text) {
var el = document.createElement('div');
el.appendChild(document.createTextNode(text));
el.className = 'cf-field-error';
return el;
},
addInputError: function (input, text) {
if (!input.cfFormField) {
return;
}
if (!input.cfFieldError) {
input.cfFieldError = document.createElement('div');
input.cfFieldError.className = 'cf-field-error';
input.cfFormField.appendChild(input.cfFieldError);
}
input.cfFieldError.textContent = text;
input.className += ' cf-has-error'
},
removeInputError: function (input) {
if (!input.cfFieldError) {
return;
}
input.cfFormField.removeChild(input.cfFieldError);
delete input.cfFieldError;
input.cfFormField.className = input.cfFormField.className.replace(/\s?cf-field-error/g, '');
input.className = input.className.replace(/\s?cf-has-error/g, '');
}
};
// Replace strings in definition with functions which call _super() and add the
// string as a class name.
for (var name in definition) {
var value = definition[name];
if (typeof value === 'string') {
definition[name] = elCssClass(value);
}
}
// Make all getters in the definition add a class name to the returned HTMLElement.
// E.g. class="jfe-get-error-message" for element returned by getErrorMessage().
for (var name in PARENT.prototype) {
if (name.substr(0, 3) === 'get') {
var cssClass = name
.replace(/^get/, 'jfe')
.replace(/([A-Z])/g, '-$1')
.toLowerCase();
var superFn = typeof definition[name] === 'function' ? definition[name] : false;
definition[name] = elCssClass(cssClass, superFn);
}
}
return PARENT.extend(definition);
function elCssClass (value, _super) {
return el(function (el) {
if (el instanceof HTMLElement) {
el.className += (el.className ? ' ' : '' ) + value;
}
}, _super);
}
function el (fn, _super) {
return function (el) {
var el = (_super || this._super).apply(this, arguments);
if(!_super) {
// Reset all class names coming from default theme.
el.className = '';
}
var callbackArgs = [el].concat(Array.prototype.slice.call(arguments));
return fn.apply(this, callbackArgs) || el;
};
}
}(JSONEditor));
// https://github.com/jdorn/json-editor/blob/master/examples/advanced.html
// TODO: Make this configurable on Data Type level.
/*eslint quotes: [2, "double"]*/
window.CONTENTFUL_FORM_EDITOR_SCHEMA = {
"title": "Person",
"oneOf": [
{
"$ref": "#/definitions/basicperson",
"title": "Basic Person"
},
{
"$ref": "#/definitions/person",
"title": "Complex Person"
}
],
"definitions": {
"basicperson": {
"title": "Person",
"type": "object",
"id": "person",
"properties": {
"name": {
"type": "string",
"description": "First and Last name",
"minLength": 4,
"propertyOrder": 10
},
"age": {
"type": "integer",
"default": 21,
"minimum": 18,
"maximum": 99,
"propertyOrder": 20
},
"gender": {
"type": "string",
"enum": [
"male",
"female"
],
"propertyOrder": 30
},
"favorite_color": {
"type": "string",
"format": "color",
"title": "favorite color",
"default": "#19CD91",
"propertyOrder": 40
}
}
},
"person": {
"$ref": "#/definitions/basicperson",
"properties": {
"location": {
"type": "object",
"title": "Location",
"properties": {
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"citystate": {
"type": "string",
"description": "This is generated automatically from the previous two fields",
"template": "{{city}}, {{state}}",
"watch": {
"city": "location.city",
"state": "location.state"
}
}
}
},
"pets": {
"type": "array",
"format": "table",
"title": "Pets",
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"cat",
"dog",
"bird",
"reptile",
"other"
],
"default": "dog"
},
"name": {
"type": "string"
},
"fixed": {
"type": "boolean",
"title": "spayed / neutered"
}
}
}
}
}
}
}
};
var cfExt = window.contentfulExtension || window.contentfulWidget;
cfExt.init(initContentfulJsonFormEditor);
function initContentfulJsonFormEditor (cfApi) {
cfApi.window.startAutoResizer();
var editorElem = createElement('div', {className: 'jfe-editor-root'});
var editor = new JSONEditor(editorElem, {
theme: 'contentful',
schema: window.CONTENTFUL_FORM_EDITOR_SCHEMA,
no_additional_properties: true,
required_by_default: true,
startval: cfApi.field.getValue(),
disable_collapse: true,
disable_properties: true,
show_errors: 'always'
});
editor.on('change', inputChanged);
function inputChanged () {
cfApi.window.updateHeight();
validateAndSave();
}
var validateAndSave = debounce(function () {
var errors = editor.validate();
if (errors.length === 0) {
var currentJSON = editor.getValue();
cfApi.field.setValue(currentJSON);
}
}, 150);
}
function createElement (elem, opts, parent) {
var e = document.createElement(elem);
var prop;
for (prop in opts) {
e[prop] = opts[prop];
}
parent = parent || document.body;
parent.appendChild(e);
return e;
}
// http://davidwalsh.name/javascript-debounce-function
function debounce (func, wait) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (!timeout) func.apply(context, args);
};
}
</script>
</body>
</html>