1
- angular . module ( 'schemaForm' ) . config ( [ 'schemaFormDecoratorsProvider' , 'sfBuilderProvider' ,
2
- function ( decoratorsProvider , sfBuilderProvider ) {
1
+
2
+ angular . module ( 'schemaForm' ) . config ( [ 'schemaFormDecoratorsProvider' , 'sfBuilderProvider' , 'sfPathProvider' ,
3
+ function ( decoratorsProvider , sfBuilderProvider , sfPathProvider ) {
3
4
var base = 'decorators/bootstrap/' ;
4
5
5
- var simpleBuilder = sfBuilderProvider . builders . si ;
6
+ var simpleTransclusion = sfBuilderProvider . builders . simpleTransclusion ;
6
7
var ngModelOptions = sfBuilderProvider . builders . ngModelOptions ;
7
8
var ngModel = sfBuilderProvider . builders . ngModel ;
8
9
9
10
var array = function ( args ) {
10
- console . log ( 'array' , args )
11
+ console . log ( 'array' , args ) ;
12
+
11
13
var items = args . fieldFrag . querySelector ( 'li.schema-form-array-items' ) ;
12
14
if ( items ) {
13
- args . state . keyRedaction = args . state . keyRedaction || 0 ;
14
- args . state . keyRedaction += args . form . key . length + 1 ;
15
+ state = angular . copy ( args . state ) ;
16
+ state . keyRedaction = state . keyRedaction || 0 ;
17
+ state . keyRedaction += args . form . key . length + 1 ;
18
+
19
+ // Special case, an array with just one item in it that is not an object.
20
+ // So then we just override the modelValue
21
+ if ( args . form . schema && args . form . schema . items &&
22
+ args . form . schema . items . type &&
23
+ args . form . schema . items . type . indexOf ( 'object' ) === - 1 &&
24
+ args . form . schema . items . type . indexOf ( 'array' ) === - 1 ) {
25
+ console . log ( 'setting state modelValue' , args . form ) ;
26
+ var strKey = sfPathProvider . stringify ( args . form . key ) . replace ( / " / g, '"' ) + '[$index]' ;
27
+ state . modelValue = 'getModelArray()[$index]' ; //(args.state.modelName || 'model') + (strKey[0] !== '[' ? '.' : '') + strKey;
28
+ //state.modelValue = 'model' + sfPathProvider.normalize(args.form.key) + '[$index]'; // 'modelArray[$index]';
29
+ } else {
30
+ state . modelName = 'item' ;
31
+ }
15
32
16
- var childFrag = args . build ( args . form . items , args . path + '.items' , args . state ) ;
33
+ var childFrag = args . build ( args . form . items , args . path + '.items' , state ) ;
17
34
items . appendChild ( childFrag ) ;
18
35
}
19
36
} ;
20
37
var defaults = [ ngModel , ngModelOptions ] ;
21
38
decoratorsProvider . defineDecorator ( 'bootstrapDecorator' , {
22
39
textarea : { template : base + 'textarea.html' , builder : defaults } ,
23
- fieldset : { template : base + 'fieldset.html' , builder : simpleBuilder } ,
24
- array : { template : base + 'array.html' , builder : [ ngModel , ngModelOptions , array ] } ,
40
+ fieldset : { template : base + 'fieldset.html' , builder : simpleTransclusion } ,
41
+ array : { template : base + 'array.html' , builder : [ ngModelOptions , ngModel , array ] } ,
25
42
tabarray : { template : base + 'tabarray.html' , replace : false } ,
26
43
tabs : { template : base + 'tabs.html' , replace : false } ,
27
- section : { template : base + 'section.html' , builder : simpleBuilder } ,
28
- conditional : { template : base + 'section.html' , builder : simpleBuilder } ,
44
+ section : { template : base + 'section.html' , builder : simpleTransclusion } ,
45
+ conditional : { template : base + 'section.html' , builder : simpleTransclusion } ,
29
46
actions : { template : base + 'actions.html' , builder : defaults } ,
30
47
select : { template : base + 'select.html' , builder : defaults } ,
31
48
checkbox : { template : base + 'checkbox.html' , builder : defaults } ,
@@ -41,4 +58,49 @@ function(decoratorsProvider, sfBuilderProvider) {
41
58
'default' : { template : base + 'default.html' , builder : defaults }
42
59
} , [ ] ) ;
43
60
44
- } ] ) ;
61
+ } ] ) . filter ( 'minLength' , function ( ) {
62
+ return function ( input , min ) {
63
+ input = input || [ ] ;
64
+ var diff = min - input . length ;
65
+ if ( diff > 0 ) {
66
+ return input . concat ( new Array ( diff ) ) ;
67
+ }
68
+ return input ;
69
+ } ;
70
+ } ) . directive ( 'sfNewArray' , function ( ) {
71
+ return {
72
+ scope : false ,
73
+ link : function ( scope , element , attrs ) {
74
+ scope . min = 0 ;
75
+ scope . appendToArray = function ( ) {
76
+ var empty ;
77
+
78
+ // Same old add empty things to the array hack :(
79
+ if ( scope . form && scope . form . schema && scope . form . schema . items ) {
80
+ if ( scope . form . schema . items . type === 'object' ) {
81
+ empty = { } ;
82
+ } else if ( scope . form . schema . items . type === 'array' ) {
83
+ empty = [ ] ;
84
+ }
85
+ }
86
+
87
+ if ( ! scope . ngModel . $modelValue ) {
88
+ scope . ngModel . $setViewValue ( [ ] ) ;
89
+ scope . ngModel . $commitViewValue ( [ ] ) ;
90
+ }
91
+ scope . ngModel . $modelValue . push ( empty ) ;
92
+
93
+ } ;
94
+
95
+ /*scope.$watch('ngModel.$modelValue', function() {
96
+ console.log(scope.ngModel.$modelValue)
97
+ scope.modelArray = scope.ngModel.$modelValue;
98
+ });*/
99
+ scope . getModelArray = function ( ) {
100
+ console . log ( scope . ngModel . $modelValue ) ;
101
+ return scope . ngModel . $modelValue ;
102
+ } ;
103
+
104
+ }
105
+ } ;
106
+ } ) ;
0 commit comments