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

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-03-21 20:21:21 +00:00
import { Clipboard } from '@angular/cdk/clipboard';
2022-09-03 15:07:43 +00:00
import { Component, EventEmitter, Input, Output } from '@angular/core';
2022-12-25 21:35:04 +00:00
import {
MatFormFieldAppearance,
SubscriptSizing,
} from '@angular/material/form-field';
import { Fail, FT } from 'picsur-shared/dist/types';
import { Logger } from 'src/app/services/logger/logger.service';
import { ErrorService } from 'src/app/util/error-manager/error.service';
2022-02-28 22:18:07 +00:00
@Component({
2022-04-18 12:46:52 +00:00
selector: 'copy-field',
templateUrl: './copy-field.component.html',
styleUrls: ['./copy-field.component.scss'],
2022-02-28 22:18:07 +00:00
})
export class CopyFieldComponent {
private readonly logger = new Logger(CopyFieldComponent.name);
2022-03-28 14:47:07 +00:00
// Two parameters: name, value
2022-03-01 19:41:55 +00:00
@Input() label: string = 'Loading...';
@Input() value: string = 'Loading...';
2022-09-03 15:07:43 +00:00
@Input() showHideButton: boolean = false;
@Input() hidden: boolean = false;
2022-12-25 21:28:53 +00:00
@Input() color: 'primary' | 'accent' | 'warn' = 'primary';
@Input() appearance: MatFormFieldAppearance = 'outline';
@Input() subscriptSizing: SubscriptSizing = 'fixed';
2022-09-03 15:07:43 +00:00
@Output('copy') onCopy = new EventEmitter<string>();
@Output('hide') onHide = new EventEmitter<boolean>();
2022-06-27 15:37:37 +00:00
constructor(
private readonly clipboard: Clipboard,
private readonly errorService: ErrorService,
2022-06-27 15:37:37 +00:00
) {}
2022-02-28 22:18:07 +00:00
public copy() {
2022-03-21 20:21:21 +00:00
if (this.clipboard.copy(this.value)) {
this.errorService.info(`Copied ${this.label}!`);
2022-09-03 15:07:43 +00:00
this.onCopy.emit(this.value);
2022-03-21 20:21:21 +00:00
return;
2022-03-01 20:51:21 +00:00
}
2022-02-28 22:18:07 +00:00
return this.errorService.showFailure(
Fail(FT.Internal, 'Copying to clipboard failed'),
this.logger,
2022-03-21 20:21:21 +00:00
);
2022-02-28 22:18:07 +00:00
}
2022-09-03 15:07:43 +00:00
public toggleHide() {
this.hidden = !this.hidden;
this.onHide.emit(this.hidden);
}
2022-02-28 22:18:07 +00:00
}