1
1
// Deps is sort of a problem for us, maybe in the future we will ask the user to depend
2
2
// on modules for add-ons
3
3
4
- var deps = [ 'ObjectPath' ] ;
4
+ var deps = [ ] ;
5
5
try {
6
6
//This throws an expection if module does not exist.
7
7
angular . module ( 'ngSanitize' ) ;
@@ -23,29 +23,31 @@ try {
23
23
angular . module ( 'schemaForm' , deps ) ;
24
24
25
25
angular . module ( 'schemaForm' ) . provider ( 'sfPath' ,
26
- [ 'ObjectPathProvider' , function ( ObjectPathProvider ) {
27
- var ObjectPath = { parse : ObjectPathProvider . parse } ;
26
+ [ function ( ) {
27
+ var sfPath = { parse : ObjectPath . parse } ;
28
28
29
29
// if we're on Angular 1.2.x, we need to continue using dot notation
30
30
if ( angular . version . major === 1 && angular . version . minor < 3 ) {
31
- ObjectPath . stringify = function ( arr ) {
31
+ sfPath . stringify = function ( arr ) {
32
32
return Array . isArray ( arr ) ? arr . join ( '.' ) : arr . toString ( ) ;
33
33
} ;
34
34
} else {
35
- ObjectPath . stringify = ObjectPathProvider . stringify ;
35
+ sfPath . stringify = ObjectPath . stringify ;
36
36
}
37
37
38
38
// We want this to use whichever stringify method is defined above,
39
39
// so we have to copy the code here.
40
- ObjectPath . normalize = function ( data , quote ) {
41
- return ObjectPath . stringify ( Array . isArray ( data ) ? data : ObjectPath . parse ( data ) , quote ) ;
40
+ sfPath . normalize = function ( data , quote ) {
41
+ return sfPath . stringify ( Array . isArray ( data ) ? data : sfPath . parse ( data ) , quote ) ;
42
42
} ;
43
43
44
- this . parse = ObjectPath . parse ;
45
- this . stringify = ObjectPath . stringify ;
46
- this . normalize = ObjectPath . normalize ;
44
+ // expose the methods in sfPathProvider
45
+ this . parse = sfPath . parse ;
46
+ this . stringify = sfPath . stringify ;
47
+ this . normalize = sfPath . normalize ;
48
+
47
49
this . $get = function ( ) {
48
- return ObjectPath ;
50
+ return sfPath ;
49
51
} ;
50
52
} ] ) ;
51
53
@@ -949,8 +951,8 @@ angular.module('schemaForm').factory('sfValidator', [function() {
949
951
/**
950
952
* Directive that handles the model arrays
951
953
*/
952
- angular . module ( 'schemaForm' ) . directive ( 'sfArray' , [ 'sfSelect' , 'schemaForm' , 'sfValidator' ,
953
- function ( sfSelect , schemaForm , sfValidator ) {
954
+ angular . module ( 'schemaForm' ) . directive ( 'sfArray' , [ 'sfSelect' , 'schemaForm' , 'sfValidator' , 'sfPath' ,
955
+ function ( sfSelect , schemaForm , sfValidator , sfPath ) {
954
956
955
957
var setIndex = function ( index ) {
956
958
return function ( form ) {
@@ -977,6 +979,14 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
977
979
// other hand it enables two way binding.
978
980
var list = sfSelect ( form . key , scope . model ) ;
979
981
982
+ // We only modify the same array instance but someone might change the array from
983
+ // the outside so let's watch for that. We use an ordinary watch since the only case
984
+ // we're really interested in is if its a new instance.
985
+ scope . $watch ( 'model' + sfPath . normalize ( form . key ) , function ( ) {
986
+ list = sfSelect ( form . key , scope . model ) ;
987
+ scope . modelArray = list ;
988
+ } ) ;
989
+
980
990
// Since ng-model happily creates objects in a deep path when setting a
981
991
// a value but not arrays we need to create the array.
982
992
if ( angular . isUndefined ( list ) ) {
@@ -996,7 +1006,7 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
996
1006
if ( form . items . length > 1 ) {
997
1007
subForm = {
998
1008
type : 'section' ,
999
- items : form . items . map ( function ( item ) {
1009
+ items : form . items . map ( function ( item ) {
1000
1010
item . ngModelOptions = form . ngModelOptions ;
1001
1011
item . readonly = form . readonly ;
1002
1012
return item ;
@@ -1024,8 +1034,20 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
1024
1034
var len = list . length ;
1025
1035
var copy = scope . copyWithIndex ( len ) ;
1026
1036
schemaForm . traverseForm ( copy , function ( part ) {
1027
- if ( part . key && angular . isDefined ( part [ 'default' ] ) ) {
1028
- sfSelect ( part . key , scope . model , part [ 'default' ] ) ;
1037
+
1038
+ if ( part . key ) {
1039
+ var def ;
1040
+ if ( angular . isDefined ( part [ 'default' ] ) ) {
1041
+ def = part [ 'default' ] ;
1042
+ }
1043
+ if ( angular . isDefined ( part . schema ) &&
1044
+ angular . isDefined ( part . schema [ 'default' ] ) ) {
1045
+ def = part . schema [ 'default' ] ;
1046
+ }
1047
+
1048
+ if ( angular . isDefined ( def ) ) {
1049
+ sfSelect ( part . key , scope . model , def ) ;
1050
+ }
1029
1051
}
1030
1052
} ) ;
1031
1053
@@ -1201,8 +1223,8 @@ FIXME: real documentation
1201
1223
1202
1224
angular . module ( 'schemaForm' )
1203
1225
. directive ( 'sfSchema' ,
1204
- [ '$compile' , 'schemaForm' , 'schemaFormDecorators' , 'sfSelect' ,
1205
- function ( $compile , schemaForm , schemaFormDecorators , sfSelect ) {
1226
+ [ '$compile' , 'schemaForm' , 'schemaFormDecorators' , 'sfSelect' , 'sfPath' ,
1227
+ function ( $compile , schemaForm , schemaFormDecorators , sfSelect , sfPath ) {
1206
1228
1207
1229
var SNAKE_CASE_REGEXP = / [ A - Z ] / g;
1208
1230
var snakeCase = function ( name , separator ) {
@@ -1279,23 +1301,35 @@ angular.module('schemaForm')
1279
1301
//clean all but pre existing html.
1280
1302
element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
1281
1303
1304
+ // Find all slots.
1305
+ var slots = { } ;
1306
+ var slotsFound = element [ 0 ] . querySelectorAll ( '*[sf-insert-field]' ) ;
1307
+
1308
+ for ( var i = 0 ; i < slotsFound . length ; i ++ ) {
1309
+ slots [ slotsFound [ i ] . getAttribute ( 'sf-insert-field' ) ] = slotsFound [ i ] ;
1310
+ }
1311
+
1282
1312
//Create directives from the form definition
1283
- angular . forEach ( merged , function ( obj , i ) {
1284
- var n = document . createElement ( attrs . sfDecorator || snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1313
+ angular . forEach ( merged , function ( obj , i ) {
1314
+ var n = document . createElement ( attrs . sfDecorator ||
1315
+ snakeCase ( schemaFormDecorators . defaultDecorator , '-' ) ) ;
1285
1316
n . setAttribute ( 'form' , 'schemaForm.form[' + i + ']' ) ;
1286
- var slot ;
1287
- try {
1288
- slot = element [ 0 ] . querySelector ( '*[sf-insert-field="' + obj . key + '"]' ) ;
1289
- } catch ( err ) {
1290
- // field insertion not supported for complex keys
1291
- slot = null ;
1292
- }
1293
- if ( slot ) {
1294
- slot . innerHTML = "" ;
1295
- slot . appendChild ( n ) ;
1296
- } else {
1297
- frag . appendChild ( n ) ;
1317
+
1318
+ // Check if there is a slot to put this in...
1319
+ if ( obj . key ) {
1320
+ var slot = slots [ sfPath . stringify ( obj . key ) ] ;
1321
+ if ( slot ) {
1322
+ while ( slot . firstChild ) {
1323
+ slot . removeChild ( slot . firstChild ) ;
1324
+ }
1325
+ slot . appendChild ( n ) ;
1326
+ return ;
1327
+ }
1298
1328
}
1329
+
1330
+ // ...otherwise add it to the frag
1331
+ frag . appendChild ( n ) ;
1332
+
1299
1333
} ) ;
1300
1334
1301
1335
element [ 0 ] . appendChild ( frag ) ;
0 commit comments