Replace new clearOnMouseOut option with undefinedHTML=undefined

API break: set `undefinedHTML` to special value `undefined` to retain
the most recent MousePosition value.
This commit is contained in:
Pete Schmitt
2018-02-08 11:49:27 -07:00
committed by ahocevar
parent 2e3e7d282a
commit 2719baa0f6
2 changed files with 17 additions and 15 deletions
+9 -5
View File
@@ -72,13 +72,17 @@ const MousePosition = function(opt_options) {
this.setProjection(options.projection); this.setProjection(options.projection);
} }
this.clearOnMouseOut_ = options.clearOnMouseOut !== undefined ? options.clearOnMouseOut : true;
/** /**
* @private * @private
* @type {string} * @type {string}
*/ */
this.undefinedHTML_ = options.undefinedHTML !== undefined ? options.undefinedHTML : ''; this.undefinedHTML_ = 'undefinedHTML' in options ? options.undefinedHTML : '';
/**
* @private
* @type {boolean}
*/
this.renderOnMouseOut_ = this.undefinedHTML_ !== undefined;
/** /**
* @private * @private
@@ -194,7 +198,7 @@ MousePosition.prototype.setMap = function(map) {
this.listenerKeys.push( this.listenerKeys.push(
listen(viewport, EventType.MOUSEMOVE, this.handleMouseMove, this) listen(viewport, EventType.MOUSEMOVE, this.handleMouseMove, this)
); );
if (this.clearOnMouseOut_) { if (this.renderOnMouseOut_) {
this.listenerKeys.push( this.listenerKeys.push(
listen(viewport, EventType.MOUSEOUT, this.handleMouseOut, this) listen(viewport, EventType.MOUSEOUT, this.handleMouseOut, this)
); );
@@ -232,7 +236,7 @@ MousePosition.prototype.setProjection = function(projection) {
* @private * @private
*/ */
MousePosition.prototype.updateHTML_ = function(pixel) { MousePosition.prototype.updateHTML_ = function(pixel) {
let html = this.undefinedHTML_; let html = this.undefinedHTML_ === undefined ? ' ' : this.undefinedHTML_;
if (pixel && this.mapProjection_) { if (pixel && this.mapProjection_) {
if (!this.transform_) { if (!this.transform_) {
const projection = this.getProjection(); const projection = this.getProjection();
+8 -10
View File
@@ -65,40 +65,38 @@ describe('ol.control.MousePosition', function() {
document.querySelector('div.ol-viewport').dispatchEvent(evt); document.querySelector('div.ol-viewport').dispatchEvent(evt);
} }
describe('clearOnMouseOut', function() { describe('undefinedHTML', function() {
it('sets div.ol-mouse-position undefinedHTML when clearOnMouseOut=true', function(done) { it('sets div.ol-mouse-position to options.undefinedHTML when mouse moves out', function(done) {
const ctrl = new MousePosition({ const ctrl = new MousePosition({
undefinedHTML: 'undefined', undefinedHTML: 'some text'
clearOnMouseOut: true
}); });
ctrl.setMap(map); ctrl.setMap(map);
map.renderSync(); map.renderSync();
const element = document.querySelector('.ol-mouse-position', map.getTarget()); const element = document.querySelector('.ol-mouse-position', map.getTarget());
expect(element.innerText).to.be('undefined'); expect(element.innerText).to.be('some text');
simulateEvent(EventType.MOUSEMOVE, 20, 30); simulateEvent(EventType.MOUSEMOVE, 20, 30);
map.renderSync(); map.renderSync();
expect(element.innerText).to.be('20,-30'); expect(element.innerText).to.be('20,-30');
map.once('postrender', function() { map.once('postrender', function() {
expect(element.innerText).to.be('undefined'); expect(element.innerText).to.be('some text');
done(); done();
}); });
simulateEvent(EventType.MOUSEOUT, width + 1, height + 1); simulateEvent(EventType.MOUSEOUT, width + 1, height + 1);
map.renderSync(); map.renderSync();
}); });
it('keeps div.ol-mouse-position set when clearOnMouseOut=false', function(done) { it('Retain mouse position in div.ol-mouse-position when options.undefinedHTML=undefined and mouse moves outside the viewport', function(done) {
const ctrl = new MousePosition({ const ctrl = new MousePosition({
undefinedHTML: 'undefined', undefinedHTML: undefined
clearOnMouseOut: false
}); });
ctrl.setMap(map); ctrl.setMap(map);
map.renderSync(); map.renderSync();
const element = document.querySelector('.ol-mouse-position', map.getTarget()); const element = document.querySelector('.ol-mouse-position', map.getTarget());
expect(element.innerText).to.be('undefined'); expect(element.innerHTML).to.be(' ');
target.dispatchEvent(new MouseEvent('mousemove')); target.dispatchEvent(new MouseEvent('mousemove'));
simulateEvent(EventType.MOUSEMOVE, 20, 30); simulateEvent(EventType.MOUSEMOVE, 20, 30);