Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix attachments deletion on rails pages

ulferts09fc6ee7363e
diff --git a/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html b/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html
index d2cb8c04e8f..cfe96ca04ee 100644
--- a/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html
+++ b/frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html
@@ -133,8 +133,7 @@
</div>
<ndc-dynamic [ndcDynamicComponent]="attachmentListComponent()"
- [ndcDynamicInputs]="{ resource: workPackage,
- selfDestroy: true }">
+ [ndcDynamicInputs]="{ resource: workPackage }">
</ndc-dynamic>
<ndc-dynamic [ndcDynamicComponent]="attachmentUploadComponent()"
diff --git a/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.component.ts b/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.component.ts
index 0a6cb0bd2d2..c4c135e568b 100644
--- a/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.component.ts
+++ b/frontend/src/app/modules/attachments/attachment-list/attachment-list-item.component.ts
@@ -41,7 +41,7 @@ export class AttachmentListItemComponent {
@Input() public resource:HalResource;
@Input() public attachment:any;
@Input() public index:any;
- @Input() public selfDestroy?:boolean;
+ @Input() destroyImmediately:boolean = true;
@Output() public removeAttachment = new EventEmitter<void>();
@@ -112,7 +112,7 @@ export class AttachmentListItemComponent {
this.removeAttachment.emit();
- if (!!this.selfDestroy) {
+ if (this.destroyImmediately) {
this
.resource
.removeAttachment(this.attachment);
diff --git a/frontend/src/app/modules/attachments/attachment-list/attachment-list.component.ts b/frontend/src/app/modules/attachments/attachment-list/attachment-list.component.ts
index d1fd9de98a3..31b3e5bcdba 100644
--- a/frontend/src/app/modules/attachments/attachment-list/attachment-list.component.ts
+++ b/frontend/src/app/modules/attachments/attachment-list/attachment-list.component.ts
@@ -42,13 +42,15 @@ import {AngularTrackingHelpers} from "core-components/angular/tracking-functions
})
export class AttachmentListComponent implements OnInit, OnChanges, OnDestroy {
@Input() public resource:HalResource;
- @Input() public selfDestroy:boolean = false;
+ @Input() public destroyImmediately:boolean = true;
trackByHref = AngularTrackingHelpers.trackByHref;
attachments:HalResource[] = [];
+ public initialAttachments:HalResource[];
public $element:JQuery;
+ public $formElement:JQuery;
constructor(protected elementRef:ElementRef,
protected states:States,
@@ -64,6 +66,7 @@ export class AttachmentListComponent implements OnInit, OnChanges, OnDestroy {
this.attachments = this.resource.attachments.elements;
this.setupResourceUpdateListener();
+ this.setupAttachmentDeletionCallback();
}
public setupResourceUpdateListener() {
@@ -81,7 +84,7 @@ export class AttachmentListComponent implements OnInit, OnChanges, OnDestroy {
}
ngOnDestroy():void {
- // Nothing to do
+ this.$formElement.off('submit.attachment-component');
}
ngOnChanges() {
@@ -98,6 +101,33 @@ export class AttachmentListComponent implements OnInit, OnChanges, OnDestroy {
private get attachmentsUpdatable() {
return (this.resource.attachments && this.resource.attachmentsBackend);
}
+
+ public setupAttachmentDeletionCallback() {
+ this.memoizeCurrentAttachments();
+
+ this.$formElement = this.$element.closest('form');
+ this.$formElement.on('submit.attachment-component', () => {
+ this.destroyRemovedAttachments();
+ });
+ }
+
+ private destroyRemovedAttachments() {
+ let missingAttachments = _.differenceBy(this.initialAttachments,
+ this.attachments,
+ (attachment:HalResource) => attachment.id);
+
+ if (missingAttachments.length) {
+ missingAttachments.forEach((attachment) => {
+ this
+ .resource
+ .removeAttachment(attachment);
+ });
+ }
+ }
+
+ private memoizeCurrentAttachments() {
+ this.initialAttachments = _.clone(this.resource.attachments.elements);
+ }
}
DynamicBootstrapper.register({
diff --git a/frontend/src/app/modules/attachments/attachment-list/attachment-list.html b/frontend/src/app/modules/attachments/attachment-list/attachment-list.html
index ae07c84b778..bc4881ef742 100644
--- a/frontend/src/app/modules/attachments/attachment-list/attachment-list.html
+++ b/frontend/src/app/modules/attachments/attachment-list/attachment-list.html
@@ -4,7 +4,7 @@
<attachment-list-item [attachment]="attachment"
[resource]="resource"
[index]="i"
- [selfDestroy]="selfDestroy"
+ [destroyImmediately]="destroyImmediately"
(removeAttachment)="removeAttachment(attachment)">
</attachment-list-item>
</ul>
diff --git a/frontend/src/app/modules/attachments/attachments.component.ts b/frontend/src/app/modules/attachments/attachments.component.ts
index e33f41085b5..5128bbffe3f 100644
--- a/frontend/src/app/modules/attachments/attachments.component.ts
+++ b/frontend/src/app/modules/attachments/attachments.component.ts
@@ -42,14 +42,11 @@ import {filter, takeUntil} from 'rxjs/operators';
})
export class AttachmentsComponent implements OnInit, OnDestroy {
@Input('resource') public resource:HalResource;
- @Input() public selfDestroy:boolean = false;
public $element:JQuery;
public allowUploading:boolean;
public destroyImmediately:boolean;
public text:any;
- public $formElement:JQuery;
- public initialAttachments:HalResource[];
constructor(protected elementRef:ElementRef,
protected I18n:I18nService,
@@ -78,17 +75,11 @@ export class AttachmentsComponent implements OnInit, OnDestroy {
this.destroyImmediately = true;
}
- this.setupAttachmentDeletionCallback();
this.setupResourceUpdateListener();
}
- public setupAttachmentDeletionCallback() {
- this.memoizeCurrentAttachments();
-
- this.$formElement = this.$element.closest('form');
- this.$formElement.on('submit.attachment-component', () => {
- this.destroyRemovedAttachments();
- });
+ ngOnDestroy():void {
+ // nothing to do
}
public setupResourceUpdateListener() {
@@ -99,45 +90,14 @@ export class AttachmentsComponent implements OnInit, OnDestroy {
)
.subscribe((newResource:HalResource) => {
this.resource = newResource || this.resource;
-
- if (this.destroyImmediately) {
- this.destroyRemovedAttachments();
- this.memoizeCurrentAttachments();
- }
});
}
- ngOnDestroy() {
- this.$formElement.off('submit.attachment-component');
- }
-
// Only show attachment list when allow uploading is set
// or when at least one attachment exists
public showAttachments() {
return this.allowUploading || _.get(this.resource, 'attachments.count', 0) > 0;
}
-
- private destroyRemovedAttachments() {
- if (this.selfDestroy) {
- return;
- }
-
- let missingAttachments = _.differenceBy(this.initialAttachments,
- this.resource.attachments.elements,
- (attachment:HalResource) => attachment.id);
-
- if (missingAttachments.length) {
- missingAttachments.forEach((attachment) => {
- this
- .resource
- .removeAttachment(attachment);
- });
- }
- }
-
- private memoizeCurrentAttachments() {
- this.initialAttachments = _.clone(this.resource.attachments.elements);
- }
}
DynamicBootstrapper.register({ selector: 'attachments', cls: AttachmentsComponent, embeddable: true });
diff --git a/frontend/src/app/modules/attachments/attachments.html b/frontend/src/app/modules/attachments/attachments.html
index 648c7d8968d..c521d159b36 100644
--- a/frontend/src/app/modules/attachments/attachments.html
+++ b/frontend/src/app/modules/attachments/attachments.html
@@ -5,7 +5,7 @@
<div id="attachments_fields">
<attachment-list *ngIf="resource.attachments"
[resource]="resource"
- [selfDestroy]="selfDestroy">
+ [destroyImmediately]="destroyImmediately">
</attachment-list>
<attachments-upload [resource]="resource"
class="hide-when-print"
diff --git a/frontend/src/app/modules/grids/widgets/custom-text/custom-text.component.html b/frontend/src/app/modules/grids/widgets/custom-text/custom-text.component.html
index c012c00fefc..d070c9e422e 100644
--- a/frontend/src/app/modules/grids/widgets/custom-text/custom-text.component.html
+++ b/frontend/src/app/modules/grids/widgets/custom-text/custom-text.component.html
@@ -29,7 +29,6 @@
</edit-form-portal>
<attachments *ngIf="active"
[resource]="resource.grid"
- [selfDestroy]="true"
data-allow-uploading="true">
</attachments>
diff --git a/frontend/src/app/modules/hal/resources/mixins/attachable-mixin.ts b/frontend/src/app/modules/hal/resources/mixins/attachable-mixin.ts
index b65b13c8fc7..7adbeb2c425 100644
--- a/frontend/src/app/modules/hal/resources/mixins/attachable-mixin.ts
+++ b/frontend/src/app/modules/hal/resources/mixins/attachable-mixin.ts
@@ -110,7 +110,7 @@ export function Attachable<TBase extends Constructor<HalResource>>(Base:TBase) {
}
/**
- * Get updated attachments and activities from the server and push the state
+ * Get updated attachments from the server and push the state
*
* Return a promise that returns the attachments. Reject, if the work package has
* no attachments.