-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathschema-form.provider.js
126 lines (110 loc) · 3.78 KB
/
schema-form.provider.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
import angular from 'angular';
import {
schemaDefaults,
jsonref,
merge,
traverseSchema,
traverseForm,
} from 'json-schema-form-core';
/**
* Schema form service.
*/
export default function() {
let postProcessFn = (form) => form;
const defaults = schemaDefaults.createDefaults();
/**
* Provider API
*/
this.defaults = defaults;
this.stdFormObj = schemaDefaults.stdFormObj;
this.defaultFormDefinition = schemaDefaults.defaultFormDefinition;
/**
* Register a post process function.
* This function is called with the fully merged
* form definition (i.e. after merging with schema)
* and whatever it returns is used as form.
*/
this.postProcess = function(fn) {
postProcessFn = fn;
};
/**
* Append default form rule
*
* @param {string} type json schema type
* @param {Function} rule a function(propertyName,propertySchema,options) that returns a form
* definition or undefined
*/
this.appendRule = function(type, rule) {
if (!this.defaults[type]) {
this.defaults[type] = [];
}
this.defaults[type].push(rule);
};
/**
* Prepend default form rule
*
* @param {string} type json schema type
* @param {Function} rule a function(propertyName,propertySchema,options) that returns a form
* definition or undefined
*/
this.prependRule = function(type, rule) {
if (!this.defaults[type]) {
this.defaults[type] = [];
}
this.defaults[type].unshift(rule);
};
/**
* Utility function to create a standard form object.
* This does *not* set the type of the form but rather all shared attributes.
* You probably want to start your rule with creating the form with this method
* then setting type and any other values you need.
* @param {Object} schema
* @param {Object} options
* @return {Object} a form field defintion
*/
this.createStandardForm = schemaDefaults.stdFormObj;
/* End Provider API */
this.$get = function() {
var service = {};
var typeDefault = this.defaults;
service.jsonref = jsonref;
/**
* Create form defaults from schema
*/
service.defaults = function(schema, types, ignore, options) {
let defaultTypes = types || typeDefault;
return schemaDefaults.defaultForm(schema, defaultTypes, ignore, options);
};
/**
* merge
*
* @param {Object} schema [description]
* @param {Array} [form=['*']] [description]
* @param {Object} [typeDefaults=service.typeDefault] [description]
* @param {boolean} ignore [description]
* @param {Object} [options={}] [description]
* @param {Boolean} [readonly=false] [description]
* @param {[type]} asyncTemplates [description]
*
* @return {[type]} [description]
*/
service.merge = function(schema, form = [ '*' ], typeDefaults=service.typeDefault, ignore, options = {}, readonly = false, asyncTemplates) {
//We look at the supplied form and extend it with schema standards
const canonical = merge(schema, form, typeDefaults, ignore, options, readonly, asyncTemplates);
return postProcessFn(canonical);
};
//Utility functions
/**
* Form defaults for schema by type
* As a form is generated from a schema these are the definitions of each json-schema type
*/
service.typeDefault = typeDefault;
/**
* Traverse a schema, applying a function(schema,path) on every sub schema
* i.e. every property of an object.
*/
service.traverseSchema = traverseSchema;
service.traverseForm = traverseForm;
return service;
};
}