UNPKG

@firestitch/address

Version:
157 lines 7.24 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var core_2 = require("@agm/core"); require("rxjs/add/operator/debounceTime"); var Subject_1 = require("rxjs/Subject"); var FsAddressSearchComponent = (function () { function FsAddressSearchComponent(_mapsAPILoader, _ngZone) { var _this = this; this._mapsAPILoader = _mapsAPILoader; this._ngZone = _ngZone; this.address = {}; this.selected = new core_1.EventEmitter(); // Address Predictions this.predictions = []; // Other this._changeAddressDebounce = new Subject_1.Subject(); this._changeAddressDebounce .debounceTime(300) .subscribe(function (value) { _this.updatePredictions(value); }); } FsAddressSearchComponent.prototype.ngOnInit = function () { this.initAddress(); this.initGoogleMap(); if (this.address && this.address.description) { this.updatePredictions(this.address.description); } }; FsAddressSearchComponent.prototype.ngOnDestroy = function () { }; FsAddressSearchComponent.prototype.initAddress = function () { this.address = Object.assign({ name: null, country: null, region: null, address: null, city: null, zip: null, lat: null, lng: null }, this.address); }; FsAddressSearchComponent.prototype.initGoogleMap = function () { var _this = this; this._mapsAPILoader .load() .then(function () { _this.googleAutocompleteService = new google.maps.places.AutocompleteService(); _this.googlePlacesService = new google.maps.places.PlacesService(_this.searchElement.nativeElement); }); }; FsAddressSearchComponent.prototype.updatePredictions = function (value) { var _this = this; if (value && this.googleAutocompleteService) { this.googleAutocompleteService.getPlacePredictions({ input: value }, function (predictions, status) { _this._ngZone.run(function () { _this.predictions.length = 0; if (status != google.maps.places.PlacesServiceStatus.OK) { return; } _this.predictions = predictions; }); }); } }; FsAddressSearchComponent.prototype.addressChanged = function (event) { this._changeAddressDebounce.next(event); }; FsAddressSearchComponent.prototype.change = function (event) { event.stopPropagation(); }; FsAddressSearchComponent.prototype.selectionChange = function (event) { var _this = this; var place = this.predictions.find(function (el) { return el.description === event.option.value; }); var newAddress = { description: place.description }; if (place && this.googlePlacesService) { this.googlePlacesService.getDetails({ placeId: place.place_id }, function (result, status) { _this._ngZone.run(function () { if (status != google.maps.places.PlacesServiceStatus.OK) { return; } newAddress.name = result.name; newAddress.lat = result.geometry.location.lat(); newAddress.lng = result.geometry.location.lng(); result.address_components.forEach(function (item) { if (item.types.some(function (type) { return type === 'country'; })) { newAddress.country = item.short_name; } if (item.types.some(function (type) { return type === 'administrative_area_level_1'; })) { newAddress.region = item.short_name; } if (item.types.some(function (type) { return type === 'locality'; })) { newAddress.city = item.long_name; } if (item.types.some(function (type) { return type === 'postal_code'; })) { newAddress.zip = item.long_name; } }); var streetNumber = result.address_components .find(function (el) { return el.types.some(function (type) { return type === 'street_number'; }); }); if (streetNumber) { newAddress.street = streetNumber.long_name + ' '; } else { var match = newAddress.description.match(/^[\d-]+/); if (match) { newAddress.street = match[0] + ' '; } } var streetAddress = result.address_components .find(function (el) { return el.types.some(function (type) { return type === 'route'; }); }); if (streetAddress) { newAddress.street += streetAddress.long_name; } _this.address = newAddress; _this.selected.emit(newAddress); }); }); } }; __decorate([ core_1.Input(), __metadata("design:type", Object) ], FsAddressSearchComponent.prototype, "address", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], FsAddressSearchComponent.prototype, "selected", void 0); __decorate([ core_1.ViewChild('search'), __metadata("design:type", core_1.ElementRef) ], FsAddressSearchComponent.prototype, "searchElement", void 0); FsAddressSearchComponent = __decorate([ core_1.Component({ selector: 'fs-address-search', templateUrl: './fs-address-search.component.html', styleUrls: ['./fs-address-search.component.css'], }), __metadata("design:paramtypes", [core_2.MapsAPILoader, core_1.NgZone]) ], FsAddressSearchComponent); return FsAddressSearchComponent; }()); exports.FsAddressSearchComponent = FsAddressSearchComponent; //# sourceMappingURL=fs-address-search.component.js.map