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 ) {
34 var base = 'decorators/bootstrap/' ;
45
5- var simpleBuilder = sfBuilderProvider . builders . si ;
6+ var simpleTransclusion = sfBuilderProvider . builders . simpleTransclusion ;
67 var ngModelOptions = sfBuilderProvider . builders . ngModelOptions ;
78 var ngModel = sfBuilderProvider . builders . ngModel ;
89
910 var array = function ( args ) {
10- console . log ( 'array' , args )
11+ console . log ( 'array' , args ) ;
12+
1113 var items = args . fieldFrag . querySelector ( 'li.schema-form-array-items' ) ;
1214 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+ }
1532
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 ) ;
1734 items . appendChild ( childFrag ) ;
1835 }
1936 } ;
2037 var defaults = [ ngModel , ngModelOptions ] ;
2138 decoratorsProvider . defineDecorator ( 'bootstrapDecorator' , {
2239 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 ] } ,
2542 tabarray : { template : base + 'tabarray.html' , replace : false } ,
2643 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 } ,
2946 actions : { template : base + 'actions.html' , builder : defaults } ,
3047 select : { template : base + 'select.html' , builder : defaults } ,
3148 checkbox : { template : base + 'checkbox.html' , builder : defaults } ,
@@ -41,4 +58,49 @@ function(decoratorsProvider, sfBuilderProvider) {
4158 'default' : { template : base + 'default.html' , builder : defaults }
4259 } , [ ] ) ;
4360
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