fix attachments deletion on rails pages

pull/7738/head
ulferts 5 years ago
parent e4a686f389
commit 09fc6ee736
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 3
      frontend/src/app/components/work-packages/wp-single-view/wp-single-view.html
  2. 4
      frontend/src/app/modules/attachments/attachment-list/attachment-list-item.component.ts
  3. 34
      frontend/src/app/modules/attachments/attachment-list/attachment-list.component.ts
  4. 2
      frontend/src/app/modules/attachments/attachment-list/attachment-list.html
  5. 44
      frontend/src/app/modules/attachments/attachments.component.ts
  6. 2
      frontend/src/app/modules/attachments/attachments.html
  7. 1
      frontend/src/app/modules/grids/widgets/custom-text/custom-text.component.html
  8. 2
      frontend/src/app/modules/hal/resources/mixins/attachable-mixin.ts

@ -133,8 +133,7 @@
</div>
<ndc-dynamic [ndcDynamicComponent]="attachmentListComponent()"
[ndcDynamicInputs]="{ resource: workPackage,
selfDestroy: true }">
[ndcDynamicInputs]="{ resource: workPackage }">
</ndc-dynamic>
<ndc-dynamic [ndcDynamicComponent]="attachmentUploadComponent()"

@ -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);

@ -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({

@ -4,7 +4,7 @@
<attachment-list-item [attachment]="attachment"
[resource]="resource"
[index]="i"
[selfDestroy]="selfDestroy"
[destroyImmediately]="destroyImmediately"
(removeAttachment)="removeAttachment(attachment)">
</attachment-list-item>
</ul>

@ -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 });

@ -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"

@ -29,7 +29,6 @@
</edit-form-portal>
<attachments *ngIf="active"
[resource]="resource.grid"
[selfDestroy]="true"
data-allow-uploading="true">
</attachments>

@ -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.

Loading…
Cancel
Save