diff --git a/src/ui/control/attribution_control.js b/src/ui/control/attribution_control.js index 78a299c08b2..dfceca415db 100644 --- a/src/ui/control/attribution_control.js +++ b/src/ui/control/attribution_control.js @@ -69,18 +69,20 @@ 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} ]; 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())}`; + const paramString = params.reduce((acc, next, i) => { + if (next.value !== undefined) { + acc += `${next.key}=${next.value}${i < params.length - 1 ? '&' : ''}`; + } + return acc; + }, `?`); + this._editLink.href = `https://www.mapbox.com/feedback/${paramString}${this._map.hash ? this._map.hash.getHashString() : ''}`; + } } diff --git a/src/ui/hash.js b/src/ui/hash.js index 9068c055d7b..9ed7bc77f0f 100644 --- a/src/ui/hash.js +++ b/src/ui/hash.js @@ -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) { @@ -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; diff --git a/src/ui/map.js b/src/ui/map.js index 315edd95540..1dc42956e09 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -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, @@ -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') { diff --git a/test/unit/ui/control/attribution.test.js b/test/unit/ui/control/attribution.test.js index f30591f4907..a4e42cf219f 100644 --- a/test/unit/ui/control/attribution.test.js +++ b/test/unit/ui/control/attribution.test.js @@ -18,7 +18,8 @@ function createMap() { layers: [], owner: 'mapbox', id: 'streets-v10', - } + }, + hash: true }); } @@ -112,9 +113,9 @@ test('AttributionControl has the correct edit map link', (t) => { map.addSource('1', {type: 'vector', attribution: 'Improve this map'}); 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(); } });