@pro6pp/infer-js 0.0.2-beta.15 → 0.0.2-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +14 -31
- package/dist/index.global.js +14 -31
- package/dist/index.mjs +15 -32
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var y=Object.defineProperty,R=Object.defineProperties,$=Object.getOwnPropertyDescriptor,D=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var E=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&E(o,t,e[t]);if(L)for(var t of L(e))N.call(e,t)&&E(o,t,e[t]);return o},m=(o,e)=>R(o,D(e));var B=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},O=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of F(e))!T.call(o,n)&&n!==t&&y(o,n,{get:()=>e[n],enumerable:!(i=$(e,n))||i.enumerable});return o};var U=o=>O(y({},"__esModule",{value:!0}),o);var j={};B(j,{InferJS:()=>v,attach:()=>q});module.exports=U(j);function _(o){return o.toLowerCase().trim()}function H(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function V(o,e){let t=_(o),i=_(e);if(i.includes(" "))return t.indexOf(i);let r=new RegExp(`(?:^|[,\\s])${H(i)}(?:$|[,\\s])`,"g").exec(t);if(r){let s=r.index,p=t[s];return p===","||p===" "?s+1:s}return-1}function P(o,e){let t=[],i=[];e.street&&i.push({value:e.street,type:"street"}),e.city&&i.push({value:e.city,type:"city"}),e.postcode&&i.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&i.push({value:String(e.street_number),type:"street_number"}),e.addition&&i.push({value:e.addition,type:"addition"});for(let n of i){let r=V(o,n.value);r!==-1&&t.push({type:n.type,value:n.value,position:r})}return t.sort((n,r)=>n.position-r.position),t}function A(o,e){if(!e||!o)return"";let t=P(o,e),i=new Set(t.map(s=>s.type)),n=[];for(let s of t)n.push(s.value);let r=["street","street_number","addition","postcode","city"];for(let s of r){if(i.has(s))continue;let p;switch(s){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&n.push(p)}return n.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},w={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},M={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},M);let i=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,n=Math.max(i,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let p=t;if(n&&Object.keys(n).length>0){let{street:d,street_number:a,postcode:c,city:l,addition:h}=n;if(d&&a&&l){let g=h?` ${h}`:"",u=c?`${c}, `:"";p=`${d} ${a}${g}, ${u}${l}`}}return this.finishSelection(p,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&w.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return w.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(w.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let p=r.includes("?")?"&":"?",d=`${r}${p}${s.toString()}`;this.fetcher(d,{signal:n}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,n);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var p,d,a,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let l of i){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);n.push(g)}}let s=n.length+(((p=e.cities)==null?void 0:p.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let i=A(this.state.query,t);return i?m(f({},e),{label:i}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(f({},M),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function z(o){if(o.length===0)return o;let e=[];for(let t of o){let i=e[e.length-1];i&&i.match===t.match?i.text+=t.text:e.push({text:t.text,match:t.match})}return e}function k(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?z(t):[{text:o,match:!1}]}var C=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -130,15 +130,23 @@
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
.pro6pp-item__label {
|
|
133
|
-
font-weight:
|
|
133
|
+
font-weight: 400;
|
|
134
134
|
flex-shrink: 1;
|
|
135
135
|
overflow: hidden;
|
|
136
136
|
text-overflow: ellipsis;
|
|
137
137
|
white-space: nowrap;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
.pro6pp-item__label--match {
|
|
141
|
+
font-weight: 520;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.pro6pp-item__label--unmatched {
|
|
145
|
+
font-weight: 400;
|
|
146
|
+
color: #4b5563;
|
|
147
|
+
}
|
|
148
|
+
|
|
140
149
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size: 13px;
|
|
142
150
|
color: #6b7280;
|
|
143
151
|
flex-shrink: 0;
|
|
144
152
|
}
|
|
@@ -158,20 +166,6 @@
|
|
|
158
166
|
text-align: center;
|
|
159
167
|
}
|
|
160
168
|
|
|
161
|
-
.pro6pp-load-more {
|
|
162
|
-
width: 100%;
|
|
163
|
-
padding: 14px;
|
|
164
|
-
background: #f9fafb;
|
|
165
|
-
border: none;
|
|
166
|
-
border-top: 1px solid #e0e0e0;
|
|
167
|
-
color: #3b82f6;
|
|
168
|
-
font-size: 14px;
|
|
169
|
-
font-weight: 600;
|
|
170
|
-
cursor: pointer;
|
|
171
|
-
flex-shrink: 0;
|
|
172
|
-
touch-action: manipulation;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
169
|
.pro6pp-loader-item {
|
|
176
170
|
padding: 10px 12px;
|
|
177
171
|
color: #6b7280;
|
|
@@ -202,23 +196,12 @@
|
|
|
202
196
|
padding: 10px 12px;
|
|
203
197
|
font-size: 14px;
|
|
204
198
|
}
|
|
205
|
-
.pro6pp-item__subtitle {
|
|
206
|
-
font-size: 12px;
|
|
207
|
-
}
|
|
208
|
-
.pro6pp-load-more {
|
|
209
|
-
padding: 12px;
|
|
210
|
-
font-size: 13px;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
.pro6pp-load-more:active {
|
|
215
|
-
background-color: #f3f4f6;
|
|
216
199
|
}
|
|
217
200
|
|
|
218
201
|
@keyframes pro6pp-spin {
|
|
219
202
|
to { transform: rotate(360deg); }
|
|
220
203
|
}
|
|
221
|
-
`;var
|
|
204
|
+
`;var v=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
222
205
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
223
206
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
224
207
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
@@ -226,8 +209,8 @@
|
|
|
226
209
|
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
210
|
<div class="pro6pp-mini-spinner"></div>
|
|
228
211
|
<span>${this.loadingText}</span>
|
|
229
|
-
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(
|
|
212
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(m(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.postcode?`${e.postcode}, `:"",i=`${e.street} ${e.street_number}, ${t}${e.city}`;this.core.selectItem({label:i,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=C,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,p)=>{if(!s.label)return;let d=document.createElement("li");d.className="pro6pp-item",p===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",k(s.label,e.query).forEach(({text:u,match:I})=>{let S=document.createElement("span");S.className=I?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",S.textContent=u,a.appendChild(S)}),d.appendChild(a);let l=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||l;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
230
213
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
231
214
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
232
215
|
</svg>
|
|
233
|
-
`,
|
|
216
|
+
`,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function q(o,e){return new v(o,e)}0&&(module.exports={InferJS,attach});
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PP=(()=>{var
|
|
1
|
+
"use strict";var Pro6PP=(()=>{var y=Object.defineProperty,R=Object.defineProperties,$=Object.getOwnPropertyDescriptor,D=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,L=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var E=(o,e,t)=>e in o?y(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&E(o,t,e[t]);if(L)for(var t of L(e))N.call(e,t)&&E(o,t,e[t]);return o},m=(o,e)=>R(o,D(e));var B=(o,e)=>{for(var t in e)y(o,t,{get:e[t],enumerable:!0})},O=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of F(e))!T.call(o,n)&&n!==t&&y(o,n,{get:()=>e[n],enumerable:!(i=$(e,n))||i.enumerable});return o};var U=o=>O(y({},"__esModule",{value:!0}),o);var j={};B(j,{InferJS:()=>v,attach:()=>q});function _(o){return o.toLowerCase().trim()}function H(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function V(o,e){let t=_(o),i=_(e);if(i.includes(" "))return t.indexOf(i);let r=new RegExp(`(?:^|[,\\s])${H(i)}(?:$|[,\\s])`,"g").exec(t);if(r){let s=r.index,p=t[s];return p===","||p===" "?s+1:s}return-1}function P(o,e){let t=[],i=[];e.street&&i.push({value:e.street,type:"street"}),e.city&&i.push({value:e.city,type:"city"}),e.postcode&&i.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&i.push({value:String(e.street_number),type:"street_number"}),e.addition&&i.push({value:e.addition,type:"addition"});for(let n of i){let r=V(o,n.value);r!==-1&&t.push({type:n.type,value:n.value,position:r})}return t.sort((n,r)=>n.position-r.position),t}function A(o,e){if(!e||!o)return"";let t=P(o,e),i=new Set(t.map(s=>s.type)),n=[];for(let s of t)n.push(s.value);let r=["street","street_number","addition","postcode","city"];for(let s of r){if(i.has(s))continue;let p;switch(s){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&n.push(p)}return n.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},w={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},M={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},M);let i=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,n=Math.max(i,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let p=t;if(n&&Object.keys(n).length>0){let{street:d,street_number:a,postcode:c,city:l,addition:h}=n;if(d&&a&&l){let g=h?` ${h}`:"",u=c?`${c}, `:"";p=`${d} ${a}${g}, ${u}${l}`}}return this.finishSelection(p,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&w.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return w.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(w.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let p=r.includes("?")?"&":"?",d=`${r}${p}${s.toString()}`;this.fetcher(d,{signal:n}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,n);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var p,d,a,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let l of i){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);n.push(g)}}let s=n.length+(((p=e.cities)==null?void 0:p.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let i=A(this.state.query,t);return i?m(f({},e),{label:i}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(f({},M),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function z(o){if(o.length===0)return o;let e=[];for(let t of o){let i=e[e.length-1];i&&i.match===t.match?i.text+=t.text:e.push({text:t.text,match:t.match})}return e}function k(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?z(t):[{text:o,match:!1}]}var C=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -130,15 +130,23 @@
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
.pro6pp-item__label {
|
|
133
|
-
font-weight:
|
|
133
|
+
font-weight: 400;
|
|
134
134
|
flex-shrink: 1;
|
|
135
135
|
overflow: hidden;
|
|
136
136
|
text-overflow: ellipsis;
|
|
137
137
|
white-space: nowrap;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
.pro6pp-item__label--match {
|
|
141
|
+
font-weight: 520;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.pro6pp-item__label--unmatched {
|
|
145
|
+
font-weight: 400;
|
|
146
|
+
color: #4b5563;
|
|
147
|
+
}
|
|
148
|
+
|
|
140
149
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size: 13px;
|
|
142
150
|
color: #6b7280;
|
|
143
151
|
flex-shrink: 0;
|
|
144
152
|
}
|
|
@@ -158,20 +166,6 @@
|
|
|
158
166
|
text-align: center;
|
|
159
167
|
}
|
|
160
168
|
|
|
161
|
-
.pro6pp-load-more {
|
|
162
|
-
width: 100%;
|
|
163
|
-
padding: 14px;
|
|
164
|
-
background: #f9fafb;
|
|
165
|
-
border: none;
|
|
166
|
-
border-top: 1px solid #e0e0e0;
|
|
167
|
-
color: #3b82f6;
|
|
168
|
-
font-size: 14px;
|
|
169
|
-
font-weight: 600;
|
|
170
|
-
cursor: pointer;
|
|
171
|
-
flex-shrink: 0;
|
|
172
|
-
touch-action: manipulation;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
169
|
.pro6pp-loader-item {
|
|
176
170
|
padding: 10px 12px;
|
|
177
171
|
color: #6b7280;
|
|
@@ -202,23 +196,12 @@
|
|
|
202
196
|
padding: 10px 12px;
|
|
203
197
|
font-size: 14px;
|
|
204
198
|
}
|
|
205
|
-
.pro6pp-item__subtitle {
|
|
206
|
-
font-size: 12px;
|
|
207
|
-
}
|
|
208
|
-
.pro6pp-load-more {
|
|
209
|
-
padding: 12px;
|
|
210
|
-
font-size: 13px;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
.pro6pp-load-more:active {
|
|
215
|
-
background-color: #f3f4f6;
|
|
216
199
|
}
|
|
217
200
|
|
|
218
201
|
@keyframes pro6pp-spin {
|
|
219
202
|
to { transform: rotate(360deg); }
|
|
220
203
|
}
|
|
221
|
-
`;var
|
|
204
|
+
`;var v=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
222
205
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
223
206
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
224
207
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
@@ -226,8 +209,8 @@
|
|
|
226
209
|
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
210
|
<div class="pro6pp-mini-spinner"></div>
|
|
228
211
|
<span>${this.loadingText}</span>
|
|
229
|
-
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(
|
|
212
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(m(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.postcode?`${e.postcode}, `:"",i=`${e.street} ${e.street_number}, ${t}${e.city}`;this.core.selectItem({label:i,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=C,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,p)=>{if(!s.label)return;let d=document.createElement("li");d.className="pro6pp-item",p===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",k(s.label,e.query).forEach(({text:u,match:I})=>{let S=document.createElement("span");S.className=I?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",S.textContent=u,a.appendChild(S)}),d.appendChild(a);let l=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||l;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
230
213
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
231
214
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
232
215
|
</svg>
|
|
233
|
-
`,
|
|
216
|
+
`,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function q(o,e){return new v(o,e)}return U(j);})();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var M=Object.defineProperty,k=Object.defineProperties;var R=Object.getOwnPropertyDescriptors;var I=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var L=(o,e,t)=>e in o?M(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))$.call(e,t)&&L(o,t,e[t]);if(I)for(var t of I(e))D.call(e,t)&&L(o,t,e[t]);return o},m=(o,e)=>k(o,R(e));function E(o){return o.toLowerCase().trim()}function F(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function N(o,e){let t=E(o),i=E(e);if(i.includes(" "))return t.indexOf(i);let r=new RegExp(`(?:^|[,\\s])${F(i)}(?:$|[,\\s])`,"g").exec(t);if(r){let s=r.index,p=t[s];return p===","||p===" "?s+1:s}return-1}function B(o,e){let t=[],i=[];e.street&&i.push({value:e.street,type:"street"}),e.city&&i.push({value:e.city,type:"city"}),e.postcode&&i.push({value:e.postcode,type:"postcode"}),e.street_number!==void 0&&e.street_number!==null&&i.push({value:String(e.street_number),type:"street_number"}),e.addition&&i.push({value:e.addition,type:"addition"});for(let n of i){let r=N(o,n.value);r!==-1&&t.push({type:n.type,value:n.value,position:r})}return t.sort((n,r)=>n.position-r.position),t}function T(o,e){if(!e||!o)return"";let t=B(o,e),i=new Set(t.map(s=>s.type)),n=[];for(let s of t)n.push(s.value);let r=["street","street_number","addition","postcode","city"];for(let s of r){if(i.has(s))continue;let p;switch(s){case"street":p=e.street;break;case"city":p=e.city;break;case"street_number":p=e.street_number!==void 0?String(e.street_number):void 0;break;case"postcode":p=e.postcode;break;case"addition":p=e.addition;break}p&&n.push(p)}return n.join(", ")}var b={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},v={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},_={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},y=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||b.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:b.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},_);let i=e.debounceMs!==void 0?e.debounceMs:b.DEBOUNCE_MS,n=Math.max(i,b.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let p=t;if(n&&Object.keys(n).length>0){let{street:d,street_number:a,postcode:c,city:l,addition:h}=n;if(d&&a&&l){let g=h?` ${h}`:"",u=c?`${c}, `:"";p=`${d} ${a}${g}, ${u}${l}`}}return this.finishSelection(p,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&v.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return v.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(v.STREET_NUMBER_PREFIX);if(h){let g=h[1];t.startsWith(g)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${b.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let p=r.includes("?")?"&":"?",d=`${r}${p}${s.toString()}`;this.fetcher(d,{signal:n}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,n);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var p,d,a,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let l of i){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;if(!r.has(h)){r.add(h);let g=this.reformatSuggestionLabel(l);n.push(g)}}let s=n.length+(((p=e.cities)==null?void 0:p.length)||0)+(((d=e.streets)==null?void 0:d.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}reformatSuggestionLabel(e){if(!e.value||typeof e.value=="string")return e;let t=e.value;if(!t.street||!t.city)return e;let i=T(this.state.query,t);return i?m(f({},e),{label:i}):e}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(f({},_),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function O(o){if(o.length===0)return o;let e=[];for(let t of o){let i=e[e.length-1];i&&i.match===t.match?i.text+=t.text:e.push({text:t.text,match:t.match})}return e}function A(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?O(t):[{text:o,match:!1}]}var S=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -130,15 +130,23 @@ var A=Object.defineProperty,M=Object.defineProperties;var _=Object.getOwnPropert
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
.pro6pp-item__label {
|
|
133
|
-
font-weight:
|
|
133
|
+
font-weight: 400;
|
|
134
134
|
flex-shrink: 1;
|
|
135
135
|
overflow: hidden;
|
|
136
136
|
text-overflow: ellipsis;
|
|
137
137
|
white-space: nowrap;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
.pro6pp-item__label--match {
|
|
141
|
+
font-weight: 520;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.pro6pp-item__label--unmatched {
|
|
145
|
+
font-weight: 400;
|
|
146
|
+
color: #4b5563;
|
|
147
|
+
}
|
|
148
|
+
|
|
140
149
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size: 13px;
|
|
142
150
|
color: #6b7280;
|
|
143
151
|
flex-shrink: 0;
|
|
144
152
|
}
|
|
@@ -158,20 +166,6 @@ var A=Object.defineProperty,M=Object.defineProperties;var _=Object.getOwnPropert
|
|
|
158
166
|
text-align: center;
|
|
159
167
|
}
|
|
160
168
|
|
|
161
|
-
.pro6pp-load-more {
|
|
162
|
-
width: 100%;
|
|
163
|
-
padding: 14px;
|
|
164
|
-
background: #f9fafb;
|
|
165
|
-
border: none;
|
|
166
|
-
border-top: 1px solid #e0e0e0;
|
|
167
|
-
color: #3b82f6;
|
|
168
|
-
font-size: 14px;
|
|
169
|
-
font-weight: 600;
|
|
170
|
-
cursor: pointer;
|
|
171
|
-
flex-shrink: 0;
|
|
172
|
-
touch-action: manipulation;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
169
|
.pro6pp-loader-item {
|
|
176
170
|
padding: 10px 12px;
|
|
177
171
|
color: #6b7280;
|
|
@@ -202,32 +196,21 @@ var A=Object.defineProperty,M=Object.defineProperties;var _=Object.getOwnPropert
|
|
|
202
196
|
padding: 10px 12px;
|
|
203
197
|
font-size: 14px;
|
|
204
198
|
}
|
|
205
|
-
.pro6pp-item__subtitle {
|
|
206
|
-
font-size: 12px;
|
|
207
|
-
}
|
|
208
|
-
.pro6pp-load-more {
|
|
209
|
-
padding: 12px;
|
|
210
|
-
font-size: 13px;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
.pro6pp-load-more:active {
|
|
215
|
-
background-color: #f3f4f6;
|
|
216
199
|
}
|
|
217
200
|
|
|
218
201
|
@keyframes pro6pp-spin {
|
|
219
202
|
to { transform: rotate(360deg); }
|
|
220
203
|
}
|
|
221
|
-
`;var
|
|
204
|
+
`;var w=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
222
205
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
223
206
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
224
207
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
225
208
|
</svg>
|
|
226
|
-
`,
|
|
209
|
+
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
210
|
<div class="pro6pp-mini-spinner"></div>
|
|
228
211
|
<span>${this.loadingText}</span>
|
|
229
|
-
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new
|
|
212
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new y(m(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=e.postcode?`${e.postcode}, `:"",i=`${e.street} ${e.street_number}, ${t}${e.city}`;this.core.selectItem({label:i,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=S,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,p)=>{if(!s.label)return;let d=document.createElement("li");d.className="pro6pp-item",p===e.selectedSuggestionIndex&&d.classList.add("pro6pp-item--active"),d.setAttribute("role","option"),d.setAttribute("aria-selected",p===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",A(s.label,e.query).forEach(({text:u,match:C})=>{let x=document.createElement("span");x.className=C?"pro6pp-item__label--match":"pro6pp-item__label--unmatched",x.textContent=u,a.appendChild(x)}),d.appendChild(a);let l=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||l;if(h!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${h}`,d.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
230
213
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
231
214
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
232
215
|
</svg>
|
|
233
|
-
`,
|
|
216
|
+
`,d.appendChild(u)}d.onmousedown=u=>u.preventDefault(),d.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(d)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function te(o,e){return new w(o,e)}export{w as InferJS,te as attach};
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"sideEffects": [
|
|
19
19
|
"*.css"
|
|
20
20
|
],
|
|
21
|
-
"version": "0.0.2-beta.
|
|
21
|
+
"version": "0.0.2-beta.16",
|
|
22
22
|
"main": "./dist/index.js",
|
|
23
23
|
"module": "./dist/index.mjs",
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"test:coverage": "vitest run --coverage"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@pro6pp/infer-core": "0.0.2-beta.
|
|
46
|
+
"@pro6pp/infer-core": "0.0.2-beta.15"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@testing-library/dom": "^10.4.1",
|