Skip to content

Commit

Permalink
add call to update and check before classList
Browse files Browse the repository at this point in the history
  • Loading branch information
zxwandrew authored and Asheem Mamoowala committed Sep 17, 2019
1 parent 4a61c42 commit 102ec0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export default class Popup extends Evented {
if (this._trackPointer) {
this._map.on('mousemove', (e) => { this._update(e.point); });
this._map.on('mouseup', (e) => { this._update(e.point); });
this._container.classList.add('mapboxgl-popup-track-pointer');
if (this._container) {
this._container.classList.add('mapboxgl-popup-track-pointer');
}
this._map._canvasContainer.classList.add('mapboxgl-track-pointer');
} else {
this._map.on('move', this._update);
Expand Down Expand Up @@ -210,7 +212,9 @@ export default class Popup extends Evented {
if (this._map) {
this._map.on('move', this._update);
this._map.off('mousemove');
this._container.classList.remove('mapboxgl-popup-track-pointer');
if (this._container) {
this._container.classList.remove('mapboxgl-popup-track-pointer');
}
this._map._canvasContainer.classList.remove('mapboxgl-track-pointer');
}

Expand All @@ -225,12 +229,14 @@ export default class Popup extends Evented {
trackPointer() {
this._trackPointer = true;
this._pos = null;

this._update();
if (this._map) {
this._map.off('move', this._update);
this._map.on('mousemove', (e) => { this._update(e.point); });
this._map.on('drag', (e) => { this._update(e.point); });
this._container.classList.add('mapboxgl-popup-track-pointer');
if (this._container) {
this._container.classList.add('mapboxgl-popup-track-pointer');
}
this._map._canvasContainer.classList.add('mapboxgl-track-pointer');
}

Expand Down Expand Up @@ -349,7 +355,6 @@ export default class Popup extends Evented {
}

_update(cursor: PointLike) {

const hasPosition = this._lngLat || this._trackPointer;

if (!this._map || !hasPosition || !this._content) { return; }
Expand All @@ -362,7 +367,9 @@ export default class Popup extends Evented {
this.options.className.split(' ').forEach(name =>
this._container.classList.add(name));
}

if (this._trackPointer) {
this._container.classList.add('mapboxgl-popup-track-pointer');
}
}

if (this.options.maxWidth && this._container.style.maxWidth !== this.options.maxWidth) {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,30 @@ test('Pointer-tracked popup is tagged with right class', (t) => {
t.end();
});

test('Pointer-tracked popup with content set later is tagged with right class ', (t) => {
const map = createMap(t);
const popup = new Popup()
.trackPointer()
.addTo(map);

popup.setText("Test");

t.equal(popup._container.classList.value.includes('mapboxgl-popup-track-pointer'), true);
t.end();
});

test('Pointer-tracked popup that is set afterwards is tagged with right class ', (t) => {
const map = createMap(t);
const popup = new Popup()
.addTo(map);

popup.setText("Test");
popup.trackPointer();

t.equal(popup._container.classList.value.includes('mapboxgl-popup-track-pointer'), true);
t.end();
});

test('Pointer-tracked popup can be repositioned with setLngLat', (t) => {
const map = createMap(t);
const popup = new Popup()
Expand All @@ -540,6 +564,7 @@ test('Pointer-tracked popup can be repositioned with setLngLat', (t) => {
.addTo(map);

t.deepEqual(popup._pos, map.project([0, 0]));
t.equal(popup._container.classList.value.includes('mapboxgl-popup-track-pointer'), false);
t.end();
});

Expand Down

0 comments on commit 102ec0c

Please sign in to comment.