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

ES6: Migrated GeoLocation #951

Merged
merged 3 commits into from
Mar 28, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ globals:
_: true
$: true
amplify: true
describe: true
expect: true
it: true
ko: true
LRUCache: true
wire: true
Expand Down
82 changes: 0 additions & 82 deletions app/script/location/GeoLocation.coffee

This file was deleted.

92 changes: 92 additions & 0 deletions app/script/location/GeoLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

'use strict';

window.z = window.z || {};

z.location = (() => {
const GOOGLE_GEOCODING_BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
const API_KEY = 'AIzaSyCKxxKw5JBZ5zEFtoirtgnw8omvH7gWzfo';
let _parse_results = results => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let _parse_results = (results) => {

let res = {};
let result = results[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let [result] = results;

res['address'] = result.formatted_address;
res['lat'] = result.geometry.location.lat;
res['lng'] = result.geometry.location.lng;
let ref = result.address_components;
for (let i = 0, len = ref.length; i < len; i++) {
let component = ref[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (let component of result.address_components) {

let name = component.long_name || component.short_name;
for (let j = 0, len1 = component.types.length; j < len1; j++) {
let type = component.types[j];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (let type of component.types) {

res[type] = name;
if (type === 'country') {
res['country_code'] = component.short_name || '';
}
}
}
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);
};

/*
Reverse loop up for geo location
@param latitude [Number] latitude
@param longitude [Number] longitude
*/
let get_location = (latitude, longitude) => 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'));
}
return $.ajax({
url: `${GOOGLE_GEOCODING_BASE_URL}?latlng=${latitude},${longitude}&key=${API_KEY}`,
}).done(response => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
.done((response) => {

if (response.status === 'OK') {
return resolve(_parse_results(response.results));
}
return resolve();
}).fail((jqXHR, textStatus, errorThrown) => reject(new Error(errorThrown)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
.fail(...

});

/*
Return link to google maps

@param lat [Number] latitude
@param lng [Number] longitude
@param name [String] location name
@param zoom [String] map zoom level
*/
let 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;
};
return {
get_location,
get_maps_url,
};
})();
35 changes: 0 additions & 35 deletions test/unit_tests/location/GeoLocationSpec.coffee

This file was deleted.

40 changes: 40 additions & 0 deletions test/unit_tests/location/GeoLocationSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Wire
* Copyright (C) 2016 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

// grunt test_init && grunt test_run:location/GeoLocation

'use strict';

describe('z.location', () => describe('get_maps_url', () => {
it('should return proper url when lat and long is given', () => {
expect(z.location.get_maps_url(52, 13)).toBe('https://google.com/maps/@52,13')
});

it('should return proper url when lat, long and zoom is given', () => {
expect(z.location.get_maps_url(52, 13, null, 14)).toBe('https://google.com/maps/@52,13,14z')
});

it('should return proper url when lat, long and name is given', () => {
expect(z.location.get_maps_url(52, 13, 'Berlin')).toBe('https://google.com/maps/place/Berlin/@52,13')
});

it('should return proper url when lat, long, name and zoom is given', () => {
expect(z.location.get_maps_url(52, 13, 'Berlin', 14)).toBe('https://google.com/maps/place/Berlin/@52,13,14z')
});
}));