UNPKG

@angular-material-extensions/google-maps-autocomplete

Version:

Autocomplete input component and directive for google-maps built with angular and material design

915 lines (880 loc) 51.9 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/forms'), require('@agm/core'), require('@angular/animations'), require('@angular/common'), require('@angular/flex-layout'), require('@angular/material/icon'), require('@angular/material/input')) : typeof define === 'function' && define.amd ? define('@angular-material-extensions/google-maps-autocomplete', ['exports', '@angular/core', '@angular/forms', '@agm/core', '@angular/animations', '@angular/common', '@angular/flex-layout', '@angular/material/icon', '@angular/material/input'], factory) : (global = global || self, factory((global['angular-material-extensions'] = global['angular-material-extensions'] || {}, global['angular-material-extensions']['google-maps-autocomplete'] = {}), global.ng.core, global.ng.forms, global.core$1, global.ng.animations, global.ng.common, global.ng.flexLayout, global.ng.material.icon, global.ng.material.input)); }(this, (function (exports, core, forms, core$1, animations, common, flexLayout, icon, input) { '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. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; function __extends(d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); } var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(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; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __createBinding(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; } function __exportStar(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) exports[p] = m[p]; } function __values(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result.default = mod; return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } function __classPrivateFieldGet(receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); } function __classPrivateFieldSet(receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; } // https://github.com/angular/angular/blob/master/packages/forms/src/directives/validators.ts var MatValidateAddressDirective = /** @class */ (function () { function MatValidateAddressDirective() { } MatValidateAddressDirective_1 = MatValidateAddressDirective; MatValidateAddressDirective.prototype.validate = function () { var _this = this; return function (control) { return _this.address ? null : { validateAddress: { valid: false } }; }; }; MatValidateAddressDirective.prototype.subscribe = function (eventEmitter) { var _this = this; this.subscription = eventEmitter.subscribe(function (address) { _this.address = address; }); }; MatValidateAddressDirective.prototype.unsubscribe = function () { this.subscription.unsubscribe(); }; Object.defineProperty(MatValidateAddressDirective.prototype, "address", { get: function () { return this._address; }, set: function (value) { this._address = value; }, enumerable: true, configurable: true }); var MatValidateAddressDirective_1; MatValidateAddressDirective = MatValidateAddressDirective_1 = __decorate([ core.Directive({ selector: '[mat-address-validate][formControlName],[MatValidateAddress][formControl],[MatValidateAddress][ngModel]', providers: [ { provide: forms.NG_VALIDATORS, useExisting: core.forwardRef(function () { return MatValidateAddressDirective_1; }), multi: true } ] }) ], MatValidateAddressDirective); return MatValidateAddressDirective; }()); (function (Appearance) { Appearance["STANDARD"] = "standard"; Appearance["FILL"] = "fill"; Appearance["OUTLINE"] = "outline"; Appearance["LEGACY"] = "legacy"; })(exports.Appearance || (exports.Appearance = {})); var MatGoogleMapsAutocompleteComponent = /** @class */ (function () { function MatGoogleMapsAutocompleteComponent(mapsAPILoader, ngZone) { this.mapsAPILoader = mapsAPILoader; this.ngZone = ngZone; this.addressLabelText = 'Address'; this.placeholderText = 'Please enter the address'; this.requiredErrorText = 'The address is required'; this.invalidErrorText = 'The address is not valid'; this.appearance = exports.Appearance.STANDARD; this.autoCompleteOptions = {}; this.onChange = new core.EventEmitter(); this.onAutocompleteSelected = new core.EventEmitter(); this.onGermanAddressMapped = new core.EventEmitter(); this.onLocationSelected = new core.EventEmitter(); this.onNewPlaceResult = new core.EventEmitter(); this.addressValidator = new MatValidateAddressDirective(); this.addressSearchControl = new forms.FormControl({ value: null }, forms.Validators.compose([ forms.Validators.required, this.addressValidator.validate() ])); this.propagateChange = function (_) { }; } MatGoogleMapsAutocompleteComponent_1 = MatGoogleMapsAutocompleteComponent; MatGoogleMapsAutocompleteComponent.prototype.ngOnInit = function () { this.addressValidator.subscribe(this.onNewPlaceResult); var options = { // types: ['address'], // componentRestrictions: {country: this.country}, placeIdOnly: this.placeIdOnly, strictBounds: this.strictBounds, // types: this.types, type: this.type }; // tslint:disable-next-line:no-unused-expression this.country ? options.componentRestrictions = { country: this.country } : null; // tslint:disable-next-line:no-unused-expression this.country ? options.types = this.types : null; this.autoCompleteOptions = Object.assign(this.autoCompleteOptions, options); this.initGoogleMapsAutocomplete(); }; MatGoogleMapsAutocompleteComponent.prototype.initGoogleMapsAutocomplete = function () { var _this = this; this.mapsAPILoader .load() .then(function () { var autocomplete = new google.maps.places.Autocomplete(_this.searchElementRef.nativeElement, _this.autoCompleteOptions); autocomplete.addListener('place_changed', function () { _this.ngZone.run(function () { // get the place result var place = autocomplete.getPlace(); var germanAddress = { gmID: place.id, icon: place.icon, url: place.url, placeID: place.place_id, displayAddress: place.formatted_address, name: place.name, vicinity: place.vicinity, locality: {}, state: {}, country: {}, geoLocation: { latitude: -1, longitude: -1 }, }; if (place.geometry && place.geometry.location) { germanAddress.geoLocation.latitude = place.geometry.location.lat(); germanAddress.geoLocation.longitude = place.geometry.location.lng(); } place.address_components.forEach(function (value) { if (value.types.indexOf('street_number') > -1) { germanAddress.streetNumber = value.short_name; } if (value.types.indexOf('route') > -1) { germanAddress.streetName = value.long_name; } if (value.types.indexOf('postal_code') > -1) { germanAddress.postalCode = Number(value.short_name); } if (value.types.indexOf('sublocality') > -1) { germanAddress.sublocality = value.long_name; } if (value.types.indexOf('locality') > -1) { germanAddress.locality.long = value.long_name; germanAddress.locality.short = value.short_name; } if (value.types.indexOf('administrative_area_level_1') > -1) { germanAddress.state.long = value.long_name; germanAddress.state.short = value.short_name; } if (value.types.indexOf('country') > -1) { germanAddress.country.long = value.long_name; germanAddress.country.short = value.short_name; } if (value.types.indexOf('administrative_area_level_3') > -1) { germanAddress.locality.short = value.short_name; } }); _this.onGermanAddressMapped.emit(germanAddress); if (!place.place_id || place.geometry === undefined || place.geometry === null) { // place result is not valid return; } else { // show dialog to select a address from the input // emit failed event _this.value = place; _this.propagateChange(_this.value); } _this.address = place.formatted_address; _this.onAutocompleteSelected.emit(place); // console.log('onAutocompleteSelected -> ', place); _this.onLocationSelected.emit({ latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng() }); }); }); }) .catch(function (err) { return console.log(err); }); }; MatGoogleMapsAutocompleteComponent.prototype.onQuery = function (event) { // console.log('onChange()', event); this.onChange.emit(this.address); }; MatGoogleMapsAutocompleteComponent.prototype.resetAddress = function () { this.address = null; this.addressSearchControl.updateValueAndValidity(); }; MatGoogleMapsAutocompleteComponent.prototype.writeValue = function (obj) { if (obj) { this.value = obj; } }; MatGoogleMapsAutocompleteComponent.prototype.registerOnChange = function (fn) { this.propagateChange = fn; }; MatGoogleMapsAutocompleteComponent.prototype.registerOnTouched = function (fn) { throw new Error('Method not implemented.'); }; MatGoogleMapsAutocompleteComponent.prototype.setDisabledState = function (isDisabled) { throw new Error('Method not implemented.'); }; var MatGoogleMapsAutocompleteComponent_1; MatGoogleMapsAutocompleteComponent.ctorParameters = function () { return [ { type: core$1.MapsAPILoader }, { type: core.NgZone } ]; }; __decorate([ core.ViewChild('search') ], MatGoogleMapsAutocompleteComponent.prototype, "searchElementRef", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "addressLabelText", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "placeholderText", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "requiredErrorText", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "invalidErrorText", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "appearance", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "value", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "address", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "country", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "placeIdOnly", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "strictBounds", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "types", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "type", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteComponent.prototype, "autoCompleteOptions", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteComponent.prototype, "onChange", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteComponent.prototype, "onAutocompleteSelected", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteComponent.prototype, "onGermanAddressMapped", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteComponent.prototype, "onLocationSelected", void 0); MatGoogleMapsAutocompleteComponent = MatGoogleMapsAutocompleteComponent_1 = __decorate([ core.Component({ selector: 'mat-google-maps-autocomplete', exportAs: 'matGoogleMapsAutocomplete', template: "<mat-form-field class=\"full-width\" [appearance]=\"appearance\">\n <mat-label>{{addressLabelText}}</mat-label>\n <input matInput\n [(ngModel)]=\"address\"\n (change)=\"onQuery($event)\"\n placeholder=\"{{placeholderText}}\"\n class=\"form-control\"\n #search\n MatValidateAddress\n required>\n <mat-error *ngIf=\"addressSearchControl.hasError('required')\">\n {{requiredErrorText}}\n </mat-error>\n <mat-error *ngIf=\"addressSearchControl.hasError('validateAddress')\">\n {{invalidErrorText}}\n </mat-error>\n</mat-form-field>\n", providers: [ { provide: forms.NG_VALUE_ACCESSOR, useExisting: core.forwardRef(function () { return MatGoogleMapsAutocompleteComponent_1; }), multi: true } ], styles: [".full-width{width:100%}"] }) ], MatGoogleMapsAutocompleteComponent); return MatGoogleMapsAutocompleteComponent; }()); function parseGermanAddress(placeResult) { var germanAddress = { gmID: placeResult.id, icon: placeResult.icon, url: placeResult.url, placeID: placeResult.place_id, displayAddress: placeResult.formatted_address, name: placeResult.name, vicinity: placeResult.vicinity, locality: {}, state: {}, country: {}, geoLocation: { latitude: -1, longitude: -1 }, }; if (placeResult.geometry && placeResult.geometry.location) { germanAddress.geoLocation.latitude = placeResult.geometry.location.lat(); germanAddress.geoLocation.longitude = placeResult.geometry.location.lng(); } if (placeResult.address_components && placeResult.address_components.length > 0) { placeResult.address_components.forEach(function (value) { if (value.types.indexOf('street_number') > -1) { germanAddress.streetNumber = value.short_name; } if (value.types.indexOf('route') > -1) { germanAddress.streetName = value.long_name; } if (value.types.indexOf('postal_code') > -1) { germanAddress.postalCode = Number(value.short_name); } if (value.types.indexOf('sublocality') > -1) { germanAddress.sublocality = value.long_name; } if (value.types.indexOf('locality') > -1) { germanAddress.locality.long = value.long_name; germanAddress.locality.short = value.short_name; } if (value.types.indexOf('administrative_area_level_1') > -1) { germanAddress.state.long = value.long_name; germanAddress.state.short = value.short_name; } if (value.types.indexOf('country') > -1) { germanAddress.country.long = value.long_name; germanAddress.country.short = value.short_name; } if (value.types.indexOf('administrative_area_level_3') > -1) { germanAddress.locality.short = value.short_name; } }); } return germanAddress; } var customAnimation = animations.animation([ animations.style({ opacity: '{{opacity}}', transform: 'scale({{scale}}) translate3d({{x}}, {{y}}, {{z}})' }), animations.animate('{{duration}} {{delay}} cubic-bezier(0.0, 0.0, 0.2, 1)', animations.style('*')) ], { params: { duration: '200ms', delay: '0ms', opacity: '0', scale: '1', x: '0', y: '0', z: '0' } }); var InputAnimations = [ animations.trigger('animate', [animations.transition('void => *', [animations.useAnimation(customAnimation)])]), animations.trigger('animateStagger', [ animations.state('50', animations.style('*')), animations.state('100', animations.style('*')), animations.state('200', animations.style('*')), animations.transition('void => 50', animations.query('@*', [animations.stagger('50ms', [animations.animateChild()])], { optional: true })), animations.transition('void => 100', animations.query('@*', [animations.stagger('100ms', [animations.animateChild()])], { optional: true })), animations.transition('void => 200', animations.query('@*', [animations.stagger('200ms', [animations.animateChild()])], { optional: true })) ]), ]; var MatSearchGoogleMapsAutocompleteComponent = /** @class */ (function () { function MatSearchGoogleMapsAutocompleteComponent(formBuilder) { this.formBuilder = formBuilder; this.appearance = exports.Appearance.STANDARD; this.searchAddressLabel = 'Search Address'; this.streetNameLabel = 'Street'; this.streetNumberLabel = 'Nr.'; this.postalCodeLabel = 'PLZ'; this.localityLabel = 'Locality'; this.vicinityLabel = 'Vicinity'; this.onGermanAddressMapped = new core.EventEmitter(); this.firstInit = true; this.propagateChange = function (_) { }; } MatSearchGoogleMapsAutocompleteComponent_1 = MatSearchGoogleMapsAutocompleteComponent; MatSearchGoogleMapsAutocompleteComponent.prototype.ngOnInit = function () { this.createAddressFormGroup(); }; MatSearchGoogleMapsAutocompleteComponent.prototype.createAddressFormGroup = function () { this.addressFormGroup = this.formBuilder.group({ streetName: [this.value && this.value.streetName ? this.value.streetName : null, forms.Validators.required], streetNumber: [this.value && this.value.streetNumber ? this.value.streetNumber : null, forms.Validators.required], postalCode: [this.value && this.value.postalCode ? this.value.postalCode : null, forms.Validators.required], vicinity: [this.value && this.value.vicinity ? this.value.vicinity : null], locality: this.formBuilder.group({ long: [this.value && this.value.locality && this.value.locality.long ? this.value.locality.long : null, forms.Validators.required], }), }); }; MatSearchGoogleMapsAutocompleteComponent.prototype.syncAutoComplete = function ($event) { if (this.germanAddress) { this.addressFormGroup.reset(); } var germanAddress = parseGermanAddress($event); this.germanAddress = germanAddress; if (germanAddress.vicinity) { this.addressFormGroup.get('vicinity').patchValue(germanAddress.vicinity); } if (germanAddress.streetName) { this.addressFormGroup.get('streetName').patchValue(germanAddress.streetName); } if (germanAddress.streetNumber) { this.addressFormGroup.get('streetNumber').patchValue(germanAddress.streetNumber.toString()); } if (germanAddress.postalCode) { this.addressFormGroup.get('postalCode').patchValue(germanAddress.postalCode); } if (germanAddress.locality && germanAddress.locality.long) { this.addressFormGroup.get('locality.long').patchValue(germanAddress.locality.long); } this.value = germanAddress; this.propagateChange(this.value); this.onGermanAddressMapped.emit(germanAddress); }; MatSearchGoogleMapsAutocompleteComponent.prototype.writeValue = function (obj) { var shouldRecreateFG = false; if (obj) { if (!this.value && this.firstInit) { shouldRecreateFG = true; } this.value = obj; this.propagateChange(this.value); if (shouldRecreateFG) { this.createAddressFormGroup(); this.firstInit = false; } } }; MatSearchGoogleMapsAutocompleteComponent.prototype.registerOnChange = function (fn) { this.propagateChange = fn; }; MatSearchGoogleMapsAutocompleteComponent.prototype.registerOnTouched = function (fn) { }; MatSearchGoogleMapsAutocompleteComponent.prototype.setDisabledState = function (isDisabled) { }; var MatSearchGoogleMapsAutocompleteComponent_1; MatSearchGoogleMapsAutocompleteComponent.ctorParameters = function () { return [ { type: forms.FormBuilder } ]; }; __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "appearance", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "searchAddressLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "streetNameLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "streetNumberLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "postalCodeLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "localityLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "vicinityLabel", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "showVicinity", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "country", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "placeIdOnly", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "strictBounds", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "types", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "type", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "readonly", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "disableSearch", void 0); __decorate([ core.Input() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "value", void 0); __decorate([ core.Output() ], MatSearchGoogleMapsAutocompleteComponent.prototype, "onGermanAddressMapped", void 0); MatSearchGoogleMapsAutocompleteComponent = MatSearchGoogleMapsAutocompleteComponent_1 = __decorate([ core.Component({ selector: 'mat-search-google-maps-autocomplete', template: "<div fxLayout=\"column\">\n <div *ngIf=\"!disableSearch\" fxFlex=\"100\">\n <!--search address-->\n <mat-form-field fxFlex=\"auto\" [appearance]=\"appearance\" [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{searchAddressLabel}}</mat-label>\n <input\n (onAutocompleteSelected)=\"syncAutoComplete($event)\"\n [country]=\"country\"\n [placeIdOnly]=\"placeIdOnly\"\n [strictBounds]=\"strictBounds\"\n [types]=\"types\"\n [type]=\"type\"\n matGoogleMapsAutocomplete\n matInput\n required\n />\n <mat-icon color=\"primary\" matSuffix>search</mat-icon>\n <!-- <mat-error>{{ 'msa.contactData.currentAddress.error' | translate }}</mat-error>-->\n </mat-form-field>\n </div>\n\n <form [formGroup]=\"addressFormGroup\" fxFlex fxLayoutGap=\"10px\">\n <div fxLayout=\"row\" fxLayoutGap=\"10px\">\n <mat-form-field fxFlex=\"80\"\n [appearance]=\"appearance\"\n [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{streetNameLabel}}</mat-label>\n <input\n [readonly]=\"readonly\"\n formControlName=\"streetName\"\n matInput\n required\n />\n <!-- <mat-icon color=\"primary\" matSuffix>add_location</mat-icon>-->\n <!-- <mat-error>{{ 'msa.contactData.currentAddress.error' | translate }}</mat-error>-->\n </mat-form-field>\n <mat-form-field fxFlex=\"20\" [appearance]=\"appearance\" [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{streetNumberLabel}}</mat-label>\n <input\n [readonly]=\"readonly\"\n formControlName=\"streetNumber\"\n matInput\n required\n />\n <!-- <mat-icon color=\"primary\" matSuffix>add_location</mat-icon>-->\n <!-- <mat-error>{{ 'msa.contactData.currentAddress.error' | translate }}</mat-error>-->\n </mat-form-field>\n </div>\n <div fxLayout=\"row\" fxLayoutGap=\"10px\">\n <mat-form-field fxFlex=\"20\" [appearance]=\"appearance\" [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{postalCodeLabel}}</mat-label>\n <input\n [readonly]=\"readonly\"\n formControlName=\"postalCode\"\n matInput\n required\n />\n <!-- <mat-icon color=\"primary\" matSuffix>add_location</mat-icon>-->\n <!-- <mat-error>{{ 'msa.contactData.currentAddress.error' | translate }}</mat-error>-->\n </mat-form-field>\n <mat-form-field *ngIf=\"showVicinity\" fxFlex=\"auto\"\n [appearance]=\"appearance\"\n [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{vicinityLabel}}</mat-label>\n <input\n [readonly]=\"readonly\"\n matInput\n formControlName=\"vicinity\"\n />\n </mat-form-field>\n <div formGroupName=\"locality\" fxFlex=\"auto\">\n <mat-form-field fxFlex=\"auto\" [appearance]=\"appearance\" [@animate]=\"{ value: '*', params: { y: '100%' } }\">\n <mat-label>{{localityLabel}}</mat-label>\n <input\n [readonly]=\"readonly\"\n formControlName=\"long\"\n matInput\n required\n />\n <mat-icon color=\"primary\" matSuffix>add_location</mat-icon>\n <!-- <mat-error>{{ 'msa.contactData.currentAddress.error' | translate }}</mat-error>-->\n </mat-form-field>\n </div>\n </div>\n </form>\n</div>\n", animations: InputAnimations, providers: [ { provide: forms.NG_VALUE_ACCESSOR, useExisting: core.forwardRef(function () { return MatSearchGoogleMapsAutocompleteComponent_1; }), multi: true } ], styles: [""] }) ], MatSearchGoogleMapsAutocompleteComponent); return MatSearchGoogleMapsAutocompleteComponent; }()); var MatGoogleMapsAutocompleteDirective = /** @class */ (function () { function MatGoogleMapsAutocompleteDirective(platformId, elemRef, mapsAPILoader, ngZone) { this.platformId = platformId; this.elemRef = elemRef; this.mapsAPILoader = mapsAPILoader; this.ngZone = ngZone; this.autoCompleteOptions = {}; this.onChange = new core.EventEmitter(); this.onAutocompleteSelected = new core.EventEmitter(); this.onGermanAddressMapped = new core.EventEmitter(); this.onLocationSelected = new core.EventEmitter(); this.onNewPlaceResult = new core.EventEmitter(); this.addressValidator = new MatValidateAddressDirective(); this.addressSearchControl = new forms.FormControl({ value: null }, forms.Validators.compose([ forms.Validators.required, this.addressValidator.validate() ])); this.propagateChange = function (_) { }; } MatGoogleMapsAutocompleteDirective_1 = MatGoogleMapsAutocompleteDirective; MatGoogleMapsAutocompleteDirective.prototype.ngOnInit = function () { if (common.isPlatformBrowser(this.platformId)) { this.addressValidator.subscribe(this.onNewPlaceResult); var options = { // types: ['address'], // componentRestrictions: {country: this.country}, placeIdOnly: this.placeIdOnly, strictBounds: this.strictBounds, // types: this.types, type: this.type }; // tslint:disable-next-line:no-unused-expression this.country ? options.componentRestrictions = { country: this.country } : null; // tslint:disable-next-line:no-unused-expression this.country ? options.types = this.types : null; this.autoCompleteOptions = Object.assign(this.autoCompleteOptions, options); this.initGoogleMapsAutocomplete(); } }; MatGoogleMapsAutocompleteDirective.prototype.initGoogleMapsAutocomplete = function () { var _this = this; this.mapsAPILoader .load() .then(function () { var autocomplete = new google.maps.places.Autocomplete(_this.elemRef.nativeElement, _this.autoCompleteOptions); autocomplete.addListener('place_changed', function () { _this.ngZone.run(function () { // get the place result var place = autocomplete.getPlace(); var germanAddress = { gmID: place.id, icon: place.icon, url: place.url, placeID: place.place_id, displayAddress: place.formatted_address, name: place.name, vicinity: place.vicinity, locality: {}, state: {}, country: {}, geoLocation: { latitude: -1, longitude: -1 }, }; if (place.geometry && place.geometry.location) { germanAddress.geoLocation.latitude = place.geometry.location.lat(); germanAddress.geoLocation.longitude = place.geometry.location.lng(); } place.address_components.forEach(function (value) { if (value.types.indexOf('street_number') > -1) { germanAddress.streetNumber = value.short_name; } if (value.types.indexOf('route') > -1) { germanAddress.streetName = value.long_name; } if (value.types.indexOf('postal_code') > -1) { germanAddress.postalCode = Number(value.short_name); } if (value.types.indexOf('sublocality') > -1) { germanAddress.sublocality = value.long_name; } if (value.types.indexOf('locality') > -1) { germanAddress.locality.long = value.long_name; germanAddress.locality.short = value.short_name; } if (value.types.indexOf('administrative_area_level_1') > -1) { germanAddress.state.long = value.long_name; germanAddress.state.short = value.short_name; } if (value.types.indexOf('country') > -1) { germanAddress.country.long = value.long_name; germanAddress.country.short = value.short_name; } if (value.types.indexOf('administrative_area_level_3') > -1) { germanAddress.locality.short = value.short_name; } }); _this.onGermanAddressMapped.emit(germanAddress); if (!place.place_id || place.geometry === undefined || place.geometry === null) { // place result is not valid return; } else { // show dialog to select a address from the input // emit failed event _this.value = place; _this.propagateChange(_this.value); } _this.address = place.formatted_address; _this.onAutocompleteSelected.emit(place); _this.onLocationSelected.emit({ latitude: place.geometry.location.lat(), longitude: place.geometry.location.lng() }); }); }); }) .catch(function (err) { return console.log(err); }); }; MatGoogleMapsAutocompleteDirective.prototype.registerOnChange = function (fn) { this.propagateChange = fn; }; MatGoogleMapsAutocompleteDirective.prototype.registerOnTouched = function (fn) { }; MatGoogleMapsAutocompleteDirective.prototype.setDisabledState = function (isDisabled) { }; MatGoogleMapsAutocompleteDirective.prototype.writeValue = function (obj) { if (obj) { this.value = obj; } }; var MatGoogleMapsAutocompleteDirective_1; MatGoogleMapsAutocompleteDirective.ctorParameters = function () { return [ { type: String, decorators: [{ type: core.Inject, args: [core.PLATFORM_ID,] }] }, { type: core.ElementRef }, { type: core$1.MapsAPILoader }, { type: core.NgZone } ]; }; __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "address", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "country", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "placeIdOnly", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "strictBounds", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "types", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "type", void 0); __decorate([ core.Input() ], MatGoogleMapsAutocompleteDirective.prototype, "autoCompleteOptions", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteDirective.prototype, "onChange", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteDirective.prototype, "onAutocompleteSelected", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteDirective.prototype, "onGermanAddressMapped", void 0); __decorate([ core.Output() ], MatGoogleMapsAutocompleteDirective.prototype, "onLocationSelected", void 0); MatGoogleMapsAutocompleteDirective = MatGoogleMapsAutocompleteDirective_1 = __decorate([ core.Directive({ selector: '[matGoogleMapsAutocomplete]', exportAs: 'matGoogleMapsAutocomplete', providers: [ { provide: forms.NG_VALUE_ACCESSOR, useExisting: core.forwardRef(function () { return MatGoogleMapsAutocompleteDirective_1; }), multi: true } ] }), __param(0, core.Inject(core.PLATFORM_ID)) ], MatGoogleMapsAutocompleteDirective); return MatGoogleMapsAutocompleteDirective; }()); var MatGoogleMa