2014-11-18 10:11:37 +00:00
|
|
|
var These3Words = (function () {
|
2014-11-18 09:10:18 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-18 10:11:37 +00:00
|
|
|
var defaultLat = 46.2323355675,
|
|
|
|
defaultLng = 6.05541944504,
|
|
|
|
defaultLabel = 'spitting-ripple-fontanel';
|
|
|
|
|
|
|
|
|
2014-11-18 13:35:16 +00:00
|
|
|
var apiGet = function (req, callback, callbackError) {
|
2014-11-18 10:11:37 +00:00
|
|
|
var request = new window.XMLHttpRequest();
|
2014-11-18 13:35:16 +00:00
|
|
|
request.open('GET', '/api/' + req, true);
|
2014-11-18 10:11:37 +00:00
|
|
|
request.onload = function () {
|
2014-11-18 09:10:18 +00:00
|
|
|
callback(request.status, JSON.parse(request.responseText));
|
|
|
|
};
|
2014-11-18 10:11:37 +00:00
|
|
|
if (typeof callbackError === "function") {
|
2014-11-18 09:10:18 +00:00
|
|
|
request.onerror = callbackError;
|
|
|
|
}
|
|
|
|
request.send();
|
|
|
|
};
|
|
|
|
|
2014-11-18 13:35:16 +00:00
|
|
|
var apiGetFromLatLng = function(latLng, callback, callbackError) {
|
|
|
|
apiGet('' + latLng.lat() + ',' + latLng.lng(), callback, callbackError);
|
|
|
|
};
|
|
|
|
|
2014-11-18 09:10:18 +00:00
|
|
|
|
2014-11-18 10:11:37 +00:00
|
|
|
var Map = function (lat, lng, label) {
|
|
|
|
this.latLng = new google.maps.LatLng(lat || defaultLat, lng || defaultLng);
|
|
|
|
this.label = label || defaultLabel;
|
2014-11-18 09:10:18 +00:00
|
|
|
|
|
|
|
this.mapCanvas = null;
|
|
|
|
this.map = null;
|
|
|
|
this.marker = null;
|
2014-11-18 13:35:16 +00:00
|
|
|
|
|
|
|
this.searchInput = null;
|
|
|
|
this.searchBox = null;
|
2014-11-18 09:10:18 +00:00
|
|
|
};
|
|
|
|
|
2014-11-18 10:11:37 +00:00
|
|
|
Map.prototype.init = function () {
|
2014-11-18 09:10:18 +00:00
|
|
|
var that = this;
|
2014-11-18 10:11:37 +00:00
|
|
|
var initMap = function () {
|
2014-11-18 09:10:18 +00:00
|
|
|
that.map = new google.maps.Map(that.mapCanvas, {
|
2014-11-18 10:11:37 +00:00
|
|
|
center: that.latLng,
|
2014-11-18 13:35:16 +00:00
|
|
|
zoom: 14,
|
|
|
|
mapTypeControlOptions: {
|
|
|
|
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
|
|
|
|
position: google.maps.ControlPosition.BOTTOM_LEFT
|
|
|
|
},
|
|
|
|
panControlOptions: {
|
|
|
|
position: google.maps.ControlPosition.LEFT_BOTTOM
|
|
|
|
},
|
|
|
|
streetViewControl: false,
|
|
|
|
zoomControlOptions: {
|
|
|
|
position: google.maps.ControlPosition.LEFT_BOTTOM
|
|
|
|
}
|
2014-11-18 09:10:18 +00:00
|
|
|
});
|
|
|
|
that.marker = new google.maps.Marker({
|
2014-11-18 10:11:37 +00:00
|
|
|
position: that.latLng,
|
2014-11-18 09:10:18 +00:00
|
|
|
map: that.map,
|
|
|
|
title: that.label
|
|
|
|
});
|
|
|
|
|
2014-11-18 10:11:37 +00:00
|
|
|
google.maps.event.addListener(that.map, 'click', function (evt) {
|
2014-11-23 11:07:19 +00:00
|
|
|
that.mapZoom = that.map.getZoom();
|
2014-11-23 11:24:59 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
// if zoom level has changed then the user
|
|
|
|
// double clicked instead of single clicked
|
|
|
|
// so we do not want to move, just zoom
|
|
|
|
if (that.map.getZoom() != that.mapZoom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
that.moveTo(evt.latLng);
|
|
|
|
},
|
|
|
|
300);
|
2014-11-18 09:10:18 +00:00
|
|
|
});
|
2014-11-18 10:56:39 +00:00
|
|
|
|
2014-11-18 13:35:16 +00:00
|
|
|
that.searchInput = document.createElement('input');
|
|
|
|
that.searchInput.value = that.label;
|
|
|
|
that.searchInput.id = 'pac-input';
|
|
|
|
that.searchInput.classList.add('controls');
|
|
|
|
that.searchInput.setAttribute('type', 'text');
|
|
|
|
that.searchInput.setAttribute('placeholder', 'These3Words');
|
|
|
|
that.map.controls[google.maps.ControlPosition.TOP_LEFT].push(
|
|
|
|
that.searchInput);
|
|
|
|
that.searchBox = new google.maps.places.SearchBox(that.searchInput);
|
|
|
|
google.maps.event.addListener(that.searchBox, 'places_changed',
|
|
|
|
function() {
|
|
|
|
var places = that.searchBox.getPlaces();
|
2014-11-18 10:56:39 +00:00
|
|
|
if (places.length > 0) {
|
|
|
|
var place = places[0];
|
|
|
|
that.map.setCenter(place.geometry.location);
|
|
|
|
that.moveTo(place.geometry.location);
|
2014-11-18 13:35:16 +00:00
|
|
|
} else {
|
|
|
|
var words = that.searchInput.value;
|
|
|
|
if (/\w+-\w+-\w+/.test(words)) {
|
|
|
|
apiGet(words, function(status, data) {
|
|
|
|
if (status >= 200 && status < 400) {
|
|
|
|
that.moveTo(new google.maps.LatLng(data.lat, data.lng), words);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-11-18 10:56:39 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-18 09:10:18 +00:00
|
|
|
};
|
|
|
|
this.mapCanvas = document.body.appendChild(document.createElement('div'));
|
|
|
|
this.mapCanvas.id = 'map-canvas';
|
|
|
|
google.maps.event.addDomListener(window, 'load', initMap);
|
2014-11-18 10:11:37 +00:00
|
|
|
window.addEventListener('popstate', function (evt) {
|
|
|
|
var state = evt.state;
|
|
|
|
if (typeof state === "object") {
|
|
|
|
console.log(state);
|
|
|
|
that.update(new google.maps.LatLng(state.lat, state.lng), state.label);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
window.history.replaceState({
|
|
|
|
lat: that.latLng.lat(),
|
|
|
|
lng: that.latLng.lng(),
|
|
|
|
label: that.label
|
|
|
|
}, that.label, '/' + that.label);
|
2014-11-18 10:22:54 +00:00
|
|
|
document.title = 'These3Words: ' + that.label;
|
2014-11-18 09:10:18 +00:00
|
|
|
};
|
|
|
|
|
2014-11-18 10:11:37 +00:00
|
|
|
Map.prototype.moveTo = function (latLng) {
|
2014-11-18 09:10:18 +00:00
|
|
|
var that = this;
|
2014-11-18 13:35:16 +00:00
|
|
|
apiGetFromLatLng(latLng, function (status, data) {
|
2014-11-18 09:10:18 +00:00
|
|
|
if (status >= 200 && status < 400) {
|
2014-11-18 10:11:37 +00:00
|
|
|
that.update(latLng, data.three);
|
|
|
|
window.history.pushState({
|
|
|
|
lat: latLng.lat(),
|
|
|
|
lng: latLng.lng(),
|
|
|
|
label: data.three
|
|
|
|
}, data.three, '/' + data.three);
|
2014-11-18 10:22:54 +00:00
|
|
|
document.title = 'These3Words: ' + data.three;
|
2014-11-18 13:35:16 +00:00
|
|
|
that.searchInput.value = data.three;
|
2014-11-18 09:10:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-11-18 10:11:37 +00:00
|
|
|
|
|
|
|
Map.prototype.update = function (latLng, label) {
|
|
|
|
this.latLng = latLng;
|
|
|
|
this.label = label;
|
|
|
|
this.marker.setPosition(latLng);
|
|
|
|
this.marker.setTitle(label);
|
2014-11-23 10:41:25 +00:00
|
|
|
if (!this.map.getBounds().contains(latLng)) {
|
|
|
|
this.map.panTo(latLng);
|
|
|
|
}
|
2014-11-18 10:11:37 +00:00
|
|
|
};
|
2014-11-18 09:10:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
return {
|
2014-11-18 14:40:05 +00:00
|
|
|
Map: Map,
|
|
|
|
apiGet: apiGet,
|
|
|
|
apiGetFromLatLng: apiGetFromLatLng
|
2014-11-18 09:10:18 +00:00
|
|
|
};
|
|
|
|
}());
|