{"version":3,"file":"pfe-clipboard.min.js","sources":["../_temp/pfe-clipboard.js"],"sourcesContent":["/*!\n * PatternFly Elements: PfeClipboard 1.12.3\n * @license\n * Copyright 2021 Red Hat, Inc.\n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n * \n*/\n\nimport PFElement from \"../../pfelement/dist/pfelement.js\";\n\nclass PfeClipboard extends PFElement {\n\n // Injected at build-time\n static get version() {\n return \"1.12.3\";\n }\n\n // Injected at build-time\n get html() {\n return `\n\n\n${!this.noIcon ? `\n
\n \n \n \n \n
\n` : \"\"}\n
\n Copy\n
\n
\n Copied\n
`;\n }\n\n static get tag() {\n return \"pfe-clipboard\";\n }\n\n static get meta() {\n return {\n title: \"Clipboard\",\n description: \"Copy current URL to clipboard.\",\n };\n }\n\n get templateUrl() {\n return \"pfe-clipboard.html\";\n }\n\n get styleUrl() {\n return \"pfe-clipboard.scss\";\n }\n\n static get events() {\n return {\n copied: `${this.tag}:copied`,\n connected: `${this.tag}:connected`,\n };\n }\n\n // Declare the type of this component\n static get PfeType() {\n return PFElement.PfeTypes.Content;\n }\n\n static get properties() {\n return {\n noIcon: {\n title: \"No icon\",\n type: Boolean,\n observer: \"_noIconChanged\",\n },\n copiedDuration: {\n title: \"Success message duration (in seconds)\",\n type: Number,\n default: 3,\n },\n role: {\n type: String,\n default: \"button\",\n },\n tabindex: {\n type: Number,\n default: 0,\n },\n copyFrom: {\n type: String,\n default: \"url\",\n observer: \"_checkForCopyTarget\",\n },\n };\n }\n\n static get slots() {\n return {\n icon: {\n title: \"Icon\",\n description: \"This field can accept an SVG, pfe-icon component, or other format for displaying an icon.\",\n slotName: \"icon\",\n slotClass: \"pfe-clipboard__icon\",\n slotId: \"icon\",\n },\n text: {\n title: \"Default text\",\n slotName: \"text\",\n slotClass: \"pfe-clipboard__text\",\n slotId: \"text\",\n },\n textSuccess: {\n title: \"Success message\",\n description: \"Shown when the URL is successfully copied to the clipboard.\",\n slotName: \"text--success\",\n slotClass: \"pfe-clipboard__text--success\",\n slotId: \"text--success\",\n },\n };\n }\n\n get contentToCopy() {\n return this._contentToCopy;\n }\n\n /**\n * Adding Getter/Setter for\n */\n set contentToCopy(contentToCopy) {\n if (contentToCopy) {\n this.removeAttribute(\"disabled\");\n this._contentToCopy = contentToCopy;\n }\n }\n\n constructor() {\n super(PfeClipboard, { type: PfeClipboard.PfeType });\n this._contentToCopy = null;\n\n this._checkForCopyTarget = this._checkForCopyTarget.bind(this);\n this.copyURLToClipboard = this.copyURLToClipboard.bind(this);\n }\n\n connectedCallback() {\n super.connectedCallback();\n\n // Since this element as the role of button we are going to listen\n // for click and as well as 'enter' and 'space' commands to trigger\n // the copy functionality\n this.addEventListener(\"click\", this._clickHandler.bind(this));\n this.addEventListener(\"keydown\", this._keydownHandler.bind(this));\n\n // Make sure the thing we might copy exists\n this._checkForCopyTarget();\n\n // Emit event when this component has connected in case copyContent needs to be set\n this.emitEvent(PfeClipboard.events.connected, {\n detail: {\n component: this,\n },\n });\n\n // This prevents a regression, default text used to be \"Copy URL\".\n // Now that component can copy _anything_ that's not ideal default text\n if (this.copyFrom === \"url\" && !this.hasSlot(\"text\")) {\n this.shadowRoot.getElementById(\"text\").innerText = \"Copy URL\";\n }\n }\n\n disconnectedCallback() {\n // Clean up after ourselves\n this.removeEventListener(\"click\", this._clickHandler.bind(this));\n this.removeEventListener(\"keydown\", this._keydownHandler.bind(this));\n super.disconnectedCallback();\n }\n\n _noIconChanged(previousValue) {\n // dirty check to see if we should rerender the template\n if (this._rendered && this.noIcon !== previousValue) {\n this.render();\n }\n }\n\n /**\n * Checks to make sure the thing we may copy exists\n */\n _checkForCopyTarget() {\n if (this.copyFrom === \"property\") {\n if (!this._contentToCopy) {\n this.setAttribute(\"disabled\", \"\");\n } else if (this.hasAttribute(\"disabled\")) {\n this.removeAttribute(\"disabled\");\n }\n }\n // If target is set to anything else, we're not doing checks for it\n else if (this.copyFrom.length) {\n this.removeAttribute(\"disabled\");\n }\n }\n\n /**\n * Event handler for any activation of the copy button\n */\n _clickHandler() {\n let text;\n switch (this.copyFrom) {\n // Copy current URL\n case \"url\":\n text = window.location.href;\n break;\n // Copy whatever is in this.contentToCopy\n case \"property\":\n if (this._contentToCopy) {\n text = this._contentToCopy;\n } else {\n this.setAttribute(\"disabled\", \"\");\n this.error(\"Set to copy property, but this.contentToCopy is not set\");\n return;\n }\n break;\n // Assume what's in the target property is a selector and copy the text from the element\n default:\n const targetElement = document.querySelector(this.copyFrom);\n if (targetElement && targetElement.tagName) {\n // What needs to be copied changes for some types of elements\n switch (targetElement.tagName.toLowerCase()) {\n // Copy the value of form fields\n case \"input\":\n text = targetElement.value;\n break;\n // Copy the text of our element\n default:\n text = targetElement.innerText;\n break;\n }\n }\n break;\n }\n\n if (!text || (typeof text === \"string\" && !text.length)) {\n this.error(\"Couldn't find text to copy.\");\n this.setAttribute(\"disabled\", \"\");\n return;\n }\n\n // Execute the copy to clipboard functionality\n this.copyTextToClipboard(text)\n .then((copiedText) => {\n // Emit event that lets others know the user has \"copied\"\n // the url. We are also going to include the url that was\n // copied.\n this.emitEvent(PfeClipboard.events.copied, {\n detail: {\n url: copiedText, // @todo deprecate\n copiedText: copiedText,\n },\n });\n // Toggle the copied state. Use the this._formattedCopiedTimeout function\n // to an appropraite setTimout length.\n this.setAttribute(\"copied\", \"\");\n setTimeout(() => {\n this.removeAttribute(\"copied\");\n }, this._formattedCopiedTimeout());\n })\n .catch((error) => {\n this.warn(error);\n this._checkForCopyTarget();\n });\n }\n\n // Formatted copied timeout value. Use the formattedCopiedTimeout function\n // to get a type safe, millisecond value of the timeout duration.\n _formattedCopiedTimeout() {\n const copiedDuration = Number(this.copiedDuration * 1000);\n if (!(copiedDuration > -1)) {\n this.warn(`copied-duration must be a valid number. Defaulting to 3 seconds.`);\n // default to 3 seconds\n return 3000;\n } else {\n return copiedDuration;\n }\n }\n\n // Listen for keyboard events and map them to their\n // corresponding mouse events.\n _keydownHandler(event) {\n let key = event.key || event.keyCode;\n switch (key) {\n case \"Enter\" || 13:\n this._clickHandler(event);\n break;\n case \" \" || 32:\n // Prevent the browser from scolling when the user hits the space key\n event.stopPropagation();\n event.preventDefault();\n this._clickHandler(event);\n break;\n }\n }\n\n /**\n * Copy arbitrary text to system clipboard\n *\n * If available, it will use the new Navigator API to access the system clipboard\n * https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard\n *\n * If unavailable, it will use the legacy execCommand(\"copy\")\n * https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand\n * @async\n * @param {string} text Text to be copied\n * @return {Promise} url\n */\n copyTextToClipboard(text) {\n if (!text) this.error(\"Copy function called, but no text was given to copy.\");\n return new Promise((resolve, reject) => {\n // If the Clipboard API is available then use that\n if (navigator.clipboard) {\n navigator.clipboard.writeText(text).then(resolve(text));\n }\n // If execCommand(\"copy\") exists then use that method\n else if (document.queryCommandEnabled(\"copy\")) {\n const dummy = document.createElement(\"input\");\n document.body.appendChild(dummy);\n dummy.value = text;\n dummy.select();\n document.execCommand(\"copy\");\n document.body.removeChild(dummy);\n resolve(text);\n } else {\n reject(new Error(\"Current browser does not support copying to the clipboard.\"));\n }\n });\n }\n\n /**\n * Copy url to the user's system clipboard\n *\n * @async\n * @deprecated This will be removed in version 2.0\n * @return {Promise} url\n */\n copyURLToClipboard() {\n const url = window.location.href;\n return this.copyTextToClipboard(url);\n }\n}\n\nPFElement.create(PfeClipboard);\n\nexport default PfeClipboard;\n"],"names":["PfeClipboard","PFElement","version","html","this","noIcon","tag","meta","title","description","templateUrl","styleUrl","events","copied","connected","PfeType","PfeTypes","Content","properties","type","Boolean","observer","copiedDuration","Number","default","role","String","tabindex","copyFrom","slots","icon","slotName","slotClass","slotId","text","textSuccess","contentToCopy","_contentToCopy","removeAttribute","[object Object]","super","_checkForCopyTarget","bind","copyURLToClipboard","connectedCallback","addEventListener","_clickHandler","_keydownHandler","emitEvent","detail","component","hasSlot","shadowRoot","getElementById","innerText","removeEventListener","disconnectedCallback","previousValue","_rendered","render","hasAttribute","setAttribute","length","window","location","href","error","targetElement","document","querySelector","tagName","toLowerCase","value","copyTextToClipboard","then","copiedText","url","setTimeout","_formattedCopiedTimeout","catch","warn","event","key","keyCode","stopPropagation","preventDefault","Promise","resolve","reject","navigator","clipboard","writeText","queryCommandEnabled","dummy","createElement","body","appendChild","select","execCommand","removeChild","Error","create"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;EA2BA,MAAMA,UAAqBC,EAGzBC,qBACE,MAAO,SAITC,WACE,MAAO,gtHAGRC,KAAKC,OAOJ,GAPa,i8DAgBfC,iBACE,MAAO,gBAGTC,kBACE,MAAO,CACLC,MAAO,YACPC,YAAa,kCAIjBC,kBACE,MAAO,qBAGTC,eACE,MAAO,qBAGTC,oBACE,MAAO,CACLC,OAAWT,KAAKE,IAAR,UACRQ,UAAcV,KAAKE,IAAR,cAKfS,qBACE,OAAOd,EAAUe,SAASC,QAG5BC,wBACE,MAAO,CACLb,OAAQ,CACNG,MAAO,UACPW,KAAMC,QACNC,SAAU,kBAEZC,eAA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