@firestitch/address
Version:
@firestitch/address
227 lines • 9.58 kB
JavaScript
"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 lodash_1 = require("lodash");
var core_2 = require("@agm/core");
require("rxjs/add/operator/startWith");
require("rxjs/add/operator/map");
var countries_1 = require("../../constants/countries");
var FsAddressComponent = (function () {
function FsAddressComponent() {
this.address = {};
this.config = {};
this.change = new core_1.EventEmitter();
this.isSearched = false;
this.countries = countries_1.COUNTRIES.slice() || [];
this.regions = [];
}
FsAddressComponent.prototype.ngOnInit = function () {
var _this = this;
this.initAddress();
this.initConfig();
this.initMap();
this.initCountries();
this.initRegions();
this.initZipAndStateLabels();
// Example ready event. Allow to use google object and map instance
if (this.agmMap) {
this._subMapReady = this.agmMap
.mapReady
.subscribe(function (map) {
_this.agmMap.triggerResize();
if (_this.address.name ||
_this.address.country ||
_this.address.region ||
_this.address.city ||
_this.address.zip) {
_this.address.lat = 9999;
_this.address.lng = 9999;
_this.search();
}
});
}
};
FsAddressComponent.prototype.ngOnDestroy = function () {
if (this.agmMap) {
this._subMapReady.unsubscribe();
}
};
FsAddressComponent.prototype.recenter = function () {
var _this = this;
this.config.map.center = { latitude: this.address.lat, longitude: this.address.lng };
this.config.map.marker.coords.latitude = this.address.lat;
this.config.map.marker.coords.longitude = this.address.lng;
this.agmMap.triggerResize()
.then(function () { return _this.agmMap._mapsWrapper.setCenter({ lat: _this.address.lat, lng: _this.address.lng }); });
};
FsAddressComponent.prototype.changeCountry = function () {
var country = lodash_1.filter(countries_1.COUNTRIES, { code: this.address.country })[0];
this.regions = country && country.regions ? country.regions : [];
this.updateCountryRegionLabels();
this.search();
};
FsAddressComponent.prototype.changeRegion = function () {
var country = lodash_1.filter(countries_1.COUNTRIES, { code: this.address.country })[0];
if (country && country.regions) {
var region = lodash_1.filter(country.regions, { code: this.address.region })[0];
this.address.region = region.code;
}
this.search();
};
FsAddressComponent.prototype.search = function (event) {
var _this = this;
if (event) {
event.stopPropagation();
}
var geocoder = new google.maps.Geocoder();
var parts = [
this.address.country,
this.address.region,
this.address.city,
this.address.zip,
this.address.street,
this.address.name
];
this.searchedAddress = parts.filter(function (part) { return part; }).join(', ');
geocoder.geocode({ address: this.searchedAddress }, function (results, status) {
_this.isSearched = true;
if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
var location_1 = results[0].geometry.location;
_this.address.description = results[0].formatted_address;
_this.address.lat = location_1.lat();
_this.address.lng = location_1.lng();
_this.config.map.center = { latitude: parseFloat(location_1.lat()), longitude: parseFloat(location_1.lng()) };
_this.config.map.marker.coords.latitude = location_1.lat();
_this.config.map.marker.coords.longitude = location_1.lng();
if (_this.agmMap) {
_this.agmMap.triggerResize();
}
}
else {
_this.address.lat = null;
_this.address.lng = null;
}
_this.change.emit(_this.address);
});
};
FsAddressComponent.prototype.initAddress = function () {
this.address = Object.assign({
name: void 0,
country: void 0,
region: void 0,
street: void 0,
city: void 0,
zip: void 0,
lat: null,
lng: null,
}, this.address);
};
FsAddressComponent.prototype.initConfig = function () {
this.config = Object.assign({
name: { required: false, visible: true },
country: { required: false, visible: true },
region: { required: true, visible: true },
city: { required: true, visible: true },
street: { required: false, visible: true },
zip: { required: true, visible: true },
}, this.config);
};
FsAddressComponent.prototype.initMap = function () {
var _this = this;
this.config.map = Object.assign({
showMap: true,
center: {
latitude: this.address.lat || 9999,
longitude: this.address.lng || 9999
},
zoom: 13,
scrollwheel: false,
streetViewControl: false,
zoomControl: false,
mapTypeControlOptions: { mapTypeIds: [] },
marker: {
id: 0,
coords: { latitude: this.address.lat, longitude: this.address.lng },
options: { draggable: true },
events: {
dragend: function (marker) {
_this.address.lat = marker.coords.lat;
_this.address.lng = marker.coords.lng;
}
}
}
}, this.config.map);
};
FsAddressComponent.prototype.initCountries = function () {
var _this = this;
if (this.config.country && this.config.country.list && this.config.country.list.length) {
this.countries.length = 0;
this.config.country.list.forEach(function (el) {
var country = countries_1.COUNTRIES.find(function (countryEl) { return countryEl.code === el; });
if (country) {
_this.countries.push(country);
}
});
}
if (this.countries.length && !this.address.country) {
this.address.country = this.countries[0].code;
}
};
FsAddressComponent.prototype.initRegions = function () {
var _this = this;
if (this.address.country && this.address.country) {
var country = countries_1.COUNTRIES.find(function (countryEl) { return countryEl.code === _this.address.country; });
if (country) {
this.regions = country['regions'] || [];
}
}
};
FsAddressComponent.prototype.initZipAndStateLabels = function () {
var country = lodash_1.filter(countries_1.COUNTRIES, { code: this.address.country })[0];
this.updateCountryRegionLabels();
};
FsAddressComponent.prototype.updateCountryRegionLabels = function () {
this.zipLabel = this.address.country === 'CA' ? 'Postal Code' : 'Zip';
this.regionLabel = this.address.country === 'CA' ? 'Province' : 'State';
};
__decorate([
core_1.ViewChild(core_2.AgmMap),
__metadata("design:type", Object)
], FsAddressComponent.prototype, "agmMap", void 0);
__decorate([
core_1.ViewChild(core_2.AgmMarker),
__metadata("design:type", Object)
], FsAddressComponent.prototype, "agmMarker", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Object)
], FsAddressComponent.prototype, "address", void 0);
__decorate([
core_1.Input(),
__metadata("design:type", Object)
], FsAddressComponent.prototype, "config", void 0);
__decorate([
core_1.Output(),
__metadata("design:type", Object)
], FsAddressComponent.prototype, "change", void 0);
FsAddressComponent = __decorate([
core_1.Component({
selector: 'fs-address',
templateUrl: './fs-address.component.html',
styleUrls: ['./fs-address.component.css'],
}),
__metadata("design:paramtypes", [])
], FsAddressComponent);
return FsAddressComponent;
}());
exports.FsAddressComponent = FsAddressComponent;
//# sourceMappingURL=fs-address.component.js.map