From c75f62ee0d550ee3984a2fb33e764dc207b3e081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Tue, 10 Nov 2015 13:32:55 +0100 Subject: [PATCH] Extract groups from options when they are embedded We return the actual embedded resource as allowedValues when they are available. As there are drop-down lists where no embedded information is given (e.g., assignee, responsible), we conditionally extract group information from embedded resources. --- .../edit-drop-down.directive.html | 2 +- .../edit-drop-down.directive.js | 32 +++++++++++++------ .../services/work-package-field.service.js | 8 ++--- .../edit-drop-down.directive.test.js | 1 + 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.html b/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.html index c343a6a88d..092ad4643a 100644 --- a/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.html +++ b/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.html @@ -10,6 +10,6 @@ title="{{ fieldController.editTitle }}" class="inplace-edit-select form--select" id="inplace-edit--write-value--{{::field.name}}" - ng-options="option.name group by customEditorController.optionGroup(option) for option in customEditorController.allowedValues track by option.hrefTracker"> + ng-options="option.name group by option.group for option in customEditorController.allowedValues track by option.hrefTracker"> diff --git a/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.js b/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.js index 54a04d5e11..0c77243c00 100644 --- a/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.js +++ b/frontend/app/components/inplace-edit/directives/field-edit/edit-drop-down/edit-drop-down.directive.js @@ -65,13 +65,30 @@ function InplaceEditorDropDownController($q, $scope, I18n, WorkPackageFieldConfi this.allowedValues = []; + function extractOptions(values) { + var options = values; + + // Extract options and groups from embedded values only + if ($scope.field.allowedValuesEmbedded()) { + options = _.map(values, function(item) { + return _.extend({}, item._links.self, { + name: item._links.self.title, + group: WorkPackageFieldConfigurationService + .getDropDownOptionGroup($scope.field.name, item) + }); + }); + } + + return options; + } + this.updateAllowedValues = function(field) { var customEditorController = this; return $q(function(resolve) { $scope.field.getAllowedValues() .then(function(values) { - + var options; var sorting = WorkPackageFieldConfigurationService .getDropdownSortingStrategy(field); @@ -79,24 +96,21 @@ function InplaceEditorDropDownController($q, $scope, I18n, WorkPackageFieldConfi values = _.sortBy(values, sorting); } + options = extractOptions(values); + if (!$scope.field.isRequired()) { - values = addEmptyOption(values); + options = addEmptyOption(options); } - addHrefTracker(values); + addHrefTracker(options); - customEditorController.allowedValues = values; + customEditorController.allowedValues = options; resolve(); }); }); }; - this.optionGroup = function(option) { - return WorkPackageFieldConfigurationService - .getDropDownOptionGroup($scope.field.name, option); - }; - var addEmptyOption = function(values) { var emptyOption = { props: { href: null, name: $scope.field.placeholder } }; diff --git a/frontend/app/components/inplace-edit/services/work-package-field.service.js b/frontend/app/components/inplace-edit/services/work-package-field.service.js index 7530eba7c4..559fa0dbd0 100644 --- a/frontend/app/components/inplace-edit/services/work-package-field.service.js +++ b/frontend/app/components/inplace-edit/services/work-package-field.service.js @@ -140,12 +140,7 @@ function WorkPackageFieldService($q, $http, $filter, I18n, WorkPackagesHelper, function getEmbeddedAllowedValues(workPackage, field) { var options = []; var schema = getSchema(workPackage); - var allowedValues = schema.props[field]._embedded.allowedValues; - options = _.map(allowedValues, function(item) { - return _.extend({}, item, { name: item.name || item.title }); - }); - - return options; + return schema.props[field]._embedded.allowedValues; } function getLinkedAllowedValues(workPackage, field) { @@ -401,6 +396,7 @@ function WorkPackageFieldService($q, $http, $filter, I18n, WorkPackagesHelper, getValue: getValue, getLabel: getLabel, getAllowedValues: getAllowedValues, + allowedValuesEmbedded: allowedValuesEmbedded, format: format, getInplaceEditStrategy: getInplaceEditStrategy, getInplaceDisplayStrategy: getInplaceDisplayStrategy diff --git a/frontend/tests/unit/tests/components/inplace-edit/directives/edit-drop-down.directive.test.js b/frontend/tests/unit/tests/components/inplace-edit/directives/edit-drop-down.directive.test.js index ff93f75a00..538ba3405f 100644 --- a/frontend/tests/unit/tests/components/inplace-edit/directives/edit-drop-down.directive.test.js +++ b/frontend/tests/unit/tests/components/inplace-edit/directives/edit-drop-down.directive.test.js @@ -58,6 +58,7 @@ describe('Inplace editor drop-down directive', function() { scope.field = { getAllowedValues: sinon.stub().returns(allowedValuePromise), + allowedValuesEmbedded: sinon.stub().returns(false), format: sinon.stub().returns({ props: { name: allowedValues[0].name } }), isRequired: sinon.stub().returns(true), value: { props: { href: allowedValues[0].href } }