11// Deps is sort of a problem for us, maybe in the future we will ask the user to depend
22// on modules for add-ons
33
4- var deps = [ 'ObjectPath' ] ;
4+ var deps = [ ] ;
55try {
66 //This throws an expection if module does not exist.
77 angular . module ( 'ngSanitize' ) ;
@@ -23,29 +23,31 @@ try {
2323angular . module ( 'schemaForm' , deps ) ;
2424
2525angular . module ( 'schemaForm' ) . provider ( 'sfPath' ,
26- [ 'ObjectPathProvider' , function ( ObjectPathProvider ) {
27- var ObjectPath = { parse : ObjectPathProvider . parse } ;
26+ [ function ( ) {
27+ var sfPath = { parse : ObjectPath . parse } ;
2828
2929 // if we're on Angular 1.2.x, we need to continue using dot notation
3030 if ( angular . version . major === 1 && angular . version . minor < 3 ) {
31- ObjectPath . stringify = function ( arr ) {
31+ sfPath . stringify = function ( arr ) {
3232 return Array . isArray ( arr ) ? arr . join ( '.' ) : arr . toString ( ) ;
3333 } ;
3434 } else {
35- ObjectPath . stringify = ObjectPathProvider . stringify ;
35+ sfPath . stringify = ObjectPath . stringify ;
3636 }
3737
3838 // We want this to use whichever stringify method is defined above,
3939 // 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 ) ;
4242 } ;
4343
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+
4749 this . $get = function ( ) {
48- return ObjectPath ;
50+ return sfPath ;
4951 } ;
5052} ] ) ;
5153
@@ -949,8 +951,8 @@ angular.module('schemaForm').factory('sfValidator', [function() {
949951/**
950952 * Directive that handles the model arrays
951953 */
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 ) {
954956
955957 var setIndex = function ( index ) {
956958 return function ( form ) {
@@ -977,6 +979,14 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
977979 // other hand it enables two way binding.
978980 var list = sfSelect ( form . key , scope . model ) ;
979981
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+
980990 // Since ng-model happily creates objects in a deep path when setting a
981991 // a value but not arrays we need to create the array.
982992 if ( angular . isUndefined ( list ) ) {
@@ -996,7 +1006,7 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
9961006 if ( form . items . length > 1 ) {
9971007 subForm = {
9981008 type : 'section' ,
999- items : form . items . map ( function ( item ) {
1009+ items : form . items . map ( function ( item ) {
10001010 item . ngModelOptions = form . ngModelOptions ;
10011011 item . readonly = form . readonly ;
10021012 return item ;
@@ -1024,8 +1034,20 @@ angular.module('schemaForm').directive('sfArray', ['sfSelect', 'schemaForm', 'sf
10241034 var len = list . length ;
10251035 var copy = scope . copyWithIndex ( len ) ;
10261036 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+ }
10291051 }
10301052 } ) ;
10311053
@@ -1201,8 +1223,8 @@ FIXME: real documentation
12011223
12021224angular . module ( 'schemaForm' )
12031225 . 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 ) {
12061228
12071229 var SNAKE_CASE_REGEXP = / [ A - Z ] / g;
12081230 var snakeCase = function ( name , separator ) {
@@ -1279,23 +1301,35 @@ angular.module('schemaForm')
12791301 //clean all but pre existing html.
12801302 element . children ( ':not(.schema-form-ignore)' ) . remove ( ) ;
12811303
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+
12821312 //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 , '-' ) ) ;
12851316 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+ }
12981328 }
1329+
1330+ // ...otherwise add it to the frag
1331+ frag . appendChild ( n ) ;
1332+
12991333 } ) ;
13001334
13011335 element [ 0 ] . appendChild ( frag ) ;
0 commit comments