Add prettier and reformat code

This commit is contained in:
Gautier Pelloux-Prayer 2019-05-16 21:31:06 +02:00
parent 68eb00bae9
commit 970a34981f
37 changed files with 3459 additions and 1969 deletions

View file

@ -1,9 +1,8 @@
BR.ControlLayers = L.Control.Layers.extend({
getActiveLayers: function () {
getActiveLayers: function() {
var result = [];
for (var i = 0; i < this._layers.length; i++) {
for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i];
if (this._map.hasLayer(obj.layer)) {
if (obj.overlay) {
@ -17,7 +16,7 @@ BR.ControlLayers = L.Control.Layers.extend({
return result;
},
getActiveBaseLayer: function () {
getActiveBaseLayer: function() {
var activeLayers = this.getActiveLayers();
for (var i = 0; i < activeLayers.length; i++) {
var obj = activeLayers[i];
@ -29,10 +28,10 @@ BR.ControlLayers = L.Control.Layers.extend({
return null;
},
removeActiveLayers: function () {
removeActiveLayers: function() {
var removed = [];
for (var i = 0; i < this._layers.length; i++) {
for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i];
if (this._map.hasLayer(obj.layer)) {
this._map.removeLayer(obj.layer);
@ -43,7 +42,7 @@ BR.ControlLayers = L.Control.Layers.extend({
return removed;
},
getLayer: function (name) {
getLayer: function(name) {
for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i];
if (obj.name === name) {
@ -54,17 +53,17 @@ BR.ControlLayers = L.Control.Layers.extend({
return null;
},
getBaseLayers: function () {
return this._layers.filter(function (obj) {
getBaseLayers: function() {
return this._layers.filter(function(obj) {
return !obj.overlay;
});
},
activateLayer: function (layer) {
activateLayer: function(layer) {
this._map.addLayer(layer);
},
activateFirstLayer: function () {
activateFirstLayer: function() {
for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i];
if (!obj.overlay) {
@ -74,26 +73,25 @@ BR.ControlLayers = L.Control.Layers.extend({
}
},
activateBaseLayerIndex: function (index) {
activateBaseLayerIndex: function(index) {
var baseLayers = this.getBaseLayers();
var obj = baseLayers[index];
this.activateLayer(obj.layer);
},
_addLayer: function (layer, name, overlay) {
_addLayer: function(layer, name, overlay) {
L.Control.Layers.prototype._addLayer.call(this, layer, name, overlay);
// override z-index assignment to fix that base layers added later
// are on top of overlays; set all base layers to 0
if (this.options.autoZIndex && layer.setZIndex) {
if (this.options.autoZIndex && layer.setZIndex) {
if (!overlay) {
// undo increase in super method
this._lastZIndex--;
layer.setZIndex(0);
}
}
}
}
});
});

View file

@ -1,12 +1,12 @@
BR.Download = L.Class.extend({
update: function (urls) {
update: function(urls) {
if (urls) {
['gpx', 'kml', 'geojson', 'csv'].forEach(function(e, i, a) {
var a = L.DomUtil.get('dl-'+e);
var a = L.DomUtil.get('dl-' + e);
a.setAttribute('href', urls[e]);
a.setAttribute('download', 'brouter.'+e);
a.setAttribute('download', 'brouter.' + e);
a.removeAttribute('disabled');
})
});
}
}
});

View file

@ -1,20 +1,20 @@
BR.Itinerary = L.Class.extend({
initialize: function () {
initialize: function() {
this._content = document.getElementById('itinerary');
this.update();
},
update: function (polyline, segments) {
var i, j, iter, html = '';
update: function(polyline, segments) {
var i,
j,
iter,
html = '';
html += '<pre class="flexgrow">';
for (i = 0; segments && i < segments.length; i++)
{
for (i = 0; segments && i < segments.length; i++) {
iter = segments[i].feature.iternity;
for (j = 0; iter && j < iter.length; j++)
{
html += iter[j] + '\n';
for (j = 0; iter && j < iter.length; j++) {
html += iter[j] + '\n';
}
}
html += '</pre>';

View file

@ -1,10 +1,9 @@
BR.Layers = L.Class.extend({
_loadLayers: function() {
this._customLayers = {};
if (BR.Util.localStorageAvailable()) {
var layers = JSON.parse(localStorage.getItem("map/customLayers"));
var layers = JSON.parse(localStorage.getItem('map/customLayers'));
for (a in layers) {
this._addLayer(a, layers[a].layer, layers[a].isOverlay);
}
@ -14,24 +13,28 @@ BR.Layers = L.Class.extend({
_loadTable: function() {
var layersData = [];
for (layer in this._customLayers) {
layersData.push([layer, this._customLayers[layer].layer._url, this._customLayers[layer].isOverlay ? "Overlay" : "Layer"]);
layersData.push([
layer,
this._customLayers[layer].layer._url,
this._customLayers[layer].isOverlay ? 'Overlay' : 'Layer'
]);
}
if (this._layersTable != null) {
this._layersTable.destroy();
}
this._layersTable = $('#custom_layers_table').DataTable({
this._layersTable = $('#custom_layers_table').DataTable({
data: layersData,
info: false,
searching: false,
paging: false,
language: {
emptyTable: i18next.t("sidebar.layers.table.empty")
emptyTable: i18next.t('sidebar.layers.table.empty')
},
columns: [
{ title: i18next.t("sidebar.layers.table.name") },
{ title: i18next.t("sidebar.layers.table.URL") },
{ title: i18next.t("sidebar.layers.table.type") }
{ title: i18next.t('sidebar.layers.table.name') },
{ title: i18next.t('sidebar.layers.table.URL') },
{ title: i18next.t('sidebar.layers.table.type') }
]
});
},
@ -39,22 +42,29 @@ BR.Layers = L.Class.extend({
init: function(map, layersControl, baseLayers, overlays) {
this._layersControl = layersControl;
this._map = map;
this._layers = {}
for (var l in overlays)
this._layers[l] = [overlays[l], true];
for (var l in baseLayers)
this._layers[l] = [baseLayers[l], false];
this._layers = {};
for (var l in overlays) this._layers[l] = [overlays[l], true];
for (var l in baseLayers) this._layers[l] = [baseLayers[l], false];
L.DomUtil.get('custom_layers_add_base').onclick = L.bind(this._addBaseLayer, this);
L.DomUtil.get('custom_layers_add_overlay').onclick = L.bind(this._addOverlay, this);
L.DomUtil.get('custom_layers_remove').onclick = L.bind(this._remove, this);
L.DomUtil.get('custom_layers_add_base').onclick = L.bind(
this._addBaseLayer,
this
);
L.DomUtil.get('custom_layers_add_overlay').onclick = L.bind(
this._addOverlay,
this
);
L.DomUtil.get('custom_layers_remove').onclick = L.bind(
this._remove,
this
);
this._loadLayers();
this._loadTable();
var table = this._layersTable;
$('#custom_layers_table tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$('#custom_layers_table tbody').on('click', 'tr', function() {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
table.$('tr.selected').removeClass('selected');
@ -62,7 +72,7 @@ BR.Layers = L.Class.extend({
}
});
L.DomUtil.get('custom_layers_button').onclick = function () {
L.DomUtil.get('custom_layers_button').onclick = function() {
$('#custom_layers').modal();
};
},
@ -74,7 +84,10 @@ BR.Layers = L.Class.extend({
this._layersControl.removeLayer(this._customLayers[name].layer);
this._map.removeLayer(this._customLayers[name].layer);
delete this._customLayers[name];
this._layersTable.row('.selected').remove().draw( false );
this._layersTable
.row('.selected')
.remove()
.draw(false);
this._sync();
}
},
@ -94,16 +107,17 @@ BR.Layers = L.Class.extend({
},
_addLayer: function(layerName, layerUrl, isOverlay) {
if (layerName in this._layers)
return
if (layerName in this._layers) return;
if (layerName in this._customLayers)
return
if (layerName in this._customLayers) return;
try {
var layer = L.tileLayer(layerUrl);
this._customLayers[layerName] = {layer: layer, isOverlay: isOverlay};
this._customLayers[layerName] = {
layer: layer,
isOverlay: isOverlay
};
if (isOverlay) {
this._layersControl.addOverlay(layer, layerName);
@ -114,17 +128,20 @@ BR.Layers = L.Class.extend({
this._sync();
return layer;
} catch (e) {
console.warn("Oops:", e);
return
console.warn('Oops:', e);
return;
}
},
_sync: function() {
if (BR.Util.localStorageAvailable()) {
localStorage.setItem("map/customLayers", JSON.stringify(this._customLayers, function(k, v) {
// dont write Leaflet.Layer in localStorage; simply keep the URL
return v._url || v;
}));
localStorage.setItem(
'map/customLayers',
JSON.stringify(this._customLayers, function(k, v) {
// dont write Leaflet.Layer in localStorage; simply keep the URL
return v._url || v;
})
);
}
}
});
});

View file

@ -3,13 +3,18 @@ BR.LayersTab = BR.ControlLayers.extend({
previewBounds: null,
saveLayers: [],
initialize: function (layersConfig, baseLayers, overlays, options) {
L.Control.Layers.prototype.initialize.call(this, baseLayers, overlays, options);
initialize: function(layersConfig, baseLayers, overlays, options) {
L.Control.Layers.prototype.initialize.call(
this,
baseLayers,
overlays,
options
);
this.layersConfig = layersConfig;
},
addTo: function (map) {
addTo: function(map) {
this._map = map;
this.onAdd(map);
@ -21,15 +26,15 @@ BR.LayersTab = BR.ControlLayers.extend({
return this;
},
initButtons: function () {
var expandTree = function (e) {
initButtons: function() {
var expandTree = function(e) {
this.jstree.open_all();
};
var collapseTree = function (e) {
var collapseTree = function(e) {
this.jstree.close_all();
};
var toggleOptionalLayers = function (e) {
var toggleOptionalLayers = function(e) {
var button = L.DomUtil.get('optional_layers_button');
var treeButtons = L.DomUtil.get('tree-button-group');
var div = L.DomUtil.get('optional-layers-tree');
@ -44,17 +49,23 @@ BR.LayersTab = BR.ControlLayers.extend({
};
L.DomUtil.get('expand_tree_button').onclick = L.bind(expandTree, this);
L.DomUtil.get('collapse_tree_button').onclick = L.bind(collapseTree, this);
L.DomUtil.get('collapse_tree_button').onclick = L.bind(
collapseTree,
this
);
L.DomUtil.get('optional_layers_button').onclick = L.bind(toggleOptionalLayers, this);
L.DomUtil.get('optional_layers_button').onclick = L.bind(
toggleOptionalLayers,
this
);
},
initJsTree: function () {
initJsTree: function() {
var layerIndex = BR.layerIndex;
var treeData = this.toJsTree(BR.confLayers.tree);
var oldSelected = null;
var onSelectNode = function (e, data) {
var onSelectNode = function(e, data) {
var layerData = layerIndex[data.node.id];
var selected = data.selected[0];
@ -66,12 +77,12 @@ BR.LayersTab = BR.ControlLayers.extend({
}
};
var onDeselectNode = function (e, data) {
var onDeselectNode = function(e, data) {
this.hidePreview();
oldSelected = null;
};
var onCheckNode = function (e, data) {
var onCheckNode = function(e, data) {
var layerData = layerIndex[data.node.id];
var layer = this.createLayer(layerData);
var name = layerData.properties.name;
@ -86,7 +97,7 @@ BR.LayersTab = BR.ControlLayers.extend({
this.storeDefaultLayers();
};
var onUncheckNode = function (e, data) {
var onUncheckNode = function(e, data) {
var obj = this.getLayerById(data.node.id);
if (!obj) return;
@ -107,28 +118,28 @@ BR.LayersTab = BR.ControlLayers.extend({
.on('deselect_node.jstree', L.bind(onDeselectNode, this))
.on('check_node.jstree', L.bind(onCheckNode, this))
.on('uncheck_node.jstree', L.bind(onUncheckNode, this))
.on('ready.jstree', function (e, data) {
.on('ready.jstree', function(e, data) {
data.instance.open_all();
})
.jstree({
plugins: [ 'checkbox' ],
plugins: ['checkbox'],
checkbox: {
whole_node: false,
tie_selection: false
},
core: {
'multiple': false,
'themes': {
'icons': false,
dots : false
multiple: false,
themes: {
icons: false,
dots: false
},
'data' : treeData
data: treeData
}
});
this.jstree = $('#optional-layers-tree').jstree(true);
},
toJsTree: function (layerTree) {
toJsTree: function(layerTree) {
var data = {
children: []
};
@ -136,11 +147,11 @@ BR.LayersTab = BR.ControlLayers.extend({
function createRootNode(name) {
var rootNode = {
'text': i18next.t('sidebar.layers.category.' + name, name),
'state': {
'disabled': true
text: i18next.t('sidebar.layers.category.' + name, name),
state: {
disabled: true
},
'children': []
children: []
};
return rootNode;
}
@ -163,12 +174,15 @@ BR.LayersTab = BR.ControlLayers.extend({
var childNode = null;
// when key required only add if configured
if (!keyObj || keyObj && BR.keys[keyObj.name]) {
if (!keyObj || (keyObj && BR.keys[keyObj.name])) {
childNode = {
'id': id,
'text': getText(props, parent),
'state': {
'checked': self.layersConfig.isDefaultLayer(id, props.overlay)
id: id,
text: getText(props, parent),
state: {
checked: self.layersConfig.isDefaultLayer(
id,
props.overlay
)
}
};
}
@ -179,7 +193,7 @@ BR.LayersTab = BR.ControlLayers.extend({
function walkObject(obj) {
for (name in obj) {
var value = obj[name];
var rootNode = createRootNode(name)
var rootNode = createRootNode(name);
outTree.children.push(rootNode);
walkTree(value, rootNode);
@ -213,7 +227,7 @@ BR.LayersTab = BR.ControlLayers.extend({
return data.children;
},
storeDefaultLayers: function () {
storeDefaultLayers: function() {
var baseLayers = [];
var overlays = [];
@ -233,7 +247,7 @@ BR.LayersTab = BR.ControlLayers.extend({
this.layersConfig.storeDefaultLayers(baseLayers, overlays);
},
createLayer: function (layerData) {
createLayer: function(layerData) {
var layer = this.layersConfig.createLayer(layerData);
var overlay = layerData.properties.overlay;
@ -243,7 +257,7 @@ BR.LayersTab = BR.ControlLayers.extend({
return layer;
},
getLayerById: function (id) {
getLayerById: function(id) {
for (var i = 0; i < this._layers.length; i++) {
var obj = this._layers[i];
if (obj.layer.id === id) {
@ -254,7 +268,7 @@ BR.LayersTab = BR.ControlLayers.extend({
return null;
},
getLayerByLegacyName: function (legacyName) {
getLayerByLegacyName: function(legacyName) {
var obj = null;
var id = this.layersConfig.legacyNameToIdMap[legacyName];
@ -265,7 +279,7 @@ BR.LayersTab = BR.ControlLayers.extend({
return obj;
},
activateDefaultBaseLayer: function () {
activateDefaultBaseLayer: function() {
var index = BR.conf.defaultBaseLayerIndex || 0;
var activeBaseLayer = this.getActiveBaseLayer();
if (!activeBaseLayer) {
@ -273,12 +287,12 @@ BR.LayersTab = BR.ControlLayers.extend({
}
},
saveRemoveActiveLayers: function () {
this.saveLayers = this.removeActiveLayers();
saveRemoveActiveLayers: function() {
this.saveLayers = this.removeActiveLayers();
},
restoreActiveLayers: function (overlaysOnly) {
for (var i = 0; i < this.saveLayers.length; i++) {
restoreActiveLayers: function(overlaysOnly) {
for (var i = 0; i < this.saveLayers.length; i++) {
var obj = this.saveLayers[i];
if (!overlaysOnly || (overlaysOnly && obj.overlay)) {
@ -296,7 +310,7 @@ BR.LayersTab = BR.ControlLayers.extend({
this.saveLayers = [];
},
removePreviewLayer: function () {
removePreviewLayer: function() {
if (this.previewLayer && this._map.hasLayer(this.previewLayer)) {
this._map.removeLayer(this.previewLayer);
this.previewLayer = null;
@ -305,7 +319,7 @@ BR.LayersTab = BR.ControlLayers.extend({
return false;
},
showPreviewBounds: function (layerData) {
showPreviewBounds: function(layerData) {
if (layerData.geometry) {
this.previewBounds = L.geoJson(layerData.geometry, {
// fill/mask outside of bounds polygon with Leaflet.snogylop
@ -319,32 +333,35 @@ BR.LayersTab = BR.ControlLayers.extend({
}
},
removePreviewBounds: function () {
removePreviewBounds: function() {
if (this.previewBounds && this._map.hasLayer(this.previewBounds)) {
this._map.removeLayer(this.previewBounds);
this.previewBounds = null;
}
},
deselectNode: function () {
deselectNode: function() {
var selected = this.jstree.get_selected();
if (selected.length > 0) {
this.jstree.deselect_node(selected[0]);
}
},
onBaselayerchange: function () {
onBaselayerchange: function() {
// execute after current input click handler,
// otherwise added overlay checkbox state doesn't update
setTimeout(L.Util.bind(function () {
this.removePreviewBounds();
this.removePreviewLayer();
this.restoreActiveLayers(true);
this.deselectNode();
}, this), 0);
setTimeout(
L.Util.bind(function() {
this.removePreviewBounds();
this.removePreviewLayer();
this.restoreActiveLayers(true);
this.deselectNode();
}, this),
0
);
},
showPreview: function (layerData) {
showPreview: function(layerData) {
var layer = this.createLayer(layerData);
this._map.addLayer(layer);
this.removePreviewBounds();
@ -357,7 +374,7 @@ BR.LayersTab = BR.ControlLayers.extend({
this.showPreviewBounds(layerData);
},
hidePreview: function (layer) {
hidePreview: function(layer) {
this._map.off('baselayerchange', this.onBaselayerchange, this);
this.removePreviewBounds();
this.removePreviewLayer();
@ -365,6 +382,6 @@ BR.LayersTab = BR.ControlLayers.extend({
}
});
BR.layersTab = function (baseLayers, overlays, options) {
return new BR.LayersTab(baseLayers, overlays, options);
BR.layersTab = function(baseLayers, overlays, options) {
return new BR.LayersTab(baseLayers, overlays, options);
};

View file

@ -4,45 +4,52 @@ BR.Message = L.Class.extend({
// Bootstrap data-api's auto-initialization doesn't work in Controls because of stopPropagation
alert: false
},
initialize: function (id, options) {
initialize: function(id, options) {
L.setOptions(this, options);
this.id = id;
},
_show: function (msg, type) {
_show: function(msg, type) {
var ele = L.DomUtil.get(this.id),
iconClass = (type === 'warning') ? 'fa-exclamation-triangle' : 'fa-times-circle',
alertClass = (type === 'warning') ? 'alert-warning' : 'alert-danger';
iconClass =
type === 'warning'
? 'fa-exclamation-triangle'
: 'fa-times-circle',
alertClass = type === 'warning' ? 'alert-warning' : 'alert-danger';
L.DomEvent.disableClickPropagation(ele);
ele.innerHTML =
'<div class="alert ' + alertClass + ' alert-dismissible fade in" role="alert">'
+ ' <button type="button" class="close" data-dismiss="alert" aria-label="Close">'
+ ' <span aria-hidden="true">&times;</span>'
+ ' </button>'
+ ' <span class="fa ' + iconClass + '" aria-hidden="true"/></span>'
+ msg
+ '</div>';
'<div class="alert ' +
alertClass +
' alert-dismissible fade in" role="alert">' +
' <button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
' <span aria-hidden="true">&times;</span>' +
' </button>' +
' <span class="fa ' +
iconClass +
'" aria-hidden="true"/></span>' +
msg +
'</div>';
if (this.options.alert) {
$('#' + this.id + ' .alert').alert();
}
},
hide: function () {
hide: function() {
$('#' + this.id + ' .alert').alert('close');
},
showError: function (err) {
showError: function(err) {
if (err == 'Error: target island detected for section 0\n') {
err = i18next.t('warning.no-route-found');
}
this._show(err, 'error');
},
showWarning: function (msg) {
showWarning: function(msg) {
this._show(msg, 'warning');
}
});

View file

@ -1,13 +1,15 @@
BR.OpacitySlider = L.Control.extend({
options: {
options: {
position: 'topleft',
callback: function(opacity) {}
},
onAdd: function (map) {
onAdd: function(map) {
var container = L.DomUtil.create('div', 'leaflet-bar control-slider'),
input = $('<input id="slider" type="text"/>'),
item = BR.Util.localStorageAvailable() ? localStorage.opacitySliderValue : null,
item = BR.Util.localStorageAvailable()
? localStorage.opacitySliderValue
: null,
value = item ? parseInt(item) : BR.conf.defaultOpacity * 100,
minOpacity = (BR.conf.minOpacity || 0) * 100;
@ -24,41 +26,53 @@ BR.OpacitySlider = L.Control.extend({
};
var removeStopClickListeners = function() {
document.removeEventListener('click', stopClickAfterSlide, true);
document.removeEventListener('mousedown', removeStopClickListeners, true);
document.removeEventListener(
'mousedown',
removeStopClickListeners,
true
);
};
$(container).html(input);
$(container).attr('title', i18next.t('map.opacity-slider'));
input.slider({
min: 0,
max: 100,
step: 1,
value: value,
orientation: 'vertical',
reversed : true,
selection: 'before', // inverted, serves as track style, see css
tooltip: 'hide'
}).on('slideStart', function (evt) {
// dragging beyond slider control selects zoom control +/- text in Firefox
L.DomUtil.disableTextSelection();
}).on('slide slideStop', { self: this }, function (evt) {
evt.data.self.options.callback(evt.value / 100);
}).on('slideStop', function (evt) {
if (BR.Util.localStorageAvailable()) {
localStorage.opacitySliderValue = evt.value;
}
L.DomUtil.enableTextSelection();
input
.slider({
min: 0,
max: 100,
step: 1,
value: value,
orientation: 'vertical',
reversed: true,
selection: 'before', // inverted, serves as track style, see css
tooltip: 'hide'
})
.on('slideStart', function(evt) {
// dragging beyond slider control selects zoom control +/- text in Firefox
L.DomUtil.disableTextSelection();
})
.on('slide slideStop', { self: this }, function(evt) {
evt.data.self.options.callback(evt.value / 100);
})
.on('slideStop', function(evt) {
if (BR.Util.localStorageAvailable()) {
localStorage.opacitySliderValue = evt.value;
}
// When dragging outside slider and over map, click event after mouseup
// adds marker when active on Chromium. So disable click (not needed)
// once after sliding.
document.addEventListener('click', stopClickAfterSlide, true);
// Firefox does not fire click event in this case, so make sure stop listener
// is always removed on next mousedown.
document.addEventListener('mousedown', removeStopClickListeners, true);
});
L.DomUtil.enableTextSelection();
// When dragging outside slider and over map, click event after mouseup
// adds marker when active on Chromium. So disable click (not needed)
// once after sliding.
document.addEventListener('click', stopClickAfterSlide, true);
// Firefox does not fire click event in this case, so make sure stop listener
// is always removed on next mousedown.
document.addEventListener(
'mousedown',
removeStopClickListeners,
true
);
});
this.options.callback(value / 100);

View file

@ -1,7 +1,7 @@
BR.Profile = L.Evented.extend({
cache: {},
initialize: function () {
initialize: function() {
var textArea = L.DomUtil.get('profile_upload');
this.editor = CodeMirror.fromTextArea(textArea, {
lineNumbers: true
@ -19,7 +19,7 @@ BR.Profile = L.Evented.extend({
var button = evt.target || evt.srcElement;
evt.preventDefault();
this._setValue("");
this._setValue('');
this.fire('clear');
button.blur();
@ -35,19 +35,30 @@ BR.Profile = L.Evented.extend({
if (profileName && BR.conf.profilesUrl && (empty || clean)) {
if (!(profileName in this.cache)) {
profileUrl = BR.conf.profilesUrl + profileName + '.brf';
BR.Util.get(profileUrl, L.bind(function(err, profileText) {
if (err) {
console.warn('Error getting profile from "' + profileUrl + '": ' + err);
return;
}
BR.Util.get(
profileUrl,
L.bind(function(err, profileText) {
if (err) {
console.warn(
'Error getting profile from "' +
profileUrl +
'": ' +
err
);
return;
}
this.cache[profileName] = profileText;
this.cache[profileName] = profileText;
// don't set when option has changed while loading
if (!this.profileName || this.profileName === profileName) {
this._setValue(profileText);
}
}, this));
// don't set when option has changed while loading
if (
!this.profileName ||
this.profileName === profileName
) {
this._setValue(profileText);
}
}, this)
);
} else {
this._setValue(this.cache[profileName]);
}
@ -72,7 +83,7 @@ BR.Profile = L.Evented.extend({
this.fire('update', {
profileText: profile,
callback: function () {
callback: function() {
$(button).button('reset');
$(button).blur();
}

View file

@ -1,19 +1,21 @@
BR.RoutingOptions = L.Evented.extend({
initialize: function () {
$('#profile-alternative').on('changed.bs.select', this._getChangeHandler());
initialize: function() {
$('#profile-alternative').on(
'changed.bs.select',
this._getChangeHandler()
);
// build option list from config
var profiles = BR.conf.profiles;
var profiles_list = L.DomUtil.get('profile');
for (var i = 0; i < profiles.length; i++) {
var option = document.createElement("option");
var option = document.createElement('option');
option.value = profiles[i];
option.text = i18next.t("navbar.profile." + profiles[i]);
option.text = i18next.t('navbar.profile.' + profiles[i]);
profiles_list.appendChild(option);
}
// set default value, used as indicator for empty custom profile
profiles_list.children[0].value = "Custom";
// set default value, used as indicator for empty custom profile
profiles_list.children[0].value = 'Custom';
// <custom> profile is empty at start, select next one
profiles_list.children[1].selected = true;
},
@ -21,19 +23,23 @@ BR.RoutingOptions = L.Evented.extend({
refreshUI: function() {
// we do not allow to select more than one profile and/or alternative at a time
// so we disable the current selected items
$('#profile-alternative').find('option:disabled').each(function(index) {
$(this).prop('disabled', false);
});
$('#profile-alternative').find('option:selected').each(function(index) {
$(this).prop('disabled', true);
});
$('#profile-alternative')
.find('option:disabled')
.each(function(index) {
$(this).prop('disabled', false);
});
$('#profile-alternative')
.find('option:selected')
.each(function(index) {
$(this).prop('disabled', true);
});
// disable custom option if it has no value yet (default value is "Custom")
var custom = L.DomUtil.get('profile').children[0];
if (custom.value === "Custom") {
if (custom.value === 'Custom') {
custom.disabled = true;
}
$('.selectpicker').selectpicker('refresh')
$('.selectpicker').selectpicker('refresh');
},
getOptions: function() {
@ -49,8 +55,12 @@ BR.RoutingOptions = L.Evented.extend({
setOptions: function(options) {
var values = [
options.profile ? options.profile : $('#profile option:selected').val(),
options.alternative ? options.alternative : $('#alternative option:selected').val()
options.profile
? options.profile
: $('#profile option:selected').val(),
options.alternative
? options.alternative
: $('#alternative option:selected').val()
];
$('.selectpicker').selectpicker('val', values);
this.refreshUI();
@ -64,18 +74,19 @@ BR.RoutingOptions = L.Evented.extend({
},
setCustomProfile: function(profile, noUpdate) {
var profiles_grp,
option;
var profiles_grp, option;
profiles_grp = L.DomUtil.get('profile');
option = profiles_grp.children[0];
option.value = profile || "Custom";
option.value = profile || 'Custom';
option.disabled = !profile;
if (profile) {
$('#profile').find('option:selected').each(function(index) {
$(this).prop('selected', false);
});
$('#profile')
.find('option:selected')
.each(function(index) {
$(this).prop('selected', false);
});
} else if (option.selected) {
// clear, select next in group when custom deselected
profiles_grp.children[1].selected = true;
@ -84,7 +95,7 @@ BR.RoutingOptions = L.Evented.extend({
option.selected = !!profile;
if (!noUpdate) {
this.fire('update', {options: this.getOptions()});
this.fire('update', { options: this.getOptions() });
}
},
@ -93,7 +104,7 @@ BR.RoutingOptions = L.Evented.extend({
option = profiles_grp.children[0],
profile = null;
if (option.value !== "Custom") {
if (option.value !== 'Custom') {
profile = option.value;
}
return profile;
@ -101,7 +112,7 @@ BR.RoutingOptions = L.Evented.extend({
_getChangeHandler: function() {
return L.bind(function(evt) {
this.fire('update', {options: this.getOptions()});
this.fire('update', { options: this.getOptions() });
}, this);
}
});

View file

@ -1,5 +1,4 @@
BR.TrackMessages = L.Class.extend({
options: {
edgeStyle: {
color: 'yellow',
@ -12,18 +11,18 @@ BR.TrackMessages = L.Class.extend({
active: false,
columnOptions: {
'Longitude': { visible: false },
'Latitude': { visible: false },
'Elevation': { title: 'elev.', className: 'dt-body-right' },
'Distance': { title: 'dist.', className: 'dt-body-right' },
'CostPerKm': { title: '$/km', className: 'dt-body-right' },
'ElevCost': { title: 'elev$', className: 'dt-body-right' },
'TurnCost': { title: 'turn$', className: 'dt-body-right' },
'NodeCost': { title: 'node$', className: 'dt-body-right' },
'InitialCost': { title: 'initial$', className: 'dt-body-right' }
Longitude: { visible: false },
Latitude: { visible: false },
Elevation: { title: 'elev.', className: 'dt-body-right' },
Distance: { title: 'dist.', className: 'dt-body-right' },
CostPerKm: { title: '$/km', className: 'dt-body-right' },
ElevCost: { title: 'elev$', className: 'dt-body-right' },
TurnCost: { title: 'turn$', className: 'dt-body-right' },
NodeCost: { title: 'node$', className: 'dt-body-right' },
InitialCost: { title: 'initial$', className: 'dt-body-right' }
},
initialize: function (map, options) {
initialize: function(map, options) {
L.setOptions(this, options);
this._map = map;
@ -32,8 +31,11 @@ BR.TrackMessages = L.Class.extend({
this.tableParent = table.parentElement;
},
update: function (polyline, segments) {
var i, messages, columns, headings,
update: function(polyline, segments) {
var i,
messages,
columns,
headings,
data = [];
if (!this.active) {
@ -50,7 +52,7 @@ BR.TrackMessages = L.Class.extend({
this._destroyTable();
if (data.length === 0) {
return;
return;
}
headings = messages[0];
@ -65,14 +67,17 @@ BR.TrackMessages = L.Class.extend({
info: false,
// flexbox workaround: without scrollY height Firefox extends to content height
// (^= minimum height with flexbox?)
scrollY: 50,
scrollY: 50,
scrollX: true,
order: []
});
// highlight track segment (graph edge) on row hover
this._setEdges(polyline, segments);
$('#datatable tbody tr').hover(L.bind(this._handleHover, this), L.bind(this._handleHoverOut, this));
$('#datatable tbody tr').hover(
L.bind(this._handleHover, this),
L.bind(this._handleHoverOut, this)
);
},
show: function() {
@ -87,9 +92,11 @@ BR.TrackMessages = L.Class.extend({
_destroyTable: function() {
var ele;
if ($.fn.DataTable.isDataTable('#datatable') ) {
if ($.fn.DataTable.isDataTable('#datatable')) {
// destroy option too slow on update, really remove elements with destroy method
$('#datatable').DataTable().destroy(true);
$('#datatable')
.DataTable()
.destroy(true);
// recreate original table element, destroy removes all
ele = document.createElement('table');
@ -143,7 +150,14 @@ BR.TrackMessages = L.Class.extend({
},
_setEdges: function(polyline, segments) {
var messages, segLatLngs, length, si, mi, latLng, i, segIndex,
var messages,
segLatLngs,
length,
si,
mi,
latLng,
i,
segIndex,
baseIndex = 0;
// track latLngs index for end node of edge
@ -184,7 +198,10 @@ BR.TrackMessages = L.Class.extend({
endIndex = this._edges[row.index()],
edgeLatLngs = trackLatLngs.slice(startIndex, endIndex + 1);
this._selectedEdge = L.polyline(edgeLatLngs, this.options.edgeStyle).addTo(this._map);
this._selectedEdge = L.polyline(
edgeLatLngs,
this.options.edgeStyle
).addTo(this._map);
},
_handleHoverOut: function(evt) {

View file

@ -1,23 +1,36 @@
BR.TrackStats = L.Class.extend({
update: function (polyline, segments) {
update: function(polyline, segments) {
var stats = this.calcStats(polyline, segments),
length1 = L.Util.formatNum(stats.trackLength/1000,1),
length3 = L.Util.formatNum(stats.trackLength/1000,3),
meanCostFactor = stats.trackLength ? L.Util.formatNum(stats.cost / stats.trackLength, 2) : '',
formattedTime = L.Util.formatNum(stats.totalTime / 60., 1),
formattedEnergy = L.Util.formatNum(stats.totalEnergy / 3600000., 2),
meanEnergy = stats.trackLength ? L.Util.formatNum(stats.totalEnergy / 36. / stats.trackLength, 2) : '';
length1 = L.Util.formatNum(stats.trackLength / 1000, 1),
length3 = L.Util.formatNum(stats.trackLength / 1000, 3),
meanCostFactor = stats.trackLength
? L.Util.formatNum(stats.cost / stats.trackLength, 2)
: '',
formattedTime = L.Util.formatNum(stats.totalTime / 60, 1),
formattedEnergy = L.Util.formatNum(stats.totalEnergy / 3600000, 2),
meanEnergy = stats.trackLength
? L.Util.formatNum(
stats.totalEnergy / 36 / stats.trackLength,
2
)
: '';
$('#distance').html(length1);
// alternative 3-digit format down to meters as tooltip
$('#distance').attr('title', length3 + ' km');
$('#ascend').html(stats.filteredAscend + ' (' + stats.plainAscend +')');
$('#ascend').html(
stats.filteredAscend + ' (' + stats.plainAscend + ')'
);
$('#cost').html(stats.cost + ' (' + meanCostFactor + ')');
$('#totaltime').html(formattedTime);
$('#totalenergy').html(formattedEnergy + ' (' + meanEnergy +')');
$('#totalenergy').html(formattedEnergy + ' (' + meanEnergy + ')');
document.getElementById('totaltime').parentElement.parentElement.hidden = !stats.totalTime;
document.getElementById('totalenergy').parentElement.parentElement.hidden = !stats.totalEnergy;
document.getElementById(
'totaltime'
).parentElement.parentElement.hidden = !stats.totalTime;
document.getElementById(
'totalenergy'
).parentElement.parentElement.hidden = !stats.totalEnergy;
},
calcStats: function(polyline, segments) {