OpenProject is the leading open source project management software.
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.
openproject/frontend/app/components/api/api-v3/hal-resources/work-package-resource.servi...

321 lines
9.0 KiB

//-- 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.
//++
import {HalResource} from './hal-resource.service';
import {opApiModule} from "../../../../angular-modules";
import {WorkPackageCacheService} from "../../../work-packages/work-package-cache.service";
import {ApiWorkPackagesService} from "../../api-work-packages/api-work-packages.service";
import IQService = angular.IQService;
interface WorkPackageResourceEmbedded {
activities:HalResource;
assignee:HalResource;
attachments:HalResource;
author:HalResource;
availableWatchers:HalResource;
category:HalResource;
children:HalResource[];
parent:HalResource;
priority:HalResource;
project:HalResource;
responsible:HalResource;
schema:HalResource;
status:HalResource;
timeEntries:HalResource[];
type:HalResource;
version:HalResource;
watchers:HalResource[];
}
interface WorkPackageResourceLinks extends WorkPackageResourceEmbedded {
addAttachment(attachment:HalResource):ng.IPromise<any>;
addChild(child:HalResource):ng.IPromise<any>;
addComment(comment:HalResource):ng.IPromise<any>;
addRelation(relation:HalResource):ng.IPromise<any>;
addWatcher(watcher:HalResource):ng.IPromise<any>;
changeParent(newParent:WorkPackageResource):ng.IPromise<any>;
copy():ng.IPromise<WorkPackageResource>;
delete():ng.IPromise<any>;
logTime():ng.IPromise<any>;
move():ng.IPromise<any>;
removeWatcher():ng.IPromise<any>;
self():ng.IPromise<any>;
update():ng.IPromise<any>;
updateImmediately(payload:any):ng.IPromise<any>;
watch():ng.IPromise<any>;
}
var $q:IQService;
var apiWorkPackages:ApiWorkPackagesService;
var wpCacheService:WorkPackageCacheService;
var NotificationsService:any;
export class WorkPackageResource extends HalResource {
public static fromCreateForm(form) {
var wp = new WorkPackageResource(form.payload.$plain(), true);
// Copy resources from form response
wp.schema = form.schema;
wp.form = $q.when(form);
wp.id = 'new-' + Date.now();
// Set update link to form
wp.$links.update = form.$links.self;
return wp;
}
public $embedded:WorkPackageResourceEmbedded;
public $links:WorkPackageResourceLinks;
public id:number|string;
public schema;
public $pristine:{ [attribute:string]:any } = {};
private form;
public get isNew():boolean {
return isNaN(Number(this.id));
}
/**
* Returns true if any field is in edition in this resource.
*/
public get dirty():boolean {
return this.modifiedFields.length > 0;
}
public get modifiedFields():string[] {
var modified = [];
angular.forEach(this.$pristine, (value, key) => {
var args = [this[key], value];
if (this[key] instanceof HalResource) {
args = args.map(arg => arg.$source);
}
if (!_.isEqual(...args)) {
modified.push(key);
}
});
return modified;
}
public get isLeaf():boolean {
var children = this.$links.children;
return !(children && children.length > 0);
}
public get isEditable():boolean {
return !!this.$links.update || this.isNew;
}
public requiredValueFor(fieldName):boolean {
var fieldSchema = this.schema[fieldName];
// The field schema may be undefined if a custom field
// is used as a column, but not available for this type.
if (angular.isUndefined(fieldSchema)) {
return false;
}
return !this[fieldName] && fieldSchema.writable && fieldSchema.required;
}
public allowedValuesFor(field):ng.IPromise<HalResource[]> {
var deferred = $q.defer();
this.getForm().then(form => {
const allowedValues = form.$embedded.schema[field].allowedValues;
if (Array.isArray(allowedValues)) {
deferred.resolve(allowedValues);
}
else {
return allowedValues.$load().then(loadedValues => {
deferred.resolve(loadedValues.elements);
});
}
});
return deferred.promise;
}
public setAllowedValueFor(field, href) {
this.allowedValuesFor(field).then(allowedValues => {
this[field] = _.find(allowedValues, entry => entry.href === href);
});
}
public getForm() {
if (!this.form) {
this.updateForm(this.$source).catch(error => {
NotificationsService.addError(error.data.message);
});
}
return this.form;
}
public updateForm(payload) {
// Always resolve form to the latest form
// This way, we won't have to actively reset it.
// But store the existing form in case of an error.
// Because if we get an error, the object returned is not a form
// and thus lacks the links the implementation depends upon.
var oldForm = this.form;
this.form = this.$links.update(payload);
var deferred = $q.defer();
this.form
.then(form => {
// Override the current schema with
// the changes from API
this.schema = form.$embedded.schema;
deferred.resolve(form);
})
.catch(error => {
this.form = oldForm;
deferred.reject(error);
});
return deferred.promise;
}
public getSchema() {
return this.getForm().then(form => {
const schema = form.$embedded.schema;
angular.forEach(schema, (field, name) => {
if (this[name] && field && field.writable && field.$isHal
&& (Array.isArray(field.allowedValues) && field.allowedValues.length > 0)) {
this[name] = _.where(field.allowedValues, {name: this[name].name})[0];
}
});
return schema;
});
}
public save() {
var deferred = $q.defer();
this.updateForm(this.$source)
.then(form => {
var payload = this.mergeWithForm(form);
this.saveResource(payload)
.then(workPackage => {
angular.extend(this, workPackage);
this.$pristine = {};
deferred.resolve(this);
})
.catch(error => {
deferred.reject(error);
})
.finally(() => {
wpCacheService.updateWorkPackage(this);
});
})
.catch(deferred.reject);
return deferred.promise;
}
public storePristine(attribute:string) {
if (angular.isDefined(this.$pristine[attribute])) {
return;
}
this.$pristine[attribute] = angular.copy(this[attribute]);
}
public restoreFromPristine(attribute:string) {
if (this.$pristine[attribute]) {
this[attribute] = this.$pristine[attribute];
}
}
public isParentOf(otherWorkPackage) {
return otherWorkPackage.parent.$links.self.$link.href === this.$links.self.$link.href;
}
protected saveResource(payload):ng.IPromise<any> {
if (this.isNew) {
return apiWorkPackages.wpApiPath().post(payload);
}
return this.$links.updateImmediately(payload);
}
private mergeWithForm(form) {
var plainPayload = form.payload.$plain();
var schema = form.$embedded.schema;
// Merge embedded properties from form payload
// Do not use properties on this, since they may be incomplete
// e.g., when switching to a type that requires a custom field.
Object.keys(plainPayload).forEach(key => {
if (typeof(schema[key]) === 'object' && schema[key].writable === true) {
plainPayload[key] = this[key];
}
});
// Merged linked properties from form payload
Object.keys(plainPayload._links).forEach(key => {
if (typeof(schema[key]) === 'object' && schema[key].writable === true) {
var value = angular.isUndefined(this[key]) ? null : this[key].href;
plainPayload._links[key] = {href: value};
}
});
return plainPayload;
}
}
function wpResource(_$q_, _apiWorkPackages_, _wpCacheService_, _NotificationsService_) {
$q = _$q_;
apiWorkPackages = _apiWorkPackages_;
wpCacheService = _wpCacheService_;
NotificationsService = _NotificationsService_;
return WorkPackageResource;
}
opApiModule.factory('WorkPackageResource', [
'$q',
'apiWorkPackages',
'wpCacheService',
'NotificationsService',
wpResource
]);