Skip to content

Commit 15e84e3

Browse files
committed
Changed to provider instead of factory
1 parent 1b483a3 commit 15e84e3

File tree

1 file changed

+107
-92
lines changed

1 file changed

+107
-92
lines changed

src/services/schema-form.js

Lines changed: 107 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,8 @@
33
* This service is not that useful outside of schema form directive
44
* but makes the code more testable.
55
*/
6-
angular.module('schemaForm').factory('schemaForm',[function(){
7-
var service = {};
6+
angular.module('schemaForm').provider('schemaForm',[function(){
87

9-
service.merge = function(schema,form,ignore) {
10-
form = form || ["*"];
11-
12-
var stdForm = service.defaults(schema,ignore);
13-
14-
//simple case, we have a "*", just put the stdForm there
15-
var idx = form.indexOf("*");
16-
if (idx !== -1) {
17-
form = form.slice(0,idx)
18-
.concat(stdForm.form)
19-
.concat(form.slice(idx+1));
20-
return form;
21-
}
22-
23-
//ok let's merge!
24-
//We look at the supplied form and extend it with schema standards
25-
var lookup = stdForm.lookup;
26-
return form.map(function(obj){
27-
28-
//handle the shortcut with just a name
29-
if (typeof obj === 'string') {
30-
obj = { key: obj };
31-
}
32-
33-
//if it's a type with items, merge 'em!
34-
if (obj.items) {
35-
obj.items = service.merge(schema,obj.items,ignore);
36-
}
37-
38-
39-
//extend with std form from schema.
40-
if (obj.key && lookup[obj.key]) {
41-
return angular.extend(lookup[obj.key],obj);
42-
}
43-
44-
return obj;
45-
});
46-
};
478

489
//Creates a form object with all common properties
4910
var stdFormObj = function(schema,options) {
@@ -98,7 +59,7 @@ angular.module('schemaForm').factory('schemaForm',[function(){
9859
}
9960
};
10061

101-
var bool = function(name,schema,options) {
62+
var checkbox = function(name,schema,options) {
10263
if (schema.type === 'boolean') {
10364
var f = stdFormObj(schema,options);
10465
f.key = options.path;
@@ -172,64 +133,118 @@ angular.module('schemaForm').factory('schemaForm',[function(){
172133

173134
};
174135

175-
//TODO: make it pluginable, i.e. others can register handlers
176-
//TODO: maybe make first selection of handlers by type to optmize
177-
//Order has importance. First handler returning an form snippet will be used
178-
var defaults = [
179-
text,
180-
select,
181-
fieldset,
182-
number,
183-
integer,
184-
bool,
185-
checkboxes
186-
];
187-
188-
189-
var defaultFormDefinition = function(name,schema,options){
190-
var def;
191-
for (var i=0;i<defaults.length; i++) {
192-
193-
def = defaults[i](name,schema,options);
194-
//first handler in list that actually returns something is our handler!
195-
if (def) {
196-
return def;
197-
}
198-
}
136+
//First sorted by schema type then a list.
137+
//Order has importance. First handler returning an form snippet will be used.
138+
var defaults = {
139+
string: [ select, text ],
140+
object: [ fieldset],
141+
number: [ number ],
142+
integer: [ integer ],
143+
boolean: [ checkbox ],
144+
array: [ checkboxes ]
199145
};
200146

201147

202-
/**
203-
* Create form defaults from schema
204-
*/
205-
service.defaults = function(schema,ignore) {
206-
var form = [];
207-
var lookup = {}; //Map path => form obj for fast lookup in merging
208-
ignore = ignore || {};
209148

210-
if (schema.type === "object") {
211-
angular.forEach(schema.properties,function(v,k){
212-
if (ignore[k] !== true) {
213-
var required = schema.required && schema.required.indexOf(k) !== -1;
214-
var def = defaultFormDefinition(k,v,{
215-
path: k, //path to this property in dot notation. Root object has no name
216-
lookup: lookup, //extra map to register with. Optimization for merger.
217-
ignore: ignore, //The ignore list of paths (sans root level name)
218-
required: required //Is it required? (v4 json schema style)
219-
});
220-
if (def) {
221-
form.push(def);
222-
}
223-
}
149+
150+
151+
152+
153+
154+
155+
156+
this.$get = function(){
157+
158+
159+
160+
var service = {};
161+
162+
service.merge = function(schema,form,ignore) {
163+
form = form || ["*"];
164+
165+
var stdForm = service.defaults(schema,ignore);
166+
167+
//simple case, we have a "*", just put the stdForm there
168+
var idx = form.indexOf("*");
169+
if (idx !== -1) {
170+
form = form.slice(0,idx)
171+
.concat(stdForm.form)
172+
.concat(form.slice(idx+1));
173+
return form;
174+
}
175+
176+
//ok let's merge!
177+
//We look at the supplied form and extend it with schema standards
178+
var lookup = stdForm.lookup;
179+
return form.map(function(obj){
180+
181+
//handle the shortcut with just a name
182+
if (typeof obj === 'string') {
183+
obj = { key: obj };
184+
}
185+
186+
//if it's a type with items, merge 'em!
187+
if (obj.items) {
188+
obj.items = service.merge(schema,obj.items,ignore);
189+
}
190+
191+
192+
//extend with std form from schema.
193+
if (obj.key && lookup[obj.key]) {
194+
return angular.extend(lookup[obj.key],obj);
195+
}
196+
197+
return obj;
224198
});
199+
};
225200

226-
} else {
227-
throw new Exception('Not implemented. Only type "object" allowed at root level of schema.');
228-
}
201+
202+
var defaultFormDefinition = function(name,schema,options){
203+
var def;
204+
for (var i=0;i<defaults.length; i++) {
229205

230-
return { form: form, lookup: lookup };
231-
};
206+
def = defaults[i](name,schema,options);
207+
//first handler in list that actually returns something is our handler!
208+
if (def) {
209+
return def;
210+
}
211+
}
212+
};
213+
214+
215+
/**
216+
* Create form defaults from schema
217+
*/
218+
service.defaults = function(schema,ignore) {
219+
var form = [];
220+
var lookup = {}; //Map path => form obj for fast lookup in merging
221+
ignore = ignore || {};
222+
223+
if (schema.type === "object") {
224+
angular.forEach(schema.properties,function(v,k){
225+
if (ignore[k] !== true) {
226+
var required = schema.required && schema.required.indexOf(k) !== -1;
227+
var def = defaultFormDefinition(k,v,{
228+
path: k, //path to this property in dot notation. Root object has no name
229+
lookup: lookup, //extra map to register with. Optimization for merger.
230+
ignore: ignore, //The ignore list of paths (sans root level name)
231+
required: required //Is it required? (v4 json schema style)
232+
});
233+
if (def) {
234+
form.push(def);
235+
}
236+
}
237+
});
238+
239+
} else {
240+
throw new Exception('Not implemented. Only type "object" allowed at root level of schema.');
241+
}
232242

243+
return { form: form, lookup: lookup };
244+
};
245+
246+
247+
return service;
248+
};
233249

234-
return service;
235250
}]);

0 commit comments

Comments
 (0)