Skip to content

Commit 479ef24

Browse files
committed
Add support for type: array
1 parent 76d9033 commit 479ef24

File tree

8 files changed

+117
-5
lines changed

8 files changed

+117
-5
lines changed

dist/bootstrap-decorator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/schema-form.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/decorators/bootstrap/bootstrap-decorator.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ angular.module('schemaForm').config(['schemaFormDecoratorsProvider',function(dec
44
decoratorsProvider.createDecorator('bootstrapDecorator',{
55
textarea: base+'textarea.html',
66
fieldset: base+'fieldset.html',
7+
list: base+'list.html',
78
tabs: base+'tabs.html',
89
section: base+'section.html',
910
conditional: base+'section.html',
@@ -54,4 +55,81 @@ angular.module('schemaForm').config(['schemaFormDecoratorsProvider',function(dec
5455
scope.title = scope.$eval(attrs.title);
5556
}
5657
};
58+
}).directive('sfList',function(){
59+
return {
60+
templateUrl: 'directives/decorators/bootstrap/list-body.html',
61+
62+
link: function(scope,element,attrs) {
63+
scope.model[scope.form.item.key] = scope.model[scope.form.item.key] || [];
64+
scope.value = scope.model[scope.form.item.key];
65+
scope.renderedValue = angular.copy(scope.value);
66+
67+
var storedValue = null;
68+
scope.$on('change', function(evt, val){
69+
var index = findElement(element[0], val.element[0]);
70+
71+
if (index == -1){
72+
// The add input
73+
storedValue = val.value;
74+
} else {
75+
scope.value[index] = val.value;
76+
}
77+
});
78+
79+
scope.$on('remove', function(evt, val){
80+
var index = findElement(element[0], val.element[0]);
81+
82+
scope.value.splice(index, 1);
83+
scope.renderedValue.splice(index, 1);
84+
val.element[0].parentNode.removeChild(val.element[0]);
85+
});
86+
87+
scope.add = function(){
88+
scope.renderedValue.push(storedValue);
89+
scope.value.push(storedValue);
90+
};
91+
}
92+
}
93+
}).directive('sfListItem',function(){
94+
return {
95+
scope: {
96+
'form': '=',
97+
'value': '='
98+
},
99+
100+
templateUrl: 'directives/decorators/bootstrap/list-item.html',
101+
102+
link: function(scope,element,attrs) {
103+
scope.form.item.key = 'value';
104+
scope.model = {
105+
value: scope.value
106+
};
107+
108+
scope.$watch('model', function(val){
109+
scope.$emit('change', {
110+
value: val.value,
111+
element: element
112+
});
113+
}, true);
114+
115+
scope.remove = function() {
116+
scope.$emit('remove', {
117+
element: element
118+
});
119+
};
120+
}
121+
}
57122
});
123+
124+
function findElement(parentEl, element){
125+
var els = parentEl.querySelectorAll('[sf-list-item]');
126+
for (var i=0; i < els.length; i++){
127+
if (els[i].isEqualNode(element)){
128+
if (els[i].tagName === 'DIV'){
129+
return -1;
130+
} else {
131+
return i;
132+
}
133+
}
134+
}
135+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ul>
2+
<li ng-repeat="val in renderedValue" sf-list-item value="val" form="form"></li>
3+
</ul>
4+
5+
<div sf-list-item form="form"></div>
6+
<a href ng-click="add()">Add</a>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
<sf-decorator form="form.item"></sf-decorator>
3+
<a href ng-click="remove()">Remove</a>
4+
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h3 ng-show="form.title">{{ form.title }}</h3>
2+
<div sf-list></div>

src/directives/schema-form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function($compile, schemaForm, schemaFormDecorators){
108108
traverse(schema,function(prop,path){
109109
//This is probably not so fast, but a simple solution.
110110
if (angular.isDefined(prop['default'])) {
111-
scope.$eval('model.'+path+' = model.'+path+' || defaltValue',{ defaltValue: prop['default']});
111+
scope.$eval('model.'+path+' = model.'+path+' || defaultValue',{ defaultValue: prop['default']});
112112
}
113113
});
114114

src/services/schema-form.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,37 @@ angular.module('schemaForm').provider('schemaForm',[function(){
148148

149149
};
150150

151+
var list = function(name,schema,options){
152+
153+
if (schema.type === "array") {
154+
var f = stdFormObj(schema,options);
155+
f.type = 'list';
156+
f.key = options.path;
157+
options.lookup[options.path] = f;
158+
159+
var required = schema.required && schema.required.indexOf(options.path) !== -1;
160+
161+
f.item = defaultFormDefinition(options.path,schema.items,{
162+
path: options.path,
163+
required: required || false,
164+
lookup: options.lookup,
165+
ignore: options.ignore
166+
});
167+
168+
return f
169+
}
170+
171+
};
172+
151173
//First sorted by schema type then a list.
152174
//Order has importance. First handler returning an form snippet will be used.
153175
var defaults = {
154176
string: [ select, text ],
155-
object: [ fieldset],
177+
object: [ fieldset ],
156178
number: [ number ],
157179
integer: [ integer ],
158180
boolean: [ checkbox ],
159-
array: [ checkboxes ]
181+
array: [ list, checkboxes ]
160182
};
161183

162184
var postProcessFn = function(form) { return form; };

0 commit comments

Comments
 (0)