From 6366fc3a6cb000e6ce2c6f2c2f8f77a73ec4a4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Macias?= Date: Wed, 13 May 2015 15:09:08 +0200 Subject: [PATCH 1/3] Make choice field required works and fix angular add empty first option --- examples/blog/config.js | 3 ++- .../ng-admin/Crud/field/maChoiceField.js | 8 +++----- .../ng-admin/Crud/field/maChoicesField.js | 19 ++----------------- src/sass/ng-admin.scss | 7 ------- 4 files changed, 7 insertions(+), 30 deletions(-) diff --git a/examples/blog/config.js b/examples/blog/config.js index b72528cd..757ce99b 100644 --- a/examples/blog/config.js +++ b/examples/blog/config.js @@ -209,7 +209,8 @@ .label('Post') .map(truncate) .targetEntity(post) - .targetField(nga.field('title')), + .targetField(nga.field('title')) + .validation({ required: true }), ]); comment.editionView() diff --git a/src/javascripts/ng-admin/Crud/field/maChoiceField.js b/src/javascripts/ng-admin/Crud/field/maChoiceField.js index 36970440..2c8cd82f 100644 --- a/src/javascripts/ng-admin/Crud/field/maChoiceField.js +++ b/src/javascripts/ng-admin/Crud/field/maChoiceField.js @@ -35,11 +35,9 @@ define(function (require) { } }, template: -'' + + '' + '' }; } diff --git a/src/javascripts/ng-admin/Crud/field/maChoicesField.js b/src/javascripts/ng-admin/Crud/field/maChoicesField.js index 21462d03..e39355bc 100644 --- a/src/javascripts/ng-admin/Crud/field/maChoicesField.js +++ b/src/javascripts/ng-admin/Crud/field/maChoicesField.js @@ -33,29 +33,14 @@ define(function (require) { for (var name in attributes) { select[name] = attributes[name]; } - scope.contains = contains; }, template: -'' + '' }; } - function contains (collection, item) { - if (!collection) { - return false; - } - for (var i = 0, l = collection.length; i < l; i++) { - if (collection[i] == item) { // == is intentional here - return true; - } - } - return false; - } - maChoicesField.$inject = []; return maChoicesField; diff --git a/src/sass/ng-admin.scss b/src/sass/ng-admin.scss index 9ba1114f..50266312 100644 --- a/src/sass/ng-admin.scss +++ b/src/sass/ng-admin.scss @@ -179,13 +179,6 @@ div.bottom-loader { } } -/** - * Fields - */ -ma-choice-field select option:empty { - display:none; -} - /** * Edition form */ From 9bae51f03ffb141ebcf7d22a1180b7070c72383a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Macias?= Date: Wed, 13 May 2015 15:49:55 +0200 Subject: [PATCH 2/3] Fix karma tests --- .../test/unit/Crud/field/maChoiceFieldSpec.js | 31 ++++++++----------- .../unit/Crud/field/maChoicesFieldSpec.js | 22 ++++++------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/javascripts/test/unit/Crud/field/maChoiceFieldSpec.js b/src/javascripts/test/unit/Crud/field/maChoiceFieldSpec.js index bc1ac39d..477d6334 100644 --- a/src/javascripts/test/unit/Crud/field/maChoiceFieldSpec.js +++ b/src/javascripts/test/unit/Crud/field/maChoiceFieldSpec.js @@ -42,11 +42,8 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - // strange: angular.js adds an initial option of value '? value ?' for IE compatibility - // so our initial option is of index 1, not 0 - expect(options[1].innerHTML).toEqual('-- select a value --'); - expect(options[1].value).toEqual(''); - expect(options[1].selected).toBeTruthy(); + expect(options[0].innerHTML).toEqual('-- select a value --'); + expect(options[0].value).toEqual(''); }); it("should provide an initial option for non-required fields", function () { @@ -56,10 +53,8 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - // strange: angular.js adds an initial option of value '? value ?' for IE compatibility - // so our initial option is of index 1, not 0 - expect(options[1].innerHTML).toEqual('foo'); - expect(options[1].value).toEqual('bar'); + expect(options[1].label).toEqual('foo'); + expect(options[1].value).toEqual('0'); }); it("should contain the choices as options", function () { @@ -71,10 +66,10 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - expect(options[1].innerHTML).toEqual('foo'); - expect(options[1].value).toEqual('bar'); - expect(options[2].innerHTML).toEqual('baz'); - expect(options[2].value).toEqual('bazValue'); + expect(options[1].label).toEqual('foo'); + expect(options[1].value).toEqual('0'); + expect(options[2].label).toEqual('baz'); + expect(options[2].value).toEqual('1'); }); it("should contain the choices from choicesFunc as options", function () { @@ -89,10 +84,10 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - expect(options[1].innerHTML).toEqual('foo'); - expect(options[1].value).toEqual('bar'); - expect(options[2].innerHTML).toEqual('baz'); - expect(options[2].value).toEqual('bazValue'); + expect(options[1].label).toEqual('foo'); + expect(options[1].value).toEqual('0'); + expect(options[2].label).toEqual('baz'); + expect(options[2].value).toEqual('1'); }); it("should pass entry to choicesFunc", function () { @@ -119,7 +114,7 @@ define(function (require) { scope.$digest(); var options = element.find('option'); expect(options[2].selected).toBeTruthy(); - expect(options[2].value).toEqual('bazValue'); + expect(options[2].value).toEqual('1'); }); }); diff --git a/src/javascripts/test/unit/Crud/field/maChoicesFieldSpec.js b/src/javascripts/test/unit/Crud/field/maChoicesFieldSpec.js index 7a67c481..18861c14 100644 --- a/src/javascripts/test/unit/Crud/field/maChoicesFieldSpec.js +++ b/src/javascripts/test/unit/Crud/field/maChoicesFieldSpec.js @@ -65,10 +65,10 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - expect(options[0].innerHTML).toEqual('foo'); - expect(options[0].value).toEqual('bar'); - expect(options[1].innerHTML).toEqual('baz'); - expect(options[1].value).toEqual('bazValue'); + expect(options[0].label).toEqual('foo'); + expect(options[0].value).toEqual('0'); + expect(options[1].label).toEqual('baz'); + expect(options[1].value).toEqual('1'); }); it("should contain the choices as options", function () { @@ -79,10 +79,10 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - expect(options[0].innerHTML).toEqual('foo'); - expect(options[0].value).toEqual('bar'); - expect(options[1].innerHTML).toEqual('baz'); - expect(options[1].value).toEqual('bazValue'); + expect(options[0].label).toEqual('foo'); + expect(options[0].value).toEqual('0'); + expect(options[1].label).toEqual('baz'); + expect(options[1].value).toEqual('1'); }); it("should have the options with the bounded value selected", function () { @@ -95,11 +95,11 @@ define(function (require) { var element = $compile(directiveUsage)(scope); scope.$digest(); var options = element.find('option'); - expect(options[0].value).toEqual('fooValue'); + expect(options[0].value).toEqual('0'); expect(options[0].selected).toBeTruthy(); - expect(options[1].value).toEqual('barValue'); + expect(options[1].value).toEqual('1'); expect(options[1].selected).toBeFalsy(); - expect(options[2].value).toEqual('bazValue'); + expect(options[2].value).toEqual('2'); expect(options[2].selected).toBeTruthy(); }); From 9201c70f297334feeec73b6a724c8c0da0084562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Macias?= Date: Wed, 13 May 2015 17:40:42 +0200 Subject: [PATCH 3/3] Fix e2e tests --- src/javascripts/test/e2e/filterViewSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/javascripts/test/e2e/filterViewSpec.js b/src/javascripts/test/e2e/filterViewSpec.js index 71dea62d..4603c227 100644 --- a/src/javascripts/test/e2e/filterViewSpec.js +++ b/src/javascripts/test/e2e/filterViewSpec.js @@ -52,8 +52,8 @@ describe('Global filter', function () { }); it('should filter on reference', function () { - // Filter on post_id '3' - $$('.filters .filter select option[value="3"]').click(); + // Filter on post_id '3' (Perspiciatis adipisci vero qui ipsam iure porro) + $$('.filters .filter select option[value="9"]').click(); $$('.filters button[type="submit"]').click(); $$('.grid tr td:nth-child(4)').then(function (tdElements) { expect(tdElements.length).toBe(2); @@ -63,8 +63,8 @@ describe('Global filter', function () { }); it('should update the pagination total', function () { - // Filter on post id '3' - $$('.filters .filter select option[value="3"]').click(); + // Filter on post id '3' (Perspiciatis adipisci vero qui ipsam iure porro) + $$('.filters .filter select option[value="9"]').click(); $$('.filters button[type="submit"]').click(); $$('ma-datagrid-pagination .total').then(function (totalElements) { expect(totalElements[0].getText()).toBe('1 - 2 on 2');