Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent changing bearing via URL hash when rotation is disabled #9156

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ui/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ class Hash {
_onHashChange() {
const loc = this._getCurrentHash();
if (loc.length >= 3 && !loc.some(v => isNaN(v))) {
const bearing = this._map.dragRotate.isEnabled() && this._map.touchZoomRotate.isEnabled() ? +loc[3] : this._map.getBearing() || 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: getBearing() always returns a number, so there shouldn't be a need for || 0.

this._map.jumpTo({
center: [+loc[2], +loc[1]],
zoom: +loc[0],
bearing: +(loc[3] || 0),
bearing,
pitch: +(loc[4] || 0)
});
return true;
Expand Down
32 changes: 32 additions & 0 deletions test/unit/ui/hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ test('hash', (t) => {
t.equal(map.getBearing(), 0);
t.equal(map.getPitch(), 0);

// map is created with `interactive: false`
// so explicitly enable rotation for this test
map.dragRotate.enable();
map.touchZoomRotate.enable();

window.location.hash = '#5/1.00/0.50/30/60';

hash._onHashChange();
Expand All @@ -67,6 +72,33 @@ test('hash', (t) => {
t.equal(map.getBearing(), 30);
t.equal(map.getPitch(), 60);

// disable rotation to test that updating
// the hash's bearing won't change the map
map.dragRotate.disable();
map.touchZoomRotate.disable();

window.location.hash = '#5/1.00/0.50/-45/60';

hash._onHashChange();

t.equal(map.getCenter().lng, 0.5);
t.equal(map.getCenter().lat, 1);
t.equal(map.getZoom(), 5);
t.equal(map.getBearing(), 30);
t.equal(map.getPitch(), 60);

// test that a hash with no bearing resets
// to the previous bearing when rotation is disabled
window.location.hash = '#5/1.00/0.50/';

hash._onHashChange();

t.equal(map.getCenter().lng, 0.5);
t.equal(map.getCenter().lat, 1);
t.equal(map.getZoom(), 5);
t.equal(map.getBearing(), 30);
t.equal(window.location.hash, '#5/1/0.5/30');

window.location.hash = '#4/wrongly/formed/hash';

t.false(hash._onHashChange());
Expand Down