@ -38,20 +38,21 @@ import {
ViewChild ,
ViewEncapsulation ,
} from '@angular/core' ;
import { Observable } from 'rxjs' ;
import { filter , map , tap } from 'rxjs/operators' ;
import { HalResource } from 'core-app/features/hal/resources/hal-resource' ;
import { HalResourceService } from 'core-app/features/hal/services/hal-resource.service' ;
import { I18nService } from 'core-app/core/i18n/i18n.service' ;
import { States } from 'core-app/core/states/states.service' ;
import { filter , map , tap } from 'rxjs/operators' ;
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin' ;
import { populateInputsFromDataset } from 'core-app/shared/components/dataset-inputs' ;
import { UploadFile } from 'core-app/core/file-upload/op-file-upload.service' ;
import { AttachmentsResourceService } from 'core-app/core/state/attachments/attachments.service' ;
import { ToastService } from 'core-app/shared/components/toaster/toast.service' ;
import { TimezoneService } from 'core-app/core/datetime/timezone.service' ;
import isNewResource from 'core-app/features/hal/helpers/is-new-resource' ;
import { IAttachment } from 'core-app/core/state/attachments/attachment.model' ;
import { Observable } from 'rxjs ';
import isNewResource from 'core-app/features/hal/helpers/is-new-resource ';
function containsFiles ( dataTransfer :DataTransfer ) : boolean {
return dataTransfer . types . indexOf ( 'Files' ) >= 0 ;
@ -82,7 +83,7 @@ export class OpAttachmentsComponent extends UntilDestroyedMixin implements OnIni
public draggingOverDropZone = false ;
public dragging = false ;
public dragging = 0 ;
@ViewChild ( 'hiddenFileInput' ) public filePicker :ElementRef < HTMLInputElement > ;
@ -95,14 +96,29 @@ export class OpAttachmentsComponent extends UntilDestroyedMixin implements OnIni
} ;
private get attachmentsSelfLink ( ) : string {
const attachments = this . resource . attachments as unknown & { href :string } ;
const attachments = this . resource . attachments as unknown & { href :string } ;
return attachments . href ;
}
private get collectionKey ( ) : string {
public get collectionKey ( ) : string {
return isNewResource ( this . resource ) ? 'new' : this . attachmentsSelfLink ;
}
private onGlobalDragLeave : ( _event :DragEvent ) = > void = ( _event ) = > {
this . dragging = Math . max ( this . dragging - 1 , 0 ) ;
this . cdRef . detectChanges ( ) ;
} ;
private onGlobalDragEnd : ( _event :DragEvent ) = > void = ( _event ) = > {
this . dragging = 0 ;
this . cdRef . detectChanges ( ) ;
} ;
private onGlobalDragEnter : ( _event :DragEvent ) = > void = ( _event ) = > {
this . dragging += 1 ;
this . cdRef . detectChanges ( ) ;
} ;
constructor (
public elementRef :ElementRef ,
protected readonly I18n :I18nService ,
@ -159,15 +175,17 @@ export class OpAttachmentsComponent extends UntilDestroyedMixin implements OnIni
} ) ,
) ;
document . body . addEventListener ( 'dragover' , this . onGlobalDragOver . bind ( this ) ) ;
document . body . addEventListener ( 'dragleave' , this . onGlobalDragEnd . bind ( this ) ) ;
document . body . addEventListener ( 'drop' , this . onGlobalDragEnd . bind ( this ) ) ;
document . body . addEventListener ( 'dragenter' , this . onGlobalDragEnter ) ;
document . body . addEventListener ( 'dragleave' , this . onGlobalDragLeave ) ;
document . body . addEventListener ( 'dragend' , this . onGlobalDragEnd ) ;
document . body . addEventListener ( 'drop' , this . onGlobalDragEnd ) ;
}
ngOnDestroy ( ) : void {
document . body . removeEventListener ( 'dragover' , this . onGlobalDragOver . bind ( this ) ) ;
document . body . removeEventListener ( 'dragleave' , this . onGlobalDragEnd . bind ( this ) ) ;
document . body . removeEventListener ( 'drop' , this . onGlobalDragEnd . bind ( this ) ) ;
document . body . removeEventListener ( 'dragenter' , this . onGlobalDragEnter ) ;
document . body . removeEventListener ( 'dragleave' , this . onGlobalDragLeave ) ;
document . body . removeEventListener ( 'dragend' , this . onGlobalDragEnd ) ;
document . body . removeEventListener ( 'drop' , this . onGlobalDragEnd ) ;
}
public triggerFileInput ( ) : void {
@ -198,7 +216,7 @@ export class OpAttachmentsComponent extends UntilDestroyedMixin implements OnIni
this . uploadFiles ( files ) ;
this . draggingOverDropZone = false ;
this . dragging = false ;
this . dragging = 0 ;
}
public onDragOver ( event :DragEvent ) : void {
@ -213,18 +231,6 @@ export class OpAttachmentsComponent extends UntilDestroyedMixin implements OnIni
this . draggingOverDropZone = false ;
}
public onGlobalDragEnd ( ) : void {
this . dragging = false ;
this . cdRef . detectChanges ( ) ;
}
public onGlobalDragOver ( ) : void {
this . dragging = true ;
this . cdRef . detectChanges ( ) ;
}
protected uploadFiles ( files :UploadFile [ ] ) : void {
let uploadFiles = files || [ ] ;
const countBefore = files . length ;