]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.ts
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / directives / copy2clipboard-button.directive.ts
CommitLineData
11fdf7f2
TL
1import { Directive, ElementRef, HostListener, Input, OnInit, Renderer2 } from '@angular/core';
2
494da23a 3import { ToastrService } from 'ngx-toastr';
11fdf7f2
TL
4
5@Directive({
6 selector: '[cdCopy2ClipboardButton]'
7})
8export class Copy2ClipboardButtonDirective implements OnInit {
9 @Input()
10 private cdCopy2ClipboardButton: string;
11
12 constructor(
13 private elementRef: ElementRef,
14 private renderer: Renderer2,
494da23a 15 private toastr: ToastrService
11fdf7f2
TL
16 ) {}
17
18 ngOnInit() {
19 const iElement = this.renderer.createElement('i');
20 this.renderer.addClass(iElement, 'icon-prepend');
21 this.renderer.addClass(iElement, 'fa');
22 this.renderer.addClass(iElement, 'fa-clipboard');
23 this.renderer.appendChild(this.elementRef.nativeElement, iElement);
24 }
25
26 private getInputElement() {
27 return document.getElementById(this.cdCopy2ClipboardButton) as HTMLInputElement;
28 }
29
30 @HostListener('click')
31 onClick() {
32 try {
33 // Create the input to hold our text.
34 const tmpInputElement = document.createElement('input');
35 tmpInputElement.value = this.getInputElement().value;
36 document.body.appendChild(tmpInputElement);
37 // Copy text to clipboard.
38 tmpInputElement.select();
39 document.execCommand('copy');
40 // Finally remove the element.
41 document.body.removeChild(tmpInputElement);
42
43 this.toastr.success('Copied text to the clipboard successfully.');
44 } catch (err) {
45 this.toastr.error('Failed to copy text to the clipboard.');
46 }
47 }
48}