import { Component, Injector, Input, Output, EventEmitter, OnInit, AfterViewInit, ViewChild } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { CreateAddressInput, AddressTypeServiceProxy, PostalCodeServiceProxy, UpdateAddressInput } from '@shared/service-proxies/service-proxies'; import * as countries from '@app/sprintship/countries.json'; import { AutoComplete } from 'primeng/primeng'; import { GooglePlaceDirective } from 'ngx-google-places-autocomplete'; import { Address } from 'ngx-google-places-autocomplete/objects/address'; @Component({ selector: 'addressForm', templateUrl: './address-form.component.html', styleUrls: ['address-form.component.less'] }) export class AddressFormComponent extends AppComponentBase { @Input() address: UpdateAddressInput = new UpdateAddressInput(); @Input('selectedCountry') selectedCountry: string; @Input('selectedCity') selectedCity: string; @Input('selectedState') selectedState: string; @Input('postalCodeId') selectedPostalCodeId: number; @Input('postalCodeValue') selectedPostalCodeValue: string; options = { componentRestriction: {} }; // @Input('country') country: string; // @Input('address1Input') address1InputValue:string; // @Input('address2Input') address2InputValue:string; // @Input('addressType') addressTypeValue: string; constructor( injector: Injector, // private _countryService: CountryServiceProxy, // private _cityService: CityServiceProxy, // private _stateService: StateServiceProxy, private _addressTypeService: AddressTypeServiceProxy, private _postalCodeService: PostalCodeServiceProxy ) { super(injector); } @ViewChild('placesRef', { static: true }) placesRef: GooglePlaceDirective; filteredCountries: any = countries.default; filteredCities: any; filteredStates: any; filteredPostal: any; filteredAddressType: any; selectedPostalCode: string; postals:any; postalComplete: any; filterChange = false postalDontExistsFlag : boolean city: string; state: string; country: string; value: string; stateFeature: boolean; street: string; route: string; plusCode: string; neightborhood: string; sublocality: string; corner: string; latitude: number; longitude: number; formattedAddress: string; placeExist: boolean = false; ngOnInit() { this.stateFeature = abp.features.isEnabled('App.StateFeature'); // console.log(this.stateFeature) if(!this.stateFeature){ this.state=""; } var that = this; // $('.kt-select2').select2(); let maxcount = 1000; this.filterAddressTypes(maxcount); // this.filterCountries(undefined); // console.log(this.selectedCountry) $("#countrySelectInput").change(function () { that.country = $("#countrySelectInput option:selected").text(); that.country = $.trim(that.country) }); // console.log(this.selectedCountry) // console.log(this.address) if(this.address){ this.placeExist = true; } } ngAfterViewInit(){ // this.filterCountries(this.selectedPostalCodeValue) // console.log(this.selectedPostalCodeValue) } ngOnChanges(){ // if(this.country){ // $('.kt-select2').select2(); // console.log("change"+this.country) // } // this.filterPostal(); // if(this.selectedPostalCodeValue){ // this.filterCountries(this.selectedPostalCodeValue) // this.filterStates(this.selectedPostalCodeValue) // this.filterCities(this.selectedPostalCodeValue) // } // if(this.country){ // console.log("hello") // } } autoCompletePostal(event){ this._postalCodeService.getAllPostalCodeAutoComplete(this.postalComplete,undefined) .subscribe(result =>{ this.postals = result.items }) } getPostalId(){ if(this.postalComplete.id == undefined){ this.selectedPostalCodeId = null this.postalDontExistsFlag = true }else{ this.postalDontExistsFlag = false this.selectedPostalCodeId = this.postalComplete.id // this.filterCountries(this.postalComplete.value) // this.filterStates(this.postalComplete.value) // this.filterCities(this.postalComplete.value) } } backAutoComplete(){ this.postalDontExistsFlag = false this.postalComplete = null } filterPostal(): void { this._postalCodeService.getDistinctPostalCode( undefined ).subscribe(result => { this.filteredPostal = result.items; }); } filterCountries(postalCode): void { this._postalCodeService.getDistinctCountry( undefined, undefined ).subscribe(result => { this.filteredCountries = result.items; $("#selectedCountry").text(this.filteredCountries[0]['country']); }); } filterStates(postalCode): void { this._postalCodeService.getDistinctState( postalCode, undefined ).subscribe(result => { this.filteredStates = result.items; $("#selectedState").text(this.filteredStates[0]['state']); }); } filterCities(postalCode): void { this._postalCodeService.getDistinctCity( postalCode, undefined ).subscribe(result => { this.filteredCities = result.items; $("#selectedCity").text(this.filteredCities[0]['city']); }); } filterAddressTypes(maxcount): void { this._addressTypeService.getAddressType( undefined, undefined, undefined, undefined, maxcount, undefined ).subscribe(result => { this.filteredAddressType = result.items; }); } public handleAddressChange(address: Address) { //initialize this.value = ''; this.country = ''; this.state = ''; this.city = ''; this.street = ''; this.route = ''; this.address.addressLine1 = ''; this.neightborhood = ''; this.sublocality = ''; this.corner = ''; // this.formattedAddress = ''; this.plusCode = ''; if(address.formatted_address!=undefined){ for (var data in address.address_components) { for (var record in address.address_components[data].types) { switch (address.address_components[data].types[record]) { case 'postal_code': this.value = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'country' || 'political': this.country = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'administrative_area_level_1': this.state = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'locality' || 'postal_town' || 'administrative_area_level_3': this.city = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'street_number' || 'premise': this.street = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'route': this.route = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'plus_code': this.plusCode = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'neighborhood': this.neightborhood = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'sublocality': this.sublocality = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; case 'corner': this.corner = address.address_components[data].long_name == undefined ? '' : address.address_components[data].long_name; break; } } } this.corner = this.corner == undefined || this.corner == '' || this.corner == ' ' ? '' : this.corner + ' '; this.plusCode = this.plusCode == undefined || this.plusCode == '' || this.plusCode == ' ' ? '' : this.plusCode + ' '; this.street = this.street == undefined || this.street == '' || this.street == ' ' ? '' : this.street + ' '; this.route = this.route == undefined || this.route == '' || this.route == ' ' ? '' : this.route + ' '; this.neightborhood = this.neightborhood == undefined || this.neightborhood == '' || this.neightborhood == ' ' ? '' : this.neightborhood + ', '; this.sublocality = this.sublocality == undefined || this.sublocality == '' || this.sublocality == ' ' ? '' : this.sublocality + ', '; this.address.addressLine1 = this.plusCode + this.corner + this.street + this.route + this.sublocality + this.sublocality; this.formattedAddress = address.formatted_address; this.longitude = address.geometry.location.lng(); this.latitude = address.geometry.location.lat(); setTimeout(function() { $('.kt-select2').select2(); }, 0); this.placeExist = true; } } onSubmitPickup(): void { try { if(this.placesRef.place.address_components == undefined || this.placesRef.place.address_components==null){ this.placeExist = false; }else{ this.placeExist = true; } } catch(e) { if (this.formattedAddress != '' && this.formattedAddress != undefined) { this.placeExist = true; } else { this.placeExist = false; } } } }