these-3-words/static/js/app.js

91 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-11-18 10:11:37 +00:00
var These3Words = (function () {
'use strict';
2014-11-18 10:11:37 +00:00
var defaultLat = 46.2323355675,
defaultLng = 6.05541944504,
defaultLabel = 'spitting-ripple-fontanel';
var apiGet = function (latLng, callback, callbackError) {
var request = new window.XMLHttpRequest();
request.open('GET', '/api/' + latLng.lat() + ',' + latLng.lng(), true);
2014-11-18 10:11:37 +00:00
request.onload = function () {
callback(request.status, JSON.parse(request.responseText));
};
2014-11-18 10:11:37 +00:00
if (typeof callbackError === "function") {
request.onerror = callbackError;
}
request.send();
};
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;
this.mapCanvas = null;
this.map = null;
this.marker = null;
};
2014-11-18 10:11:37 +00:00
Map.prototype.init = function () {
var that = this;
2014-11-18 10:11:37 +00:00
var initMap = function () {
that.map = new google.maps.Map(that.mapCanvas, {
2014-11-18 10:11:37 +00:00
center: that.latLng,
zoom: 14
});
that.marker = new google.maps.Marker({
2014-11-18 10:11:37 +00:00
position: that.latLng,
map: that.map,
title: that.label
});
2014-11-18 10:11:37 +00:00
google.maps.event.addListener(that.map, 'click', function (evt) {
that.moveTo(evt.latLng);
});
};
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:11:37 +00:00
Map.prototype.moveTo = function (latLng) {
var that = this;
2014-11-18 10:11:37 +00:00
apiGet(latLng, function (status, data) {
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:11:37 +00:00
Map.prototype.update = function (latLng, label) {
this.latLng = latLng;
this.label = label;
this.marker.setPosition(latLng);
this.marker.setTitle(label);
};
return {
Map: Map
};
}());