From 0f0693647f252783d7cb4b0ecd6f1ef00c333167 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Fri, 28 Aug 2015 00:10:39 +0200 Subject: [PATCH 1/3] Make the boolean type handle null values Closes #225 --- doc/Configuration-reference.md | 24 ++++++++++++++++++- examples/blog/config.js | 12 +++++++++- package.json | 2 +- .../ng-admin/Crud/column/maBooleanColumn.js | 4 ++-- .../ng-admin/Crud/field/maChoiceField.js | 5 ++-- .../Crud/fieldView/BooleanFieldView.js | 4 ++-- 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/doc/Configuration-reference.md b/doc/Configuration-reference.md index 8a62d171..49a783b1 100644 --- a/doc/Configuration-reference.md +++ b/doc/Configuration-reference.md @@ -357,7 +357,7 @@ A field is the representation of a property of an entity. * [`datetime` Field Type](#datetime-field-type) * [`number` Field Type](#number-field-type) * `float` Field Type -* `boolean` Field Type +* [`boolean` Field Type] * [`choice` and `choices` Field Types](#choice-and-choices-field-types) * `json` Field Type * [`file` Field Type](#file-field-type) @@ -483,6 +483,28 @@ Format for number to string conversion. Based on [Numeral.js](http://numeraljs.c nga.field('cost', 'number').format('$0,0.00'); // now 1234.5 will render as '$1,234.50' +### `boolean` Field Type + +A field of type `boolean` can have 3 values: true, false, or null. That's why the form widget for such a field is a dropdown and not a checkbox. + +* `choices(array)` +Array of choices used for the boolean values. By default: + + [ + { value: null, label: 'undefined' }, + { value: true, label: 'true' }, + { value: false, label: 'false' } + ] + + Override it with custom labels to fit your needs: + + nga.fields('power_user', 'boolean') + .choices([ + { value: null, label: 'not yet decided' }, + { value: true, label: 'enabled' }, + { value: false, label: 'disabled' } + ]); + ### `choice` and `choices` Field Types * `choices(array|function)` diff --git a/examples/blog/config.js b/examples/blog/config.js index 4da752a8..91dfa463 100644 --- a/examples/blog/config.js +++ b/examples/blog/config.js @@ -235,8 +235,18 @@ .label('Upper name') .template('{{ entry.values.name.toUpperCase() }}') ]) + .filters([ + nga.field('published', 'boolean') + ]) .batchActions([]) // disable checkbox column and batch delete - .listActions(['show']); + .listActions(['show', 'edit']); + + tag.editionView() + .fields([ + nga.field('name'), + nga.field('published', 'boolean') + .choices([{ value: null, label: 'null' }, { value: true, label: 'yes'}, {value: false,label: 'no' }]) + ]) tag.showView() .fields([ diff --git a/package.json b/package.json index f9994b06..1e0fb7a3 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "url": "git://github.com/marmelab/ng-admin.git" }, "devDependencies": { - "admin-config": "^0.2.14", + "admin-config": "^0.2.15", "angular": "~1.3.15", "angular-bootstrap": "^0.12.0", "angular-mocks": "1.3.14", diff --git a/src/javascripts/ng-admin/Crud/column/maBooleanColumn.js b/src/javascripts/ng-admin/Crud/column/maBooleanColumn.js index cdd4eec1..a4669654 100644 --- a/src/javascripts/ng-admin/Crud/column/maBooleanColumn.js +++ b/src/javascripts/ng-admin/Crud/column/maBooleanColumn.js @@ -10,9 +10,9 @@ define(function (require) { value: '&', }, link: function(scope) { - scope.isOk = !!scope.value(); + scope.value = scope.value(); }, - template: '' + template: '' }; } diff --git a/src/javascripts/ng-admin/Crud/field/maChoiceField.js b/src/javascripts/ng-admin/Crud/field/maChoiceField.js index 8a8df8b1..4478c613 100644 --- a/src/javascripts/ng-admin/Crud/field/maChoiceField.js +++ b/src/javascripts/ng-admin/Crud/field/maChoiceField.js @@ -10,7 +10,8 @@ function maChoiceField($compile) { 'value': '=', 'entry': '=?', 'datastore': '&?', - 'refresh': '&' + 'refresh': '&', + 'choices': '&?' }, restrict: 'E', compile: function() { @@ -32,7 +33,7 @@ function maChoiceField($compile) { refreshAttributes = 'refresh-delay="refreshDelay" refresh="refresh({ $search: $select.search })"'; } - var choices = field.choices ? field.choices() : []; + var choices = scope.choices() ? scope.choices : (field.choices ? field.choices() : []); var attributes = field.attributes(); scope.placeholder = (attributes && attributes.placeholder) || 'Filter values'; diff --git a/src/javascripts/ng-admin/Crud/fieldView/BooleanFieldView.js b/src/javascripts/ng-admin/Crud/fieldView/BooleanFieldView.js index e8ea9277..aebdab71 100644 --- a/src/javascripts/ng-admin/Crud/fieldView/BooleanFieldView.js +++ b/src/javascripts/ng-admin/Crud/fieldView/BooleanFieldView.js @@ -1,6 +1,6 @@ module.exports = { getReadWidget: () => '', getLinkWidget: () => '' + module.exports.getReadWidget() + '', - getFilterWidget: () => '', - getWriteWidget: () => '' + getFilterWidget: () => ``, + getWriteWidget: () => `
` } From cd7aa5a2c182adc4a4908cd278997cf9134578b0 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Fri, 28 Aug 2015 00:28:55 +0200 Subject: [PATCH 2/3] Add e2e test --- src/javascripts/test/e2e/EditionViewSpec.js | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/javascripts/test/e2e/EditionViewSpec.js b/src/javascripts/test/e2e/EditionViewSpec.js index 9b006e26..47c9b561 100644 --- a/src/javascripts/test/e2e/EditionViewSpec.js +++ b/src/javascripts/test/e2e/EditionViewSpec.js @@ -67,6 +67,28 @@ describe('EditionView', function () { }); }); + describe('BooleanField', function() { + beforeEach(function() { + browser.get(browser.baseUrl + '#/tags/edit/5'); + }); + + it('should render as a choice field', function () { + $$('.ng-admin-field-published .ui-select-container') + .then(function(uiSelect) { + expect(uiSelect.length).toBe(1) + }) + .then(function() { + return $$('.ng-admin-field-published .btn').first().click(); + }) + .then(function() { + return $$('.ng-admin-field-published .ui-select-choices-row'); + }) + .then(function(choices) { + expect(choices.length).toBe(3) + }); + }); + }) + describe('DetailLink', function() { beforeEach(function() { browser.baseUrl + '#/posts/edit/1'; From dc2aae54aad67be2545fad581209c291cd18f017 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Mon, 31 Aug 2015 11:08:28 +0200 Subject: [PATCH 3/3] Fix e2e test --- src/javascripts/test/e2e/EditionViewSpec.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/javascripts/test/e2e/EditionViewSpec.js b/src/javascripts/test/e2e/EditionViewSpec.js index 47c9b561..bb0a1330 100644 --- a/src/javascripts/test/e2e/EditionViewSpec.js +++ b/src/javascripts/test/e2e/EditionViewSpec.js @@ -91,12 +91,15 @@ describe('EditionView', function () { describe('DetailLink', function() { beforeEach(function() { - browser.baseUrl + '#/posts/edit/1'; + browser.get(browser.baseUrl + '#/posts/edit/1'); }); it('should redirect to corresponding detail view even for referenced_list entity', function () { - $$('#row-comments td a').first().click(); - browser.getLocationAbsUrl().then(function(url){ + $$('#row-comments td a').first().click() + .then(function() { + return browser.getLocationAbsUrl(); + }) + .then(function(url){ expect(url).toContain('/comments/edit/'); }); });