Implement multi storage service

For saving multiple inplace forms at once in
a certain order.
pull/3753/head
Alex Dik 9 years ago
parent f1c9af9d2e
commit 1fa4b39d12
  1. 41
      frontend/app/components/inplace-edit/directives/edit-pane/edit-pane.directive.js
  2. 82
      frontend/app/components/inplace-edit/services/inplace-edit-multi-storage.service.js
  3. 13
      frontend/app/components/inplace-edit/services/inplace-edit-storage.service.js
  4. 35
      frontend/app/components/work-packages/directives/work-package-comment/work-package-comment.directive.js

@ -88,11 +88,12 @@ function inplaceEditorEditPane(EditableFieldsState, FocusHelper, $timeout) {
inplaceEditorEditPane.$inject = ['EditableFieldsState', 'FocusHelper', '$timeout'];
function InplaceEditorEditPaneController($scope, $element, $location, $timeout,
EditableFieldsState, NotificationsService, inplaceEditStorage) {
function InplaceEditorEditPaneController($rootScope, $scope, $element, $location, $timeout,
EditableFieldsState, NotificationsService, inplaceEditStorage, inplaceEditMultiStorage) {
var vm = this;
var field = $scope.field;
var wpStore = inplaceEditMultiStorage.stores.workPackage;
this.submit = function() {
var detectedViolations = [];
@ -113,20 +114,7 @@ function InplaceEditorEditPaneController($scope, $element, $location, $timeout,
EditableFieldsState.errors[field.name] = detectedViolations.join(' ');
}
inplaceEditStorage.saveWorkPackage()
.then(function() {
$location.hash(null);
$timeout(function() {
$element[0].scrollIntoView(false);
});
})
.catch(function (errors) {
$scope.focusInput();
var errorMessages = _.flatten(_.map(errors), true);
NotificationsService.addError(I18n.t('js.label_validation_error'), errorMessages);
});
inplaceEditMultiStorage.save();
};
this.discardEditing = function() {
@ -139,6 +127,7 @@ function InplaceEditorEditPaneController($scope, $element, $location, $timeout,
};
this.markActive = function() {
wpStore.active = true;
EditableFieldsState.currentField = field.name;
};
@ -182,6 +171,22 @@ function InplaceEditorEditPaneController($scope, $element, $location, $timeout,
$scope.$on('workPackageRefreshed', function() {
vm.discardEditing();
});
$rootScope.$on('inplaceEditMultiStorage.save.workPackage', function (event, promise) {
promise.then(function() {
$location.hash(null);
$timeout(function() {
$element[0].scrollIntoView(false);
});
}).catch(function (errors) {
$scope.focusInput();
var errorMessages = _.flatten(_.map(errors), true);
NotificationsService.addError(I18n.t('js.label_validation_error'), errorMessages);
});
});
}
InplaceEditorEditPaneController.$inject = ['$scope', '$element', '$location', '$timeout',
'EditableFieldsState', 'NotificationsService', 'inplaceEditStorage'];
InplaceEditorEditPaneController.$inject = ['$rootScope', '$scope', '$element', '$location',
'$timeout', 'EditableFieldsState', 'NotificationsService', 'inplaceEditStorage',
'inplaceEditMultiStorage'];

@ -0,0 +1,82 @@
// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
// ++
angular
.module('openproject.inplace-edit')
.factory('inplaceEditMultiStorage', inplaceEditMultiStorage);
function inplaceEditMultiStorage($rootScope, $q, inplaceEditStorage, EditableFieldsState) {
return {
save: function () {
var promises = [];
angular.forEach(_.sortBy(this.stores, 'index'), function (store) {
if (store.active) {
promises[store.index] = store.run().then(function () {
store.active = false;
});
$rootScope.$emit('inplaceEditMultiStorage.save.' + store.name, promises[store.index]);
}
});
return $q.all(promises).then(function () {
EditableFieldsState.errors = null;
EditableFieldsState.currentField = null;
EditableFieldsState.isBusy = false;
$rootScope.$broadcast('workPackageRefreshRequired');
}).catch(function () {
EditableFieldsState.isBusy = false;
});
},
stores: {
workPackage: {
name: 'workPackage',
active: false,
index: 0,
run: function () {
return inplaceEditStorage.saveWorkPackage();
}
},
comment: {
name: 'comment',
active: false,
index: 1,
run: function () {
return inplaceEditStorage.addComment(this.value);
},
value: null
}
}
};
}

@ -37,7 +37,6 @@ function inplaceEditStorage($q, $rootScope, EditableFieldsState, WorkPackageServ
saveWorkPackage: function () {
var deferred = $q.defer(),
handleErrors = function (errors) {
EditableFieldsState.isBusy = false;
deferred.reject(errors);
EditableFieldsState.errors = null
};
@ -56,10 +55,7 @@ function inplaceEditStorage($q, $rootScope, EditableFieldsState, WorkPackageServ
$rootScope.$broadcast('workPackageUpdatedInEditor', updatedWorkPackage);
$rootScope.$broadcast('uploadPendingAttachments', updatedWorkPackage);
$rootScope.$broadcast('workPackageRefreshRequired');
EditableFieldsState.errors = null;
EditableFieldsState.currentField = null;
EditableFieldsState.editAll.stop();
deferred.resolve(updatedWorkPackage);
@ -101,14 +97,7 @@ function inplaceEditStorage($q, $rootScope, EditableFieldsState, WorkPackageServ
},
addComment: function (value) {
return ActivityService.createComment(EditableFieldsState.workPackage, value)
.then(function() {
$rootScope.$broadcast('workPackageRefreshRequired');
EditableFieldsState.isBusy = false;
})
.catch(function () {
EditableFieldsState.isBusy = false;
});
return ActivityService.createComment(EditableFieldsState.workPackage, value);
}
};
}

@ -30,11 +30,13 @@ angular
.module('openproject.workPackages.directives')
.directive('workPackageComment', workPackageComment);
function workPackageComment($timeout, $location, EditableFieldsState, FocusHelper, I18n,
inplaceEditStorage, ConfigurationService, AutoCompleteHelper, NotificationsService) {
function workPackageComment($rootScope, $timeout, $location, EditableFieldsState, FocusHelper, I18n,
inplaceEditMultiStorage, ConfigurationService, AutoCompleteHelper, NotificationsService) {
function commentFieldDirectiveController($scope, $element) {
var field = {};
var field = {},
commentStore = inplaceEditMultiStorage.stores.comment;
$scope.field = field;
var ctrl = this;
@ -65,17 +67,7 @@ function workPackageComment($timeout, $location, EditableFieldsState, FocusHelpe
return;
}
inplaceEditStorage.addComment(ctrl.writeValue)
.then(function() {
ctrl.discardEditing();
NotificationsService.addSuccess(I18n.t('js.work_packages.comment_added'));
var nextActivity = ctrl.activities.length + 1;
$location.hash('activity-' + (nextActivity));
})
.catch(function() {
NotificationsService.addError(I18n.t('js.work_packages.comment_send_failed'));
});
inplaceEditMultiStorage.save();
};
ctrl.initialize = function(withText) {
@ -91,6 +83,7 @@ function workPackageComment($timeout, $location, EditableFieldsState, FocusHelpe
}
field.value = ctrl.writeValue;
commentStore.value = field.value;
};
ctrl.initialize();
@ -126,6 +119,7 @@ function workPackageComment($timeout, $location, EditableFieldsState, FocusHelpe
};
ctrl.markActive = function() {
commentStore.active = true;
EditableFieldsState.currentField = ctrl.field;
};
@ -142,6 +136,19 @@ function workPackageComment($timeout, $location, EditableFieldsState, FocusHelpe
$scope.$on('workPackage.comment.quoteThis', function(evt, quote) {
ctrl.startEditing(quote);
});
$rootScope.$on('inplaceEditMultiStorage.save.comment', function (event, promise) {
promise.then(function() {
ctrl.discardEditing();
NotificationsService.addSuccess(I18n.t('js.work_packages.comment_added'));
var nextActivity = ctrl.activities.length + 1;
$location.hash('activity-' + (nextActivity));
}).catch(function() {
NotificationsService.addError(I18n.t('js.work_packages.comment_send_failed'));
});
});
}
return {

Loading…
Cancel
Save