UNPKG

@mochabug/adapt-web

Version:

Browser client library for Adapt automation platform

2 lines (1 loc) 2.91 kB
import{AdaptCapWidget}from"./AdaptCapWidget.js";const OBSERVED_ATTRIBUTES=["automation-id","dark-mode","worker-count","hidden-field-name","troubleshooting-url","disable-haptics"],BaseElement=typeof HTMLElement<"u"?HTMLElement:class{};class AdaptCapElement extends BaseElement{constructor(){super();this._widget=null}static get observedAttributes(){return OBSERVED_ATTRIBUTES}get client(){return this._client}set client(v){this._client=v,this._destroyWidget(),this._tryInit()}get automationId(){return this.getAttribute("automation-id")}set automationId(v){v===null?this.removeAttribute("automation-id"):this.setAttribute("automation-id",v)}get darkMode(){return this.hasAttribute("dark-mode")}set darkMode(v){v?this.setAttribute("dark-mode",""):this.removeAttribute("dark-mode")}connectedCallback(){this._tryInit()}disconnectedCallback(){this._destroyWidget()}attributeChangedCallback(name,oldValue,newValue){if(oldValue!==newValue){if(name==="automation-id"){this._destroyWidget(),this._tryInit();return}if(name==="dark-mode"){this._widget?.setDarkMode(this.darkMode);return}(name==="worker-count"||name==="hidden-field-name"||name==="troubleshooting-url"||name==="disable-haptics")&&(this._destroyWidget(),this._tryInit())}}_tryInit(){if(this._widget||!this.isConnected)return;const automationId=this.automationId;if(!automationId){console.error('[adapt] <adapt-cap> is missing the required "automation-id" attribute. The element will not initialize.');return}if(!this._client){console.error('[adapt] <adapt-cap> is missing the required "client" property. Set it via JavaScript: el.client = createConnectClient({ id: "..." })');return}const workerCountAttr=this.getAttribute("worker-count"),hiddenFieldName=this.getAttribute("hidden-field-name"),troubleshootingUrl=this.getAttribute("troubleshooting-url");this._widget=new AdaptCapWidget({container:this,automationId,client:this._client,onSolve:(token,expires)=>{this.onSolveCallback?.(token,expires),this.dispatchEvent(new CustomEvent("adapt-cap-solve",{detail:{token,expires},bubbles:!0}))},onError:error=>{this.onErrorCallback?.(error),this.dispatchEvent(new CustomEvent("adapt-cap-error",{detail:{error},bubbles:!0}))},...workerCountAttr!==null&&{workerCount:Number(workerCountAttr)},...hiddenFieldName&&{hiddenFieldName},...troubleshootingUrl&&{troubleshootingUrl},...this.hasAttribute("disable-haptics")&&{disableHaptics:!0},...this.i18n!==void 0&&{i18n:this.i18n}}),this.darkMode&&this._widget.setDarkMode(!0)}_destroyWidget(){this._widget&&(this._widget.destroy(),this._widget=null)}}if(typeof customElements<"u"&&!customElements.get("adapt-cap")){customElements.define("adapt-cap",AdaptCapElement);const styleId="adapt-element-display";if(!document.getElementById(styleId)){const s=document.createElement("style");s.id=styleId,s.textContent="adapt-automation, adapt-cap { display: block; }",document.head.appendChild(s)}}export{AdaptCapElement};