thingsboard/ui-ngx/src/app/shared/components/button/copy-button.component.ts

96 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-06-04 20:49:01 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2021-06-04 20:49:01 +03:00
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
2021-06-07 10:49:04 +03:00
import { ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
2021-06-04 20:49:01 +03:00
import { ClipboardService } from 'ngx-clipboard';
2023-02-17 19:24:01 +02:00
import { TooltipPosition } from '@angular/material/tooltip';
2021-06-04 20:49:01 +03:00
import { TranslateService } from '@ngx-translate/core';
2023-02-21 19:18:04 +02:00
import { ThemePalette } from '@angular/material/core';
import { coerceBoolean } from '@shared/decorators/coercion';
2021-06-04 20:49:01 +03:00
@Component({
selector: 'tb-copy-button',
styleUrls: ['copy-button.component.scss'],
templateUrl: './copy-button.component.html'
})
export class CopyButtonComponent {
private timer;
copied = false;
@Input()
copyText: string;
@Input()
2023-05-03 15:59:31 +03:00
@coerceBoolean()
2021-06-04 20:49:01 +03:00
disabled = false;
2023-12-01 16:07:45 +02:00
// @deprecated need to use icon input
2023-12-01 16:05:43 +02:00
@Input()
mdiIcon: string;
2021-06-04 20:49:01 +03:00
@Input()
icon: string;
@Input()
tooltipText: string;
@Input()
tooltipPosition: TooltipPosition;
@Input()
style: {[key: string]: any} = {};
@Input()
2023-02-21 19:18:04 +02:00
color: ThemePalette;
@Input()
@coerceBoolean()
2023-05-03 15:59:31 +03:00
miniButton = true;
@Output()
2021-06-07 10:49:04 +03:00
successCopied = new EventEmitter<string>();
2021-06-04 20:49:01 +03:00
constructor(private clipboardService: ClipboardService,
private translate: TranslateService,
private cd: ChangeDetectorRef) {
}
copy($event: Event): void {
$event.stopPropagation();
if (this.timer) {
clearTimeout(this.timer);
}
this.clipboardService.copy(this.copyText);
this.successCopied.emit(this.copyText);
2021-06-04 20:49:01 +03:00
this.copied = true;
this.timer = setTimeout(() => {
this.copied = false;
this.cd.detectChanges();
}, 1500);
}
get matTooltipText(): string {
return this.copied ? this.translate.instant('ota-update.copied') : this.tooltipText;
}
get matTooltipPosition(): TooltipPosition {
return this.copied ? 'below' : this.tooltipPosition;
}
2021-06-04 20:49:01 +03:00
}