]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Add Copy2ClipboardButtonDirective
authorVolker Theile <vtheile@suse.com>
Fri, 27 Apr 2018 13:50:01 +0000 (15:50 +0200)
committerVolker Theile <vtheile@suse.com>
Fri, 27 Apr 2018 13:59:57 +0000 (15:59 +0200)
Signed-off-by: Volker Theile <vtheile@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/shared.module.ts

diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.spec.ts
new file mode 100644 (file)
index 0000000..e50fd86
--- /dev/null
@@ -0,0 +1,8 @@
+import { Copy2ClipboardButtonDirective } from './copy2clipboard-button.directive';
+
+describe('Copy2clipboardButtonDirective', () => {
+  it('should create an instance', () => {
+    const directive = new Copy2ClipboardButtonDirective(null, null, null);
+    expect(directive).toBeTruthy();
+  });
+});
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/copy2clipboard-button.directive.ts
new file mode 100644 (file)
index 0000000..7421a3c
--- /dev/null
@@ -0,0 +1,46 @@
+import { Directive, ElementRef, HostListener, Input, OnInit, Renderer2 } from '@angular/core';
+
+import { ToastsManager } from 'ng2-toastr';
+
+@Directive({
+  selector: '[cdCopy2ClipboardButton]'
+})
+export class Copy2ClipboardButtonDirective implements OnInit {
+
+  @Input('cdCopy2ClipboardButton') private cdCopy2ClipboardButton: string;
+
+  constructor(private elementRef: ElementRef,
+              private renderer: Renderer2,
+              private toastr: ToastsManager) {}
+
+  ngOnInit() {
+    const iElement = this.renderer.createElement('i');
+    this.renderer.addClass(iElement, 'icon-prepend');
+    this.renderer.addClass(iElement, 'fa');
+    this.renderer.addClass(iElement, 'fa-clipboard');
+    this.renderer.appendChild(this.elementRef.nativeElement, iElement);
+  }
+
+  private getInputElement() {
+    return document.getElementById(this.cdCopy2ClipboardButton) as HTMLInputElement;
+  }
+
+  @HostListener('click')
+  onClick() {
+    try {
+      // Create the input to hold our text.
+      const tmpInputElement = document.createElement('input');
+      tmpInputElement.value = this.getInputElement().value;
+      document.body.appendChild(tmpInputElement);
+      // Copy text to clipboard.
+      tmpInputElement.select();
+      document.execCommand('copy');
+      // Finally remove the element.
+      document.body.removeChild(tmpInputElement);
+
+      this.toastr.success('Copied text to the clipboard successfully.');
+    } catch (err) {
+      this.toastr.error('Failed to copy text to the clipboard.');
+    }
+  }
+}
index 04354179a1efce28d88b48c6ac1b7a691f981cf7..d1ef1649579716ecf4e0e1c9e9c7b9f460573b51 100644 (file)
@@ -5,6 +5,7 @@ import { ApiModule } from './api/api.module';
 import { ComponentsModule } from './components/components.module';
 import { DataTableModule } from './datatable/datatable.module';
 import { AutofocusDirective } from './directives/autofocus.directive';
+import { Copy2ClipboardButtonDirective } from './directives/copy2clipboard-button.directive';
 import { DimlessBinaryDirective } from './directives/dimless-binary.directive';
 import { PasswordButtonDirective } from './directives/password-button.directive';
 import { PipesModule } from './pipes/pipes.module';
@@ -25,6 +26,7 @@ import { ServicesModule } from './services/services.module';
   declarations: [
     PasswordButtonDirective,
     DimlessBinaryDirective,
+    Copy2ClipboardButtonDirective,
     AutofocusDirective
   ],
   exports: [
@@ -32,6 +34,7 @@ import { ServicesModule } from './services/services.module';
     PipesModule,
     ServicesModule,
     PasswordButtonDirective,
+    Copy2ClipboardButtonDirective,
     DimlessBinaryDirective,
     DataTableModule,
     ApiModule,