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';
|
2023-06-15 11:09:23 +00:00
|
|
|
import { FT, Fail } from 'picsur-shared/dist/types/failable';
|
|
|
|
import { Logger } from '../../services/logger/logger.service';
|
|
|
|
import { ClipboardService } from '../../util/clipboard.service';
|
|
|
|
import { ErrorService } from '../../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 {
|
2022-09-04 17:37:52 +00:00
|
|
|
private readonly logger = new Logger(CopyFieldComponent.name);
|
|
|
|
|
2022-03-28 14:47:07 +00:00
|
|
|
// Two parameters: name, value
|
2023-06-15 11:09:23 +00:00
|
|
|
@Input() label = 'Loading...';
|
|
|
|
@Input() value = 'Loading...';
|
2022-03-01 19:41:55 +00:00
|
|
|
|
2023-06-15 11:09:23 +00:00
|
|
|
@Input() showHideButton = false;
|
|
|
|
@Input() hidden = false;
|
2022-09-03 15:07:43 +00:00
|
|
|
|
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(
|
2023-06-05 22:39:59 +00:00
|
|
|
private readonly clipboard: ClipboardService,
|
2022-09-04 17:37:52 +00:00
|
|
|
private readonly errorService: ErrorService,
|
2022-06-27 15:37:37 +00:00
|
|
|
) {}
|
2022-02-28 22:18:07 +00:00
|
|
|
|
2023-06-05 22:39:59 +00:00
|
|
|
public async copy() {
|
|
|
|
if (await this.clipboard.copy(this.value)) {
|
2022-09-04 17:37:52 +00:00
|
|
|
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
|
|
|
|
2022-09-04 17:37:52 +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
|
|
|
}
|