Add prettier and reformat code
This commit is contained in:
parent
68eb00bae9
commit
970a34981f
37 changed files with 3459 additions and 1969 deletions
246
js/index.js
246
js/index.js
|
|
@ -5,14 +5,20 @@
|
|||
*/
|
||||
|
||||
(function() {
|
||||
|
||||
var mapContext;
|
||||
|
||||
function verifyTouchStyle(mapContext) {
|
||||
// revert touch style (large icons) when touch screen detection is available and negative
|
||||
// see https://github.com/nrenner/brouter-web/issues/69
|
||||
if (L.Browser.touch && BR.Browser.touchScreenDetectable && !BR.Browser.touchScreen) {
|
||||
L.DomUtil.removeClass(mapContext.map.getContainer(), 'leaflet-touch');
|
||||
if (
|
||||
L.Browser.touch &&
|
||||
BR.Browser.touchScreenDetectable &&
|
||||
!BR.Browser.touchScreen
|
||||
) {
|
||||
L.DomUtil.removeClass(
|
||||
mapContext.map.getContainer(),
|
||||
'leaflet-touch'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -52,28 +58,31 @@
|
|||
router = L.bRouter(); //brouterCgi dummyRouter
|
||||
|
||||
drawButton = L.easyButton({
|
||||
states: [{
|
||||
stateName: 'deactivate-draw',
|
||||
icon: 'fa-pencil active',
|
||||
onClick: function (control) {
|
||||
routing.draw(false);
|
||||
control.state('activate-draw');
|
||||
states: [
|
||||
{
|
||||
stateName: 'deactivate-draw',
|
||||
icon: 'fa-pencil active',
|
||||
onClick: function(control) {
|
||||
routing.draw(false);
|
||||
control.state('activate-draw');
|
||||
},
|
||||
title: i18next.t('map.draw-route-stop')
|
||||
},
|
||||
title: i18next.t('map.draw-route-stop')
|
||||
}, {
|
||||
stateName: 'activate-draw',
|
||||
icon: 'fa-pencil',
|
||||
onClick: function (control) {
|
||||
routing.draw(true);
|
||||
control.state('deactivate-draw');
|
||||
},
|
||||
title: i18next.t('map.draw-route-start')
|
||||
}]
|
||||
{
|
||||
stateName: 'activate-draw',
|
||||
icon: 'fa-pencil',
|
||||
onClick: function(control) {
|
||||
routing.draw(true);
|
||||
control.state('deactivate-draw');
|
||||
},
|
||||
title: i18next.t('map.draw-route-start')
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
reverseRouteButton = L.easyButton(
|
||||
'fa-random',
|
||||
function () {
|
||||
function() {
|
||||
routing.reverse();
|
||||
},
|
||||
i18next.t('map.reverse-route')
|
||||
|
|
@ -81,15 +90,18 @@
|
|||
|
||||
deletePointButton = L.easyButton(
|
||||
'<span><i class="fa fa-caret-left"></i><i class="fa fa-map-marker" style="margin-left: 1px; color: gray;"></i></span>',
|
||||
function () {
|
||||
routing.removeWaypoint(routing.getLast(), function(err, data) {});
|
||||
function() {
|
||||
routing.removeWaypoint(routing.getLast(), function(
|
||||
err,
|
||||
data
|
||||
) {});
|
||||
},
|
||||
i18next.t('map.delete-last-point')
|
||||
);
|
||||
|
||||
deleteRouteButton = L.easyButton(
|
||||
'fa-trash-o',
|
||||
function () {
|
||||
function() {
|
||||
bootbox.prompt({
|
||||
size: 'small',
|
||||
title: i18next.t('map.delete-route'),
|
||||
|
|
@ -138,11 +150,11 @@
|
|||
profile.update(evt.options);
|
||||
});
|
||||
|
||||
BR.NogoAreas.MSG_BUTTON = i18next.t("map.nogo.draw");
|
||||
BR.NogoAreas.MSG_BUTTON_CANCEL = i18next.t("map.nogo.cancel");
|
||||
BR.NogoAreas.MSG_CREATE = i18next.t("map.nogo.click-drag");
|
||||
BR.NogoAreas.MSG_DISABLED = i18next.t("map.nogo.edit");
|
||||
BR.NogoAreas.MSG_ENABLED = i18next.t("map.nogo.help");
|
||||
BR.NogoAreas.MSG_BUTTON = i18next.t('map.nogo.draw');
|
||||
BR.NogoAreas.MSG_BUTTON_CANCEL = i18next.t('map.nogo.cancel');
|
||||
BR.NogoAreas.MSG_CREATE = i18next.t('map.nogo.click-drag');
|
||||
BR.NogoAreas.MSG_DISABLED = i18next.t('map.nogo.edit');
|
||||
BR.NogoAreas.MSG_ENABLED = i18next.t('map.nogo.help');
|
||||
nogos = new BR.NogoAreas();
|
||||
nogos.on('update', updateRoute);
|
||||
|
||||
|
|
@ -159,14 +171,19 @@
|
|||
profile.on('update', function(evt) {
|
||||
BR.message.hide();
|
||||
var profileId = routingOptions.getCustomProfile();
|
||||
router.uploadProfile(profileId, evt.profileText, function(err, profileId) {
|
||||
router.uploadProfile(profileId, evt.profileText, function(
|
||||
err,
|
||||
profileId
|
||||
) {
|
||||
if (!err) {
|
||||
routingOptions.setCustomProfile(profileId, true);
|
||||
updateRoute({
|
||||
options: routingOptions.getOptions()
|
||||
});
|
||||
if (!saveWarningShown) {
|
||||
profile.message.showWarning(i18next.t('warning.temporary-profile'));
|
||||
profile.message.showWarning(
|
||||
i18next.t('warning.temporary-profile')
|
||||
);
|
||||
saveWarningShown = true;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -197,7 +214,9 @@
|
|||
styles: BR.conf.routingStyles
|
||||
});
|
||||
|
||||
routing.on('routing:routeWaypointEnd routing:setWaypointsEnd', function(evt) {
|
||||
routing.on('routing:routeWaypointEnd routing:setWaypointsEnd', function(
|
||||
evt
|
||||
) {
|
||||
search.clear();
|
||||
onUpdate(evt && evt.err);
|
||||
});
|
||||
|
|
@ -240,16 +259,16 @@
|
|||
}
|
||||
|
||||
download.update(urls);
|
||||
};
|
||||
}
|
||||
|
||||
routing.addTo(map);
|
||||
elevation.addBelow(map);
|
||||
|
||||
|
||||
sidebar = BR.sidebar({
|
||||
defaultTabId: BR.conf.transit ? 'tab_itinerary' : 'tab_profile',
|
||||
listeningTabs: {
|
||||
'tab_profile': profile,
|
||||
'tab_data': trackMessages
|
||||
tab_profile: profile,
|
||||
tab_data: trackMessages
|
||||
}
|
||||
}).addTo(map);
|
||||
if (BR.conf.transit) {
|
||||
|
|
@ -257,16 +276,24 @@
|
|||
}
|
||||
|
||||
nogos.addTo(map);
|
||||
drawToolbar = L.easyBar([drawButton, reverseRouteButton, nogos.getButton(), deletePointButton, deleteRouteButton]).addTo(map);
|
||||
drawToolbar = L.easyBar([
|
||||
drawButton,
|
||||
reverseRouteButton,
|
||||
nogos.getButton(),
|
||||
deletePointButton,
|
||||
deleteRouteButton
|
||||
]).addTo(map);
|
||||
nogos.preventRoutePointOnCreate(routing);
|
||||
|
||||
if (BR.keys.strava) {
|
||||
BR.stravaSegments(map, layersControl);
|
||||
}
|
||||
|
||||
map.addControl(new BR.OpacitySlider({
|
||||
callback: L.bind(routing.setOpacity, routing)
|
||||
}));
|
||||
map.addControl(
|
||||
new BR.OpacitySlider({
|
||||
callback: L.bind(routing.setOpacity, routing)
|
||||
})
|
||||
);
|
||||
|
||||
// initial option settings (after controls are added and initialized with onAdd)
|
||||
router.setOptions(nogos.getOptions());
|
||||
|
|
@ -274,12 +301,11 @@
|
|||
profile.update(routingOptions.getOptions());
|
||||
|
||||
var onHashChangeCb = function(url) {
|
||||
var url2params = function (s) {
|
||||
var url2params = function(s) {
|
||||
s = s.replace(/;/g, '|');
|
||||
var p = {};
|
||||
var sep = '&';
|
||||
if (s.search('&') !== -1)
|
||||
sep = '&';
|
||||
if (s.search('&') !== -1) sep = '&';
|
||||
var params = s.split(sep);
|
||||
for (var i = 0; i < params.length; i++) {
|
||||
var tmp = params[i].split('=');
|
||||
|
|
@ -287,7 +313,7 @@
|
|||
p[tmp[0]] = decodeURIComponent(tmp[1]);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
};
|
||||
if (url == null) return;
|
||||
var opts = router.parseUrlParams(url2params(url));
|
||||
router.setOptions(opts);
|
||||
|
|
@ -313,10 +339,12 @@
|
|||
// do not initialize immediately
|
||||
urlHash = new L.Hash(null, null);
|
||||
urlHash.additionalCb = function() {
|
||||
var url = router.getUrl(routing.getWaypoints(), null).substr('brouter?'.length+1);
|
||||
url = url.replace(/\|/g, ';');
|
||||
return url.length > 0 ? '&' + url : null;
|
||||
};
|
||||
var url = router
|
||||
.getUrl(routing.getWaypoints(), null)
|
||||
.substr('brouter?'.length + 1);
|
||||
url = url.replace(/\|/g, ';');
|
||||
return url.length > 0 ? '&' + url : null;
|
||||
};
|
||||
urlHash.onHashChangeCb = onHashChangeCb;
|
||||
urlHash.onInvalidHashChangeCb = onInvalidHashChangeCb;
|
||||
urlHash.init(map, {
|
||||
|
|
@ -325,7 +353,7 @@
|
|||
|
||||
// activate configured default base layer or first if no hash,
|
||||
// only after hash init, by using the same delay
|
||||
setTimeout(function () {
|
||||
setTimeout(function() {
|
||||
layersControl.activateDefaultBaseLayer();
|
||||
}, urlHash.changeDefer);
|
||||
|
||||
|
|
@ -334,27 +362,30 @@
|
|||
// waypoint add, move, delete (but last)
|
||||
routing.on('routing:routeWaypointEnd', urlHash.onMapMove, urlHash);
|
||||
// delete last waypoint
|
||||
routing.on('waypoint:click', function (evt) {
|
||||
var r = evt.marker._routing;
|
||||
if (!r.prevMarker && !r.nextMarker) {
|
||||
urlHash.onMapMove();
|
||||
}
|
||||
}, urlHash);
|
||||
routing.on(
|
||||
'waypoint:click',
|
||||
function(evt) {
|
||||
var r = evt.marker._routing;
|
||||
if (!r.prevMarker && !r.nextMarker) {
|
||||
urlHash.onMapMove();
|
||||
}
|
||||
},
|
||||
urlHash
|
||||
);
|
||||
|
||||
$(window).resize(function () {
|
||||
$(window).resize(function() {
|
||||
elevation.addBelow(map);
|
||||
});
|
||||
|
||||
$('#elevation-chart').on('show.bs.collapse', function () {
|
||||
$('#elevation-chart').on('show.bs.collapse', function() {
|
||||
$('#elevation-btn').addClass('active');
|
||||
});
|
||||
$('#elevation-chart').on('hidden.bs.collapse', function () {
|
||||
$('#elevation-chart').on('hidden.bs.collapse', function() {
|
||||
$('#elevation-btn').removeClass('active');
|
||||
// we must fetch tiles that are located behind elevation-chart
|
||||
map._onResize();
|
||||
});
|
||||
|
||||
|
||||
var onHide = function() {
|
||||
if (this.id && BR.Util.localStorageAvailable()) {
|
||||
localStorage.removeItem(this.id);
|
||||
|
|
@ -366,33 +397,39 @@
|
|||
}
|
||||
};
|
||||
// on page load, we want to restore collapsible elements from previous usage
|
||||
$('.collapse').on('hidden.bs.collapse', onHide)
|
||||
.on('shown.bs.collapse', onShow)
|
||||
.each(function() {
|
||||
if (this.id && BR.Util.localStorageAvailable() && localStorage[this.id] === 'true') {
|
||||
$(this).collapse('show');
|
||||
}
|
||||
});
|
||||
$('.collapse')
|
||||
.on('hidden.bs.collapse', onHide)
|
||||
.on('shown.bs.collapse', onShow)
|
||||
.each(function() {
|
||||
if (
|
||||
this.id &&
|
||||
BR.Util.localStorageAvailable() &&
|
||||
localStorage[this.id] === 'true'
|
||||
) {
|
||||
$(this).collapse('show');
|
||||
}
|
||||
});
|
||||
|
||||
$('#submitNogos').on('click', function () {
|
||||
$('#submitNogos').on('click', function() {
|
||||
var geoJSONPromise;
|
||||
var nogoURL = $('#nogoURL').val();
|
||||
var nogoFile = $('#nogoFile')[0].files[0];
|
||||
if (nogoURL) {
|
||||
// TODO: Handle {{bbox}}
|
||||
geoJSONPromise = fetch(nogoURL).then(function (response) {
|
||||
geoJSONPromise = fetch(nogoURL).then(function(response) {
|
||||
response.json();
|
||||
});
|
||||
} else if (nogoFile) {
|
||||
geoJSONPromise = new Promise(function (resolve, reject) {
|
||||
geoJSONPromise = new Promise(function(resolve, reject) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
reader.onload = function() {
|
||||
resolve(reader.result);
|
||||
}
|
||||
};
|
||||
reader.readAsText(nogoFile);
|
||||
}).then(function (response) { return JSON.parse(response); });
|
||||
}
|
||||
else {
|
||||
}).then(function(response) {
|
||||
return JSON.parse(response);
|
||||
});
|
||||
} else {
|
||||
$('#nogoError').text('Error: Missing file or URL.');
|
||||
$('#nogoError').css('display', 'block');
|
||||
return false;
|
||||
|
|
@ -416,46 +453,54 @@
|
|||
return false;
|
||||
}
|
||||
|
||||
geoJSONPromise.then(function (response) {
|
||||
geoJSONPromise.then(function(response) {
|
||||
// Iterate on features in order to discard features without geometry
|
||||
var cleanedGeoJSONFeatures = []
|
||||
turf.featureEach(response, function (feature) {
|
||||
var cleanedGeoJSONFeatures = [];
|
||||
turf.featureEach(response, function(feature) {
|
||||
if (turf.getGeom(feature)) {
|
||||
var maybeBufferedFeature = feature;
|
||||
// Eventually buffer GeoJSON
|
||||
if (nogoBuffer != 0) {
|
||||
maybeBufferedFeature = turf.buffer(
|
||||
maybeBufferedFeature, nogoBuffer, { units: 'meters' }
|
||||
maybeBufferedFeature,
|
||||
nogoBuffer,
|
||||
{ units: 'meters' }
|
||||
);
|
||||
}
|
||||
cleanedGeoJSONFeatures.push(maybeBufferedFeature);
|
||||
}
|
||||
});
|
||||
|
||||
var geoJSON = L.geoJson(turf.featureCollection(cleanedGeoJSONFeatures), {
|
||||
onEachFeature: function (feature, layer) {
|
||||
layer.options.nogoWeight = feature.properties.nogoWeight || nogoWeight;
|
||||
var geoJSON = L.geoJson(
|
||||
turf.featureCollection(cleanedGeoJSONFeatures),
|
||||
{
|
||||
onEachFeature: function(feature, layer) {
|
||||
layer.options.nogoWeight =
|
||||
feature.properties.nogoWeight || nogoWeight;
|
||||
}
|
||||
}
|
||||
});
|
||||
var nogosPoints = geoJSON.getLayers().filter(function (e) {
|
||||
);
|
||||
var nogosPoints = geoJSON.getLayers().filter(function(e) {
|
||||
return e.feature.geometry.type === 'Point';
|
||||
});
|
||||
nogosPoints = nogosPoints.map(function (item) {
|
||||
nogosPoints = nogosPoints.map(function(item) {
|
||||
var radius = item.feature.properties.radius || nogoRadius;
|
||||
if (radius > 0) {
|
||||
return L.circle(item.getLatLng(), { radius: radius });
|
||||
}
|
||||
return null;
|
||||
});
|
||||
nogosPoints = nogosPoints.filter(function (e) { return e; });
|
||||
nogosPoints = nogosPoints.filter(function(e) {
|
||||
return e;
|
||||
});
|
||||
nogos.setOptions({
|
||||
nogos: nogosPoints,
|
||||
polygons: geoJSON.getLayers().filter(function (e) {
|
||||
polygons: geoJSON.getLayers().filter(function(e) {
|
||||
return e.feature.geometry.type === 'Polygon';
|
||||
}),
|
||||
polylines: geoJSON.getLayers().filter(function (e) {
|
||||
polylines: geoJSON.getLayers().filter(function(e) {
|
||||
return e.feature.geometry.type === 'LineString';
|
||||
}),
|
||||
})
|
||||
});
|
||||
updateRoute({
|
||||
options: nogos.getOptions()
|
||||
|
|
@ -472,17 +517,20 @@
|
|||
i18next
|
||||
.use(window.i18nextXHRBackend)
|
||||
.use(window.i18nextBrowserLanguageDetector)
|
||||
.init({
|
||||
fallbackLng: 'en',
|
||||
backend: {
|
||||
loadPath: 'dist/locales/{{lng}}.json',
|
||||
}
|
||||
}, function(err, t) {
|
||||
jqueryI18next.init(i18next, $);
|
||||
$('html').localize();
|
||||
.init(
|
||||
{
|
||||
fallbackLng: 'en',
|
||||
backend: {
|
||||
loadPath: 'dist/locales/{{lng}}.json'
|
||||
}
|
||||
},
|
||||
function(err, t) {
|
||||
jqueryI18next.init(i18next, $);
|
||||
$('html').localize();
|
||||
|
||||
mapContext = BR.Map.initMap();
|
||||
verifyTouchStyle(mapContext);
|
||||
initApp(mapContext);
|
||||
});
|
||||
mapContext = BR.Map.initMap();
|
||||
verifyTouchStyle(mapContext);
|
||||
initApp(mapContext);
|
||||
}
|
||||
);
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue