@dllcn/ngx-clipboard
Version:
444 lines (435 loc) • 14.6 kB
JavaScript
import { WINDOW } from 'ngx-window-token';
import { Subject } from 'rxjs';
import { DOCUMENT, CommonModule } from '@angular/common';
import { Inject, Injectable, Optional, Directive, TemplateRef, ViewContainerRef, EventEmitter, HostListener, Input, Output, NgModule, defineInjectable, inject } from '@angular/core';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
// The following code is heavily copy from https://github.com/zenorocha/clipboard.js
var ClipboardService = /** @class */ (function () {
function ClipboardService(document, window) {
this.document = document;
this.window = window;
this.copySubject = new Subject();
this.copyResponse$ = this.copySubject.asObservable();
}
Object.defineProperty(ClipboardService.prototype, "isSupported", {
get: /**
* @return {?}
*/
function () {
return !!this.document.queryCommandSupported && !!this.document.queryCommandSupported('copy') && !!this.window;
},
enumerable: true,
configurable: true
});
/**
* @param {?} element
* @return {?}
*/
ClipboardService.prototype.isTargetValid = /**
* @param {?} element
* @return {?}
*/
function (element) {
if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {
if (element.hasAttribute('disabled')) {
throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
}
return true;
}
throw new Error('Target should be input or textarea');
};
/**
* copyFromInputElement
*/
/**
* copyFromInputElement
* @param {?} targetElm
* @return {?}
*/
ClipboardService.prototype.copyFromInputElement = /**
* copyFromInputElement
* @param {?} targetElm
* @return {?}
*/
function (targetElm) {
try {
this.selectTarget(targetElm);
/** @type {?} */
var re = this.copyText();
this.clearSelection(targetElm, this.window);
return re && this.isCopySuccessInIE11();
}
catch (error) {
return false;
}
};
// this is for IE11 return true even if copy fail
// this is for IE11 return true even if copy fail
/**
* @return {?}
*/
ClipboardService.prototype.isCopySuccessInIE11 =
// this is for IE11 return true even if copy fail
/**
* @return {?}
*/
function () {
/** @type {?} */
var clipboardData = this.window['clipboardData'];
if (clipboardData && clipboardData.getData) {
if (!clipboardData.getData('Text')) {
return false;
}
}
return true;
};
/**
* Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
*/
/**
* Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
* @param {?} content
* @param {?=} container
* @return {?}
*/
ClipboardService.prototype.copyFromContent = /**
* Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
* @param {?} content
* @param {?=} container
* @return {?}
*/
function (content, container) {
if (container === void 0) { container = this.window.document.body; }
// check if the temp textarea still belongs to the current container.
// In case we have multiple places using ngx-clipboard, one is in a modal using container but the other one is not.
if (this.tempTextArea && !container.contains(this.tempTextArea)) {
this.destroy(this.tempTextArea.parentElement);
}
if (!this.tempTextArea) {
this.tempTextArea = this.createTempTextArea(this.document, this.window);
try {
container.appendChild(this.tempTextArea);
}
catch (error) {
throw new Error('Container should be a Dom element');
}
}
this.tempTextArea.value = content;
return this.copyFromInputElement(this.tempTextArea);
};
// remove temporary textarea if any
// remove temporary textarea if any
/**
* @param {?=} container
* @return {?}
*/
ClipboardService.prototype.destroy =
// remove temporary textarea if any
/**
* @param {?=} container
* @return {?}
*/
function (container) {
if (container === void 0) { container = this.window.document.body; }
if (this.tempTextArea) {
container.removeChild(this.tempTextArea);
// removeChild doesn't remove the reference from memory
this.tempTextArea = undefined;
}
};
// select the target html input element
// select the target html input element
/**
* @private
* @param {?} inputElement
* @return {?}
*/
ClipboardService.prototype.selectTarget =
// select the target html input element
/**
* @private
* @param {?} inputElement
* @return {?}
*/
function (inputElement) {
inputElement.select();
inputElement.setSelectionRange(0, inputElement.value.length);
return inputElement.value.length;
};
/**
* @private
* @return {?}
*/
ClipboardService.prototype.copyText = /**
* @private
* @return {?}
*/
function () {
return this.document.execCommand('copy');
};
// Moves focus away from `target` and back to the trigger, removes current selection.
// Moves focus away from `target` and back to the trigger, removes current selection.
/**
* @private
* @param {?} inputElement
* @param {?} window
* @return {?}
*/
ClipboardService.prototype.clearSelection =
// Moves focus away from `target` and back to the trigger, removes current selection.
/**
* @private
* @param {?} inputElement
* @param {?} window
* @return {?}
*/
function (inputElement, window) {
// tslint:disable-next-line:no-unused-expression
inputElement && inputElement.focus();
window.getSelection().removeAllRanges();
};
// create a fake textarea for copy command
// create a fake textarea for copy command
/**
* @private
* @param {?} doc
* @param {?} window
* @return {?}
*/
ClipboardService.prototype.createTempTextArea =
// create a fake textarea for copy command
/**
* @private
* @param {?} doc
* @param {?} window
* @return {?}
*/
function (doc, window) {
/** @type {?} */
var isRTL = doc.documentElement.getAttribute('dir') === 'rtl';
/** @type {?} */
var ta;
ta = doc.createElement('textarea');
// Prevent zooming on iOS
ta.style.fontSize = '12pt';
// Reset box model
ta.style.border = '0';
ta.style.padding = '0';
ta.style.margin = '0';
// Move element out of screen horizontally
ta.style.position = 'absolute';
ta.style[isRTL ? 'right' : 'left'] = '-9999px';
// Move element to the same position vertically
/** @type {?} */
var yPosition = window.pageYOffset || doc.documentElement.scrollTop;
ta.style.top = yPosition + 'px';
ta.setAttribute('readonly', '');
return ta;
};
/**
* Pushes copy operation response to copySubject, to provide global access
* to the response.
*/
/**
* Pushes copy operation response to copySubject, to provide global access
* to the response.
* @param {?} response
* @return {?}
*/
ClipboardService.prototype.pushCopyReponse = /**
* Pushes copy operation response to copySubject, to provide global access
* to the response.
* @param {?} response
* @return {?}
*/
function (response) {
this.copySubject.next(response);
};
ClipboardService.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */
ClipboardService.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] },
{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [WINDOW,] }] }
]; };
/** @nocollapse */ ClipboardService.ngInjectableDef = defineInjectable({ factory: function ClipboardService_Factory() { return new ClipboardService(inject(DOCUMENT), inject(WINDOW, 8)); }, token: ClipboardService, providedIn: "root" });
return ClipboardService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var ClipboardDirective = /** @class */ (function () {
function ClipboardDirective(clipboardSrv) {
this.clipboardSrv = clipboardSrv;
this.cbOnSuccess = new EventEmitter();
this.cbOnError = new EventEmitter();
}
// tslint:disable-next-line:no-empty
// tslint:disable-next-line:no-empty
/**
* @return {?}
*/
ClipboardDirective.prototype.ngOnInit =
// tslint:disable-next-line:no-empty
/**
* @return {?}
*/
function () { };
/**
* @return {?}
*/
ClipboardDirective.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.clipboardSrv.destroy(this.container);
};
/**
* @param {?} event
* @return {?}
*/
ClipboardDirective.prototype.onClick = /**
* @param {?} event
* @return {?}
*/
function (event) {
if (!this.clipboardSrv.isSupported) {
this.handleResult(false, undefined, event);
}
else if (this.targetElm && this.clipboardSrv.isTargetValid(this.targetElm)) {
this.handleResult(this.clipboardSrv.copyFromInputElement(this.targetElm), this.targetElm.value, event);
}
else if (this.cbContent) {
this.handleResult(this.clipboardSrv.copyFromContent(this.cbContent, this.container), this.cbContent, event);
}
};
/**
* Fires an event based on the copy operation result.
* @param succeeded
*/
/**
* Fires an event based on the copy operation result.
* @private
* @param {?} succeeded
* @param {?} copiedContent
* @param {?} event
* @return {?}
*/
ClipboardDirective.prototype.handleResult = /**
* Fires an event based on the copy operation result.
* @private
* @param {?} succeeded
* @param {?} copiedContent
* @param {?} event
* @return {?}
*/
function (succeeded, copiedContent, event) {
/** @type {?} */
var response = {
isSuccess: succeeded,
event: event
};
if (succeeded) {
response = Object.assign(response, {
content: copiedContent,
successMessage: this.cbSuccessMsg
});
this.cbOnSuccess.emit(response);
}
else {
this.cbOnError.emit(response);
}
this.clipboardSrv.pushCopyReponse(response);
};
ClipboardDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngxClipboard]'
},] }
];
/** @nocollapse */
ClipboardDirective.ctorParameters = function () { return [
{ type: ClipboardService }
]; };
ClipboardDirective.propDecorators = {
targetElm: [{ type: Input, args: ['ngxClipboard',] }],
container: [{ type: Input }],
cbContent: [{ type: Input }],
cbSuccessMsg: [{ type: Input }],
cbOnSuccess: [{ type: Output }],
cbOnError: [{ type: Output }],
onClick: [{ type: HostListener, args: ['click', ['$event.target'],] }]
};
return ClipboardDirective;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var ClipboardIfSupportedDirective = /** @class */ (function () {
function ClipboardIfSupportedDirective(_clipboardService, _viewContainerRef, _templateRef) {
this._clipboardService = _clipboardService;
this._viewContainerRef = _viewContainerRef;
this._templateRef = _templateRef;
}
/**
* @return {?}
*/
ClipboardIfSupportedDirective.prototype.ngOnInit = /**
* @return {?}
*/
function () {
if (this._clipboardService.isSupported) {
this._viewContainerRef.createEmbeddedView(this._templateRef);
}
};
ClipboardIfSupportedDirective.decorators = [
{ type: Directive, args: [{
selector: '[ngxClipboardIfSupported]'
},] }
];
/** @nocollapse */
ClipboardIfSupportedDirective.ctorParameters = function () { return [
{ type: ClipboardService },
{ type: ViewContainerRef },
{ type: TemplateRef }
]; };
return ClipboardIfSupportedDirective;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var ClipboardModule = /** @class */ (function () {
function ClipboardModule() {
}
ClipboardModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
declarations: [ClipboardDirective, ClipboardIfSupportedDirective],
exports: [ClipboardDirective, ClipboardIfSupportedDirective]
},] }
];
return ClipboardModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { ClipboardService, ClipboardDirective, ClipboardModule, ClipboardIfSupportedDirective };
//# sourceMappingURL=dllcn-ngx-clipboard.js.map