From 09fc6ee7363e9c8ae01e810cf36c6730cc22ddb6 Mon Sep 17 00:00:00 2001 From: ulferts Date: Fri, 27 Sep 2019 15:32:44 +0200 Subject: [PATCH] fix attachments deletion on rails pages --- .../wp-single-view/wp-single-view.html | 3 +- .../attachment-list-item.component.ts | 4 +- .../attachment-list.component.ts | 34 +++++++++++++- .../attachment-list/attachment-list.html | 2 +- .../attachments/attachments.component.ts | 44 +------------------ .../app/modules/attachments/attachments.html | 2 +- .../custom-text/custom-text.component.html | 1 - .../hal/resources/mixins/attachable-mixin.ts | 2 +- 8 files changed, 40 insertions(+), 52 deletions(-) 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 d2cb8c04e8..cfe96ca04e 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 @@ + [ndcDynamicInputs]="{ resource: workPackage }"> (); @@ -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 d1fd9de98a..31b3e5bcdb 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 ae07c84b77..bc4881ef74 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 @@ diff --git a/frontend/src/app/modules/attachments/attachments.component.ts b/frontend/src/app/modules/attachments/attachments.component.ts index e33f41085b..5128bbffe3 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 648c7d8968..c521d159b3 100644 --- a/frontend/src/app/modules/attachments/attachments.html +++ b/frontend/src/app/modules/attachments/attachments.html @@ -5,7 +5,7 @@
+ [destroyImmediately]="destroyImmediately"> 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 b65b13c8fc..7adbeb2c42 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>(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.