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; line-height: 1.4em;
} }
.hint { .hint {
color: orangered; color: orange;
}
.error {
color: red;
} }
.heading { .heading {

View file

@ -19,7 +19,6 @@
<div id="map"></div> <div id="map"></div>
<div id="message" class="info hidden"> <div id="message" class="info hidden">
<span class="hint">Requires a local desktop installation of BRouter</span><br>
</div> </div>
<div id="header" class="hidden"> <div id="header" class="hidden">
<div class="title"><span class="title-name">BRouter web</span>&nbsp;&nbsp;<sup class="version">alpha2</sup></div> <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()); map.addControl(new BR.Search());
} }
function initApp() { function initApp() {
var router, var router,
routing, routing,
@ -94,6 +94,21 @@
router = L.bRouter(); //brouterCgi dummyRouter 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) { function updateRoute(evt) {
router.setOptions(evt.options); router.setOptions(evt.options);
routing.rerouteAllSegments(onUpdate); routing.rerouteAllSegments(onUpdate);
@ -114,12 +129,22 @@
elevation = new BR.Elevation(); elevation = new BR.Elevation();
profile = new BR.Profile(); profile = new BR.Profile();
profile.on('update', function(evt) { profile.on('update', function(evt) {
hideError();
var profileId = routingOptions.getCustomProfile(); var profileId = routingOptions.getCustomProfile();
router.uploadProfile(profileId, evt.profileText, function(profile) { router.uploadProfile(profileId, evt.profileText, function(err, profile) {
routingOptions.setCustomProfile(profile); if (!err) {
routingOptions.setCustomProfile(profile);
} else {
showError(err);
if (profile) {
routingOptions.setCustomProfile(profile, true);
router.setOptions(routingOptions.getOptions());
}
}
}); });
}); });
profile.on('clear', function(evt) { profile.on('clear', function(evt) {
hideError();
routingOptions.setCustomProfile(null); routingOptions.setCustomProfile(null);
}); });

View file

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