@@ -6,58 +6,10 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
66 var ngModelOptions = sfBuilderProvider . builders . ngModelOptions ;
77 var ngModel = sfBuilderProvider . builders . ngModel ;
88 var sfField = sfBuilderProvider . builders . sfField ;
9+ var condition = sfBuilderProvider . builders . condition ;
10+ var array = sfBuilderProvider . builders . array ;
911
10- var condition = function ( args ) {
11- // Do we have a condition? Then we slap on an ng-if on all children,
12- // but be nice to existing ng-if.
13- if ( args . form . condition ) {
14- var evalExpr = 'evalExpr(' + args . path + '.contidion, { model: model, "arrayIndex": $index})' ;
15- if ( args . form . key ) {
16- var strKey = sfPathProvider . stringify ( args . form . key ) ;
17- evalExpr = 'evalExpr(' + args . path + '.condition,{ model: model, "arrayIndex": $index, ' +
18- '"modelValue": model' + ( strKey [ 0 ] === '[' ? '' : '.' ) + strKey + '})' ;
19- }
20-
21- var children = args . fieldFrag . children ;
22- for ( var i = 0 ; i < children . length ; i ++ ) {
23- var child = children [ i ] ;
24- var ngIf = child . getAttribute ( 'ng-if' ) ;
25- child . setAttribute (
26- 'ng-if' ,
27- ngIf ?
28- '(' + ngIf +
29- ') || (' + evalExpr + ')'
30- : evalExpr
31- ) ;
32- }
33- }
34- } ;
35-
36- var array = function ( args ) {
37- var items = args . fieldFrag . querySelector ( '[schema-form-array-items]' ) ;
38- if ( items ) {
39- state = angular . copy ( args . state ) ;
40- state . keyRedaction = state . keyRedaction || 0 ;
41- state . keyRedaction += args . form . key . length + 1 ;
42-
43- // Special case, an array with just one item in it that is not an object.
44- // So then we just override the modelValue
45- if ( args . form . schema && args . form . schema . items &&
46- args . form . schema . items . type &&
47- args . form . schema . items . type . indexOf ( 'object' ) === - 1 &&
48- args . form . schema . items . type . indexOf ( 'array' ) === - 1 ) {
49- var strKey = sfPathProvider . stringify ( args . form . key ) . replace ( / " / g, '"' ) + '[$index]' ;
50- state . modelValue = 'modelArray[$index]' ; //(args.state.modelName || 'model') + (strKey[0] !== '[' ? '.' : '') + strKey;
51- //state.modelValue = 'model' + sfPathProvider.normalize(args.form.key) + '[$index]'; // 'modelArray[$index]';
52- } else {
53- state . modelName = 'item' ;
54- }
55-
56- var childFrag = args . build ( args . form . items , args . path + '.items' , state ) ;
57- items . appendChild ( childFrag ) ;
58- }
59- } ;
60-
12+ // Tabs is so bootstrap specific that it stays here.
6113 var tabs = function ( args ) {
6214 if ( args . form . tabs && args . form . tabs . length > 0 ) {
6315 var tabContent = args . fieldFrag . querySelector ( '.tab-content' ) ;
@@ -100,170 +52,4 @@ function(decoratorsProvider, sfBuilderProvider, sfPathProvider) {
10052 'default' : { template : base + 'default.html' , builder : defaults }
10153 } , [ ] ) ;
10254
103- } ] )
104-
105- /* Directives here are WIP, will be moved to main repo or their own files when solidifying */
106- . directive ( 'sfNewArray' , [ 'sfSelect' , 'sfPath' , function ( sel , sfPath ) {
107- return {
108- scope : false ,
109- link : function ( scope , element , attrs ) {
110- scope . min = 0 ;
111-
112- scope . modelArray = scope . $eval ( attrs . sfNewArray ) ;
113-
114- // We need to have a ngModel to hook into validation. It doesn't really play well with
115- // arrays though so we both need to trigger validation and onChange.
116- // So we watch the value as well. But watching an array can be tricky. We wan't to know
117- // when it changes so we can validate,
118- var watchFn = function ( ) {
119- //scope.modelArray = modelArray;
120- scope . modelArray = scope . $eval ( attrs . sfNewArray ) ;
121- // validateField method is exported by schema-validate
122- if ( scope . validateField ) {
123- scope . validateField ( ) ;
124- }
125- } ;
126-
127- var onChangeFn = function ( ) {
128- if ( scope . form && scope . form . onChange ) {
129- if ( angular . isFunction ( form . onChange ) ) {
130- form . onChange ( ctrl . $modelValue , form ) ;
131- } else {
132- scope . evalExpr ( form . onChange , { 'modelValue' : ctrl . $modelValue , form : form } ) ;
133- }
134- }
135- } ;
136-
137- // We need the form definition to make a decision on how we should listen.
138- var once = scope . $watch ( 'form' , function ( form ) {
139- if ( ! form ) {
140- return ;
141- }
142-
143- // Always start with one empty form unless configured otherwise.
144- // Special case: don't do it if form has a titleMap
145- if ( ! form . titleMap && form . startEmpty !== true && ( ! scope . modelArray || scope . modelArray . length === 0 ) ) {
146- scope . appendToArray ( ) ;
147- }
148-
149- // If we have "uniqueItems" set to true, we must deep watch for changes.
150- if ( scope . form && scope . form . schema && scope . form . schema . uniqueItems === true ) {
151- scope . $watch ( attrs . sfNewArray , watchFn , true ) ;
152-
153- // We still need to trigger onChange though.
154- scope . $watch ( [ attrs . sfNewArray , attrs . sfNewArray + '.length' ] , onChangeFn ) ;
155-
156- } else {
157- // Otherwise we like to check if the instance of the array has changed, or if something
158- // has been added/removed.
159- if ( scope . $watchGroup ) {
160- scope . $watchGroup ( [ attrs . sfNewArray , attrs . sfNewArray + '.length' ] , function ( ) {
161- watchFn ( ) ;
162- onChangeFn ( ) ;
163- } ) ;
164- } else {
165- // Angular 1.2 support
166- scope . $watch ( attrs . sfNewArray , function ( ) {
167- watchFn ( ) ;
168- onChangeFn ( ) ;
169- } ) ;
170- scope . $watch ( attrs . sfNewArray + '.length' , function ( ) {
171- watchFn ( ) ;
172- onChangeFn ( ) ;
173- } ) ;
174- }
175- }
176-
177- // Title Map handling
178- // If form has a titleMap configured we'd like to enable looping over
179- // titleMap instead of modelArray, this is used for intance in
180- // checkboxes. So instead of variable number of things we like to create
181- // a array value from a subset of values in the titleMap.
182- // The problem here is that ng-model on a checkbox doesn't really map to
183- // a list of values. This is here to fix that.
184- if ( form . titleMap && form . titleMap . length > 0 ) {
185- scope . titleMapValues = [ ] ;
186-
187- // We watch the model for changes and the titleMapValues to reflect
188- // the modelArray
189- var updateTitleMapValues = function ( arr ) {
190- scope . titleMapValues = [ ] ;
191- arr = arr || [ ] ;
192-
193- form . titleMap . forEach ( function ( item ) {
194- scope . titleMapValues . push ( arr . indexOf ( item . value ) !== - 1 ) ;
195- } ) ;
196- } ;
197- //Catch default values
198- updateTitleMapValues ( scope . modelArray ) ;
199-
200- // TODO: Refactor and see if we can get rid of this watch by piggy backing on the
201- // validation watch.
202- scope . $watchCollection ( 'modelArray' , updateTitleMapValues ) ;
203-
204- //To get two way binding we also watch our titleMapValues
205- scope . $watchCollection ( 'titleMapValues' , function ( vals , old ) {
206- if ( vals && vals !== old ) {
207- var arr = scope . modelArray ;
208-
209- // Apparently the fastest way to clear an array, readable too.
210- // http://jsperf.com/array-destroy/32
211- while ( arr . length > 0 ) {
212- arr . pop ( ) ;
213- }
214- form . titleMap . forEach ( function ( item , index ) {
215- if ( vals [ index ] ) {
216- arr . push ( item . value ) ;
217- }
218- } ) ;
219-
220- // Time to validate the rebuilt array.
221- // validateField method is exported by schema-validate
222- if ( scope . validateField ) {
223- scope . validateField ( ) ;
224- }
225- }
226- } ) ;
227- }
228-
229- once ( ) ;
230- } ) ;
231-
232- scope . appendToArray = function ( ) {
233-
234- var empty ;
235-
236- // Same old add empty things to the array hack :(
237- if ( scope . form && scope . form . schema ) {
238- if ( scope . form . schema . items ) {
239- if ( scope . form . schema . items . type === 'object' ) {
240- empty = { } ;
241- } else if ( scope . form . schema . items . type === 'array' ) {
242- empty = [ ] ;
243- }
244- }
245- }
246-
247- var model = scope . modelArray ;
248- if ( ! model ) {
249- // Create and set an array if needed.
250- var selection = sfPath . parse ( attrs . sfNewArray ) ;
251- model = [ ] ;
252- sel ( selection , scope , model ) ;
253- scope . modelArray = model ;
254- }
255- model . push ( empty ) ;
256-
257- return model ;
258- } ;
259-
260- scope . deleteFromArray = function ( index ) {
261- var model = scope . modelArray ;
262- if ( model ) {
263- model . splice ( index , 1 ) ;
264- }
265- return model ;
266- } ;
267- }
268- } ;
26955} ] ) ;
0 commit comments