Skip to content

Commit 21ca189

Browse files
committed
Extension methods to the schemaFormProvider
Letting you add rules so you can change or create new default forms for different schema types and other combinations.
1 parent 15e84e3 commit 21ca189

File tree

3 files changed

+114
-21
lines changed

3 files changed

+114
-21
lines changed

karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ module.exports = function(config) {
3737
reporters: ['progress','coverage','growler'],
3838

3939
preprocessors: {
40-
'src/*.js': 'coverage',
41-
'src/**/*.html': 'ng-html2js'
40+
'src/**/*.js': ['coverage'],
41+
'src/**/*.html': ['ng-html2js']
4242
},
4343

4444
// optionally, configure the reporter

src/services/schema-form.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
*/
66
angular.module('schemaForm').provider('schemaForm',[function(){
77

8+
var defaultFormDefinition = function(name,schema,options){
9+
var rules = defaults[schema.type];
10+
if (rules) {
11+
var def;
12+
for (var i=0;i<rules.length; i++) {
13+
def = rules[i](name,schema,options);
14+
//first handler in list that actually returns something is our handler!
15+
if (def) {
16+
return def;
17+
}
18+
}
19+
}
20+
};
821

922
//Creates a form object with all common properties
1023
var stdFormObj = function(schema,options) {
@@ -146,17 +159,50 @@ angular.module('schemaForm').provider('schemaForm',[function(){
146159

147160

148161

162+
/**
163+
* Provider API
164+
*/
165+
this.defaults = defaults;
149166

167+
/**
168+
* Append default form rule
169+
* @param {string} type json schema type
170+
* @param {Function} rule a function(propertyName,propertySchema,options) that returns a form definition or undefined
171+
*/
172+
this.appendRule = function(type,rule) {
173+
if (!defaults[type]) {
174+
defaults[type] = [];
175+
}
176+
defaults[type].push(rule);
177+
};
150178

179+
/**
180+
* Prepend default form rule
181+
* @param {string} type json schema type
182+
* @param {Function} rule a function(propertyName,propertySchema,options) that returns a form definition or undefined
183+
*/
184+
this.prependRule = function(type,rule) {
185+
if (!defaults[type]) {
186+
defaults[type] = [];
187+
}
188+
defaults[type].unshift(rule);
189+
};
151190

191+
/**
192+
* Utility function to create a standard form object.
193+
* This does *not* set the type of the form but rather all shared attributes.
194+
* You probably want to start your rule with creating the form with this method
195+
* then setting type and any other values you need.
196+
* @param {Object} schema
197+
* @param {Object} options
198+
* @return {Object} a form field defintion
199+
*/
200+
this.createStandardForm = stdFormObj;
201+
/* End Provider API */
152202

153203

154-
155-
156204
this.$get = function(){
157205

158-
159-
160206
var service = {};
161207

162208
service.merge = function(schema,form,ignore) {
@@ -198,18 +244,6 @@ angular.module('schemaForm').provider('schemaForm',[function(){
198244
});
199245
};
200246

201-
202-
var defaultFormDefinition = function(name,schema,options){
203-
var def;
204-
for (var i=0;i<defaults.length; i++) {
205-
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-
};
213247

214248

215249
/**
@@ -237,7 +271,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
237271
});
238272

239273
} else {
240-
throw new Exception('Not implemented. Only type "object" allowed at root level of schema.');
274+
throw new Error('Not implemented. Only type "object" allowed at root level of schema.');
241275
}
242276

243277
return { form: form, lookup: lookup };

test/schema-form-test.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
chai.should();
33

44
describe('Schema form',function(){
5-
beforeEach(module('templates'));
6-
beforeEach(module('schemaForm'));
75

86
describe('directive',function(){
7+
beforeEach(module('templates'));
8+
beforeEach(module('schemaForm'));
9+
10+
911

1012
var exampleSchema = {
1113
"type": "object",
@@ -719,6 +721,9 @@ describe('Schema form',function(){
719721

720722

721723
describe('service',function(){
724+
beforeEach(module('templates'));
725+
beforeEach(module('schemaForm'));
726+
722727
it('should generate default form def from a schema',function(){
723728
inject(function(schemaForm){
724729

@@ -836,6 +841,57 @@ describe('Schema form',function(){
836841
});
837842
});
838843

844+
it('should be extendable with new defaults',function(){
845+
module(function(schemaFormProvider){
846+
schemaFormProvider.prependRule('string',function(name,schema,options){
847+
if (schema.format === 'foobar') {
848+
var f = schemaFormProvider.createStandardForm(schema,options);
849+
f.type = 'foobar';
850+
return f;
851+
}
852+
});
853+
854+
schemaFormProvider.appendRule('string',function(name,schema,options){
855+
var f = schemaFormProvider.createStandardForm(schema,options);
856+
f.type = 'notused';
857+
return f;
858+
});
859+
});
860+
861+
inject(function(schemaForm){
862+
863+
var schema = {
864+
"type": "object",
865+
"properties": {
866+
"name": {
867+
"title": "Name",
868+
"format": "foobar",
869+
"description": "Gimme yea name lad",
870+
"type": "string"
871+
},
872+
"gender": {
873+
"title": "Choose",
874+
"type": "string",
875+
"enum": [
876+
"undefined",
877+
"null",
878+
"NaN",
879+
]
880+
}
881+
}
882+
};
883+
884+
//no form is implicitly ['*']
885+
var defaults = schemaForm.defaults(schema).form;
886+
defaults[0].type.should.be.equal('foobar');
887+
defaults[0].title.should.be.equal('Name');
888+
defaults[1].type.should.be.equal('select');
889+
defaults[1].title.should.be.equal('Choose');
890+
891+
});
892+
});
893+
894+
839895

840896
it('should ignore parts of schema in ignore list',function(){
841897
inject(function(schemaForm){
@@ -912,6 +968,9 @@ describe('Schema form',function(){
912968
});
913969

914970
describe('decorator factory service',function(){
971+
beforeEach(module('templates'));
972+
beforeEach(module('schemaForm'));
973+
915974
it('should enable you to create new decorator directives',function(){
916975
module(function(schemaFormDecoratorsProvider){
917976
schemaFormDecoratorsProvider.createDecorator('foobar',{ 'foo':'/bar.html' },[angular.noop]);

0 commit comments

Comments
 (0)