UNPKG

@flows/js-components

Version:

Built-in components for Flows JS SDK

2 lines (1 loc) 14.3 kB
"use strict";var flows_js_components=(()=>{var M=Object.defineProperty;var ge=Object.getOwnPropertyDescriptor;var we=Object.getOwnPropertyNames;var be=Object.prototype.hasOwnProperty;var Ie=(e,t)=>{for(var r in t)M(e,r,{get:t[r],enumerable:!0})},xe=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of we(t))!be.call(e,o)&&o!==r&&M(e,o,{get:()=>t[o],enumerable:!(n=ge(t,o))||n.enumerable});return e};var Se=e=>xe(M({},"__esModule",{value:!0}),e);var lt={};Ie(lt,{render:()=>Ce,updateSlotComponents:()=>it});var N=[],Ce=e=>{N.forEach(t=>{t.cleanup(),t.el&&t.el.remove()}),N=[],e.blocks.forEach(t=>{let r=t.type==="component"?e.components[t.component]:t.type==="tour-component"?e.tourComponents[t.component]:null;if(r){let{cleanup:n,element:o}=r(t.props);N.push({el:o,cleanup:n,blockId:t.id}),o&&document.body.appendChild(o)}})};var Be=Symbol.for("preact-signals");function j(){if(w>1)w--;else{for(var e,t=!1;B!==void 0;){var r=B;for(B=void 0,P++;r!==void 0;){var n=r.o;if(r.o=void 0,r.f&=-3,!(8&r.f)&&D(r))try{r.c()}catch(o){t||(e=o,t=!0)}r=n}}if(P=0,w--,t)throw e}}var a=void 0;var B=void 0,w=0,P=0,L=0;function R(e){if(a!==void 0){var t=e.n;if(t===void 0||t.t!==a)return t={i:0,S:e,p:a.s,n:void 0,t:a,e:void 0,x:void 0,r:t},a.s!==void 0&&(a.s.n=t),a.s=t,e.n=t,32&a.f&&e.S(t),t;if(t.i===-1)return t.i=0,t.n!==void 0&&(t.n.p=t.p,t.p!==void 0&&(t.p.n=t.n),t.p=a.s,t.n=void 0,a.s.n=t,a.s=t),t}}function c(e){this.v=e,this.i=0,this.n=void 0,this.t=void 0}c.prototype.brand=Be;c.prototype.h=function(){return!0};c.prototype.S=function(e){this.t!==e&&e.e===void 0&&(e.x=this.t,this.t!==void 0&&(this.t.e=e),this.t=e)};c.prototype.U=function(e){if(this.t!==void 0){var t=e.e,r=e.x;t!==void 0&&(t.x=r,e.e=void 0),r!==void 0&&(r.e=t,e.x=void 0),e===this.t&&(this.t=r)}};c.prototype.subscribe=function(e){var t=this;return y(function(){var r=t.value,n=a;a=void 0;try{e(r)}finally{a=n}})};c.prototype.valueOf=function(){return this.value};c.prototype.toString=function(){return this.value+""};c.prototype.toJSON=function(){return this.value};c.prototype.peek=function(){var e=a;a=void 0;try{return this.value}finally{a=e}};Object.defineProperty(c.prototype,"value",{get:function(){var e=R(this);return e!==void 0&&(e.i=this.i),this.v},set:function(e){if(e!==this.v){if(P>100)throw new Error("Cycle detected");this.v=e,this.i++,L++,w++;try{for(var t=this.t;t!==void 0;t=t.x)t.t.N()}finally{j()}}}});function E(e){return new c(e)}function D(e){for(var t=e.s;t!==void 0;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function q(e){for(var t=e.s;t!==void 0;t=t.n){var r=t.S.n;if(r!==void 0&&(t.r=r),t.S.n=t,t.i=-1,t.n===void 0){e.s=t;break}}}function J(e){for(var t=e.s,r=void 0;t!==void 0;){var n=t.p;t.i===-1?(t.S.U(t),n!==void 0&&(n.n=t.n),t.n!==void 0&&(t.n.p=n)):r=t,t.S.n=t.r,t.r!==void 0&&(t.r=void 0),t=n}e.s=r}function b(e){c.call(this,void 0),this.x=e,this.s=void 0,this.g=L-1,this.f=4}(b.prototype=new c).h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===L))return!0;if(this.g=L,this.f|=1,this.i>0&&!D(this))return this.f&=-2,!0;var e=a;try{q(this),a=this;var t=this.x();(16&this.f||this.v!==t||this.i===0)&&(this.v=t,this.f&=-17,this.i++)}catch(r){this.v=r,this.f|=16,this.i++}return a=e,J(this),this.f&=-2,!0};b.prototype.S=function(e){if(this.t===void 0){this.f|=36;for(var t=this.s;t!==void 0;t=t.n)t.S.S(t)}c.prototype.S.call(this,e)};b.prototype.U=function(e){if(this.t!==void 0&&(c.prototype.U.call(this,e),this.t===void 0)){this.f&=-33;for(var t=this.s;t!==void 0;t=t.n)t.S.U(t)}};b.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var e=this.t;e!==void 0;e=e.x)e.t.N()}};Object.defineProperty(b.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var e=R(this);if(this.h(),e!==void 0&&(e.i=this.i),16&this.f)throw this.v;return this.v}});function I(e){return new b(e)}function H(e){var t=e.u;if(e.u=void 0,typeof t=="function"){w++;var r=a;a=void 0;try{t()}catch(n){throw e.f&=-2,e.f|=8,U(e),n}finally{a=r,j()}}}function U(e){for(var t=e.s;t!==void 0;t=t.n)t.S.U(t);e.x=void 0,e.s=void 0,H(e)}function Ee(e){if(a!==this)throw new Error("Out-of-order effect");J(this),a=e,this.f&=-2,8&this.f&&U(this),j()}function O(e){this.x=e,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32}O.prototype.c=function(){var e=this.S();try{if(8&this.f||this.x===void 0)return;var t=this.x();typeof t=="function"&&(this.u=t)}finally{e()}};O.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,H(this),q(this),w++;var e=a;return a=this,Ee.bind(this,e)};O.prototype.N=function(){2&this.f||(this.f|=2,this.o=B,B=this)};O.prototype.d=function(){this.f|=8,1&this.f||U(this)};function y(e){var t=new O(e);try{t.c()}catch(r){throw t.d(),r}return t.d.bind(t)}var Oe=Object.defineProperty,Ae=Object.defineProperties,_e=Object.getOwnPropertyDescriptors,G=Object.getOwnPropertySymbols,Te=Object.prototype.hasOwnProperty,Le=Object.prototype.propertyIsEnumerable,V=(e,t,r)=>t in e?Oe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,v=(e,t)=>{for(var r in t||(t={}))Te.call(t,r)&&V(e,r,t[r]);if(G)for(var r of G(t))Le.call(t,r)&&V(e,r,t[r]);return e},k=(e,t)=>Ae(e,_e(t)),X=(e,{body:t,method:r,version:n})=>fetch(e,{method:r,headers:{"Content-Type":"application/json","x-flows-version":n},body:t?JSON.stringify(t):void 0}).then(async o=>{var i;let s=await o.text(),l=s?JSON.parse(s):void 0;if(!o.ok){let d=l;throw new Error((i=d==null?void 0:d.message)!=null?i:o.statusText)}return l}),Me=(e,t)=>({getBlocks:r=>X(`${e}/v2/sdk/blocks`,{method:"POST",body:r,version:t}),sendEvent:r=>X(`${e}/v2/sdk/events`,{method:"POST",body:r,version:t})});function Ne(e){var t;return typeof e=="string"||typeof e=="symbol"?e:Object.is((t=e==null?void 0:e.valueOf)==null?void 0:t.call(e),-0)?"-0":String(e)}function Pe(e){let t=[],r=e.length;if(r===0)return t;let n=0,o="",i="",s=!1;for(e.charCodeAt(0)===46&&(t.push(""),n++);n<r;){let l=e[n];i?l==="\\"&&n+1<r?(n++,o+=e[n]):l===i?i="":o+=l:s?l==='"'||l==="'"?i=l:l==="]"?(s=!1,t.push(o),o=""):o+=l:l==="["?(s=!0,o&&(t.push(o),o="")):l==="."?o&&(t.push(o),o=""):o+=l,n++}return o&&t.push(o),t}function Q(e){return e!==null&&(typeof e=="object"||typeof e=="function")}function je(e,t){return e===t||Number.isNaN(e)&&Number.isNaN(t)}var Ue=/^(?:0|[1-9]\d*)$/;function $e(e,t=Number.MAX_SAFE_INTEGER){switch(typeof e){case"number":return Number.isInteger(e)&&e>=0&&e<t;case"symbol":return!1;case"string":return Ue.test(e)}}function Fe(e){return typeof e=="symbol"||e instanceof Symbol}var Ke=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,We=/^\w*$/;function ze(e,t){return Array.isArray(e)?!1:typeof e=="number"||typeof e=="boolean"||e==null||Fe(e)?!0:typeof e=="string"&&(We.test(e)||!Ke.test(e))||t!=null&&Object.hasOwn(t,e)}var Re=(e,t,r)=>{let n=e[t];(!(Object.hasOwn(e,t)&&je(n,r))||r===void 0&&!(t in e))&&(e[t]=r)};function De(e,t,r,n){if(e==null&&!Q(e))return e;let o=ze(t,e)?[t]:Array.isArray(t)?t:typeof t=="string"?Pe(t):[t],i=e;for(let s=0;s<o.length&&i!=null;s++){let l=Ne(o[s]),d;if(s===o.length-1)d=r(i[l]);else{let f=i[l],u=n(f);d=u!==void 0?u:Q(f)?f:$e(o[s+1])?[]:{}}Re(i,l,d),i=i[l]}return e}function qe(e,t,r){return De(e,t,()=>r,()=>{})}var Je=({block:e,exitNodeCb:t,removeBlock:r,setStateMemory:n})=>{var o,i,s;let l=({properties:u,parentKey:p})=>{var h;let _=v({},u);return Object.entries(u).forEach(([S,T])=>{Array.isArray(T)&&(_[S]=T.map((C,ye)=>typeof C=="object"?l({properties:C,parentKey:[p,S,ye].filter(ke=>ke!==void 0).join(".")}):C))}),delete _.f__exit_nodes,(h=u.f__exit_nodes)==null||h.forEach(S=>{let T=()=>t([p,S].filter(C=>C!==void 0).join("."));_[S]=T}),_},d=l({properties:e.data});for(let u of(o=e.propertyMeta)!=null?o:[])if(u.type==="state-memory"){let p={value:(i=u.value)!=null?i:!1,setValue:h=>{n(u.key,h)},triggers:(s=u.triggers)!=null?s:[]};qe(d,u.key,p)}let f=e.exitNodes.reduce((u,p)=>{let h=()=>(r(e.id),t(p));return u[p]=h,u},{});return v(v({__flows:{id:e.id,key:e.key,workflowId:e.workflowId}},d),f)},He="#22262d",Ge="#fff";var vt=`color:${Ge};background:${He};padding:2px 4px;border-radius:4px`;var ee=(e,t)=>t?Array.isArray(t)?t.some(r=>ee(e,r)):typeof e!="string"?!1:new RegExp(t).test(e):!0,te=(e,t)=>t===void 0?!0:Array.isArray(t)?t.some(r=>te(e,r)):e===t,re=(e,t)=>t===void 0?!0:Array.isArray(t)?t.every(r=>re(e,r)):e!==t,oe=(e,t)=>t?Array.isArray(t)?t.some(r=>oe(e,r)):typeof e!="string"?!1:e.includes(t):!0,ne=(e,t)=>t?Array.isArray(t)?t.every(r=>ne(e,r)):typeof e!="string"?!1:!e.includes(t):!0,F=({operator:e,pathname:t,value:r})=>e==="eq"?te(t,r):e==="ne"?re(t,r):e==="contains"?oe(t,r):e==="notContains"?ne(t,r):e==="regex"?ee(t,r):!0;var ie=(e,t)=>new Proxy(e,{get(r,n,o){return n==="props"&&t(e.id),Reflect.get(r,n,o)}}),Ve=E(),K=E(),m=E([]),W=e=>{m.value=m.value.filter(t=>t.id!==e)},Xe=(e,t)=>{m.value=m.value.map(r=>r.id===e?t(r):r)},x=E([]);y(()=>{let e=m.value.filter(o=>o.type==="tour"),t=x.peek(),r=new Map(t.map(o=>[o.blockId,o])),n=e.map(o=>{var i,s;let l=r.get(o.id),d=(s=(i=l==null?void 0:l.currentBlockIndex)!=null?i:o.currentTourIndex)!=null?s:0;return{blockId:o.id,currentBlockIndex:d}});x.value=n});var Qe="@flows/js",Ye="1.9.1",Ze=`${Qe}@${Ye}`,g=async e=>{let t=Ve.value;if(!t)return;let{environment:r,organizationId:n,userId:o,apiUrl:i}=t;await Me(i,Ze).sendEvent(k(v({},e),{environment:r,organizationId:n,userId:o}))},Y=new Set,le=async e=>{Y.has(e)||(Y.add(e),await g({name:"block-activated",blockId:e}))},se=(e,t)=>{x.value=x.value.map(r=>r.blockId===e?t(r):r)},et=(e,t)=>{if(t===0)return;let r=t-1;se(e.id,n=>k(v({},n),{currentBlockIndex:r})),g({name:"tour-update",blockId:e.id,properties:{currentTourIndex:r}})},$=(e,t)=>{var r,n;if(t===((n=(r=e.tourBlocks)==null?void 0:r.length)!=null?n:1)-1)W(e.id),g({name:"transition",blockId:e.id,propertyKey:"complete"});else{let o=t+1;se(e.id,i=>k(v({},i),{currentBlockIndex:o})),g({name:"tour-update",blockId:e.id,properties:{currentTourIndex:o}})}},tt=e=>{W(e),g({name:"transition",blockId:e,propertyKey:"cancel"})};var A=new Map;y(()=>{let e=K.value,t=m.value,r=x.value,n=new Map(t.map(o=>[o.id,o]));r.forEach(o=>{var i,s,l;let d=n.get(o.blockId);if(!d)return;let f=(i=d.tourBlocks)==null?void 0:i.at(o.currentBlockIndex);if(!f)return;let u=A.get(o.blockId);u&&u.stepId!==f.id&&(clearTimeout(u.timeoutId),A.delete(o.blockId));let p=f.tourWait;if(p&&(p.interaction==="navigation"&&F({pathname:e,operator:(s=p.page)==null?void 0:s.operator,value:(l=p.page)==null?void 0:l.value})&&$(d,o.currentBlockIndex),p.interaction==="delay"&&p.ms!==void 0&&!A.has(o.blockId))){let h=window.setTimeout(()=>{$(d,o.currentBlockIndex),A.delete(o.blockId)},p.ms);A.set(o.blockId,{timeoutId:h,stepId:f.id})}})});var ae=e=>{if(!e.componentType)return[];let t=Je({block:e,removeBlock:W,exitNodeCb:n=>g({name:"transition",blockId:e.id,propertyKey:n}),setStateMemory:async(n,o)=>{Xe(e.id,i=>{var s;return k(v({},i),{propertyMeta:(s=i.propertyMeta)==null?void 0:s.map(l=>l.type==="state-memory"&&l.key===n?k(v({},l),{value:o}):l)})}),await g({name:"set-state-memory",blockId:e.id,propertyKey:n,properties:{value:o}})}}),r={id:e.id,type:"component",component:e.componentType,props:t};return ie(r,le)},ue=(e,t)=>{let r=e.tourBlocks;if(!(r!=null&&r.length))return[];let n=r.at(t);if(!(n!=null&&n.componentType))return[];let o=t===0,i=()=>{et(e,t)},s=()=>{$(e,t)},l=()=>{tt(e.id)},d={id:n.id,tourBlockId:e.id,type:"tour-component",component:n.componentType,props:k(v({__flows:{id:n.id,key:n.key,workflowId:n.workflowId}},n.data),{previous:o?void 0:i,continue:s,cancel:l})};return ie(d,le)},de=I(()=>m.value.filter(e=>F({pathname:K.value,operator:e.page_targeting_operator,value:e.page_targeting_values}))),pe=I(()=>{let e=new Map(m.value.map(t=>[t.id,t]));return x.value.filter(t=>{var r;let n=e.get(t.blockId),o=(r=n==null?void 0:n.tourBlocks)==null?void 0:r.at(t.currentBlockIndex);return F({pathname:K.value,operator:o==null?void 0:o.page_targeting_operator,value:o==null?void 0:o.page_targeting_values})}).flatMap(t=>{let r=e.get(t.blockId);return r?k(v({},t),{block:r}):[]})}),kt=I(()=>{let e=de.value.filter(r=>!r.slottable).flatMap(ae),t=pe.value.filter(r=>{var n;let o=(n=r.block.tourBlocks)==null?void 0:n.at(r.currentBlockIndex);return!(o!=null&&o.slottable)}).flatMap(r=>ue(r.block,r.currentBlockIndex));return[...e,...t]}),rt=I(()=>de.value.filter(e=>e.slottable)),ce=e=>"type"in e,Z=e=>{var t,r,n;if(ce(e))return(t=e.slotIndex)!=null?t:0;let o=(r=e.block.tourBlocks)==null?void 0:r.at(e.currentBlockIndex);return(n=o==null?void 0:o.slotIndex)!=null?n:0},fe=new Map,ot=e=>{let t=I(()=>{let r=rt.value.filter(o=>o.slottable&&o.slotId===e),n=pe.value.filter(o=>{var i;let s=(i=o.block.tourBlocks)==null?void 0:i.at(o.currentBlockIndex);return(s==null?void 0:s.slottable)&&s.slotId===e});return[...r,...n].sort((o,i)=>Z(o)-Z(i)).flatMap(o=>ce(o)?ae(o):ue(o.block,o.currentBlockIndex))});return fe.set(e,t),t};var z=e=>{var t,r;return(r=(t=fe.get(e))==null?void 0:t.value)!=null?r:ot(e).value};var ve=(e,t)=>y(()=>{t(z(e))});var he={},me={},nt=e=>{if(typeof window=="undefined"||customElements.get("flows-slot"))return;class t extends HTMLElement{constructor(){super(...arguments);this.mountedElements=[];this.blocks=[]}get slotId(){var o;return(o=this.getAttribute("data-slot-id"))!=null?o:""}get placeholderElement(){return this.querySelector("[data-placeholder]")}connectedCallback(){var i;this.blocks=z(this.slotId);let o=(i=e.addSlotBlocksChangeListener)!=null?i:ve;this.changeListenerDispose=o(this.slotId,s=>{this.blocks=s,this.render()}),this.render()}disconnectedCallback(){var o;(o=this.changeListenerDispose)==null||o.call(this),this.unmount()}render(){this.unmount(),this.placeholderElement&&(this.blocks.length?this.placeholderElement.hidden=!0:this.placeholderElement.hidden=!1),this.blocks.forEach(o=>{let i=o.type==="component"?he[o.component]:o.type==="tour-component"?me[o.component]:null;if(i){let{cleanup:s,element:l}=i(o.props);this.mountedElements.push({el:l,cleanup:s,blockId:o.id}),l&&this.appendChild(l)}})}unmount(){this.mountedElements.forEach(o=>{o.cleanup(),o.el&&o.el.remove()}),this.mountedElements=[]}}customElements.define("flows-slot",t)},it=e=>{nt({addSlotBlocksChangeListener:e.addSlotBlocksChangeListener}),he=e.components,me=e.tourComponents};return Se(lt);})();