+import { TestBed } from '@angular/core/testing';
+
+import { I18n } from '@ngx-translate/i18n-polyfill';
+
+import { configureTestBed, i18nProviders } from '../../../testing/unit-test-helper';
import { Copy2ClipboardButtonDirective } from './copy2clipboard-button.directive';
describe('Copy2clipboardButtonDirective', () => {
+ configureTestBed({
+ providers: [i18nProviders]
+ });
+
it('should create an instance', () => {
- const directive = new Copy2ClipboardButtonDirective(null, null, null);
+ const i18n = TestBed.get(I18n);
+ const directive = new Copy2ClipboardButtonDirective(null, null, null, i18n);
expect(directive).toBeTruthy();
});
});
import { Directive, ElementRef, HostListener, Input, OnInit, Renderer2 } from '@angular/core';
+import { I18n } from '@ngx-translate/i18n-polyfill';
import { ToastrService } from 'ngx-toastr';
export class Copy2ClipboardButtonDirective implements OnInit {
@Input()
private cdCopy2ClipboardButton: string;
+ @Input()
+ private formatted = 'no';
constructor(
private elementRef: ElementRef,
private renderer: Renderer2,
- private toastr: ToastrService
+ private toastr: ToastrService,
+ private i18n: I18n
) {}
ngOnInit() {
const iElement = this.renderer.createElement('i');
this.renderer.addClass(iElement, 'fa');
this.renderer.addClass(iElement, 'fa-clipboard');
+ this.renderer.setAttribute(iElement, 'title', this.i18n('Copy to clipboard'));
this.renderer.appendChild(this.elementRef.nativeElement, iElement);
}
@HostListener('click')
onClick() {
try {
+ const tagName = this.formatted === '' ? 'textarea' : 'input';
// Create the input to hold our text.
- const tmpInputElement = document.createElement('input');
+ const tmpInputElement = document.createElement(tagName);
tmpInputElement.value = this.getInputElement().value;
document.body.appendChild(tmpInputElement);
// Copy text to clipboard.