packages/location/src/lib/location-picker.component.ts
Component with map and autocomplete input to pick a location. Implements ControlValueAccessor
providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => LocationPickerComponent), multi: true,
}
|
selector | ec-location-picker |
styleUrls | ./location-picker.component.scss |
templateUrl | ./location-picker.component.html |
Properties |
|
Methods |
Inputs |
constructor(geocodeService: GeocodeService, formService: FormService, keycommands: KeycommandsService)
|
||||||||||||
Parameters :
|
disabled | |
Type : boolean
|
|
readonly? |
formControl | |
Type : FormControl
|
|
The form control that holds the location |
placeholder | |
Placeholder for search input |
showRawValue | |
Default value : true
|
|
If true, the raw location value will be visible |
registerOnChange | ||||
registerOnChange(fn)
|
||||
Parameters :
Returns :
void
|
registerOnTouched |
registerOnTouched()
|
Returns :
void
|
setDisabledState | ||||
setDisabledState(isDisabled)
|
||||
Parameters :
Returns :
void
|
setValue | |||||||||
setValue(value, fromSearch?: boolean)
|
|||||||||
Sets value of map and propagates change
Parameters :
Returns :
void
|
updateAddress |
updateAddress()
|
updates the address string by reverse geo lookup
Returns :
void
|
writeValue | ||||||
writeValue(value: any)
|
||||||
Writes value to editor on outside model change.
Parameters :
Returns :
void
|
focus | ||||||
focus(focus)
|
||||||
Inherited from
DefaultInputComponent
|
||||||
Defined in
DefaultInputComponent:34
|
||||||
Parameters :
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:28
|
Returns :
void
|
Public formService |
Type : FormService
|
Public geocodeService |
Type : GeocodeService
|
input |
Type : InputComponent
|
Form input component |
Public keycommands |
Type : KeycommandsService
|
map |
Type : LocationMapComponent
|
Decorators :
@ViewChild(LocationMapComponent, {static: true})
|
The nested LocationMapComponent |
propagateChange |
Default value : () => {...}
|
search |
Type : LocationSearchComponent
|
Decorators :
@ViewChild(LocationSearchComponent, {static: true})
|
The nested LocationSearchComponent |
Public control |
Type : FormControl
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:21
|
The form control that is used |
Public field |
Type : Field
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:15
|
The field for which the input is meant. |
focusEvent |
Type : EventEmitter<boolean>
|
Default value : new EventEmitter()
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:23
|
Emitter to focus the input field |
Public formService |
Type : FormService
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:26
|
Public group |
Type : FormGroup
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:19
|
The form group that is used |
Public item |
Type : Item<any>
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:17
|
The item that is targeted by the input |
Public keycommands |
Type : KeycommandsService
|
Inherited from
DefaultInputComponent
|
Defined in
DefaultInputComponent:26
|
import { Component, forwardRef, Input, ViewChild } from '@angular/core';
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
import { DefaultInputComponent, FormService, InputComponent, KeycommandsService } from '@ec.components/ui';
import { GeocodeService } from './geocode.service';
import { LocationMapComponent } from './location-map.component';
import { LocationSearchComponent } from './location-search.component';
/** Component with map and autocomplete input to pick a location. Implements ControlValueAccessor */
@Component({
selector: 'ec-location-picker',
templateUrl: './location-picker.component.html',
styleUrls: ['./location-picker.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => LocationPickerComponent),
multi: true,
},
],
})
export class LocationPickerComponent extends DefaultInputComponent implements ControlValueAccessor {
/** The form control that holds the location */
@Input() formControl: FormControl;
/** If true, the raw location value will be visible*/
@Input() showRawValue = true;
/** Placeholder for search input */
@Input() placeholder;
/** The nested LocationMapComponent */
@ViewChild(LocationMapComponent, { static: true }) map: LocationMapComponent;
/** The nested LocationSearchComponent */
@ViewChild(LocationSearchComponent, { static: true }) search: LocationSearchComponent;
/** Form input component */
input: InputComponent;
/** readonly? */
@Input() disabled: boolean;
constructor(
public geocodeService: GeocodeService,
public formService: FormService,
public keycommands: KeycommandsService
) {
super(formService, keycommands);
}
/** Sets value of map and propagates change */
setValue(value, fromSearch?: boolean) {
if (!value) {
this.search.clear();
}
this.map.setValue(value);
if (!fromSearch) {
this.updateAddress();
}
this.propagateChange(value);
}
/** Writes value to editor on outside model change. */
writeValue(value: any) {
this.map.setValue(value);
this.updateAddress();
}
/** updates the address string by reverse geo lookup */
updateAddress() {
const value = this.map.value;
if (!value) {
this.search.searchInput.nativeElement.value = '';
return;
}
this.geocodeService.getNearestAddress(value).then((results) => {
if (results.length) {
this.search.searchInput.nativeElement.value = results[0].formatted_address;
} else {
this.search.searchInput.nativeElement.value = '';
}
});
}
propagateChange = (_: any) => {};
registerOnChange(fn) {
this.propagateChange = fn;
}
registerOnTouched() {}
setDisabledState(isDisabled) {
this.disabled = isDisabled;
}
}
<ec-location-search [placeholder]="field?.getPlaceholder()||placeholder" (changed)="setValue($event,true)" [disabled]="disabled"></ec-location-search>
<ec-location-map (changed)="setValue($event)" [disabled]="disabled"></ec-location-map>
<a class="btn btn_square" (click)="setValue(null)" *ngIf="map.value&&!disabled">
<ec-icon name="trash"></ec-icon>
</a>
<small *ngIf="showRawValue&&map?.value">{{map.value | json}}</small>
./location-picker.component.scss