Skip to content

Commit

Permalink
fix hash, make consistent w existing hash option
Browse files Browse the repository at this point in the history
  • Loading branch information
Molly Lloyd committed May 23, 2017
1 parent dc8664f commit 00a7082
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/ui/control/attribution_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class AttributionControl {
_updateEditLink() {
if (!this._editLink) this._editLink = this._container.querySelector('.mapbox-improve-map');
const params = [
{key: "pitch", value: this._map.getPitch()},
{key: "bearing", value: this._map.getBearing()},
{key: "owner", value: this.styleOwner},
{key: "id", value: this.styleId},
{key: "access_token", value: config.ACCESS_TOKEN}
Expand All @@ -79,8 +77,8 @@ class AttributionControl {
if (this._editLink) {
const center = this._map.getCenter();
const paramString = params.reduce((acc, next, i) => acc += next.value !== undefined ? `${next.key}=${next.value}${i < params.length - 1 ? '&' : ''}` : '', `?`);
this._editLink.href = `https://www.mapbox.com/feedback/${paramString}#/${
Math.round(center.lng * 1000) / 1000}/${Math.round(center.lat * 1000) / 1000}/${Math.round(this._map.getZoom())}`;
this._editLink.href = `https://www.mapbox.com/feedback/${paramString}${this._map.hash ? this._map.hash.getHashString() : ''}`

}
}

Expand Down
31 changes: 18 additions & 13 deletions src/ui/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ class Hash {
return this;
}

getHashString() {
const center = this._map.getCenter(),
zoom = this._map.getZoom(),
bearing = this._map.getBearing(),
pitch = this._map.getPitch(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));

let hash = `#${Math.round(zoom * 100) / 100
}/${Math.round(center.lat * Math.pow(10, precision))/Math.pow(10, precision)
}/${Math.round(center.lng * Math.pow(10, precision))/Math.pow(10, precision)}`;

if (bearing || pitch) hash += (`/${Math.round(bearing * 10) / 10}`);
if (pitch) hash += (`/${Math.round(pitch)}`);
return hash;
}

_onHashChange() {
const loc = window.location.hash.replace('#', '').split('/');
if (loc.length >= 3) {
Expand All @@ -57,21 +73,10 @@ class Hash {
}

_updateHash() {
const center = this._map.getCenter(),
zoom = this._map.getZoom(),
bearing = this._map.getBearing(),
pitch = this._map.getPitch(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));

let hash = `#${Math.round(zoom * 100) / 100
}/${center.lat.toFixed(precision)
}/${center.lng.toFixed(precision)}`;

if (bearing || pitch) hash += (`/${Math.round(bearing * 10) / 10}`);
if (pitch) hash += (`/${Math.round(pitch)}`);

const hash = this.getHashString(this._map);
window.history.replaceState('', '', hash);
}

}

module.exports = Hash;
6 changes: 3 additions & 3 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ class Map extends Camera {

bindHandlers(this, options);

this._hash = options.hash && (new Hash()).addTo(this);
this.hash = options.hash && (new Hash()).addTo(this);
// don't set position from options if set through hash
if (!this._hash || !this._hash._onHashChange()) {
if (!this.hash || !this.hash._onHashChange()) {
this.jumpTo({
center: options.center,
zoom: options.zoom,
Expand Down Expand Up @@ -1458,7 +1458,7 @@ class Map extends Camera {
* methods on the map.
*/
remove() {
if (this._hash) this._hash.remove();
if (this.hash) this.hash.remove();
browser.cancelFrame(this._frameId);
this.setStyle(null);
if (typeof window !== 'undefined') {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/ui/control/attribution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function createMap() {
layers: [],
owner: 'mapbox',
id: 'streets-v10',
}
},
hash: true
});

}
Expand Down Expand Up @@ -112,9 +113,9 @@ test('AttributionControl has the correct edit map link', (t) => {
map.addSource('1', {type: 'vector', attribution: '<a class="mapbox-improve-map" href="https://www.mapbox.com/feedback/" target="_blank">Improve this map</a>'});
map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
t.equal(attribution._editLink.href, 'https://www.mapbox.com/feedback/?pitch=0&bearing=0&owner=mapbox&id=streets-v10&access_token=pk.123#/0/0/0', 'edit link contains map location data');
t.equal(attribution._editLink.href, 'https://www.mapbox.com/feedback/?owner=mapbox&id=streets-v10&access_token=pk.123#0/0/0', 'edit link contains map location data');
map.setZoom(2);
t.equal(attribution._editLink.href, 'https://www.mapbox.com/feedback/?pitch=0&bearing=0&owner=mapbox&id=streets-v10&access_token=pk.123#/0/0/2', 'edit link updates on mapmove');
t.equal(attribution._editLink.href, 'https://www.mapbox.com/feedback/?owner=mapbox&id=streets-v10&access_token=pk.123#2/0/0', 'edit link updates on mapmove');
t.end();
}
});
Expand Down

0 comments on commit 00a7082

Please sign in to comment.