error handling for profile upload

This commit is contained in:
Norbert Renner 2014-05-27 18:21:21 +02:00
parent 3f41bc9a82
commit f8768c5f8e
4 changed files with 42 additions and 10 deletions

View file

@ -60,7 +60,10 @@ div.elevation {
line-height: 1.4em;
}
.hint {
color: orangered;
color: orange;
}
.error {
color: red;
}
.heading {

View file

@ -19,7 +19,6 @@
<div id="map"></div>
<div id="message" class="info hidden">
<span class="hint">Requires a local desktop installation of BRouter</span><br>
</div>
<div id="header" class="hidden">
<div class="title"><span class="title-name">BRouter web</span>&nbsp;&nbsp;<sup class="version">alpha2</sup></div>

View file

@ -76,7 +76,7 @@
map.addControl(new BR.Search());
}
function initApp() {
var router,
routing,
@ -94,6 +94,21 @@
router = L.bRouter(); //brouterCgi dummyRouter
function showError(err) {
var ele =L.DomUtil.get('message');
ele.innerText = err;
L.DomUtil.removeClass(ele, 'hidden');
L.DomUtil.addClass(ele, 'error');
}
function hideError() {
var ele =L.DomUtil.get('message');
if (!L.DomUtil.hasClass(ele, 'hidden')) {
L.DomUtil.addClass(ele, 'hidden');
ele.innerText = '';
}
}
function updateRoute(evt) {
router.setOptions(evt.options);
routing.rerouteAllSegments(onUpdate);
@ -114,12 +129,22 @@
elevation = new BR.Elevation();
profile = new BR.Profile();
profile.on('update', function(evt) {
hideError();
var profileId = routingOptions.getCustomProfile();
router.uploadProfile(profileId, evt.profileText, function(profile) {
routingOptions.setCustomProfile(profile);
router.uploadProfile(profileId, evt.profileText, function(err, profile) {
if (!err) {
routingOptions.setCustomProfile(profile);
} else {
showError(err);
if (profile) {
routingOptions.setCustomProfile(profile, true);
router.setOptions(routingOptions.getOptions());
}
}
});
});
profile.on('clear', function(evt) {
hideError();
routingOptions.setCustomProfile(null);
});

View file

@ -103,19 +103,24 @@ L.BRouter = L.Class.extend({
xhr.open('POST', url, true);
xhr.onload = L.bind(this._handleProfileResponse, this, xhr, cb);
xhr.onerror = function(evt) {
var xhr = this;
cb('Upload error: ' + xhr.statusText);
};
// send profile text only, as text/plain;charset=UTF-8
xhr.send(profileText);
},
_handleProfileResponse: function(xhr, cb) {
var profile;
var response,
profile;
if (xhr.status === 200 && xhr.responseText && xhr.responseText.length > 0) {
// e.g. profileid=1400498280259
profile = xhr.responseText.split('=')[1];
cb(profile);
response = JSON.parse(xhr.responseText);
cb(response.error, response.profileid);
} else {
cb('Profile error: no or empty response from server');
}
},