Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate /location to camelCase #2483

Merged
merged 2 commits into from Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
}
};
96 changes: 50 additions & 46 deletions app/script/location/GeoLocation.js
Expand Up @@ -22,31 +22,40 @@
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 = ([{address_components: addressComponents, formatted_address: formattedAddress, geometry}]) => {
const locationResult = {
address: formattedAddress,
lat: geometry.location.lat,
lng: geometry.location.lng,
};

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

types.forEach(type => {
locationResult[type] = name;
const isCountry = type === 'country';
if (isCountry) {
locationResult.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);
});
});

const {
administrative_area_level_1: areaLevel1,
administrative_area_level_2: areaLevel2,
administrative_area_level_3: areaLevel3,
locality,
natural_feature: naturalFeature,
} = locationResult;

locationResult.place = locality || naturalFeature || areaLevel3 || areaLevel2 || areaLevel1;

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

/**
Expand All @@ -55,19 +64,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 +83,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
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.