UNPKG

16.3 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../../../projects/ngx-clipboard/src/lib/ngx-clipboard.service.ts","../../../projects/ngx-clipboard/src/lib/ngx-clipboard.directive.ts","../../../projects/ngx-clipboard/src/lib/ngx-clipboard-if-supported.directive.ts","../../../projects/ngx-clipboard/src/lib/ngx-clipboard.module.ts"],"names":["ClipboardService","document","window","this","copySubject","Subject","copyResponse$","asObservable","config","prototype","configure","copy","content","isSupported","pushCopyResponse","isSuccess","copyResult","copyFromContent","Object","defineProperty","queryCommandSupported","isTargetValid","element","HTMLInputElement","HTMLTextAreaElement","hasAttribute","Error","copyFromInputElement","targetElm","isFocus","selectTarget","re","copyText","clearSelection","undefined","isCopySuccessInIE11","error","clipboardData","getData","container","body","tempTextArea","contains","destroy","parentElement","createTempTextArea","appendChild","value","toReturn","cleanUpAfterCopy","removeChild","inputElement","select","setSelectionRange","length","execCommand","focus","_a","getSelection","removeAllRanges","doc","ta","isRTL","documentElement","getAttribute","createElement","style","fontSize","border","padding","margin","position","yPosition","pageYOffset","scrollTop","top","setAttribute","response","next","pushCopyReponse","Injectable","args","providedIn","Inject","DOCUMENT","Optional","type","WINDOW","ClipboardDirective","clipboardSrv","cbOnSuccess","EventEmitter","cbOnError","ngOnInit","ngOnDestroy","onClick","event","handleResult","cbContent","succeeded","copiedContent","assign","successMessage","cbSuccessMsg","emit","Directive","selector","Input","Output","HostListener","ClipboardIfSupportedDirective","_clipboardService","_viewContainerRef","_templateRef","createEmbeddedView","ViewContainerRef","TemplateRef","NgModule","imports","CommonModule","declarations","exports"],"mappings":"qbAiBI,SAAAA,EAAqCC,EAAmDC,GAAnDC,KAAAF,SAAAA,EAAmDE,KAAAD,OAAAA,EALhFC,KAAAC,YAAc,IAAIC,EAAAA,QACnBF,KAAAG,cAAgDH,KAAKC,YAAYG,eAEhEJ,KAAAK,OAA0B,UAI3BR,EAAAS,UAAAC,UAAA,SAAUF,GACbL,KAAKK,OAASA,GAGXR,EAAAS,UAA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CAAC,mCAEN0B,EAAAA,yBAGAA,EAAAA,4BAGAA,EAAAA,2BAGAC,EAAAA,0BAGAA,EAAAA,wBAWAC,EAAAA,aAAY5B,KAAA,CAAC,QAAS,CAAC,sCC5BxB,SAAA6B,EACYC,EACAC,EACAC,GAFA9G,KAAA4G,kBAAAA,EACA5G,KAAA6G,kBAAAA,EACA7G,KAAA8G,aAAAA,SAGZH,EAAArG,UAAAoF,SAAA,WACQ1F,KAAK4G,kBAAkBlG,aACvBV,KAAK6G,kBAAkBE,mBAAmB/G,KAAK8G,wCAZ1DR,EAAAA,UAASxB,KAAA,CAAC,CACPyB,SAAU,yEAHL1G,SAFgCmH,EAAAA,wBAAbC,EAAAA,qBCW5B,iCALCC,EAAAA,SAAQpC,KAAA,CAAC,CACNqC,QAAS,CAACC,EAAAA,cACVC,aAAc,CAAChC,EAAoBsB,GACnCW,QAAS,CAACjC,EAAoBsB","sourcesContent":["import { DOCUMENT } from '@angular/common';\r\nimport { Inject, Injectable, Optional } from '@angular/core';\r\nimport { WINDOW } from 'ngx-window-token';\r\nimport { Observable, Subject } from 'rxjs';\r\n\r\nimport { ClipboardParams, IClipboardResponse } from './interface';\r\n\r\n/**\r\n * The following code is heavily copied from https://github.com/zenorocha/clipboard.js\r\n */\r\n@Injectable({ providedIn: 'root' })\r\nexport class ClipboardService {\r\n private copySubject = new Subject<IClipboardResponse>();\r\n public copyResponse$: Observable<IClipboardResponse> = this.copySubject.asObservable();\r\n private tempTextArea: HTMLTextAreaElement | undefined;\r\n private config: ClipboardParams = {};\r\n\r\n constructor(@Inject(DOCUMENT) public document: any, @Optional() @Inject(WINDOW) private window: any) {}\r\n\r\n public configure(config: ClipboardParams) {\r\n this.config = config;\r\n }\r\n\r\n public copy(content: string): void {\r\n if (!this.isSupported || !content) {\r\n return this.pushCopyResponse({ isSuccess: false, content });\r\n }\r\n const copyResult = this.copyFromContent(content);\r\n if (copyResult) {\r\n return this.pushCopyResponse({ content, isSuccess: copyResult });\r\n }\r\n return this.pushCopyResponse({ isSuccess: false, content });\r\n }\r\n\r\n public get isSupported(): boolean {\r\n return !!this.document.queryCommandSupported && !!this.document.queryCommandSupported('copy') && !!this.window;\r\n }\r\n\r\n public isTargetValid(element: HTMLInputElement | HTMLTextAreaElement): boolean {\r\n if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {\r\n if (element.hasAttribute('disabled')) {\r\n throw new Error('Invalid \"target\" attribute. Please use \"readonly\" instead of \"disabled\" attribute');\r\n }\r\n return true;\r\n }\r\n throw new Error('Target should be input or textarea');\r\n }\r\n\r\n /**\r\n * Attempts to copy from an input `targetElm`\r\n */\r\n public copyFromInputElement(targetElm: HTMLInputElement | HTMLTextAreaElement, isFocus = true): boolean {\r\n try {\r\n this.selectTarget(targetElm);\r\n const re = this.copyText();\r\n this.clearSelection(isFocus ? targetElm : undefined, this.window);\r\n return re && this.isCopySuccessInIE11();\r\n } catch (error) {\r\n return false;\r\n }\r\n }\r\n\r\n /**\r\n * This is a hack for IE11 to return `true` even if copy fails.\r\n */\r\n public isCopySuccessInIE11(): boolean {\r\n const clipboardData = this.window['clipboardData'];\r\n if (clipboardData && clipboardData.getData) {\r\n if (!clipboardData.getData('Text')) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n }\r\n\r\n /**\r\n * Creates a fake textarea element, sets its value from `text` property,\r\n * and makes a selection on it.\r\n */\r\n public copyFromContent(content: string, container: HTMLElement = this.document.body): boolean {\r\n // check if the temp textarea still belongs to the current container.\r\n // In case we have multiple places using ngx-clipboard, one is in a modal using container but the other one is not.\r\n if (this.tempTextArea && !container.contains(this.tempTextArea)) {\r\n this.destroy(this.tempTextArea.parentElement || undefined);\r\n }\r\n\r\n if (!this.tempTextArea) {\r\n this.tempTextArea = this.createTempTextArea(this.document, this.window);\r\n try {\r\n container.appendChild(this.tempTextArea);\r\n } catch (error) {\r\n throw new Error('Container should be a Dom element');\r\n }\r\n }\r\n this.tempTextArea.value = content;\r\n\r\n const toReturn = this.copyFromInputElement(this.tempTextArea, false);\r\n if (this.config.cleanUpAfterCopy) {\r\n this.destroy(this.tempTextArea.parentElement || undefined);\r\n }\r\n return toReturn;\r\n }\r\n\r\n /**\r\n * Remove temporary textarea if any exists.\r\n */\r\n public destroy(container: HTMLElement = this.document.body): void {\r\n if (this.tempTextArea) {\r\n container.removeChild(this.tempTextArea);\r\n // removeChild doesn't remove the reference from memory\r\n this.tempTextArea = undefined;\r\n }\r\n }\r\n\r\n /**\r\n * Select the target html input element.\r\n */\r\n private selectTarget(inputElement: HTMLInputElement | HTMLTextAreaElement): number | undefined {\r\n inputElement.select();\r\n inputElement.setSelectionRange(0, inputElement.value.length);\r\n return inputElement.value.length;\r\n }\r\n\r\n private copyText(): boolean {\r\n return this.document.execCommand('copy');\r\n }\r\n\r\n /**\r\n * Moves focus away from `target` and back to the trigger, removes current selection.\r\n */\r\n private clearSelection(inputElement: HTMLInputElement | HTMLTextAreaElement | undefined, window: Window): void {\r\n inputElement && inputElement.focus();\r\n window.getSelection()?.removeAllRanges();\r\n }\r\n\r\n /**\r\n * Creates a fake textarea for copy command.\r\n */\r\n private createTempTextArea(doc: Document, window: Window): HTMLTextAreaElement {\r\n const isRTL = doc.documentElement.getAttribute('dir') === 'rtl';\r\n let ta: HTMLTextAreaElement;\r\n ta = doc.createElement('textarea');\r\n // Prevent zooming on iOS\r\n ta.style.fontSize = '12pt';\r\n // Reset box model\r\n ta.style.border = '0';\r\n ta.style.padding = '0';\r\n ta.style.margin = '0';\r\n // Move element out of screen horizontally\r\n ta.style.position = 'absolute';\r\n ta.style[isRTL ? 'right' : 'left'] = '-9999px';\r\n // Move element to the same position vertically\r\n const yPosition = window.pageYOffset || doc.documentElement.scrollTop;\r\n ta.style.top = yPosition + 'px';\r\n ta.setAttribute('readonly', '');\r\n return ta;\r\n }\r\n\r\n /**\r\n * Pushes copy operation response to copySubject, to provide global access\r\n * to the response.\r\n */\r\n public pushCopyResponse(response: IClipboardResponse): void {\r\n this.copySubject.next(response);\r\n }\r\n\r\n /**\r\n * @deprecated use pushCopyResponse instead.\r\n */\r\n public pushCopyReponse(response: IClipboardResponse): void {\r\n this.pushCopyResponse(response);\r\n }\r\n}\r\n","import { Directive, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output } from '@angular/core';\r\n\r\nimport { IClipboardResponse } from './interface';\r\nimport { ClipboardService } from './ngx-clipboard.service';\r\n\r\n@Directive({\r\n selector: '[ngxClipboard]'\r\n})\r\nexport class ClipboardDirective implements OnInit, OnDestroy {\r\n // https://github.com/maxisam/ngx-clipboard/issues/239#issuecomment-623330624\r\n // tslint:disable-next-line:no-input-rename\r\n @Input('ngxClipboard')\r\n public targetElm: HTMLInputElement | HTMLTextAreaElement | undefined | '';\r\n @Input()\r\n public container: HTMLElement;\r\n\r\n @Input()\r\n public cbContent: string | undefined;\r\n\r\n @Input()\r\n public cbSuccessMsg: string;\r\n\r\n @Output()\r\n public cbOnSuccess: EventEmitter<IClipboardResponse> = new EventEmitter<IClipboardResponse>();\r\n\r\n @Output()\r\n public cbOnError: EventEmitter<any> = new EventEmitter<any>();\r\n constructor(private clipboardSrv: ClipboardService) {}\r\n\r\n // tslint:disable-next-line:no-empty\r\n public ngOnInit() {}\r\n\r\n public ngOnDestroy() {\r\n this.clipboardSrv.destroy(this.container);\r\n }\r\n\r\n @HostListener('click', ['$event.target'])\r\n public onClick(event: Event) {\r\n if (!this.clipboardSrv.isSupported) {\r\n this.handleResult(false, undefined, event);\r\n } else if (this.targetElm && this.clipboardSrv.isTargetValid(this.targetElm)) {\r\n this.handleResult(this.clipboardSrv.copyFromInputElement(this.targetElm), this.targetElm.value, event);\r\n } else if (this.cbContent) {\r\n this.handleResult(this.clipboardSrv.copyFromContent(this.cbContent, this.container), this.cbContent, event);\r\n }\r\n }\r\n\r\n /**\r\n * Fires an event based on the copy operation result.\r\n * @param succeeded\r\n */\r\n private handleResult(succeeded: boolean, copiedContent: string | undefined, event: Event) {\r\n let response: IClipboardResponse = {\r\n isSuccess: succeeded,\r\n event\r\n };\r\n\r\n if (succeeded) {\r\n response = Object.assign(response, {\r\n content: copiedContent,\r\n successMessage: this.cbSuccessMsg\r\n });\r\n this.cbOnSuccess.emit(response);\r\n } else {\r\n this.cbOnError.emit(response);\r\n }\r\n\r\n this.clipboardSrv.pushCopyResponse(response);\r\n }\r\n}\r\n","import { Directive, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';\r\n\r\nimport { ClipboardService } from './ngx-clipboard.service';\r\n\r\n@Directive({\r\n selector: '[ngxClipboardIfSupported]'\r\n})\r\nexport class ClipboardIfSupportedDirective implements OnInit {\r\n constructor(\r\n private _clipboardService: ClipboardService,\r\n private _viewContainerRef: ViewContainerRef,\r\n private _templateRef: TemplateRef<any>\r\n ) {}\r\n\r\n ngOnInit() {\r\n if (this._clipboardService.isSupported) {\r\n this._viewContainerRef.createEmbeddedView(this._templateRef);\r\n }\r\n }\r\n}\r\n","import { CommonModule } from '@angular/common';\r\nimport { NgModule } from '@angular/core';\r\n\r\nimport { ClipboardIfSupportedDirective } from './ngx-clipboard-if-supported.directive';\r\nimport { ClipboardDirective } from './ngx-clipboard.directive';\r\n\r\n@NgModule({\r\n imports: [CommonModule],\r\n declarations: [ClipboardDirective, ClipboardIfSupportedDirective],\r\n exports: [ClipboardDirective, ClipboardIfSupportedDirective]\r\n})\r\nexport class ClipboardModule {}\r\n"]}
\No newline at end of file