Skip to content

Commit

Permalink
chore: Migrate /location to camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed Feb 15, 2018
1 parent 77cc352 commit 0bf46b5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 63 deletions.
4 changes: 2 additions & 2 deletions app/script/components/deviceCard.js
Expand Up @@ -68,9 +68,9 @@ z.components.DeviceCard = class DeviceCard {

_update_location() {
if (this.device && this.device.location) {
z.location.get_location(this.device.location.lat, this.device.location.lon).then(retrieved_location => {
z.location.getLocation(this.device.location.lat, this.device.location.lon).then(retrieved_location => {
if (retrieved_location) {
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.country_code}`);
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.countryCode}`);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/script/entity/message/Location.js
Expand Up @@ -33,7 +33,7 @@ z.entity.Location = class Location extends z.entity.Asset {
this.zoom = '';

this.link_src = ko.pureComputed(() => {
return z.location.get_maps_url(this.latitude, this.longitude, this.name, this.zoom);
return z.location.getMapsUrl(this.latitude, this.longitude, this.name, this.zoom);
});
}
};
98 changes: 52 additions & 46 deletions app/script/location/GeoLocation.js
Expand Up @@ -22,31 +22,42 @@
window.z = window.z || {};

z.location = (() => {
const GOOGLE_GEOCODING_BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
const API_KEY = 'AIzaSyCKxxKw5JBZ5zEFtoirtgnw8omvH7gWzfo';
const _parse_results = results => {
const res = {};
const [result] = results;
res.address = result.formatted_address;
res.lat = result.geometry.location.lat;
res.lng = result.geometry.location.lng;
for (const component of result.address_components) {
const name = component.long_name || component.short_name;
for (const type of component.types) {
res[type] = name;
if (type === 'country') {
res.country_code = component.short_name || '';
const GOOGLE_GEOCODE_BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';

const _parseResults = ([result]) => {
const {
administrative_area_level_1: areaLevel1,
administrative_area_level_2: areaLevel2,
administrative_area_level_3: areaLevel3,
address_components: addressComponents,
formatted_address: formattedAddress,
geometry,
locality,
natural_feature: naturalFeature,
} = result;

const parsedResults = {
address: formattedAddress,
lat: geometry.location.lat,
lng: geometry.location.lng,
place: locality || naturalFeature || areaLevel3 || areaLevel2 || areaLevel1,
};

addressComponents.forEach(({long_name: longName, short_name: shortName, types}) => {
const name = longName || shortName;

types.forEach(type => {
parsedResults[type] = name;
const isCountry = type === 'country';
if (isCountry) {
parsedResults.countryCode = shortName || '';
}
}
}
res.place =
res.locality ||
res.natural_feature ||
res.administrative_area_level_3 ||
res.administrative_area_level_2 ||
res.administrative_area_level_1;
delete (res.political != null);
return z.util.ObjectUtil.escape_properties(res);
});
});

delete parsedResults.political;
return z.util.ObjectUtil.escape_properties(parsedResults);
};

/**
Expand All @@ -55,19 +66,17 @@ z.location = (() => {
* @param {number} longitude - Longitude of location
* @returns {Promise} Resolves with the location information
*/
const get_location = (latitude, longitude) => {
const _getLocation = (latitude, longitude) => {
return new Promise((resolve, reject) => {
if (latitude == null || longitude == null) {
reject(new Error('You need to specify latitude and longitude in order to retrieve the location'));
}
$.ajax({
url: `${GOOGLE_GEOCODING_BASE_URL}?latlng=${latitude},${longitude}&key=${API_KEY}`,
})

const requestUrl = `${GOOGLE_GEOCODE_BASE_URL}?latlng=${latitude},${longitude}&key=${API_KEY}`;
$.ajax({url: requestUrl})
.done(response => {
if (response.status === 'OK') {
return resolve(_parse_results(response.results));
}
return resolve();
const isStatusOk = response.status === 'OK';
return isStatusOk ? resolve(_parseResults(response.results)) : resolve();
})
.fail((jqXHR, textStatus, errorThrown) => reject(new Error(errorThrown)));
});
Expand All @@ -76,27 +85,24 @@ z.location = (() => {
/**
* Return link to Google Maps
*
* @param {number} lat - Latitude of location
* @param {number} lng - Longitude of location
* @param {number} latitude - Latitude of location
* @param {number} longitude - Longitude of location
* @param {string} name - Name of location
* @param {string} zoom - Map zoom level
* @returns {string} URL to location in Google Maps
*/
const get_maps_url = (lat, lng, name, zoom) => {
let base_url;
base_url = 'https://google.com/maps/';
if (name != null) {
base_url += `place/${name}/`;
}
base_url += `@${lat},${lng}`;
if (zoom != null) {
base_url += `,${zoom}z`;
}
return base_url;
const _getMapsUrl = (latitude, longitude, name, zoom) => {
const baseUrl = 'https://google.com/maps/';

const nameParam = name ? `place/${name}/` : '';
const locationParam = `@${latitude},${longitude}`;
const zoomParam = zoom ? `,${zoom}z` : '';

return `${baseUrl}${nameParam}${locationParam}${zoomParam}`;
};

return {
get_location,
get_maps_url,
getLocation: _getLocation,
getMapsUrl: _getMapsUrl,
};
})();
Expand Up @@ -75,9 +75,9 @@ z.ViewModel.content.PreferencesDeviceDetailsViewModel = class PreferencesDeviceD
}

_update_device_location(location) {
z.location.get_location(location.lat, location.lon).then(retrieved_location => {
z.location.getLocation(location.lat, location.lon).then(retrieved_location => {
if (retrieved_location) {
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.country_code}`);
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.countryCode}`);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/script/view_model/content/PreferencesDevicesViewModel.js
Expand Up @@ -74,9 +74,9 @@ z.ViewModel.content.PreferencesDevicesViewModel = class PreferencesDevicesViewMo
}

_update_device_location(location) {
z.location.get_location(location.lat, location.lon).then(retrieved_location => {
z.location.getLocation(location.lat, location.lon).then(retrieved_location => {
if (retrieved_location) {
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.country_code}`);
this._update_activation_location(`${retrieved_location.place}, ${retrieved_location.countryCode}`);
}
});
}
Expand Down
20 changes: 10 additions & 10 deletions test/unit_tests/location/GeoLocationSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0bf46b5

Please sign in to comment.