forked from jdorn/json-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults.js
313 lines (299 loc) · 10.6 KB
/
defaults.js
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
// Set the default theme
JSONEditor.defaults.theme = 'html';
// Set the default template engine
JSONEditor.defaults.template = 'default';
// Default options when initializing JSON Editor
JSONEditor.defaults.options = {};
// String translate function
JSONEditor.defaults.translate = function(key, variables) {
var lang = JSONEditor.defaults.languages[JSONEditor.defaults.language];
if(!lang) throw "Unknown language "+JSONEditor.defaults.language;
var string = lang[key] || JSONEditor.defaults.languages[JSONEditor.defaults.default_language][key];
if(typeof string === "undefined") throw "Unknown translate string "+key;
if(variables) {
for(var i=0; i<variables.length; i++) {
string = string.replace(new RegExp('\\{\\{'+i+'}}','g'),variables[i]);
}
}
return string;
};
// Translation strings and default languages
JSONEditor.defaults.default_language = 'en';
JSONEditor.defaults.language = JSONEditor.defaults.default_language;
JSONEditor.defaults.languages.en = {
/**
* When a property is not set
*/
error_notset: "Property must be set",
/**
* When a string must not be empty
*/
error_notempty: "Value required",
/**
* When a value is not one of the enumerated values
*/
error_enum: "Value must be one of the enumerated values",
/**
* When a value doesn't validate any schema of a 'anyOf' combination
*/
error_anyOf: "Value must validate against at least one of the provided schemas",
/**
* When a value doesn't validate
* @variables This key takes one variable: The number of schemas the value does not validate
*/
error_oneOf: 'Value must validate against exactly one of the provided schemas. It currently validates against {{0}} of the schemas.',
/**
* When a value does not validate a 'not' schema
*/
error_not: "Value must not validate against the provided schema",
/**
* When a value does not match any of the provided types
*/
error_type_union: "Value must be one of the provided types",
/**
* When a value does not match the given type
* @variables This key takes one variable: The type the value should be of
*/
error_type: "Value must be of type {{0}}",
/**
* When the value validates one of the disallowed types
*/
error_disallow_union: "Value must not be one of the provided disallowed types",
/**
* When the value validates a disallowed type
* @variables This key takes one variable: The type the value should not be of
*/
error_disallow: "Value must not be of type {{0}}",
/**
* When a value is not a multiple of or divisible by a given number
* @variables This key takes one variable: The number mentioned above
*/
error_multipleOf: "Value must be a multiple of {{0}}",
/**
* When a value is greater than it's supposed to be (exclusive)
* @variables This key takes one variable: The maximum
*/
error_maximum_excl: "Value must be less than {{0}}",
/**
* When a value is greater than it's supposed to be (inclusive
* @variables This key takes one variable: The maximum
*/
error_maximum_incl: "Value must be at most {{0}}",
/**
* When a value is lesser than it's supposed to be (exclusive)
* @variables This key takes one variable: The minimum
*/
error_minimum_excl: "Value must be greater than {{0}}",
/**
* When a value is lesser than it's supposed to be (inclusive)
* @variables This key takes one variable: The minimum
*/
error_minimum_incl: "Value must be at least {{0}}",
/**
* When a value have too many characters
* @variables This key takes one variable: The maximum character count
*/
error_maxLength: "Value must be at most {{0}} characters long",
/**
* When a value does not have enough characters
* @variables This key takes one variable: The minimum character count
*/
error_minLength: "Value must be at least {{0}} characters long",
/**
* When a value does not match a given pattern
*/
error_pattern: "Value must match the pattern {{0}}",
/**
* When an array has additional items whereas it is not supposed to
*/
error_additionalItems: "No additional items allowed in this array",
/**
* When there are to many items in an array
* @variables This key takes one variable: The maximum item count
*/
error_maxItems: "Value must have at most {{0}} items",
/**
* When there are not enough items in an array
* @variables This key takes one variable: The minimum item count
*/
error_minItems: "Value must have at least {{0}} items",
/**
* When an array is supposed to have unique items but has duplicates
*/
error_uniqueItems: "Array must have unique items",
/**
* When there are too many properties in an object
* @variables This key takes one variable: The maximum property count
*/
error_maxProperties: "Object must have at most {{0}} properties",
/**
* When there are not enough properties in an object
* @variables This key takes one variable: The minimum property count
*/
error_minProperties: "Object must have at least {{0}} properties",
/**
* When a required property is not defined
* @variables This key takes one variable: The name of the missing property
*/
error_required: "Object is missing the required property '{{0}}'",
/**
* When there is an additional property is set whereas there should be none
* @variables This key takes one variable: The name of the additional property
*/
error_additional_properties: "No additional properties allowed, but property {{0}} is set",
/**
* When a dependency is not resolved
* @variables This key takes one variable: The name of the missing property for the dependency
*/
error_dependency: "Must have property {{0}}",
/**
* Text on Delete All buttons
*/
button_delete_all: "All",
/**
* Title on Delete All buttons
*/
button_delete_all_title: "Delete All",
/**
* Text on Delete Last buttons
* @variable This key takes one variable: The title of object to delete
*/
button_delete_last: "Last {{0}}",
/**
* Title on Delete Last buttons
* @variable This key takes one variable: The title of object to delete
*/
button_delete_last_title: "Delete Last {{0}}",
/**
* Title on Add Row buttons
* @variable This key takes one variable: The title of object to add
*/
button_add_row_title: "Add {{0}}",
/**
* Title on Move Down buttons
*/
button_move_down_title: "Move down",
/**
* Title on Move Up buttons
*/
button_move_up_title: "Move up",
/**
* Title on Delete Row buttons
* @variable This key takes one variable: The title of object to delete
*/
button_delete_row_title: "Delete {{0}}",
/**
* Title on Delete Row buttons, short version (no parameter with the object title)
*/
button_delete_row_title_short: "Delete",
/**
* Title on Collapse buttons
*/
button_collapse: "Collapse",
/**
* Title on Expand buttons
*/
button_expand: "Expand"
};
// Miscellaneous Plugin Settings
JSONEditor.plugins = {
ace: {
theme: ''
},
epiceditor: {
},
sceditor: {
},
select2: {
},
selectize: {
}
};
// Default per-editor options
$each(JSONEditor.defaults.editors, function(i,editor) {
JSONEditor.defaults.editors[i].options = editor.options || {};
});
// Set the default resolvers
// Use "multiple" as a fall back for everything
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(typeof schema.type !== "string") return "multiple";
});
// If the type is not set but properties are defined, we can infer the type is actually object
JSONEditor.defaults.resolvers.unshift(function(schema) {
// If the schema is a simple type
if(!schema.type && schema.properties ) return "object";
});
// If the type is set and it's a basic type, use the primitive editor
JSONEditor.defaults.resolvers.unshift(function(schema) {
// If the schema is a simple type
if(typeof schema.type === "string") return schema.type;
});
// Boolean editors
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(schema.type === 'boolean') {
// If explicitly set to 'checkbox', use that
if(schema.format === "checkbox" || (schema.options && schema.options.checkbox)) {
return "checkbox";
}
// Otherwise, default to select menu
return (JSONEditor.plugins.selectize.enable) ? 'selectize' : 'select';
}
});
// Use the multiple editor for schemas where the `type` is set to "any"
JSONEditor.defaults.resolvers.unshift(function(schema) {
// If the schema can be of any type
if(schema.type === "any") return "multiple";
});
// Editor for base64 encoded files
JSONEditor.defaults.resolvers.unshift(function(schema) {
// If the schema can be of any type
if(schema.type === "string" && schema.media && schema.media.binaryEncoding==="base64") {
return "base64";
}
});
// Editor for uploading files
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(schema.type === "string" && schema.format === "url" && schema.options && schema.options.upload === true) {
if(window.FileReader) return "upload";
}
});
// Use the table editor for arrays with the format set to `table`
JSONEditor.defaults.resolvers.unshift(function(schema) {
// Type `array` with format set to `table`
if(schema.type == "array" && schema.format == "table") {
return "table";
}
});
// Use the `select` editor for dynamic enumSource enums
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(schema.enumSource) return (JSONEditor.plugins.selectize.enable) ? 'selectize' : 'select';
});
// Use the `enum` or `select` editors for schemas with enumerated properties
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(schema["enum"]) {
if(schema.type === "array" || schema.type === "object") {
return "enum";
}
else if(schema.type === "number" || schema.type === "integer" || schema.type === "string") {
return (JSONEditor.plugins.selectize.enable) ? 'selectize' : 'select';
}
}
});
// Specialized editors for arrays of strings
JSONEditor.defaults.resolvers.unshift(function(schema) {
if(schema.type === "array" && schema.items && !(Array.isArray(schema.items)) && schema.uniqueItems && ['string','number','integer'].indexOf(schema.items.type) >= 0) {
// For enumerated strings, number, or integers
if(schema.items.enum) {
return 'multiselect';
}
// For non-enumerated strings (tag editor)
else if(JSONEditor.plugins.selectize.enable && schema.items.type === "string") {
return 'arraySelectize';
}
}
});
// Use the multiple editor for schemas with `oneOf` set
JSONEditor.defaults.resolvers.unshift(function(schema) {
// If this schema uses `oneOf` or `anyOf`
if(schema.oneOf || schema.anyOf) return "multiple";
});