Files
Picsur/frontend/src/app/components/copy-field/copy-field.component.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-03-21 21:21:21 +01:00
import { Clipboard } from '@angular/cdk/clipboard';
2022-09-03 17:07:43 +02:00
import { Component, EventEmitter, Input, Output } from '@angular/core';
2022-06-05 12:20:16 +02:00
import { SnackBarType } from 'src/app/models/dto/snack-bar-type.dto';
2022-04-18 14:46:52 +02:00
import { UtilService } from 'src/app/util/util-module/util.service';
2022-02-28 23:18:07 +01:00
@Component({
2022-04-18 14:46:52 +02:00
selector: 'copy-field',
templateUrl: './copy-field.component.html',
styleUrls: ['./copy-field.component.scss'],
2022-02-28 23:18:07 +01:00
})
export class CopyFieldComponent {
2022-03-28 16:47:07 +02:00
// Two parameters: name, value
2022-03-01 20:41:55 +01:00
@Input() label: string = 'Loading...';
@Input() value: string = 'Loading...';
2022-09-03 17:07:43 +02:00
@Input() showHideButton: boolean = false;
@Input() hidden: boolean = false;
@Output('copy') onCopy = new EventEmitter<string>();
@Output('hide') onHide = new EventEmitter<boolean>();
2022-06-27 17:37:37 +02:00
constructor(
private readonly utilService: UtilService,
private readonly clipboard: Clipboard,
) {}
2022-02-28 23:18:07 +01:00
public copy() {
2022-03-21 21:21:21 +01:00
if (this.clipboard.copy(this.value)) {
this.utilService.showSnackBar(`Copied ${this.label}!`, SnackBarType.Info);
2022-09-03 17:07:43 +02:00
this.onCopy.emit(this.value);
2022-03-21 21:21:21 +01:00
return;
2022-03-01 21:51:21 +01:00
}
2022-02-28 23:18:07 +01:00
2022-03-21 21:21:21 +01:00
return this.utilService.showSnackBar(
'Copying to clipboard failed',
2022-06-05 12:20:16 +02:00
SnackBarType.Error,
2022-03-21 21:21:21 +01:00
);
2022-02-28 23:18:07 +01:00
}
2022-09-03 17:07:43 +02:00
public toggleHide() {
this.hidden = !this.hidden;
this.onHide.emit(this.hidden);
}
2022-02-28 23:18:07 +01:00
}