/** * ideal-postcodes-autocomplete - Frontend UK Address Autocomplete Library for Ideal Postcodes API * @version v0.2.1 * @link https://ideal-postcodes.co.uk * @license MIT */ var __extends=this&&this.__extends||function(t,e){function r(){this.constructor=t}for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)},Autocomplete;!function(t){var e;!function(t){var e=/-/,r=/bo'ness/i,n=/\w+&\w+/,o=/^(of|le|upon|on|the)$/,i=/^(in|de|under|upon|y|on|over|the|by)$/,s=function(t){return t=t.toLowerCase(),t.match(o)?t:t.match(n)?t.toUpperCase():t.charAt(0).toUpperCase()+t.slice(1)},u=function(t){return null===t.match(e)?t:t.split("-").map(function(t){return t.match(i)?t.toLowerCase():s(t)}).join("-")},a=function(t){return t.match(r)?"Bo'Ness":t};t.titleizePostTown=function(t){return t.split(" ").map(s).map(u).map(a).join(" ")},t.create=function(t,e){var r=document.createElement(t);for(var n in e){var o=e[n];if("inside"===n){var i="string"==typeof o?document.querySelector(o):o;i.appendChild(r)}else if("around"===n){var s="string"==typeof o?document.querySelector(o):o;s.parentNode.insertBefore(r,s),r.appendChild(s)}else void 0!==r[n]?r[n]=o:r.setAttribute(n,o)}return r},t.addClass=function(t,e){var r=t.className.split(" ");return r.some(function(t){return t===e})?t:(r.push(e),t.className=r.join(" ").trim(),t)},t.removeClass=function(t,e){var r=t.className.split(" ");return t.className=r.filter(function(t){return t!==e}).join(" ").trim(),t},t.toArray=function(t){return"string"==typeof t?t.split(","):t},t.removeOrganisation=function(t){return 0===t.organisation_name.length?t:(t.line_1===t.organisation_name&&(t.line_1=t.line_2,t.line_2=t.line_3,t.line_3=""),t)}}(e=t.Utils||(t.Utils={}))}(Autocomplete||(Autocomplete={}));var Autocomplete;!function(t){var e=t.Utils.create,r=function(){function r(t){this.initialiseInterface(t).initialiseCallbacks(t).initialiseEventListeners(t).refresh()}return r.prototype.initialiseInterface=function(t){return this.suggestions=[],this.highlightIndex=-1,this.input=document.querySelector(t.inputField),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("aria-autocomplete","list"),this.container=e("div",{className:"idpc_autocomplete",around:this.input}),this.suggestionList=e("ul",{className:"hidden idpc_ul",inside:this.container}),this},r.prototype.initialiseCallbacks=function(e){var r=this,n=function(){};return t.interfaceCallbacks.forEach(function(t){r[t]=e[t]?e[t]:n}),this},r.prototype.initialiseEventListeners=function(t){return this.input.addEventListener("input",this._onInput.bind(this)),this.input.addEventListener("blur",this._onBlur.bind(this,"blur")),this.input.addEventListener("focus",this._onFocus.bind(this)),this.input.addEventListener("keydown",this._onKeyDown.bind(this)),this.suggestionList.addEventListener("mousedown",this._onMousedown.bind(this)),this},r.prototype._onBlur=function(){this.onBlur(),this.close("blur")},r.prototype._onFocus=function(){this.onFocus(),this.refresh()},r.prototype._onInput=function(t){this.onInput(t)},r.prototype._onMousedown=function(t){var e=this.suggestionList,r=t.target;if(r!==e){for(;r&&!/li/i.test(r.nodeName);)r=r.parentNode;r&&0===t.button&&(t.preventDefault(),this.select(r))}},r.prototype._onKeyDown=function(t){var e=t.keyCode;this.opened()&&(13===e&&this.selected()?(t.preventDefault(),this.select()):8===e?this.onInput(t):27===e?this.close("esc"):38!==e&&40!==e||(t.preventDefault(),this[38===e?"previous":"next"]()))},r.prototype.detach=function(){return this.container.removeChild(this.suggestionList),this.container.parentElement.removeChild(this.container),this.container=null,this.suggestionList=null,this},r.prototype.setMessage=function(t){return void 0===t||0===t.length?this.refresh():(this.highlightIndex=-1,this.suggestionList.innerHTML="",this.suggestionList.appendChild(e("li",{innerHTML:t,class:"idpc_error"})),this.open(),this)},r.prototype.refresh=function(){var t=this,r=this.suggestions;return this.highlightIndex=-1,this.suggestionList.innerHTML="",r.forEach(function(r){t.suggestionList.appendChild(e("li",{innerHTML:r.suggestion,"aria-selected":"false"}))}),0===this.suggestionList.children.length?this.close():this.open(),this},r.prototype.setSuggestions=function(t){return this.suggestions=t,this.refresh(),this},r.prototype.close=function(e){return this.opened()?(this.onClose(),t.Utils.addClass(this.suggestionList,"hidden"),this):this},r.prototype.open=function(){return this.onOpen(),t.Utils.removeClass(this.suggestionList,"hidden"),this},r.prototype.opened=function(){return!this.suggestionList.className.split(" ").some(function(t){return"hidden"===t})},r.prototype.next=function(){var t=this.suggestionList.children.length;return this.goto(this.highlightIndexr+n&&(this.suggestionList.scrollTop=e-n+o),this},r.prototype.goto=function(t){var e=this.suggestionList.children;if(0===e.length)return this;this.selected()&&e[this.highlightIndex].setAttribute("aria-selected","false"),this.highlightIndex=t;var r=e[t];return t>-1&&e.length>0?(r.setAttribute("aria-selected","true"),this.scrollToView(r)):this.scrollToView(e[0]),this},r.prototype.select=function(t){if(t){var e=void 0;for(e=0;t=t.previousElementSibling;e++);this.highlightIndex=e}return this.highlightIndex>-1&&this.highlightIndex-1},r}();t.Interface=r,t.interfaceCallbacks=["onOpen","onBlur","onClose","onFocus","onInput","onSelect"]}(Autocomplete||(Autocomplete={}));var IdealPostcodes;!function(t){t.API_URL="api.ideal-postcodes.co.uk",t.TLS=!0,t.VERSION="v1",t.DEFAULT_TIMEOUT=1e4,t.STRICT_AUTHORISATION=!1}(IdealPostcodes||(IdealPostcodes={})),window.IdealPostcodes=IdealPostcodes;var IdealPostcodes;!function(t){var e;!function(t){t.now=function(){return Date.now()},t.debounce=function(e,r){function n(){var c=t.now()-u;c>0&&c0&&(o+="?"+i);var s=e.getXhr();s.open(r.method,o,!0);try{for(var u in r.headers)s.setRequestHeader(u,r.headers[u])}catch(t){}var a=setTimeout(function(){s.onreadystatechange=function(){},s.abort(),n(new Error("Request timeout"),null,s)},r.timeout);return s.onreadystatechange=function(){var r;if(4===s.readyState){if(clearTimeout(a),200!==s.status)return n(t.Errors.parse(s),{},s);try{r=e.blankRe.test(s.responseText)?{}:JSON.parse(s.responseText)}catch(t){return n(new Error("parsererror"),null,s)}return n(null,r,s)}},s.send(r.data),s}}(e=t.Transport||(t.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(t){var e;!function(e){var r=0,n=function(){},o=function(r,n){r.queryString.callback=n;var o=r.headers,i=e.deconstructAuthenticationHeader(o.Authorization);return t.Utils.extend(r.queryString,i),e.generateQueryString(r.queryString)},i=function(t){var e=t.code;return e&&"number"==typeof e?parseInt(String(e).slice(0,3)):500};e.jsonpRequest=function(e,s){r+=1;var u=e.url;if(e.method&&"get"!==e.method.toLowerCase())return s(new Error("Browser is unable to perform non-GET requests"),null,null),null;var a="idpc_"+t.Utils.now()+"_"+r,c=o(e,a);c.length>0&&(u+="?"+c);var l=document.getElementsByTagName("script")[0]||document.head,h=setTimeout(function(){p(),s(new Error("Request timeout"),null,null)},e.timeout),p=function(){d.parentNode&&d.parentNode.removeChild(d),window[a]=n,h&&clearTimeout(h)};window[a]=function(e){p();var r=i(e),n={responseText:e,status:r};return 200!==n.status?s(t.Errors.parseErrorResponse(e,r),null,n):s(null,e,n)};var d=document.createElement("script");return d.src=u,d.type="text/javascript",l.parentNode.insertBefore(d,l),null}}(e=t.Transport||(t.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(t){var e;!function(e){e.defaultHeaders={Accept:"text/javascript, application/javascript"},e.request=function(r,n){var o={url:r.url,method:r.method||"GET",headers:r.headers||{},queryString:r.queryString||{},timeout:r.timeout||t.DEFAULT_TIMEOUT,data:r.data||null};return t.Utils.extend(o.headers,e.defaultHeaders),e.legacyBrowser()?e.jsonpRequest(o,n):e.xhrRequest(o,n)}}(e=t.Transport||(t.Transport={}))}(IdealPostcodes||(IdealPostcodes={}));var IdealPostcodes;!function(t){var e=t.Utils.extend,r=t.Transport,n=r.constructHeaders,o=r.constructQueryString,i=r.constructAddressQueryString,s=r.constructAutocompleteQueryString,u=function(){function r(e){void 0===e&&(e={});var r=this;this.api_key=e.api_key,this.tls=void 0===e.tls?t.TLS:e.tls,this.version=void 0===e.version?t.VERSION:e.version,this.baseUrl=void 0===e.baseUrl?t.API_URL:e.baseUrl,this.strictAuthorisation=void 0===e.strictAuthorisation?t.STRICT_AUTHORISATION:e.strictAuthorisation,this.cache=new t.Cache;var n=this;this.autocompleteCallback=function(){},this.debouncedAutocomplete=t.Utils.debounce(function(t){r.lookupAutocomplete(t,n.autocompleteCallback)})}return r.prototype.apiUrl=function(){return"http"+(this.tls?"s":"")+"://"+this.baseUrl+"/"+this.version},r.prototype.ping=function(e){t.Transport.request({url:"http"+(this.tls?"s":"")+"://"+this.baseUrl},e)},r.prototype.lookupPostcode=function(e,r){var i=this;e.api_key=this.api_key;var s=n(e),u=o(e),a=this.cache.getPostcodeQuery(e);return a?r(null,a):void t.Transport.request({url:this.apiUrl()+"/postcodes/"+encodeURIComponent(e.postcode),headers:s,queryString:u},function(t,n,o){return t&&4040===t.responseCode?r(null,[],o):t?r(t,null,o):(i.cache.cachePostcodeQuery(e,n.result),r(null,n.result,o))})},r.prototype.lookupAddress=function(r,s){var u=this;r.api_key=this.api_key;var a=n(r),c=o(r);e(c,i(r));var l=this.cache.getAddressQuery(r);return l?s(null,l):void t.Transport.request({url:this.apiUrl()+"/addresses",headers:a,queryString:c},function(t,e,n){return t?s(t,null,n):(u.cache.cacheAddressQuery(r,e.result),s(null,e.result,n))})},r.prototype.lookupAutocomplete=function(r,i){var u=this;r.api_key=this.api_key;var a=n(r),c=o(r);e(c,s(r));var l=this.cache.getAutocompleteQuery(r);return l?i(null,l,null,r):(this.strictAuthorisation||(c.api_key=this.api_key,delete a.Authorization),void t.Transport.request({url:this.apiUrl()+"/autocomplete/addresses",headers:a,queryString:c},function(t,e,n){return t?i(t,null,n,r):(u.cache.cacheAutocompleteQuery(r,e.result),i(null,e.result,n,r))}))},r.prototype.lookupUdprn=function(e,r){var i=this;e.api_key=this.api_key;var s=n(e),u=o(e),a=this.cache.getUdprnQuery(e);return a?r(null,a):void t.Transport.request({url:this.apiUrl()+"/udprn/"+e.id,headers:s,queryString:u},function(t,n,o){return t?r(t,null,o):(i.cache.cacheUdprnQuery(e,n.result),r(null,n.result,o))})},r.prototype.lookupUmprn=function(e,r){var i=this;e.api_key=this.api_key;var s=n(e),u=o(e),a=this.cache.getUmprnQuery(e);return a?r(null,a):void t.Transport.request({url:this.apiUrl()+"/umprn/"+e.id,headers:s,queryString:u},function(t,n,o){return t?r(t,null,o):(i.cache.cacheUmprnQuery(e,n.result),r(null,n.result,o))})},r.prototype.checkKeyUsability=function(e,r){t.Transport.request({url:this.apiUrl()+"/keys/"+this.api_key,queryString:o(e)},function(t,e,n){return t?r(t,null,n):r(null,e.result,n)})},r.prototype.autocompleteAddress=function(t){this.debouncedAutocomplete(t)},r.prototype.registerAutocompleteCallback=function(t){this.autocompleteCallback=t},r}();t.Client=u}(IdealPostcodes||(IdealPostcodes={}));var Autocomplete;!function(t){t.validClientOptions=["licensee","filter","tags"],t.validSearchFilters=["postcode_outward","post_town"]}(Autocomplete||(Autocomplete={})),void 0!==window.IdealPostcodes&&(window.IdealPostcodes.Autocomplete=Autocomplete);var Autocomplete;!function(t){var e=function(){function e(t){var e=this;this.requestIdCounter=0,this.lastRequestId=0;var r=["inputField","checkKey","removeOrganisation","titleizePostTown"];r.forEach(function(r){return e[r]=t[r]}),this.configureApiRequests(t),this.initialiseClient(t),this.initialiseOutputFields(t.outputFields),this.initialiseCallbacks(t),this.initialiseInterface(t)}return e.prototype.configureApiRequests=function(e){var r=this;this.options={},this.searchFilters={},t.validClientOptions.forEach(function(t){void 0!==e[t]&&(r.options[t]=e[t])})},e.prototype.initialiseClient=function(t){this.client=new IdealPostcodes.Client(t)},e.prototype.setSearchFilter=function(t){this.searchFilters=t},e.prototype.initialiseOutputFields=function(e){var r={};for(var n in e)e.hasOwnProperty(n)&&(r[n]=t.Utils.toArray(e[n]));this.outputFields=r},e.prototype.initialiseCallbacks=function(t){var e=function(){};this.onOpen=t.onOpen||e,this.onBlur=t.onBlur||e,this.onClose=t.onClose||e,this.onFocus=t.onFocus||e,this.onInput=t.onInput||e,this.onLoaded=t.onLoaded||e,this.onSearchError=t.onSearchError||e,this.onFailedCheck=t.onFailedCheck||e,this.onAddressSelected=t.onAddressSelected||e,this.onAddressRetrieved=t.onAddressRetrieved||e,this.onSuggestionsRetrieved=t.onSuggestionsRetrieved||e},e.prototype.initialiseInterface=function(t){var e=this;this.checkKey?this.client.checkKeyUsability(this.options,function(r,n){return r?e.onFailedCheck.call(e):void(n.available?e.attachInterface(t):e.onFailedCheck.call(e))}):this.attachInterface(t)},e.prototype._onInterfaceInput=function(){var e=this;return function(r){e.onInput&&e.onInput(r),e.interface.setMessage(),e.requestIdCounter+=1;var n={query:this.input.value,_id:e.requestIdCounter};e.options.licensee&&(n.licensee=e.options.licensee),t.validSearchFilters.forEach(function(t){e.searchFilters[t]&&(n[t]=e.searchFilters[t])}),e.client.autocompleteAddress(n)}},e.prototype._onInterfaceSelect=function(){var e=this;return function(r){var n=this;e.onAddressSelected.call(this,r),e.interface.setMessage();var o=function(r,o){return r?(e.interface.setMessage("Unable to retrieve your address. Please enter your address manually"),e.onSearchError(r)):(e.onAddressRetrieved.call(n,o),e.removeOrganisation&&(o=t.Utils.removeOrganisation(o)),void e.populateAddress(o))},i=IdealPostcodes.Utils.extend({},e.options);r.umprn?(i.id=r.umprn,e.client.lookupUmprn(i,o)):(i.id=r.udprn,e.client.lookupUdprn(i,o))}},e.prototype.attachInterface=function(e){var r=this;if(!this.interface){var n=this,o={inputField:e.inputField,onInput:n._onInterfaceInput(),onSelect:n._onInterfaceSelect()};t.interfaceCallbacks.forEach(function(t){o[t]||e[t]&&(o[t]=e[t])}),n.interface=new t.Interface(o),n.client.registerAutocompleteCallback(function(t,e,o,i){if(t)return n.interface.setMessage("Unable to retrieve address suggestions. Please enter your address manually"),n.onSearchError(t);var s=e.hits;return r.onSuggestionsRetrieved.call(r,s,i),i&&void 0!==i._id?void(i._id>n.lastRequestId&&(n.lastRequestId=i._id,n.interface.setSuggestions(s))):n.interface.setSuggestions(s)}),this.onLoaded.call(this)}},e.prototype.detachInterface=function(){this.interface&&(this.interface.detach(),this.interface=null)},e.prototype.populateAddress=function(e){var r=this,n=this.outputFields,o=function(e,n){var o=e[n];return r.titleizePostTown&&"post_town"===n?t.Utils.titleizePostTown(o):o},i=function(t){n.hasOwnProperty(t)&&n[t].forEach(function(r){for(var n=document.querySelectorAll(r),i=0;i