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

MousePosition: allow rendering of empty string #12488

Merged
merged 2 commits into from Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ol/control/MousePosition.js
Expand Up @@ -35,8 +35,8 @@ const COORDINATE_FORMAT = 'coordinateFormat';
* @property {string} [undefinedHTML=' '] Markup to show when coordinates are not
* available (e.g. when the pointer leaves the map viewport). By default, the last position
* will be replaced with `' '` (` `) when the pointer leaves the viewport. To
* retain the last rendered position, set this option to something falsey (like an empty
* string `''`).
* retain the last rendered position, set this option to something falsey. An exception is an
* empty string `''`. It does not count as falsey in this case.
*/

/**
Expand Down Expand Up @@ -89,7 +89,8 @@ class MousePosition extends Control {
* @private
* @type {boolean}
*/
this.renderOnMouseOut_ = !!this.undefinedHTML_;
this.renderOnMouseOut_ =
this.undefinedHTML_ === '' || !!this.undefinedHTML_;

/**
* @private
Expand Down
27 changes: 25 additions & 2 deletions test/browser/spec/ol/control/mouseposition.test.js
Expand Up @@ -105,9 +105,9 @@ describe('ol/control/MousePosition', function () {
expect(element.innerHTML).to.be(' ');
});

it('retains the mouse position when undefinedHTML is falsey and mouse moves outside the viewport', function () {
it('retains the mouse position when undefinedHTML is false and mouse moves outside the viewport', function () {
const ctrl = new MousePosition({
undefinedHTML: '',
undefinedHTML: false,
});
ctrl.setMap(map);
map.renderSync();
Expand All @@ -127,6 +127,29 @@ describe('ol/control/MousePosition', function () {
simulateEvent(EventType.POINTEROUT, width + 1, height + 1);
expect(element.innerHTML).to.be('20,-30');
});

it('renders an empty string if undefinedHTML is an empty string and mouse moves outside the viewport', function () {
const ctrl = new MousePosition({
undefinedHTML: '',
});
ctrl.setMap(map);
map.renderSync();

const element = document.querySelector(
'.ol-mouse-position',
map.getTarget()
);

simulateEvent(EventType.POINTEROUT, width + 1, height + 1);
expect(element.innerHTML).to.be('');

target.dispatchEvent(new PointerEvent('pointermove'));
simulateEvent(EventType.POINTERMOVE, 20, 30);
expect(element.innerHTML).to.be('20,-30');

simulateEvent(EventType.POINTEROUT, width + 1, height + 1);
expect(element.innerHTML).to.be('');
});
});
});
});