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

Popup.addClassName() works before popup is on the map (fix #9677) #10143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 22 additions & 15 deletions src/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ export default class Popup extends Evented {
_lngLat: LngLat;
_trackPointer: boolean;
_pos: ?Point;
_classList: DOMTokenList; // stores all user-defined classes

constructor(options: PopupOptions) {
super();
this.options = extend(Object.create(defaultOptions), options);
bindAll(['_update', '_onClose', 'remove', '_onMouseMove', '_onMouseUp', '_onDrag'], this);

const classListElem = DOM.create('div');
classListElem.className = this.options.className;
this._classList = classListElem.classList;
}

/**
Expand Down Expand Up @@ -440,6 +445,7 @@ export default class Popup extends Evented {
* popup.addClassName('some-class')
*/
addClassName(className: string) {
this._classList.add(className);
if (this._container) {
this._container.classList.add(className);
}
Expand All @@ -455,23 +461,12 @@ export default class Popup extends Evented {
* popup.removeClassName('some-class')
*/
removeClassName(className: string) {
this._classList.remove(className);
if (this._container) {
this._container.classList.remove(className);
}
}

/**
* Sets the popup's offset.
*
* @param offset Sets the popup's offset.
* @returns {Popup} `this`
*/
setOffset (offset?: Offset) {
this.options.offset = offset;
this._update();
return this;
}

/**
* Add or remove the given CSS class on the popup container, depending on whether the container currently has that class.
*
Expand All @@ -484,11 +479,24 @@ export default class Popup extends Evented {
* popup.toggleClassName('toggleClass')
*/
toggleClassName(className: string) {
this._classList.toggle(className);
if (this._container) {
return this._container.classList.toggle(className);
}
}

/**
* Sets the popup's offset.
*
* @param offset Sets the popup's offset.
* @returns {Popup} `this`
*/
setOffset (offset?: Offset) {
this.options.offset = offset;
this._update();
return this;
}

_createCloseButton() {
if (this.options.closeButton) {
this._closeButton = DOM.create('button', 'mapboxgl-popup-close-button', this._content);
Expand Down Expand Up @@ -520,9 +528,8 @@ export default class Popup extends Evented {
this._container = DOM.create('div', 'mapboxgl-popup', this._map.getContainer());
this._tip = DOM.create('div', 'mapboxgl-popup-tip', this._container);
this._container.appendChild(this._content);
if (this.options.className) {
this.options.className.split(' ').forEach(name =>
this._container.classList.add(name));
for (let idx = 0; idx < this._classList.length; idx++) {
this._container.classList.add(this._classList.item(idx));
}

if (this._trackPointer) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/ui/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,30 @@ test('Popup adds classes from className option, methods for class manipulations
t.end();
});

test('Popup classes can be manipulated before the popup is on the map', (t) => {
const map = createMap(t);
const popup = new Popup({className: 'some classes'})
.setText('Test')
.setLngLat([0, 0]);

popup.addClassName('addedClass');
popup.removeClassName('some');
popup.toggleClassName('toggle');

t.throws(() => popup.addClassName('should throw exception'), window.DOMException);

popup.addTo(map);

const popupContainer = popup.getElement();

t.ok(!popupContainer.classList.contains('some'));
t.ok(popupContainer.classList.contains('classes'));
t.ok(popupContainer.classList.contains('addedClass'));
t.ok(popupContainer.classList.contains('toggle'));

t.end();
});

test('Cursor-tracked popup disappears on mouseout', (t) => {
const map = createMap(t);

Expand Down