Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/packages / core / location / google / LocationContainer.js
Size: Mime:
import * as tslib_1 from "tslib";
import { observable, action } from 'xmobx/mobx';
import { isNonEmptyArray } from 'exotic';
const state = {
    permissions: {
        // @todo here check if isError when accessing
        get location() {
            return typeof window === 'object' && navigator.geolocation;
        },
    },
    get hasLocation() {
        return typeof window === 'object' && 'geolocation' in navigator;
    },
};
/**
 * @alias ToggleGeoLocation
 * @alias LocationStore
 */
class LocationContainer {
    constructor() {
        this.longitude = 0;
        this.latitude = 0;
        this.isLocationEnabled = false;
        this.results = [];
        this.zipCode = '';
        /**
         * @api https://developers.google.com/maps/documentation/javascript/geocoding
         */
        this.fromCoordinatesToGeocoder = () => {
            if (typeof google === 'object') {
                const geocoder = new google.maps.Geocoder();
                return Promise.resolve(geocoder);
            }
            else {
                return new Promise(resolve => setTimeout(this.fromCoordinatesToGeocoder, 1000));
            }
        };
    }
    /**
     * @type {Computed}
     * @alias geocoderLongLat
     */
    get latlng() {
        const latlng = new google.maps.LatLng(this.latitude, this.longitude);
        return {
            latLng: latlng,
        };
    }
    handleGeoSuccess(position) {
        this.latitude = position.coords.latitude;
        this.longitude = position.coords.longitude;
        this.updateZipCode();
    }
    /**
     * @todo HAS TO DO SOMETHING
     * @description @modifies this.isLocationEnabled
     */
    handleGeoFailure() {
        this.isLocationEnabled = false;
        this.callDone(undefined, this.isLocationEnabled);
    }
    /**
     * not sure about this
     */
    updateGeoLocation() {
        return new Promise(resolve => {
            this.callDone = resolve;
            if (state.hasLocation && state.permissions.location) {
                navigator.geolocation.getCurrentPosition(this.handleGeoSuccess, this.handleGeoFailure);
            }
            else {
                console.debug('Geolocation is not supported by your browser');
            }
        });
    }
    /**
     * @description @modifies this.status based on location results
     */
    handleLocation(addressComponent) {
        // types[] is an array indicating the type of the address component.
        // this.results[0].address_components[componentIndex]
        const isTypeWeWant = (type) => type === 'postal_code';
        const filteredTypesThatSeemCopyPasted = addressComponent.types.filter(isTypeWeWant);
        if (isNonEmptyArray(filteredTypesThatSeemCopyPasted)) {
            this.isLocationEnabled = true;
            this.zipCode = addressComponent.long_name;
        }
    }
    /**
     * @description @modifies this.results &
     */
    handleGeocoderResults(results, status) {
        this.results = results;
        const IS_OK = status === google.maps.GeocoderStatus.OK;
        if (IS_OK) {
            // console.log('Results: ', results)
            if (isNonEmptyArray(results)) {
                results[0].address_components.forEach(this.handleLocation);
                this.callDone(this.zipCode, this.isLocationEnabled);
            }
            else {
                console.log('Geocode was not successful for the following reason: ' + status);
            }
        }
    }
    /**
     * @borrows {GeoCoder.geocode}
     * @private
     * @see this.latlng
     * @see this.fromCoordinatesToGeocoder
     * @see this.handleGeocoderResults
     */
    async extractZipCode() {
        const geocoder = await this.fromCoordinatesToGeocoder();
        geocoder.geocode(this.latlng, this.handleGeocoderResults);
    }
    /**
     * @see this.extractZipCode
     */
    updateZipCode() {
        this.extractZipCode();
    }
}
tslib_1.__decorate([
    observable
], LocationContainer.prototype, "longitude", void 0);
tslib_1.__decorate([
    observable
], LocationContainer.prototype, "latitude", void 0);
tslib_1.__decorate([
    observable
], LocationContainer.prototype, "isLocationEnabled", void 0);
tslib_1.__decorate([
    observable
], LocationContainer.prototype, "results", void 0);
tslib_1.__decorate([
    observable
], LocationContainer.prototype, "zipCode", void 0);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "handleGeoSuccess", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "handleGeoFailure", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "updateGeoLocation", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "handleLocation", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "handleGeocoderResults", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "extractZipCode", null);
tslib_1.__decorate([
    action.bound
], LocationContainer.prototype, "updateZipCode", null);
const locationContainerObj = new LocationContainer();
export { LocationContainer, locationContainerObj };
export { locationContainerObj as locationContainer };
export default locationContainerObj;
//# sourceMappingURL=LocationContainer.js.map