Refactor EditableFieldsService and EditPaneController

Move isActiveField and getPendingFormChanges to EditableFieldsState service.
pull/3646/head
Alex Dik 9 years ago
parent eb32cf8eb1
commit 9b290d18f3
  1. 23
      frontend/app/work_packages/directives/inplace_editor/inplace-editor-edit-pane-directive.js
  2. 9
      frontend/app/work_packages/services/editable-fields-state.js
  3. 26
      frontend/tests/unit/tests/work_packages/services/editable-fields-state-test.js

@ -82,7 +82,7 @@ module.exports = function(
this.submitField = function(notify) {
var submit = $q.defer();
var fieldController = $scope.fieldController;
var pendingFormChanges = getPendingFormChanges();
var pendingFormChanges = EditableFieldsState.getPendingFormChanges();
var detectedViolations = [];
var handleFailure = function(e) {
setFailure(e);
@ -147,7 +147,7 @@ module.exports = function(
this.discardEditing = function() {
$scope.fieldController.isEditing = false;
delete EditableFieldsState.submissionPromises['work_package'];
delete getPendingFormChanges()[$scope.fieldController.field];
delete EditableFieldsState.getPendingFormChanges()[$scope.fieldController.field];
$scope.fieldController.updateWriteValue();
if (
EditableFieldsState.errors &&
@ -158,10 +158,7 @@ module.exports = function(
};
this.isActive = function() {
if (EditableFieldsState.forcedEditState) {
return false;
}
return EditableFieldsState.currentField === $scope.fieldController.field;
return EditableFieldsState.isActiveField($scope.fieldController.field);
};
this.markActive = function() {
@ -173,14 +170,6 @@ module.exports = function(
EditableFieldsState.currentField = $scope.fieldController.field;
};
this.getPendingFormChanges = getPendingFormChanges;
function getPendingFormChanges() {
var form = EditableFieldsState.workPackage.form;
form.pendingChanges = form.pendingChanges || angular.copy(form.embedded.payload.props);
return form.pendingChanges;
}
function afterError() {
$scope.fieldController.state.isBusy = false;
$scope.focusInput();
@ -216,7 +205,7 @@ module.exports = function(
);
if (fieldController.field === 'date' && strategy === 'date') {
form.pendingChanges = scope.editPaneController.getPendingFormChanges();
form.pendingChanges = EditableFieldsState.getPendingFormChanges();
form.pendingChanges['startDate'] =
form.pendingChanges['dueDate'] =
fieldController.writeValue ? fieldController.writeValue['dueDate'] : null;
@ -257,9 +246,7 @@ module.exports = function(
scope.$watch('fieldController.writeValue', function(writeValue) {
if (scope.fieldController.isEditing) {
var pendingChanges = scope
.editPaneController
.getPendingFormChanges();
var pendingChanges = EditableFieldsState.getPendingFormChanges();
pendingChanges[scope.fieldController.field] = writeValue;
}
}, true);

@ -34,6 +34,15 @@ module.exports = function() {
currentField: null,
submissionPromises: {},
forcedEditState: false,
isActiveField: function (field) {
return !(this.forcedEditState || this.editAll.state) && this.currentField === field;
},
getPendingFormChanges: function () {
var form = this.workPackage.form;
return form.pendingChanges = form.pendingChanges || angular.copy(form.embedded.payload.props);
},
editAll: {
focusField: 'subject',

@ -33,11 +33,33 @@ describe('EditableFieldsState service', function () {
beforeEach(inject(function (_EditableFieldsState_) {
EditableFieldsState = _EditableFieldsState_;
EditableFieldsState.workPackage = { links: {} };
eAll = EditableFieldsState.editAll;
}));
describe('is active field method', function () {
var field = 'my_field', other = 'other_field';
beforeEach(function () {
EditableFieldsState.currentField = field;
});
it('checks if the given field is active or not', function () {
expect(EditableFieldsState.isActiveField(field)).to.be.true;
expect(EditableFieldsState.isActiveField(other)).to.be.false;
});
it('returns false if editAll.state or forcedEditState are set', function () {
EditableFieldsState.editAll.state = true;
EditableFieldsState.forcedEditState = true;
expect(EditableFieldsState.isActiveField(field)).to.be.false;
});
});
describe('edit all', function () {
beforeEach(function () {
EditableFieldsState.workPackage = { links: {} };
eAll = EditableFieldsState.editAll;
});
it('toggle state switches its current state', function () {
expect(EditableFieldsState.state === eAll.toggleState()).to.be.false;
});

Loading…
Cancel
Save