Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 10x 10x 10x | import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { FieldType, FieldTypeConfig, FormlyAttributes, FormlyConfig } from '@ngx-formly/core';
import { getErrorMessage } from './errors';
@Component({
selector: 'formly-field-location',
host: { 'class': 'field location-field' },
template: `
<div class="form-array">
<input type="number"
class="grow"
placeholder="Longitude"
i18n-placeholder
min="-180"
max="180"
step="any"
aria-label="Longitude"
i18n-aria-label
[value]="lng"
[disabled]="formControl.disabled"
(input)="setLng($any($event.target).value)"
(blur)="blur($any($event.target))"
[formlyAttributes]="field"
[class.is-invalid]="showError">
<input type="number"
class="grow"
placeholder="Latitude"
i18n-placeholder
min="-90"
max="90"
step="any"
[id]="field.id + '-lat'"
[name]="(field.name || field.id) + '-lat'"
aria-label="Latitude"
i18n-aria-label
[value]="lat"
[disabled]="formControl.disabled"
(input)="setLat($any($event.target).value)"
(blur)="blur($any($event.target))"
[class.is-invalid]="showError">
<button type="button"
title="Use current location"
i18n-title
aria-label="Use current location"
i18n-aria-label
[disabled]="formControl.disabled"
(click)="detectLocation()"
i18n>📍️</button>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
ReactiveFormsModule,
FormlyAttributes,
],
})
export class FormlyFieldLocationI extends FieldType<FieldTypeConfig> {
private showedError = false;
constructor(
private config: FormlyConfig,
private cd: ChangeDetectorRef,
) {
super();
}
get lng(): number {
return this.formControl.value?.[0] ?? 0;
}
get lat(): number {
return this.formControl.value?.[1] ?? 0;
}
setLng(value: string) {
const lng = parseFloat(value);
if (!isNaN(lng)) {
this.formControl.setValue([lng, this.lat]);
this.formControl.markAsDirty();
}
}
setLat(value: string) {
const lat = parseFloat(value);
if (!isNaN(lat)) {
this.formControl.setValue([this.lng, lat]);
this.formControl.markAsDirty();
}
}
detectLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
pos => {
this.formControl.setValue([pos.coords.longitude, pos.coords.latitude]);
this.formControl.markAsDirty();
this.cd.markForCheck();
},
err => console.error('Geolocation error:', err.message),
);
}
}
validate(input: HTMLInputElement) {
if (this.showError) {
input.setCustomValidity(getErrorMessage(this.field, this.config));
input.reportValidity();
}
}
blur(input: HTMLInputElement) {
if (this.showError && !this.showedError) {
this.showedError = true;
this.validate(input);
} else {
this.showedError = false;
}
}
}
|