ngx-autocom-place
Version:
Angular GoogleMaps Places Api Autocomplete Directive
16 lines (14 loc) • 10.4 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("ngx-autocom-place",["exports","@angular/core","rxjs","rxjs/operators"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-autocom-place"]={},e.ng.core,e.rxjs,e.rxjs.operators)}(this,(function(e,t,n,o){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function i(e,t,n,o){return new(n||(n=Promise))((function(i,r){function a(e){try{c(o.next(e))}catch(e){r(e)}}function s(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((o=o.apply(e,t||[])).next())}))}function r(e,t){var n,o,i,r,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function s(r){return function(s){return function(r){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,o&&(i=2&r[0]?o.return:r[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,r[1])).done)return i;switch(o=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return a.label++,{value:r[1],done:!1};case 5:a.label++,o=r[1],r=[0];continue;case 7:r=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){a=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){a.label=r[1];break}if(6===r[0]&&a.label<i[1]){a.label=i[1],i=r;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(r);break}i[2]&&a.ops.pop(),a.trys.pop();continue}r=t.call(e,a)}catch(e){r=[6,e],o=0}finally{n=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,s])}}}Object.create;Object.create;var a=function(){function e(){this.autocompleteService=new google.maps.places.AutocompleteService,this.placeService=new google.maps.places.PlacesService(document.createElement("div"))}return e.prototype.getPredictions=function(e,t){var n=this;this.sessionToken||(this.sessionToken=new google.maps.places.AutocompleteSessionToken);var o=Object.assign({input:e,sessionToken:this.sessionToken},t);return new Promise((function(e,t){n.autocompleteService.getPlacePredictions(o,(function(n,o){o===google.maps.places.PlacesServiceStatus.OK||o===google.maps.places.PlacesServiceStatus.ZERO_RESULTS?e(n):t(o)}))}))},e.prototype.getPlaceDetails=function(e){var t=this;return new Promise((function(n,o){t.placeService.getDetails({placeId:e,sessionToken:t.sessionToken},(function(e,i){t.sessionToken=null,i===google.maps.places.PlacesServiceStatus.OK?n(e):o(i)}))}))},e}();a.decorators=[{type:t.Injectable}],a.ctorParameters=function(){return[]};var s=function(){function e(e,n){this.el=e,this.ngxAutocomplaceService=n,this.points=[],this.selectedPlace=new t.EventEmitter,this.predictions=new t.EventEmitter,this.options={types:["geocode"]},this.handleEnterKeydown=!0,this.hideResultDropdown=!1,this.debounceTime=150,this.downArrowIndex=-1}return e.prototype.ngOnInit=function(){return i(this,void 0,void 0,(function(){var e,t,i,a,s=this;return r(this,(function(r){switch(r.label){case 0:return this.el.nativeElement instanceof HTMLInputElement?(this.inputElement=this.el.nativeElement,[3,6]):[3,1];case 1:return"ION-INPUT"!==this.el.nativeElement.tagName?[3,3]:(e=this,[4,this.el.nativeElement.getInputElement()]);case 2:return e.inputElement=r.sent(),[3,6];case 3:return"ION-SEARCHBAR"!==this.el.nativeElement.tagName?[3,5]:(t=this,[4,this.el.nativeElement.getInputElement()]);case 4:return t.inputElement=r.sent(),[3,6];case 5:return[2];case 6:return(i=document.createElement("style")).innerHTML='@keyframes beginBrowserAutofill{0%{}to{}}@keyframes endBrowserAutofill{0%{}to{}}.pac-container{background-color:#fff;position:absolute!important;z-index:1000;border-radius:2px;border-top:1px solid #d9d9d9;font-family:Arial,sans-serif;box-shadow:0 2px 6px rgba(0,0,0,0.3);-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;overflow:hidden}.pac-logo:after{content:"";padding:1px 1px 1px 0;height:18px;box-sizing:border-box;text-align:right;display:block;background-image:url(https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white3.png);background-position:right;background-repeat:no-repeat;background-size:120px 14px}.hdpi.pac-logo:after{background-image:url(https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white3_hdpi.png)}.pac-item{cursor:default;padding:0 4px;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;line-height:30px;text-align:left;border-top:1px solid #e6e6e6;font-size:11px;color:#999}.pac-item:hover{background-color:#fafafa}.pac-item-selected,.pac-item-selected:hover{background-color:#ebf2fe}.pac-matched{font-weight:700}.pac-item-query{font-size:13px;padding-right:3px;color:#000}.pac-icon{width:15px;height:20px;margin-right:7px;margin-top:6px;display:inline-block;vertical-align:top;background-image:url(https://maps.gstatic.com/mapfiles/api-3/images/autocomplete-icons.png);background-size:34px}.hdpi .pac-icon{background-image:url(https://maps.gstatic.com/mapfiles/api-3/images/autocomplete-icons_hdpi.png)}.pac-icon-search{background-position:-1px -1px}.pac-item-selected .pac-icon-search{background-position:-18px -1px}.pac-icon-marker{background-position:-1px -161px}.pac-item-selected .pac-icon-marker{background-position:-18px -161px}.pac-placeholder{color:gray}.pac-target-input:-webkit-autofill{animation-name:beginBrowserAutofill}.pac-target-input:not(:-webkit-autofill){animation-name:endBrowserAutofill}',(a=document.querySelector("script")).parentNode.insertBefore(i,a),this.inputElement.addEventListener("blur",(function(){s.blurEvent()})),this.inputSubscription=n.fromEvent(this.inputElement,"input").pipe(o.debounceTime(this.debounceTime)).subscribe((function(e){s.inputEvent(e)})),this.inputElement.addEventListener("keydown",(function(e){s.keydownEvent(e)})),[2]}}))}))},e.prototype.createPacContainer=function(e,t){var n=this;this.resultPacContainer=document.createElement("div");var o=e.getBoundingClientRect();this.resultPacContainer.classList.add("pac-container","pac-logo","hdpi"),this.resultPacContainer.style.position="absolute",this.resultPacContainer.style.top=o.bottom+"px",this.resultPacContainer.style.left=o.left+"px",this.resultPacContainer.style.width=o.right-o.left+"px",t.forEach((function(e){n.resultPacContainer.appendChild(n.createPacItem(e.place_id,e.structured_formatting.main_text,e.structured_formatting.secondary_text,e.structured_formatting.main_text_matched_substrings[0].length))})),document.querySelector("body").appendChild(this.resultPacContainer)},e.prototype.createPacItem=function(e,t,n,o){var i=this,r=document.createElement("div");r.classList.add("pac-item"),r.id=e;var a=document.createElement("span");a.classList.add("pac-icon","pac-icon-marker");var s=document.createElement("span");s.classList.add("pac-item-query");var c=document.createElement("span");c.classList.add("pac-matched"),c.innerText=t.substr(0,o),s.appendChild(c),s.appendChild(document.createTextNode(t.substr(o)));var l=document.createElement("span");return l.appendChild(document.createTextNode(n)),r.appendChild(a),r.appendChild(s),r.appendChild(l),r.addEventListener("click",(function(){i.getPlaceDetails(e)})),r},e.prototype.getPlaceDetails=function(e){var t=this;this.ngxAutocomplaceService.getPlaceDetails(e).then((function(e){t.selectedPlace.emit(e)}))},e.prototype.blurEvent=function(){var e=this;setTimeout((function(){e.resultPacContainer&&e.removePacItems()}),250)},e.prototype.inputEvent=function(e){var t=this;this.inputElement&&this.inputElement.value?this.ngxAutocomplaceService.getPredictions(this.inputElement.value,this.options).then((function(e){t.removePacItems(),e&&(t.hideResultDropdown||(t.results=e,t.createPacContainer(t.inputElement,e)),t.predictions.emit(e))})):this.removePacItems()},e.prototype.keydownEvent=function(e){var t,n;this.handleEnterKeydown&&("Enter"!==e.key&&13!==e.keyCode||this.results&&this.results.length>0&&(this.getPlaceDetails(this.results[this.downArrowIndex>-1?this.downArrowIndex:0].place_id),this.removePacItems())),40===e.keyCode&&(this.downArrowIndex++,this.downArrowIndex>0&&(null===(t=document.querySelectorAll(".pac-item")[this.downArrowIndex-1])||void 0===t||t.classList.remove("pac-item-selected")),null===(n=document.querySelectorAll(".pac-item")[this.downArrowIndex])||void 0===n||n.classList.add("pac-item-selected"))},e.prototype.removePacItems=function(){this.resultPacContainer&&(this.resultPacContainer.remove(),this.resultPacContainer=null),this.downArrowIndex=-1},e.prototype.ngOnDestroy=function(){var e=this;this.inputElement.removeEventListener("blur",(function(){e.blurEvent()})),this.inputSubscription.unsubscribe(),this.inputElement.removeEventListener("keydown",(function(t){e.keydownEvent(t)}))},e}();s.decorators=[{type:t.Directive,args:[{selector:"[ngxAutocomPlace]"}]}],s.ctorParameters=function(){return[{type:t.ElementRef},{type:a}]},s.propDecorators={selectedPlace:[{type:t.Output}],predictions:[{type:t.Output}],options:[{type:t.Input}],handleEnterKeydown:[{type:t.Input}],hideResultDropdown:[{type:t.Input}],debounceTime:[{type:t.Input}]};var c=function(){};c.decorators=[{type:t.NgModule,args:[{imports:[],declarations:[s],exports:[s],providers:[a]}]}],e.NgxAutocomPlaceDirective=s,e.NgxAutocomPlaceModule=c,e.ɵa=a,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-autocom-place.umd.min.js.map