UNPKG

12.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common'), require('@angular/core'), require('ngx-window-token'), require('rxjs')) :
3 typeof define === 'function' && define.amd ? define('ngx-clipboard', ['exports', '@angular/common', '@angular/core', 'ngx-window-token', 'rxjs'], factory) :
4 (global = global || self, factory(global['ngx-clipboard'] = {}, global.ng.common, global.ng.core, global.i2, global.rxjs));
5}(this, (function (exports, i1, i0, i2, rxjs) { 'use strict';
6
7 /**
8 * The following code is heavily copied from https://github.com/zenorocha/clipboard.js
9 */
10 var ClipboardService = /** @class */ (function () {
11 function ClipboardService(document, window) {
12 this.document = document;
13 this.window = window;
14 this.copySubject = new rxjs.Subject();
15 this.copyResponse$ = this.copySubject.asObservable();
16 this.config = {};
17 }
18 ClipboardService.prototype.configure = function (config) {
19 this.config = config;
20 };
21 ClipboardService.prototype.copy = function (content) {
22 if (!this.isSupported || !content) {
23 return this.pushCopyResponse({ isSuccess: false, content: content });
24 }
25 var copyResult = this.copyFromContent(content);
26 if (copyResult) {
27 return this.pushCopyResponse({ content: content, isSuccess: copyResult });
28 }
29 return this.pushCopyResponse({ isSuccess: false, content: content });
30 };
31 Object.defineProperty(ClipboardService.prototype, "isSupported", {
32 get: function () {
33 return !!this.document.queryCommandSupported && !!this.document.queryCommandSupported('copy') && !!this.window;
34 },
35 enumerable: false,
36 configurable: true
37 });
38 ClipboardService.prototype.isTargetValid = function (element) {
39 if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {
40 if (element.hasAttribute('disabled')) {
41 throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
42 }
43 return true;
44 }
45 throw new Error('Target should be input or textarea');
46 };
47 /**
48 * Attempts to copy from an input `targetElm`
49 */
50 ClipboardService.prototype.copyFromInputElement = function (targetElm, isFocus) {
51 if (isFocus === void 0) { isFocus = true; }
52 try {
53 this.selectTarget(targetElm);
54 var re = this.copyText();
55 this.clearSelection(isFocus ? targetElm : undefined, this.window);
56 return re && this.isCopySuccessInIE11();
57 }
58 catch (error) {
59 return false;
60 }
61 };
62 /**
63 * This is a hack for IE11 to return `true` even if copy fails.
64 */
65 ClipboardService.prototype.isCopySuccessInIE11 = function () {
66 var clipboardData = this.window['clipboardData'];
67 if (clipboardData && clipboardData.getData) {
68 if (!clipboardData.getData('Text')) {
69 return false;
70 }
71 }
72 return true;
73 };
74 /**
75 * Creates a fake textarea element, sets its value from `text` property,
76 * and makes a selection on it.
77 */
78 ClipboardService.prototype.copyFromContent = function (content, container) {
79 if (container === void 0) { container = this.document.body; }
80 // check if the temp textarea still belongs to the current container.
81 // In case we have multiple places using ngx-clipboard, one is in a modal using container but the other one is not.
82 if (this.tempTextArea && !container.contains(this.tempTextArea)) {
83 this.destroy(this.tempTextArea.parentElement || undefined);
84 }
85 if (!this.tempTextArea) {
86 this.tempTextArea = this.createTempTextArea(this.document, this.window);
87 try {
88 container.appendChild(this.tempTextArea);
89 }
90 catch (error) {
91 throw new Error('Container should be a Dom element');
92 }
93 }
94 this.tempTextArea.value = content;
95 var toReturn = this.copyFromInputElement(this.tempTextArea, false);
96 if (this.config.cleanUpAfterCopy) {
97 this.destroy(this.tempTextArea.parentElement || undefined);
98 }
99 return toReturn;
100 };
101 /**
102 * Remove temporary textarea if any exists.
103 */
104 ClipboardService.prototype.destroy = function (container) {
105 if (container === void 0) { container = this.document.body; }
106 if (this.tempTextArea) {
107 container.removeChild(this.tempTextArea);
108 // removeChild doesn't remove the reference from memory
109 this.tempTextArea = undefined;
110 }
111 };
112 /**
113 * Select the target html input element.
114 */
115 ClipboardService.prototype.selectTarget = function (inputElement) {
116 inputElement.select();
117 inputElement.setSelectionRange(0, inputElement.value.length);
118 return inputElement.value.length;
119 };
120 ClipboardService.prototype.copyText = function () {
121 return this.document.execCommand('copy');
122 };
123 /**
124 * Moves focus away from `target` and back to the trigger, removes current selection.
125 */
126 ClipboardService.prototype.clearSelection = function (inputElement, window) {
127 var _a;
128 inputElement && inputElement.focus();
129 (_a = window.getSelection()) === null || _a === void 0 ? void 0 : _a.removeAllRanges();
130 };
131 /**
132 * Creates a fake textarea for copy command.
133 */
134 ClipboardService.prototype.createTempTextArea = function (doc, window) {
135 var isRTL = doc.documentElement.getAttribute('dir') === 'rtl';
136 var ta;
137 ta = doc.createElement('textarea');
138 // Prevent zooming on iOS
139 ta.style.fontSize = '12pt';
140 // Reset box model
141 ta.style.border = '0';
142 ta.style.padding = '0';
143 ta.style.margin = '0';
144 // Move element out of screen horizontally
145 ta.style.position = 'absolute';
146 ta.style[isRTL ? 'right' : 'left'] = '-9999px';
147 // Move element to the same position vertically
148 var yPosition = window.pageYOffset || doc.documentElement.scrollTop;
149 ta.style.top = yPosition + 'px';
150 ta.setAttribute('readonly', '');
151 return ta;
152 };
153 /**
154 * Pushes copy operation response to copySubject, to provide global access
155 * to the response.
156 */
157 ClipboardService.prototype.pushCopyResponse = function (response) {
158 this.copySubject.next(response);
159 };
160 /**
161 * @deprecated use pushCopyResponse instead.
162 */
163 ClipboardService.prototype.pushCopyReponse = function (response) {
164 this.pushCopyResponse(response);
165 };
166 return ClipboardService;
167 }());
168 ClipboardService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ClipboardService_Factory() { return new ClipboardService(i0.ɵɵinject(i1.DOCUMENT), i0.ɵɵinject(i2.WINDOW, 8)); }, token: ClipboardService, providedIn: "root" });
169 ClipboardService.decorators = [
170 { type: i0.Injectable, args: [{ providedIn: 'root' },] }
171 ];
172 ClipboardService.ctorParameters = function () { return [
173 { type: undefined, decorators: [{ type: i0.Inject, args: [i1.DOCUMENT,] }] },
174 { type: undefined, decorators: [{ type: i0.Optional }, { type: i0.Inject, args: [i2.WINDOW,] }] }
175 ]; };
176
177 var ClipboardDirective = /** @class */ (function () {
178 function ClipboardDirective(clipboardSrv) {
179 this.clipboardSrv = clipboardSrv;
180 this.cbOnSuccess = new i0.EventEmitter();
181 this.cbOnError = new i0.EventEmitter();
182 }
183 // tslint:disable-next-line:no-empty
184 ClipboardDirective.prototype.ngOnInit = function () { };
185 ClipboardDirective.prototype.ngOnDestroy = function () {
186 this.clipboardSrv.destroy(this.container);
187 };
188 ClipboardDirective.prototype.onClick = function (event) {
189 if (!this.clipboardSrv.isSupported) {
190 this.handleResult(false, undefined, event);
191 }
192 else if (this.targetElm && this.clipboardSrv.isTargetValid(this.targetElm)) {
193 this.handleResult(this.clipboardSrv.copyFromInputElement(this.targetElm), this.targetElm.value, event);
194 }
195 else if (this.cbContent) {
196 this.handleResult(this.clipboardSrv.copyFromContent(this.cbContent, this.container), this.cbContent, event);
197 }
198 };
199 /**
200 * Fires an event based on the copy operation result.
201 * @param succeeded
202 */
203 ClipboardDirective.prototype.handleResult = function (succeeded, copiedContent, event) {
204 var response = {
205 isSuccess: succeeded,
206 event: event
207 };
208 if (succeeded) {
209 response = Object.assign(response, {
210 content: copiedContent,
211 successMessage: this.cbSuccessMsg
212 });
213 this.cbOnSuccess.emit(response);
214 }
215 else {
216 this.cbOnError.emit(response);
217 }
218 this.clipboardSrv.pushCopyResponse(response);
219 };
220 return ClipboardDirective;
221 }());
222 ClipboardDirective.decorators = [
223 { type: i0.Directive, args: [{
224 selector: '[ngxClipboard]'
225 },] }
226 ];
227 ClipboardDirective.ctorParameters = function () { return [
228 { type: ClipboardService }
229 ]; };
230 ClipboardDirective.propDecorators = {
231 targetElm: [{ type: i0.Input, args: ['ngxClipboard',] }],
232 container: [{ type: i0.Input }],
233 cbContent: [{ type: i0.Input }],
234 cbSuccessMsg: [{ type: i0.Input }],
235 cbOnSuccess: [{ type: i0.Output }],
236 cbOnError: [{ type: i0.Output }],
237 onClick: [{ type: i0.HostListener, args: ['click', ['$event.target'],] }]
238 };
239
240 var ClipboardIfSupportedDirective = /** @class */ (function () {
241 function ClipboardIfSupportedDirective(_clipboardService, _viewContainerRef, _templateRef) {
242 this._clipboardService = _clipboardService;
243 this._viewContainerRef = _viewContainerRef;
244 this._templateRef = _templateRef;
245 }
246 ClipboardIfSupportedDirective.prototype.ngOnInit = function () {
247 if (this._clipboardService.isSupported) {
248 this._viewContainerRef.createEmbeddedView(this._templateRef);
249 }
250 };
251 return ClipboardIfSupportedDirective;
252 }());
253 ClipboardIfSupportedDirective.decorators = [
254 { type: i0.Directive, args: [{
255 selector: '[ngxClipboardIfSupported]'
256 },] }
257 ];
258 ClipboardIfSupportedDirective.ctorParameters = function () { return [
259 { type: ClipboardService },
260 { type: i0.ViewContainerRef },
261 { type: i0.TemplateRef }
262 ]; };
263
264 var ClipboardModule = /** @class */ (function () {
265 function ClipboardModule() {
266 }
267 return ClipboardModule;
268 }());
269 ClipboardModule.decorators = [
270 { type: i0.NgModule, args: [{
271 imports: [i1.CommonModule],
272 declarations: [ClipboardDirective, ClipboardIfSupportedDirective],
273 exports: [ClipboardDirective, ClipboardIfSupportedDirective]
274 },] }
275 ];
276
277 /*
278 * Public API Surface of ngx-clipboard
279 */
280
281 /**
282 * Generated bundle index. Do not edit.
283 */
284
285 exports.ClipboardDirective = ClipboardDirective;
286 exports.ClipboardIfSupportedDirective = ClipboardIfSupportedDirective;
287 exports.ClipboardModule = ClipboardModule;
288 exports.ClipboardService = ClipboardService;
289
290 Object.defineProperty(exports, '__esModule', { value: true });
291
292})));
293//# sourceMappingURL=ngx-clipboard.umd.js.map