/* * This file is part of the TYPO3 CMS project. * * It is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, either version 2 * of the License, or any later version. * * For the full copyright and license information, please read the * LICENSE.txt file that was distributed with this source code. * * The TYPO3 project - inspiring people to share! */ class c{constructor(){typeof HTMLInputElement.prototype.clearable!="function"&&this.registerClearable()}static createCloseButton(t){const l=` `,e=document.createElement("button");return e.type="button",e.tabIndex=-1,e.title=t,e.ariaLabel=t,e.innerHTML=l,e.style.visibility="hidden",e.classList.add("close"),e}registerClearable(){HTMLInputElement.prototype.clearable=async function(t={}){if(this.isClearable)return;if(typeof t!="object")throw new Error("Passed options must be an object, "+typeof t+" given");this.classList.add("form-control-clearable");const l=document.activeElement===this,e=document.createElement("div");e.classList.add("form-control-clearable-wrapper"),this.parentNode.insertBefore(e,this),e.appendChild(this);let i="Clear input";if(this.dataset.clearableLabel)i=this.dataset.clearableLabel;else{const{default:s}=await import("~labels/core.core");i=s.get("labels.inputfield.clearButton.title")}const n=c.createCloseButton(i),a=()=>{n.style.visibility=this.value.length===0?"hidden":"visible"};n.addEventListener("click",s=>{s.preventDefault(),this.value="",typeof t.onClear=="function"&&t.onClear(this),this.dispatchEvent(new Event("change",{bubbles:!0,cancelable:!0})),this.dispatchEvent(new CustomEvent("typo3:internal:clear")),a(),this.focus()}),e.appendChild(n),this.addEventListener("blur",s=>{this.parentNode.contains(s.relatedTarget)&&this.focus()}),this.addEventListener("focus",a),this.addEventListener("keyup",a),a(),this.isClearable=!0,l&&this.focus()}}}var r=new c;export{r as default};