diff --git a/README.md b/README.md index 6ef011e..ed55417 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,15 @@ http://brouter.de/brouter-web/ As an alternative to the above online version, the standalone server of BRouter can also be run on your local desktop. -### Install +### Install BRouter (server with routing engine) 1. download and unzip latest [BRouter revision](http://brouter.de/brouter/revisions.html) -e.g. for Linux (replace ``~/opt/`` with your preferred install dir and ``1_0_1`` with latest version): +e.g. for Linux (replace ``~/opt/`` with your preferred install dir and ``1_1`` with latest version): mkdir ~/opt/brouter cd ~/opt/brouter - wget http://brouter.de/brouter_bin/brouter_1_0_1.zip - unzip brouter_1_0_1.zip + wget http://brouter.de/brouter_bin/brouter_1_1.zip + unzip brouter_1_1.zip chmod +x ./standalone/server.sh mv segments3 segments2 # workaround until scripts are updated fix line endings with ``fromdos`` or ``dos2unix`` (might need to be installed first) @@ -27,10 +27,28 @@ fix line endings with ``fromdos`` or ``dos2unix`` (might need to be installed fi fromdos ./standalone/server.sh 2. download one or more [data file(s)](http://brouter.de/brouter/segments3/) (rd5) into ``segments2`` dir +### Install BRouter-Web (client) + +1. download BRouter-Web as subdirectory ``brouter-web`` of the ``brouter`` directory + + wget https://github.com/nrenner/brouter-web/archive/master.zip + unzip master.zip + mv brouter-web-master brouter-web + +2. configure URL to ``profiles2`` directory +set ``BR.conf.profilesUrl`` in [config.js](config.js), e.g. uncomment: + + BR.conf.profilesUrl = 'http://localhost:8000/profiles2/'; + ### Run -1. start server in the ``standalone`` directory with ``./server.sh`` or ``server.cmd`` (Windows) -2. open http://nrenner.github.io/brouter-web/ +1. start BRouter server in the ``standalone`` directory with ``./server.sh`` or ``server.cmd`` (Windows) +2. serve the ``brouter`` directory for BRouter-Web +This is needed for pre-loading the selected profile (unless you allowed local file access in the Browser). Depending on your setup (see [How to run things locally](https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally)), start a web server in the ``brouter`` directory, e.g.: + + python -m SimpleHTTPServer + +2. open http://localhost:8000/brouter-web/ ## License diff --git a/config.js b/config.js index 2d5573b..13849f9 100644 --- a/config.js +++ b/config.js @@ -26,6 +26,7 @@ ]; BR.conf.host = 'http://h2096617.stratoserver.net:443'; + BR.conf.profilesUrl = 'http://brouter.de/brouter/profiles2/'; } else { @@ -41,6 +42,12 @@ BR.conf.host = 'http://localhost:17777'; + // Pre-loading selected profile disabled locally. Needs brouter-web to run on a + // local web server with the profiles in a subdirectory or allowing file access + // in the Browser (security!), see + // https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally + //BR.conf.profilesUrl = 'http://localhost:8000/profiles2/'; + //BR.conf.profilesUrl = 'file://YOUR_PATH_TO/profiles2/'; } })(); diff --git a/index.html b/index.html index f4f4cab..0c17862 100644 --- a/index.html +++ b/index.html @@ -107,6 +107,7 @@ BR = {}; + diff --git a/js/Util.js b/js/Util.js new file mode 100644 index 0000000..0ae6582 --- /dev/null +++ b/js/Util.js @@ -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); + } + +}; \ No newline at end of file diff --git a/js/control/Profile.js b/js/control/Profile.js index e0e7be1..c76a722 100644 --- a/js/control/Profile.js +++ b/js/control/Profile.js @@ -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(); diff --git a/js/index.js b/js/index.js index e471340..791ed46 100644 --- a/js/index.js +++ b/js/index.js @@ -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 })); } diff --git a/js/plugin/Permalink.Routing.js b/js/plugin/Permalink.Routing.js index 71026d8..240ddf5 100644 --- a/js/plugin/Permalink.Routing.js +++ b/js/plugin/Permalink.Routing.js @@ -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);