@abgov/ui-components-common
Version:
Government of Alberta - UI Web components
2 lines (1 loc) • 7.81 kB
JavaScript
;var S=Object.defineProperty;var y=r=>{throw TypeError(r)};var M=(r,t,e)=>t in r?S(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var l=(r,t,e)=>M(r,typeof t!="symbol"?t+"":t,e),C=(r,t,e)=>t.has(r)||y("Cannot "+e);var V=(r,t,e)=>t.has(r)?y("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(r):t.set(r,e);var _=(r,t,e)=>(C(r,t,"access private method"),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class N{constructor(t){l(this,"validators");this.validators=t||{}}add(t,...e){this.validators[t]=e}validate(t){const e={};return Object.entries(this.validators).forEach(([i,a])=>{const n=a.map(s=>s(t[i])).find(s=>!!s);n&&(e[i]=n)}),e}}function w(){return[c("Day is required"),h({min:1,max:31,minMsg:"Day must be between 1 and 31",maxMsg:"Day must be between 1 and 31"})]}function E(){return[c("Month is required"),h({min:0,max:11,minMsg:"Month must be between Jan and Dec",maxMsg:"Month must be between Jan and Dec"})]}function R(){const r=new Date().getFullYear();return[c("Year is required"),h({min:1900,max:r,minMsg:"Year must be greater than 1900",maxMsg:`Year must be less than ${r}`})]}function c(r){return t=>(r=r||"Required",typeof t=="number"&&!isNaN(t)||t?"":r)}function D(r){const t=new RegExp(/^\+?[\d-() ]{10,18}$/);return m(t,r||"Invalid phone number")}function I(r){const t=new RegExp(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);return m(t,r||"Invalid email address")}function $(){return r=>{if(!r)return"";const t="121212121".split("").map(a=>parseInt(a)),e=r.replace(/\D/g,"");return e.length!==9?"SIN must contain 9 numbers":e.split("").map(a=>parseInt(a)).map((a,n)=>{const s=a*t[n];return s<10?s:`${s}`.split("").map(u=>parseInt(u)).reduce((u,f)=>u+f,0)}).reduce((a,n)=>a+n,0)%10===0?"":"Invalid SIN"}}function x(){return m(/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,"Invalid postal code")}function m(r,t){return e=>!e||e.match(r)?"":t}function L({invalidMsg:r,minMsg:t,maxMsg:e,min:i,max:a}={}){return n=>{let s=new Date(-1);return`${n||""}`.length===0?"":(typeof n=="string"&&(s=new Date(n)),typeof n=="number"&&(s=new Date(n)),n.toDateString&&(s=n),s.getDate()===-1?r||"Invalid date":i&&s<i?t||`Must be after ${i}`:a&&s>a?e||`Must be before ${a}`:"")}}function h({invalidTypeMsg:r,minMsg:t,maxMsg:e,min:i=-Number.MAX_VALUE,max:a=Number.MAX_VALUE}={}){return n=>{let s=Number.MAX_VALUE;return`${n??""}`.length===0?"":(typeof n=="string"&&(s=parseFloat(n)),typeof n=="number"&&(s=n),isNaN(s)?r||"Must be a numeric value":s>a?e||`Must be less than or equal to ${a}`:s<i?t||`Must be greater than or equal to ${i}`:"")}}function T({invalidTypeMsg:r,minMsg:t,maxMsg:e,min:i=-Number.MAX_VALUE,max:a=Number.MAX_VALUE}){return n=>`${n||""}`.length===0?"":typeof n!="string"?r||"Invalid type":n.length>a?e||`Must be less than ${a} characters`:n.length<i?t||`Must be greater than ${i} characters`:""}function b(r,t,e,i){if(!r){console.error("dispatch element is null");return}r.dispatchEvent(new CustomEvent(t,{composed:!0,bubbles:i==null?void 0:i.bubbles,detail:e}))}function o(r,t,e,i){if(!r){console.error("dispatch element is null");return}r.dispatchEvent(new CustomEvent("msg",{composed:!0,bubbles:i==null?void 0:i.bubbles,detail:{action:t,data:e}}))}var d,q,v;class U{constructor(t){V(this,d);l(this,"state");l(this,"_formData");l(this,"_formRef");l(this,"_isCompleting",!1);this.type=t}init(t){if(this._formRef){console.warn("init: form element has already been set");return}this._formRef=t.detail.el,this.state={uuid:crypto.randomUUID(),form:{},history:[],editting:"",status:"not-started"}}initList(t){this._formRef=t.detail.el,this.state=[]}initState(t,e){o(this._formRef,"external::init:state",t),typeof t=="string"?this.state=JSON.parse(t):Array.isArray(t)||(this.state=t),e&&setTimeout(e,200)}updateListState(t){const e=t.detail;Array.isArray(e.data)&&(this.state=e.data)}updateObjectState(t){var i,a;if(Array.isArray(this.state))return;const e=t.detail;e.type==="list"?this.state={...this.state,form:{...((i=this.state)==null?void 0:i.form)||{},[e.id]:e.data}}:this.state={...this.state,...e.data,form:{...((a=this.state)==null?void 0:a.form)||{},...e.data.form},history:e.data.history}}getStateList(){return this.state?Array.isArray(this.state)?this.state.length===0?[]:this.state.map(t=>Object.values(t.form).filter(e=>{var i;return((i=e==null?void 0:e.data)==null?void 0:i.type)==="details"}).map(e=>{var i;return e.data.type==="details"&&((i=e.data)==null?void 0:i.fieldsets)||{}}).reduce((e,i)=>{for(const[a,n]of Object.entries(i))e[a]=n.value;return e},{})):(console.warn("Utils:getStateList: unable to update the state of a non-multi form type",this.state),[]):[]}getStateValue(t,e){if(Array.isArray(this.state))return console.error("getStateValue: unable to update the state of a multi form type"),"";if(!this.state)return console.error("getStateValue: state has not yet been set"),"";const i=this.state.form[t].data;return i.type!=="details"?"":i.fieldsets[e].value}continueTo(t){if(!t){console.error("continueTo [name] is undefined");return}o(this._formRef,"external::continue",{next:t})}validate(t,e,i,a){var p;const{el:n,state:s,cancelled:u}=t.detail,f=(p=s==null?void 0:s[e])==null?void 0:p.value;if(window.scrollTo({top:0,behavior:"smooth"}),u)return[!0,f];for(const A of i){const g=A(f);if(_(this,d,v).call(this,n,e,g,a),g)return[!1,""]}return[!0,f]}validateGroup(t,e,i){let a=0;const n={};for(const s of e){const[u]=this.validate(t,s,i,{grouped:!0});u&&(n[s]=!0,a++)}return[a,n]}edit(t){o(this._formRef,"external::alter:state",{index:t,operation:"edit"})}remove(t){o(this._formRef,"external::alter:state",{index:t,operation:"remove"})}complete(){if(!this._formRef){console.error("complete: form ref is not set");return}if(this._isCompleting){console.warn("complete: completion already in progress");return}this._isCompleting=!0,o(this._formRef,"fieldset::submit",null,{bubbles:!0}),this._isCompleting=!1}completeSubform(){if(!this._formRef){console.error("completeSubform: form ref is not set");return}if(this._isCompleting){console.warn("completeSubform: completion already in progress");return}const t=this._formRef;this._isCompleting=!0;const e=i=>{t.removeEventListener("_stateChange",e),b(t,"_complete",{},{bubbles:!0}),this._isCompleting=!1};t.addEventListener("_stateChange",e),b(t,"_continue",null,{bubbles:!0})}clean(t){return t.history.reduce((e,i)=>(e[i]=t.form[i],e),{})}}d=new WeakSet,q=function(t){var e;Array.isArray(t.data)&&(Array.isArray(this.state)||(this.state={...this.state,form:{...((e=this.state)==null?void 0:e.form)||{},[t.id]:t.data}}))},v=function(t,e,i,a){t.dispatchEvent(new CustomEvent("msg",{composed:!0,detail:{action:"external::set:error",data:{name:e,msg:i,grouped:a==null?void 0:a.grouped}}}))};const Y=["basic","success","failure"];function O(r,t){const e=crypto.randomUUID();return t={uuid:e,type:"basic",...t||{}},!t.duration&&t.type&&Y.includes(t.type)&&(t.duration="short"),o(document.body,"goa:temp-notification",{message:r,...t},{bubbles:!0}),e}function P(r){o(document.body,"goa:temp-notification:dismiss",r,{bubbles:!0})}function j(r,t){o(document.body,"goa:temp-notification:progress",{uuid:r,progress:t},{bubbles:!0})}const F={show:O,dismiss:P,setProgress:j};exports.FormValidator=N;exports.PublicFormController=U;exports.SINValidator=$;exports.TemporaryNotification=F;exports.birthDayValidator=w;exports.birthMonthValidator=E;exports.birthYearValidator=R;exports.dateValidator=L;exports.dispatch=b;exports.emailValidator=I;exports.lengthValidator=T;exports.numericValidator=h;exports.phoneNumberValidator=D;exports.postalCodeValidator=x;exports.regexValidator=m;exports.relay=o;exports.requiredValidator=c;