Load profile content for selected profile

This commit is contained in:
Norbert Renner 2015-03-17 20:39:26 +01:00
parent 433d4c4b85
commit 93d0b7f7df
7 changed files with 109 additions and 10 deletions

30
js/Util.js Normal file
View file

@ -0,0 +1,30 @@
BR.Util = {
get: function(url, cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function() {
if ((xhr.status === 200 || xhr.status === 0) && xhr.responseText) {
cb(null, xhr.responseText);
} else {
cb(BR.Util.getError(xhr));
}
};
xhr.onerror = function() {
cb(BR.Util.getError(xhr));
};
xhr.send();
},
getError: function(xhr) {
var msg = 'no response from server';
if (xhr.responseText) {
msg = xhr.responseText;
} else if (xhr.status || xhr.statusText) {
msg = xhr.status + ': ' + xhr.statusText;
}
return new Error(msg);
}
};

View file

@ -1,22 +1,58 @@
BR.Profile = L.Class.extend({
cache: {},
initialize: function () {
L.DomUtil.get('upload').onclick = L.bind(this._upload, this);
L.DomUtil.get('clear').onclick = L.bind(this.clear, this);
this.ele = document.profile_upload.profile;
},
clear: function(evt) {
var button = evt.target || evt.srcElement;
evt.preventDefault();
document.profile_upload.profile.value = null;
this.ele.value = null;
this.ele.defaultValue = this.ele.value;
this.fire('clear');
button.blur();
},
update: function(options) {
var profileName = options.profile,
profileUrl,
ele = this.ele,
dirty = ele.defaultValue !== ele.value;
this.profileName = profileName;
if (profileName && BR.conf.profilesUrl && (!ele.value || !dirty)) {
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;
}
this.cache[profileName] = profileText;
// don't set when option has changed while loading
if (!this.profileName || this.profileName === profileName) {
ele.value = profileText;
ele.defaultValue = ele.value;
}
}, this));
} else {
ele.value = this.cache[profileName];
ele.defaultValue = ele.value;
}
}
},
_upload: function(evt) {
var button = evt.target || evt.srcElement,
profile = document.profile_upload.profile.value;
profile = this.ele.value;
$(button).button('uploading');
evt.preventDefault();

View file

@ -120,6 +120,9 @@
routingOptions = new BR.RoutingOptions();
routingOptions.on('update', updateRoute);
routingOptions.on('update', function(evt) {
profile.update(evt.options);
});
nogos = new BR.NogoAreas();
nogos.on('update', updateRoute);
@ -229,6 +232,7 @@
// initial option settings (after controls are added and initialized with onAdd, before permalink)
router.setOptions(nogos.getOptions());
router.setOptions(routingOptions.getOptions());
profile.update(routingOptions.getOptions());
map.addControl(new L.Control.Permalink({
text: 'Permalink',
@ -237,7 +241,8 @@
routingOptions: routingOptions,
nogos: nogos,
router: router,
routing: routing
routing: routing,
profile: profile
}));
}

View file

@ -87,12 +87,14 @@ L.Control.Permalink.include({
var router = this.options.router,
routing = this.options.routing,
routingOptions = this.options.routingOptions,
nogos = this.options.nogos;
nogos = this.options.nogos,
profile = this.options.profile;
var opts = router.parseUrlParams(e.params);
router.setOptions(opts);
routingOptions.setOptions(opts);
nogos.setOptions(opts);
profile.update(opts);
if (opts.lonlats) {
routing.draw(false);