Skip to content

Commit 0358f83

Browse files
committed
Getter for default decorator name
First decorator that registeres gets automatically set as default.
1 parent eafc395 commit 0358f83

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/directives/schema-form.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ FIXME: real documentation
55

66
angular.module('schemaForm')
77
.directive('sfSchema',
8-
['$compile','schemaForm',
9-
function($compile, schemaForm){
8+
['$compile','schemaForm','schemaFormDecorators',
9+
function($compile, schemaForm, schemaFormDecorators){
1010

1111
//recurse through the entire schema.
1212
//FIXME: no support for arrays
@@ -18,7 +18,13 @@ function($compile, schemaForm){
1818
});
1919
};
2020

21-
21+
var SNAKE_CASE_REGEXP = /[A-Z]/g;
22+
function snake_case(name, separator){
23+
separator = separator || '_';
24+
return name.replace(SNAKE_CASE_REGEXP, function(letter, pos) {
25+
return (pos ? separator : '') + letter.toLowerCase();
26+
});
27+
}
2228

2329
return {
2430
scope: {
@@ -74,11 +80,10 @@ function($compile, schemaForm){
7480

7581
//Create directives from the form definition
7682
angular.forEach(merged,function(obj,i){
77-
var n = document.createElement(attrs.sfDecorator || 'bootstrap-decorator');
83+
var n = document.createElement(attrs.sfDecorator || snake_case(schemaFormDecorators.defaultDecorator,'-'));
7884
n.setAttribute('type',obj.type);
7985
n.setAttribute('form','schemaForm.form['+i+']');
8086
frag.appendChild(n);
81-
8287
});
8388

8489
//clean all but pre existing html.
@@ -87,10 +92,8 @@ function($compile, schemaForm){
8792
element[0].appendChild(frag);
8893

8994
//compile only children
90-
9195
$compile(element.children())(scope);
9296

93-
9497
//ok, now that that is done let's set any defaults
9598
traverse(schema,function(prop,path){
9699
//This is probably not so fast, but a simple solution.

src/services/decorators.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider',function($compileProvider){
2-
2+
var defaultDecorator = 'bootstrapDecorator';
33
var directives = {};
44

5-
65
var templateUrl = function(name,form) {
76
var directive = directives[name];
87

@@ -39,6 +38,10 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
3938
rules: rules || []
4039
};
4140

41+
if (!directives[defaultDecorator]) {
42+
defaultDecorator = name;
43+
}
44+
4245
$compileProvider.directive(name,['$parse','$compile','$http','$templateCache',
4346
function($parse, $compile, $http, $templateCache){
4447

@@ -97,8 +100,11 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
97100

98101
//Service is just a getter for directive mappings and rules
99102
this.$get = function(){
100-
return function(name) {
101-
return directives[name];
103+
return {
104+
directive: function(name) {
105+
return directives[name];
106+
},
107+
defaultDecorator: defaultDecorator
102108
};
103109
};
104110

0 commit comments

Comments
 (0)