From 91b86c96b7b6004ea66e507684fea07b31d7973e Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Fri, 18 Mar 2016 21:55:52 +0000 Subject: [PATCH 1/6] Added arrayIndices to allow conditions to access the array index in nested arrays --- src/services/builder.js | 7 ++ src/services/decorators.js | 2 +- test/directives/schema-form-test.js | 120 ++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/services/builder.js b/src/services/builder.js index 69a93a15c..c1a6bbcdc 100644 --- a/src/services/builder.js +++ b/src/services/builder.js @@ -111,7 +111,14 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s '.condition, { model: model, "arrayIndex": $index})'; if (args.form.key) { var strKey = sfPathProvider.stringify(args.form.key); + var arrayDepth = (args.path.split('.items[').length - 1); + var arrayIndices = '$index'; + for (var i = 1; i < arrayDepth; i++) { + arrayIndices = Array(i + 1).join('$parent.') + '$index,' + arrayIndices; + } + evalExpr = 'evalExpr(' + args.path + '.condition,{ model: model, "arrayIndex": $index, ' + + '"arrayIndices": [' + arrayIndices + '],' + '"modelValue": model' + (strKey[0] === '[' ? '' : '.') + strKey + '})'; } diff --git a/src/services/decorators.js b/src/services/decorators.js index 1aa0987bd..f4a2d5df1 100644 --- a/src/services/decorators.js +++ b/src/services/decorators.js @@ -207,7 +207,7 @@ angular.module('schemaForm').provider('schemaFormDecorators', var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})'; if (form.key) { - evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath.stringify(form.key) + '})'; + evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "arrayIndices": [' + form.key.filter(function(e) { return e === parseInt(e, 10)}).toString() + '], "modelValue": model' + sfPath.stringify(form.key) + '})'; } angular.forEach(element.children(), function(child) { diff --git a/test/directives/schema-form-test.js b/test/directives/schema-form-test.js index a34553c86..26db62811 100644 --- a/test/directives/schema-form-test.js +++ b/test/directives/schema-form-test.js @@ -2537,6 +2537,126 @@ describe('directive',function(){ }); + it('should remove or add fields in an array depending on conditions using arrayIndices', function (done) { + + inject(function ($compile, $rootScope) { + var scope = $rootScope.$new(); + scope.model = { + "transportCategory": [ + { + "mode": "Car", + "transportOption": [ + { + "name": "Bertie", + "forSale": "yes", + "price": 100 + }, + { + "name": "Lightning McQueen", + "forSale": "no" + } + ] + }, + { + "mode": "Horse", + "transportOption": [ + { + "name": "Phar Lap", + "forSale": "no" + }, + { + "name": "Greyhound", + "forSale": "yes", + "price": 1000 + } + ] + } + ] + }; + + scope.schema = { + type: "object", + properties: { + transportCategory: { + type: "array", + items: { + type: "object", + properties: { + mode: { type: "string", enum: ["Car", "Motorbike", "Horse"] }, + transportOption: { + type: "array", + items: { + type: "object", + properties: { + name: { type: "string" }, + numberOfWheels: { type: "number" }, + forSale: { type: "string", enum: ["yes", "no"] }, + price: { type: "number" } + } + } + } + } + } + } + } + }; + + scope.form = [ + { + key: "transportCategory", + items: [ + "transportCategory[].mode", + { + key: "transportCategory[].transportOption", + items: [ + "transportCategory[].transportOption[].name", + { + key: "transportCategory[].transportOption[].numberOfWheels", + condition: "model.transportCategory[arrayIndices[0]].mode != 'Horse'" + }, + "transportCategory[].transportOption[].forSale", + { + key: "transportCategory[].transportOption[].price", + condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].forSale == 'yes'" + } + ] + } + ] + } + ]; + + var tmpl = angular.element('
'); + + $compile(tmpl)(scope); + $rootScope.$apply(); + + /*** numberOfWheels condition tests ***/ + tmpl.children().eq(0).children().eq(1).children().eq(0).find('input[name="numberOfWheels"]').length.should.be.eq(2); + tmpl.children().eq(0).children().eq(1).children().eq(1).find('input[name="numberOfWheels"]').length.should.be.eq(0); + //numberOfWheels [0][0] + tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(0).children().eq(2).children().eq(0).text().should.be.equal('numberOfWheels'); + //numberOfWheels [0][1] + tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(1).children().eq(2).children().eq(0).text().should.be.equal('numberOfWheels'); + //numberOfWheels [1][0] + tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(0).children().eq(2).children().eq(0).text().should.be.equal('forSale'); + //numberOfWheels [1][1] + tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(1).children().eq(2).children().eq(0).text().should.be.equal('forSale'); + + /*** price field condition tests ***/ + tmpl.children().eq(0).children().eq(1).children().find('input[name="price"]').length.should.be.eq(2); + + //price [0][0] + tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(0).find('input[name="price"]').length.should.be.eq(1); + //price [0][1] + tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(1).find('input[name="price"]').length.should.be.eq(0); + //price [1][0] + tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(0).find('input[name="price"]').length.should.be.eq(0); + //price [1][1] + tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(1).find('input[name="price"]').length.should.be.eq(1); + + done(); + }); + }); }); }); From c95cb9cbcb16aa8739aa3328a804c794c8e02cf2 Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Tue, 5 Apr 2016 22:38:24 +0100 Subject: [PATCH 2/6] Alternative method of determining array depth --- src/services/builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/builder.js b/src/services/builder.js index c1a6bbcdc..8b3b19f45 100644 --- a/src/services/builder.js +++ b/src/services/builder.js @@ -111,7 +111,7 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s '.condition, { model: model, "arrayIndex": $index})'; if (args.form.key) { var strKey = sfPathProvider.stringify(args.form.key); - var arrayDepth = (args.path.split('.items[').length - 1); + var arrayDepth = args.form.key.filter(function(e) { return e === '' }).length; var arrayIndices = '$index'; for (var i = 1; i < arrayDepth; i++) { arrayIndices = Array(i + 1).join('$parent.') + '$index,' + arrayIndices; From 730fbed94e786a644606a8e811d10194c64ed335 Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Thu, 7 Apr 2016 22:55:05 +0100 Subject: [PATCH 3/6] Add tests for array.object.object.array structure --- test/directives/schema-form-test.js | 154 +++++++++++++++++++++++----- 1 file changed, 127 insertions(+), 27 deletions(-) diff --git a/test/directives/schema-form-test.js b/test/directives/schema-form-test.js index 26db62811..740a75b07 100644 --- a/test/directives/schema-form-test.js +++ b/test/directives/schema-form-test.js @@ -2549,11 +2549,25 @@ describe('directive',function(){ { "name": "Bertie", "forSale": "yes", - "price": 100 + "price": 100, + "history": { + "historyKnown": "no" + } }, { "name": "Lightning McQueen", - "forSale": "no" + "forSale": "no", + "history": { + "historyKnown": "yes", + "previousOwners": [ + { + "ownerName": "" + }, + { + "ownerName": "Ben" + } + ] + } } ] }, @@ -2567,7 +2581,15 @@ describe('directive',function(){ { "name": "Greyhound", "forSale": "yes", - "price": 1000 + "price": 1000, + "history": { + "historyKnown": "yes", + "previousOwners": [ + { + "ownerName": "Tom" + } + ] + } } ] } @@ -2591,7 +2613,24 @@ describe('directive',function(){ name: { type: "string" }, numberOfWheels: { type: "number" }, forSale: { type: "string", enum: ["yes", "no"] }, - price: { type: "number" } + price: { type: "number" }, + history: { + type: "object", + properties: { + historyKnown: { type: "string", enum: ["yes", "no"] }, + previousOwners: { + type: "array", + items: { + type: "object", + properties: { + ownerName: { type: "string" }, + purchaseDate: { type: "string" }, + logBookProvided: { type: "string", enum: ["yes", "no"] } + } + } + } + } + } } } } @@ -2618,6 +2657,22 @@ describe('directive',function(){ { key: "transportCategory[].transportOption[].price", condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].forSale == 'yes'" + }, + "transportCategory[].transportOption[].history.historyKnown", + { + key: "transportCategory[].transportOption[].history.previousOwners", + condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.historyKnown == 'yes'", + items: [ + "transportCategory[].transportOption[].history.previousOwners[].ownerName", + { + key: "transportCategory[].transportOption[].history.previousOwners[].purchaseDate", + condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.previousOwners[arrayIndices[2]].ownerName.length > 2", + }, + { + key: "transportCategory[].transportOption[].history.previousOwners[].logBookProvided", + condition: "model.transportCategory[arrayIndices[0]].mode != 'Horse' && model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.previousOwners[arrayIndices[2]].ownerName.length > 2" + } + ] } ] } @@ -2630,29 +2685,74 @@ describe('directive',function(){ $compile(tmpl)(scope); $rootScope.$apply(); - /*** numberOfWheels condition tests ***/ - tmpl.children().eq(0).children().eq(1).children().eq(0).find('input[name="numberOfWheels"]').length.should.be.eq(2); - tmpl.children().eq(0).children().eq(1).children().eq(1).find('input[name="numberOfWheels"]').length.should.be.eq(0); - //numberOfWheels [0][0] - tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(0).children().eq(2).children().eq(0).text().should.be.equal('numberOfWheels'); - //numberOfWheels [0][1] - tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(1).children().eq(2).children().eq(0).text().should.be.equal('numberOfWheels'); - //numberOfWheels [1][0] - tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(0).children().eq(2).children().eq(0).text().should.be.equal('forSale'); - //numberOfWheels [1][1] - tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(1).children().eq(2).children().eq(0).text().should.be.equal('forSale'); - - /*** price field condition tests ***/ - tmpl.children().eq(0).children().eq(1).children().find('input[name="price"]').length.should.be.eq(2); - - //price [0][0] - tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(0).find('input[name="price"]').length.should.be.eq(1); - //price [0][1] - tmpl.children().eq(0).children().eq(1).children().eq(0).children().eq(2).children().eq(1).children().eq(1).find('input[name="price"]').length.should.be.eq(0); - //price [1][0] - tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(0).find('input[name="price"]').length.should.be.eq(0); - //price [1][1] - tmpl.children().eq(0).children().eq(1).children().eq(1).children().eq(2).children().eq(1).children().eq(1).find('input[name="price"]').length.should.be.eq(1); + //References to sections of the rendered form to make the test more readable + var renderedForm = { + node: tmpl.children().eq(0).children().eq(1), + transportCategory: [ + { + node: tmpl.children().eq(0).children().eq(1).children().eq(0), + }, + { + node: tmpl.children().eq(0).children().eq(1).children().eq(1), + } + ] + }; + + renderedForm.transportCategory[0]['transportOption'] = [ + { node: renderedForm.transportCategory[0].node.children().eq(2).children().eq(1).children().eq(0) }, + { node: renderedForm.transportCategory[0].node.children().eq(2).children().eq(1).children().eq(1) } + ]; + + renderedForm.transportCategory[1]['transportOption'] = [ + { node: renderedForm.transportCategory[1].node.children().eq(2).children().eq(1).children().eq(0) }, + { node: renderedForm.transportCategory[1].node.children().eq(2).children().eq(1).children().eq(1) } + ]; + + renderedForm.transportCategory[0].transportOption[1]['history'] = { + previousOwners: [ + renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(0), + renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(1) + ] + }; + + /*** transportCategory[].transportOption[].numberOfWheels condition tests ***/ + renderedForm.transportCategory[0].node.find('input[name="numberOfWheels"]').length.should.be.eq(2); + renderedForm.transportCategory[1].node.find('input[name="numberOfWheels"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[0].node.find('input[name="numberOfWheels"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].node.find('input[name="numberOfWheels"]').length.should.be.eq(1); + renderedForm.transportCategory[1].transportOption[0].node.find('input[name="numberOfWheels"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[1].node.find('input[name="numberOfWheels"]').length.should.be.eq(0); + + /*** transportCategory[].transportOption[].price field condition tests ***/ + renderedForm.node.children().find('input[name="price"]').length.should.be.eq(2); + renderedForm.transportCategory[0].transportOption[0].node.find('input[name="price"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].node.find('input[name="price"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[0].node.find('input[name="price"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[1].node.find('input[name="price"]').length.should.be.eq(1); + + /*** transportCategory[].transportOption[].history.previousOwners.ownerName field condition tests ***/ + renderedForm.transportCategory[0].transportOption[0].node.find('input[name="ownerName"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].node.find('input[name="ownerName"]').length.should.be.eq(2); + renderedForm.transportCategory[1].transportOption[0].node.find('input[name="ownerName"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[1].node.find('input[name="ownerName"]').length.should.be.eq(1); + + /*** transportCategory[].transportOption[].history.previousOwners[].purchaseDate field condition tests ***/ + renderedForm.transportCategory[0].transportOption[0].node.find('input[name="purchaseDate"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].node.find('input[name="purchaseDate"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].find('input[name="purchaseDate"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].find('input[name="purchaseDate"]').length.should.be.eq(1); + + renderedForm.transportCategory[1].transportOption[0].node.find('input[name="purchaseDate"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[1].node.find('input[name="purchaseDate"]').length.should.be.eq(1); + + /*** transportCategory[].transportOption[].history.previousOwners[].logBookProvided field condition tests ***/ + renderedForm.transportCategory[0].transportOption[0].node.find('select[name="logBookProvided"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].node.find('select[name="logBookProvided"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].find('select[name="logBookProvided"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].find('select[name="logBookProvided"]').length.should.be.eq(1); + + renderedForm.transportCategory[1].transportOption[0].node.find('select[name="logBookProvided"]').length.should.be.eq(0); + renderedForm.transportCategory[1].transportOption[1].node.find('select[name="logBookProvided"]').length.should.be.eq(0); done(); }); From e1383aa2b981829bf81315b8fc47e0b096992359 Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Thu, 7 Apr 2016 23:57:40 +0100 Subject: [PATCH 4/6] Bug fix: 2 x $parent required --- src/services/builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/builder.js b/src/services/builder.js index 8b3b19f45..1d8ec622d 100644 --- a/src/services/builder.js +++ b/src/services/builder.js @@ -114,7 +114,7 @@ angular.module('schemaForm').provider('sfBuilder', ['sfPathProvider', function(s var arrayDepth = args.form.key.filter(function(e) { return e === '' }).length; var arrayIndices = '$index'; for (var i = 1; i < arrayDepth; i++) { - arrayIndices = Array(i + 1).join('$parent.') + '$index,' + arrayIndices; + arrayIndices = Array(i + 1).join('$parent.$parent.') + '$index,' + arrayIndices; } evalExpr = 'evalExpr(' + args.path + '.condition,{ model: model, "arrayIndex": $index, ' + From c13017b2c2d9f92e28b04654f173bac8651568dd Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Thu, 26 May 2016 10:10:50 +0100 Subject: [PATCH 5/6] Test for conditions on 4 deep array Added a 4th level to the nested array to confirm arrayIndices work --- test/directives/schema-form-test.js | 60 ++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/test/directives/schema-form-test.js b/test/directives/schema-form-test.js index 740a75b07..f9a3344f4 100644 --- a/test/directives/schema-form-test.js +++ b/test/directives/schema-form-test.js @@ -2564,7 +2564,17 @@ describe('directive',function(){ "ownerName": "" }, { - "ownerName": "Ben" + "ownerName": "Arlo", + "logBookProvided": "yes", + "logBookEntry": [ + { + "entryId": 2, + "entryDate": "2015-06-23" + }, + { + "entryId": 4 + } + ] } ] } @@ -2625,7 +2635,18 @@ describe('directive',function(){ properties: { ownerName: { type: "string" }, purchaseDate: { type: "string" }, - logBookProvided: { type: "string", enum: ["yes", "no"] } + logBookProvided: { type: "string", enum: ["yes", "no"] }, + logBookEntry: { + type: "array", + items: { + type: "object", + properties: { + entryId: { type: "number" }, + entryDate: { type: "string" }, + entryNote: { type: "string" } + } + } + } } } } @@ -2671,7 +2692,19 @@ describe('directive',function(){ { key: "transportCategory[].transportOption[].history.previousOwners[].logBookProvided", condition: "model.transportCategory[arrayIndices[0]].mode != 'Horse' && model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.previousOwners[arrayIndices[2]].ownerName.length > 2" - } + }, + { + key: "transportCategory[].transportOption[].history.previousOwners[].logBookEntry", + condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.previousOwners[arrayIndices[2]].logBookProvided == 'yes'", + items: [ + "transportCategory[].transportOption[].history.previousOwners[].logBookEntry[].entryId", + "transportCategory[].transportOption[].history.previousOwners[].logBookEntry[].entryDate", + { + key: "transportCategory[].transportOption[].history.previousOwners[].logBookEntry[].entryNote", + condition: "model.transportCategory[arrayIndices[0]].transportOption[arrayIndices[1]].history.previousOwners[arrayIndices[2]].logBookEntry[arrayIndices[3]].entryDate.length > 2" + } + ] + } ] } ] @@ -2710,10 +2743,15 @@ describe('directive',function(){ renderedForm.transportCategory[0].transportOption[1]['history'] = { previousOwners: [ - renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(0), - renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(1) + { node: renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(0) }, + { node: renderedForm.transportCategory[0].transportOption[1].node.children().eq(5).children().eq(1) } ] }; + + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1]['logBookEntry'] = [ + { node: renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].node.children().eq(1).children().eq(4).children().eq(1).children().eq(0) }, + { node: renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].node.children().eq(1).children().eq(4).children().eq(1).children().eq(1) } + ]; /*** transportCategory[].transportOption[].numberOfWheels condition tests ***/ renderedForm.transportCategory[0].node.find('input[name="numberOfWheels"]').length.should.be.eq(2); @@ -2739,8 +2777,8 @@ describe('directive',function(){ /*** transportCategory[].transportOption[].history.previousOwners[].purchaseDate field condition tests ***/ renderedForm.transportCategory[0].transportOption[0].node.find('input[name="purchaseDate"]').length.should.be.eq(0); renderedForm.transportCategory[0].transportOption[1].node.find('input[name="purchaseDate"]').length.should.be.eq(1); - renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].find('input[name="purchaseDate"]').length.should.be.eq(0); - renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].find('input[name="purchaseDate"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].node.find('input[name="purchaseDate"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].node.find('input[name="purchaseDate"]').length.should.be.eq(1); renderedForm.transportCategory[1].transportOption[0].node.find('input[name="purchaseDate"]').length.should.be.eq(0); renderedForm.transportCategory[1].transportOption[1].node.find('input[name="purchaseDate"]').length.should.be.eq(1); @@ -2748,12 +2786,16 @@ describe('directive',function(){ /*** transportCategory[].transportOption[].history.previousOwners[].logBookProvided field condition tests ***/ renderedForm.transportCategory[0].transportOption[0].node.find('select[name="logBookProvided"]').length.should.be.eq(0); renderedForm.transportCategory[0].transportOption[1].node.find('select[name="logBookProvided"]').length.should.be.eq(1); - renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].find('select[name="logBookProvided"]').length.should.be.eq(0); - renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].find('select[name="logBookProvided"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[0].node.find('select[name="logBookProvided"]').length.should.be.eq(0); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].node.find('select[name="logBookProvided"]').length.should.be.eq(1); renderedForm.transportCategory[1].transportOption[0].node.find('select[name="logBookProvided"]').length.should.be.eq(0); renderedForm.transportCategory[1].transportOption[1].node.find('select[name="logBookProvided"]').length.should.be.eq(0); + /*** transportCategory[].transportOption[].history.previousOwners[].logBookEntry[].entryNote field condition tests ***/ + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].logBookEntry[0].node.find('input[name="entryNote"]').length.should.be.eq(1); + renderedForm.transportCategory[0].transportOption[1].history.previousOwners[1].logBookEntry[1].node.find('input[name="entryNote"]').length.should.be.eq(0); + done(); }); }); From ac6cee43cd765995e91c19c0430b69593a470817 Mon Sep 17 00:00:00 2001 From: Joel Kent Date: Wed, 8 Jun 2016 15:52:30 +0100 Subject: [PATCH 6/6] Array index and array indices for onClick --- src/directives/field.js | 15 ++++++++++++--- src/services/decorators.js | 8 +++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/directives/field.js b/src/directives/field.js index 66147d3f2..f3c980962 100644 --- a/src/directives/field.js +++ b/src/directives/field.js @@ -48,14 +48,23 @@ angular.module('schemaForm').directive('sfField', }; scope.buttonClick = function($event, form) { + var arrayDepth = form.key.filter(function(e) { return e === '' }).length; + var arrayIndices = (arrayDepth > 1 ? Array(arrayDepth - 1).join('$parent.$parent.$parent.') + '$parent.$parent.$index,' : ''); + for (var i = arrayDepth; i > 2; i--) { + arrayIndices += Array(i - 1).join('$parent.$parent.$parent.') + '$index,'; + } + arrayIndices += '$index'; + arrayIndices = scope.$eval('[' + arrayIndices + ']'); + if (angular.isFunction(form.onClick)) { - form.onClick($event, form); + + form.onClick($event, form, arrayIndices[arrayDepth-1], arrayIndices); } else if (angular.isString(form.onClick)) { if (sfSchema) { //evaluating in scope outside of sfSchemas isolated scope - sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form}); + sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayDepth-1], arrayIndices: arrayIndices}); } else { - scope.$eval(form.onClick, {'$event': $event, form: form}); + scope.$eval(form.onClick, {'$event': $event, form: form, arrayIndex: '$index', arrayIndices: '[' + arrayIndices + ']'}); } } }; diff --git a/src/services/decorators.js b/src/services/decorators.js index f4a2d5df1..ad773e104 100644 --- a/src/services/decorators.js +++ b/src/services/decorators.js @@ -74,14 +74,16 @@ angular.module('schemaForm').provider('schemaFormDecorators', }; scope.buttonClick = function($event, form) { + var arrayIndices = form.key.filter(function(e) { return e === parseInt(e, 10)}); + if (angular.isFunction(form.onClick)) { - form.onClick($event, form); + form.onClick($event, form, arrayIndices[arrayIndices.length-1], arrayIndices); } else if (angular.isString(form.onClick)) { if (sfSchema) { //evaluating in scope outside of sfSchemas isolated scope - sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form}); + sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayIndices.length-1], arrayIndices: arrayIndices}); } else { - scope.$eval(form.onClick, {'$event': $event, form: form}); + scope.$eval(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayIndices.length-1], arrayIndices: arrayIndices}); } } };