kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
363 lines
12 KiB
363 lines
12 KiB
11 years ago
|
//-- copyright
|
||
|
// OpenProject is a project management system.
|
||
10 years ago
|
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||
11 years ago
|
//
|
||
|
// 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.
|
||
|
//++
|
||
10 years ago
|
/* globals URI */
|
||
11 years ago
|
|
||
9 years ago
|
angular
|
||
|
.module('openproject.services')
|
||
|
.factory('WorkPackageService', WorkPackageService);
|
||
|
|
||
|
function WorkPackageService($http, PathHelper, WorkPackagesHelper, HALAPIResource,
|
||
|
DEFAULT_FILTER_PARAMS, DEFAULT_PAGINATION_OPTIONS, $rootScope, $window, $q, $cacheFactory,
|
||
|
AuthorisationService, EditableFieldsState, WorkPackageFieldService, NotificationsService) {
|
||
|
|
||
9 years ago
|
var workPackage,
|
||
|
workPackageCache = $cacheFactory('workPackageCache');
|
||
11 years ago
|
|
||
10 years ago
|
function getPendingChanges(workPackage) {
|
||
|
var data = {
|
||
|
// _links: {}
|
||
|
};
|
||
|
if (workPackage.form) {
|
||
|
_.forEach(workPackage.form.pendingChanges, function(value, field) {
|
||
|
if (WorkPackageFieldService.isSpecified(workPackage, field)) {
|
||
10 years ago
|
if(field === 'date') {
|
||
10 years ago
|
if(WorkPackageFieldService.isMilestone(workPackage)) {
|
||
10 years ago
|
data['startDate'] = data['dueDate'] = value ? value : null;
|
||
10 years ago
|
return;
|
||
10 years ago
|
}
|
||
10 years ago
|
data['startDate'] = value['startDate'];
|
||
|
data['dueDate'] = value['dueDate'];
|
||
|
return;
|
||
|
}
|
||
|
if (WorkPackageFieldService.isSavedAsLink(workPackage, field)) {
|
||
|
data._links = data._links || {};
|
||
10 years ago
|
data._links[field] = value ? value.props : { href: null };
|
||
10 years ago
|
} else {
|
||
|
data[field] = value;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
if (_.isEmpty(data)) {
|
||
|
return null;
|
||
|
} else {
|
||
|
return JSON.stringify(data);
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
var WorkPackageService = {
|
||
10 years ago
|
initializeWorkPackage: function(projectIdentifier, initialData) {
|
||
10 years ago
|
var changes = _.clone(initialData);
|
||
10 years ago
|
var wp = {
|
||
9 years ago
|
isNew: true,
|
||
10 years ago
|
embedded: {},
|
||
10 years ago
|
props: {},
|
||
10 years ago
|
links: {
|
||
|
update: HALAPIResource
|
||
|
.setup(PathHelper
|
||
9 years ago
|
.apiV3WorkPackageFormPath(projectIdentifier)),
|
||
9 years ago
|
updateImmediately: HALAPIResource.setup(
|
||
9 years ago
|
PathHelper.apiv3ProjectWorkPackagesPath(projectIdentifier),
|
||
10 years ago
|
{ method: 'post' }
|
||
|
)
|
||
10 years ago
|
}
|
||
10 years ago
|
};
|
||
|
var options = { ajax: {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
Accept: 'application/hal+json'
|
||
|
},
|
||
10 years ago
|
data: JSON.stringify(changes),
|
||
10 years ago
|
contentType: 'application/json; charset=utf-8'
|
||
|
}};
|
||
|
|
||
10 years ago
|
return wp.links.update.fetch(options)
|
||
10 years ago
|
.then(function(form) {
|
||
10 years ago
|
form.pendingChanges = changes;
|
||
10 years ago
|
wp.form = form;
|
||
|
EditableFieldsState.workPackage = wp;
|
||
|
EditableFieldsState.errors = null;
|
||
|
return wp;
|
||
|
});
|
||
|
},
|
||
|
|
||
11 years ago
|
getWorkPackage: function(id) {
|
||
9 years ago
|
var path = PathHelper.apiV3WorkPackagePath(id),
|
||
|
resource = HALAPIResource.setup(path);
|
||
|
|
||
11 years ago
|
return resource.fetch().then(function (wp) {
|
||
10 years ago
|
return $q.all([
|
||
|
WorkPackageService.loadWorkPackageForm(wp),
|
||
|
wp.links.schema.fetch()
|
||
|
]).then(function(result) {
|
||
|
wp.form = result[0];
|
||
|
wp.schema = result[1];
|
||
|
workPackage = wp;
|
||
|
EditableFieldsState.workPackage = wp;
|
||
10 years ago
|
EditableFieldsState.errors = null;
|
||
10 years ago
|
return wp;
|
||
|
});
|
||
11 years ago
|
});
|
||
|
},
|
||
|
|
||
11 years ago
|
getWorkPackagesByQueryId: function(projectIdentifier, queryId) {
|
||
|
var url = projectIdentifier ? PathHelper.apiProjectWorkPackagesPath(projectIdentifier) : PathHelper.apiWorkPackagesPath();
|
||
11 years ago
|
var params = queryId ? { query_id: queryId } : DEFAULT_FILTER_PARAMS;
|
||
11 years ago
|
return WorkPackageService.doQuery(url, params);
|
||
|
},
|
||
|
|
||
10 years ago
|
getWorkPackages: function(projectIdentifier, query, paginationOptions) {
|
||
11 years ago
|
var url = projectIdentifier ? PathHelper.apiProjectWorkPackagesPath(projectIdentifier) : PathHelper.apiWorkPackagesPath();
|
||
|
var params = {};
|
||
|
|
||
10 years ago
|
if(query) {
|
||
10 years ago
|
angular.extend(params, query.toUpdateParams());
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
if(paginationOptions) {
|
||
|
angular.extend(params, {
|
||
|
page: paginationOptions.page,
|
||
|
per_page: paginationOptions.perPage
|
||
|
});
|
||
|
} else {
|
||
10 years ago
|
angular.extend(params, {
|
||
|
page: DEFAULT_PAGINATION_OPTIONS.page,
|
||
|
per_page: DEFAULT_PAGINATION_OPTIONS.perPage,
|
||
|
});
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
return WorkPackageService.doQuery(url, params);
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
loadWorkPackageColumnsData: function(workPackages, columnNames, group_by) {
|
||
11 years ago
|
var url = PathHelper.apiWorkPackagesColumnDataPath();
|
||
11 years ago
|
|
||
|
var params = {
|
||
|
'ids[]': workPackages.map(function(workPackage){
|
||
|
return workPackage.id;
|
||
|
}),
|
||
11 years ago
|
'column_names[]': columnNames,
|
||
|
'group_by': group_by
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
return WorkPackageService.doQuery(url, params);
|
||
11 years ago
|
},
|
||
|
|
||
|
// Note: Should this be on a project-service?
|
||
11 years ago
|
getWorkPackagesSums: function(projectIdentifier, query, columns){
|
||
11 years ago
|
var columnNames = columns.map(function(column){
|
||
|
return column.name;
|
||
|
});
|
||
|
|
||
11 years ago
|
if (projectIdentifier){
|
||
|
var url = PathHelper.apiProjectWorkPackagesSumsPath(projectIdentifier);
|
||
|
} else {
|
||
|
var url = PathHelper.apiWorkPackagesSumsPath();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
var params = angular.extend(query.toParams(), {
|
||
11 years ago
|
'column_names[]': columnNames
|
||
11 years ago
|
});
|
||
11 years ago
|
|
||
11 years ago
|
return WorkPackageService.doQuery(url, params);
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
augmentWorkPackagesWithColumnsData: function(workPackages, columns, group_by) {
|
||
11 years ago
|
var columnNames = columns.map(function(column) {
|
||
11 years ago
|
return column.name;
|
||
|
});
|
||
11 years ago
|
|
||
11 years ago
|
return WorkPackageService.loadWorkPackageColumnsData(workPackages, columnNames, group_by)
|
||
11 years ago
|
.then(function(data){
|
||
|
var columnsData = data.columns_data;
|
||
11 years ago
|
var columnsMeta = data.columns_meta;
|
||
|
|
||
|
angular.forEach(columns, function(column, i){
|
||
|
column.total_sum = columnsMeta.total_sums[i];
|
||
|
if (columnsMeta.group_sums) column.group_sums = columnsMeta.group_sums[i];
|
||
|
|
||
|
angular.forEach(workPackages, function(workPackage, j) {
|
||
|
WorkPackagesHelper.augmentWorkPackageWithData(workPackage, column.name, !!column.custom_field, columnsData[i][j]);
|
||
11 years ago
|
});
|
||
11 years ago
|
});
|
||
|
|
||
|
return workPackages;
|
||
|
});
|
||
|
},
|
||
|
|
||
10 years ago
|
loadWorkPackageForm: function(workPackage) {
|
||
10 years ago
|
if (this.authorizedFor(workPackage, 'update')) {
|
||
|
var options = { ajax: {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
Accept: 'application/hal+json'
|
||
|
},
|
||
9 years ago
|
data: getPendingChanges(workPackage),
|
||
10 years ago
|
contentType: 'application/json; charset=utf-8'
|
||
|
}, force: true};
|
||
|
|
||
|
return workPackage.links.update.fetch(options).then(function(form) {
|
||
|
workPackage.form = form;
|
||
|
return form;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return $q.when();
|
||
|
},
|
||
|
|
||
|
authorizedFor: function(workPackage, action) {
|
||
|
var modelName = 'work_package' + workPackage.id;
|
||
|
|
||
|
AuthorisationService.initModelAuth(modelName, workPackage.links);
|
||
|
|
||
|
return AuthorisationService.can(modelName, action);
|
||
10 years ago
|
},
|
||
|
|
||
10 years ago
|
updateWithPayload: function(workPackage, payload) {
|
||
|
var options = { ajax: {
|
||
|
method: 'PATCH',
|
||
|
url: workPackage.links.updateImmediately.href,
|
||
|
headers: {
|
||
|
Accept: 'application/hal+json'
|
||
|
},
|
||
|
data: JSON.stringify(payload),
|
||
|
contentType: 'application/json; charset=utf-8'
|
||
|
}, force: true};
|
||
|
return workPackage.links.updateImmediately.fetch(options);
|
||
|
},
|
||
|
|
||
9 years ago
|
updateWorkPackage: function(workPackage) {
|
||
10 years ago
|
var options = { ajax: {
|
||
10 years ago
|
method: workPackage.links.updateImmediately.props.method,
|
||
9 years ago
|
url: workPackage.links.updateImmediately.props.href,
|
||
10 years ago
|
headers: {
|
||
10 years ago
|
Accept: 'application/hal+json'
|
||
10 years ago
|
},
|
||
10 years ago
|
data: getPendingChanges(workPackage),
|
||
10 years ago
|
contentType: 'application/json; charset=utf-8'
|
||
10 years ago
|
}, force: true};
|
||
10 years ago
|
return workPackage.links.updateImmediately.fetch(options);
|
||
10 years ago
|
},
|
||
|
|
||
10 years ago
|
addWorkPackageRelation: function(workPackage, toId, relationType) {
|
||
|
var options = { ajax: {
|
||
10 years ago
|
method: 'POST',
|
||
10 years ago
|
data: JSON.stringify({
|
||
10 years ago
|
to_id: toId,
|
||
|
relation_type: relationType
|
||
10 years ago
|
}),
|
||
10 years ago
|
contentType: 'application/json; charset=utf-8'
|
||
10 years ago
|
} };
|
||
9 years ago
|
return workPackage.links.addRelation.fetch(options).then(function(relation) {
|
||
10 years ago
|
return relation;
|
||
|
});
|
||
|
},
|
||
|
|
||
10 years ago
|
removeWorkPackageRelation: function(relation) {
|
||
10 years ago
|
var options = { ajax: { method: 'DELETE' } };
|
||
10 years ago
|
return relation.links.remove.fetch(options).then(function(response){
|
||
|
return response;
|
||
|
});
|
||
|
},
|
||
|
|
||
11 years ago
|
doQuery: function(url, params) {
|
||
11 years ago
|
return $http({
|
||
|
method: 'GET',
|
||
|
url: url,
|
||
|
params: params,
|
||
11 years ago
|
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
|
||
|
}).then(function(response){
|
||
10 years ago
|
return response.data;
|
||
|
},
|
||
|
function(failedResponse) {
|
||
9 years ago
|
NotificationsService.addError(
|
||
9 years ago
|
I18n.t('js.work_packages.query.errors.unretrievable_query')
|
||
9 years ago
|
);
|
||
10 years ago
|
}
|
||
|
);
|
||
11 years ago
|
},
|
||
|
|
||
10 years ago
|
performBulkDelete: function(ids, defaultHandling) {
|
||
|
if (defaultHandling && !$window.confirm(I18n.t('js.text_work_packages_destroy_confirmation'))) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
var params = {
|
||
10 years ago
|
'ids[]': ids
|
||
11 years ago
|
};
|
||
10 years ago
|
var promise = $http['delete'](PathHelper.workPackagesBulkDeletePath(), { params: params });
|
||
10 years ago
|
|
||
|
if (defaultHandling) {
|
||
10 years ago
|
promise.success(function(data, status) {
|
||
10 years ago
|
// TODO wire up to API and process API response
|
||
9 years ago
|
NotificationsService.addSuccess(
|
||
|
I18n.t('js.work_packages.message_successful_bulk_delete')
|
||
|
);
|
||
10 years ago
|
$rootScope.$emit('workPackagesRefreshRequired');
|
||
10 years ago
|
})
|
||
|
.error(function(data, status) {
|
||
9 years ago
|
// FIXME catch this kind of failover in angular instead of redirecting
|
||
|
// to a rails-based legacy view
|
||
|
window.location = PathHelper.workPackageDeletePath(ids);
|
||
|
|
||
10 years ago
|
// TODO wire up to API and processs API response
|
||
9 years ago
|
// NotificationsService.addError(
|
||
|
// I18n.t('js.work_packages.message_error_during_bulk_delete')
|
||
|
// );
|
||
10 years ago
|
});
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
return promise;
|
||
9 years ago
|
},
|
||
|
|
||
9 years ago
|
toggleWatch: function(workPackage) {
|
||
|
var toggleWatchLink = (workPackage.links.watch === undefined) ?
|
||
|
workPackage.links.unwatch : workPackage.links.watch;
|
||
|
var fetchOptions = { method: toggleWatchLink.props.method };
|
||
|
|
||
|
if(toggleWatchLink.props.payload !== undefined) {
|
||
|
fetchOptions.contentType = 'application/json; charset=utf-8';
|
||
|
fetchOptions.data = JSON.stringify(toggleWatchLink.props.payload);
|
||
|
}
|
||
|
|
||
|
return toggleWatchLink.fetch({ajax: fetchOptions});
|
||
|
},
|
||
|
|
||
9 years ago
|
cache: function() {
|
||
|
return workPackageCache;
|
||
11 years ago
|
}
|
||
|
};
|
||
|
|
||
|
return WorkPackageService;
|
||
9 years ago
|
}
|
||
|
WorkPackageService.$inject = ['$http', 'PathHelper', 'WorkPackagesHelper', 'HALAPIResource',
|
||
|
'DEFAULT_FILTER_PARAMS', 'DEFAULT_PAGINATION_OPTIONS', '$rootScope', '$window', '$q',
|
||
|
'$cacheFactory', 'AuthorisationService', 'EditableFieldsState', 'WorkPackageFieldService',
|
||
|
'NotificationsService'];
|