forked from jsonform/jsonform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.js
More file actions
222 lines (211 loc) · 7.58 KB
/
Copy pathplayground.js
File metadata and controls
222 lines (211 loc) · 7.58 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
/*global $, ace, console*/
$('document').ready(function () {
var formObject = {
schema: {
example: {
title: 'JSON Form example to start from',
type: 'string',
'enum': [
'gettingstarted',
'schema-basic',
'schema-morecomplex',
'schema-array',
'fields-common',
'fields-password',
'fields-textarea',
'fields-ace',
'fields-color',
'fields-checkbox',
'fields-checkboxes',
'fields-select',
'fields-radios',
'fields-radiobuttons',
'fields-range',
'fields-imageselect',
'fields-fieldset',
'fields-advancedfieldset',
'fields-authfieldset',
'fields-section',
'fields-actions',
'fields-array',
'fields-tabarray',
'fields-tabarray-maxitems',
'fields-selectfieldset',
'fields-selectfieldset-key',
'fields-submit',
'fields-help',
'fields-hidden',
'fields-questions',
'templating-idx',
'templating-value',
'templating-values',
'templating-tpldata',
'events',
'previousvalues',
'factory-sleek'
],
'default': 'gettingstarted'
},
greatform: {
title: 'JSON Form object to render',
type: 'string'
}
},
form: [
{
key: 'example',
notitle: true,
prepend: 'Try with',
htmlClass: 'trywith',
titleMap: {
'gettingstarted': 'Getting started',
'schema-basic': 'JSON Schema - A basic example',
'schema-morecomplex': 'JSON Schema - Slightly more complex example',
'schema-array': 'JSON Schema - Arrays',
'fields-common': 'Fields - Common properties',
'fields-password': 'Fields - Gathering secrets: the password type',
'fields-textarea': 'Fields - Large text: the textarea type',
'fields-ace': 'Fields - Code (JavaScript, JSON...): the ace type',
'fields-color': 'Fields - Color picker: the color type',
'fields-checkbox': 'Fields - Boolean flag: the checkbox type',
'fields-checkboxes': 'Fields - Multiple options: the checkboxes type',
'fields-select': 'Fields - Selection list: the select type',
'fields-radios': 'Fields - A list of radio buttons: the radios type',
'fields-radiobuttons': 'Fields - Radio buttons as real buttons: the radio buttons type',
'fields-range': 'Fields - Number: the range type',
'fields-imageselect': 'Fields - Image selector: the imageselect type',
'fields-fieldset': 'Fields - Grouping: the fieldset type',
'fields-advancedfieldset': 'Fields - Advanced options section: the advancedfieldset type',
'fields-authfieldset': 'Fields - Authentication settings section: the authfieldset type',
'fields-section': 'Fields - Generic group: the section type',
'fields-actions': 'Fields - Group of buttons: the actions type',
'fields-array': 'Fields - Generic array: the array type',
'fields-tabarray': 'Fields - Arrays with tabs: the tabarray type',
'fields-tabarray-maxitems': 'Fields - Arrays with tabs: the tabarray type w/ maxItems',
'fields-selectfieldset': 'Fields - Alternative: the selectfieldset type',
'fields-selectfieldset-key': 'Fields - Alternative with schema key',
'fields-submit': 'Fields - Submit the form: the submit type',
'fields-help': 'Fields - Guide users: the help type',
'fields-hidden': 'Fields - Hidden form values: the hidden type',
'fields-questions': 'Fields - Series of questions: the questions type',
'templating-idx': 'Templating - item index with idx',
'templating-value': 'Templating - tab legend with value and valueInLegend',
'templating-values': 'Templating - values.xxx to reference another field',
'templating-tpldata': 'Templating - Using the tpldata property',
'events': 'Using event handlers',
'previousvalues': 'Using previously submitted values',
'factory-sleek': 'Joshfire Factory - Sleek template'
},
onChange: function (evt) {
var selected = $(evt.target).val();
loadExample(selected);
if (history) history.pushState(
{ example: selected},
'Example - ' + selected,
'?example=' + selected);
}
},
{
key: 'greatform',
type: 'ace',
aceMode: 'json',
width: '100%',
height: '' + (window.innerHeight - 140) + 'px',
notitle: true,
onChange: function () {
generateForm();
}
}
]
};
/**
* Extracts a potential form to load from query string
*/
var getRequestedExample = function () {
var query = window.location.search.substring(1);
var vars = query.split('&');
var param = null;
for (var i = 0; i < vars.length; i++) {
param = vars[i].split('=');
if (param[0] === 'example') {
return param[1];
}
}
return null;
};
/**
* Loads and displays the example identified by the given name
*/
var loadExample = function (example) {
$.ajax({
url: 'examples/' + example + '.json',
dataType: 'text'
}).done(function (code) {
var aceId = $('#form .ace_editor').attr('id');
var editor = ace.edit(aceId);
editor.getSession().setValue(code);
}).fail(function () {
$('#result').html('Sorry, I could not retrieve the example!');
});
};
/**
* Displays the form entered by the user
* (this function runs whenever once per second whenever the user
* changes the contents of the ACE input field)
*/
var generateForm = function () {
var values = $('#form').jsonFormValue();
// Reset result pane
$('#result').html('');
// Parse entered content as JavaScript
// (mostly JSON but functions are possible)
var createdForm = null;
try {
// Most examples should be written in pure JSON,
// but playground is helpful to check behaviors too!
eval('createdForm=' + values.greatform);
}
catch (e) {
$('#result').html('<pre>Entered content is not yet a valid' +
' JSON Form object.\n\nJavaScript parser returned:\n' +
e + '</pre>');
return;
}
// Render the resulting form, binding to onSubmitValid
try {
createdForm.onSubmitValid = function (values) {
if (console && console.log) {
console.log('Values extracted from submitted form', values);
}
window.alert('Form submitted. Values object:\n' +
JSON.stringify(values, null, 2));
};
createdForm.onSubmit = function (errors, values) {
if (errors) {
console.log('Validation errors', errors);
return false;
}
return true;
};
$('#result').html('<form id="result-form" class="form-vertical"></form>');
$('#result-form').jsonForm(createdForm);
}
catch (e) {
$('#result').html('<pre>Entered content is not yet a valid' +
' JSON Form object.\n\nThe JSON Form library returned:\n' +
e + '</pre>');
return;
}
};
// Render the form
$('#form').jsonForm(formObject);
// Wait until ACE is loaded
var itv = window.setInterval(function() {
var example = getRequestedExample() || 'gettingstarted';
$('.trywith select').val(example);
if (window.ace) {
window.clearInterval(itv);
loadExample(example);
}
}, 1000);
});