Fix custom controls with user-provided element

This commit is contained in:
Andreas Hocevar
2020-05-15 17:50:44 +02:00
parent 455d0beb52
commit e5167f4e12
11 changed files with 43 additions and 24 deletions

View File

@@ -48,10 +48,10 @@ class Attribution extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
render: options.render, render: options.render,
target: options.target, target: options.target,
}); });
this.createElement();
/** /**
* @private * @private

View File

@@ -50,11 +50,16 @@ class Control extends BaseObject {
constructor(options) { constructor(options) {
super(); super();
const element = options.element;
if (element && !options.target && !element.style.pointerEvents) {
element.style.pointerEvents = 'auto';
}
/** /**
* @protected * @protected
* @type {HTMLElement} * @type {HTMLElement}
*/ */
this.element = options.element ? options.element : null; this.element = element ? element : null;
/** /**
* @private * @private
@@ -83,17 +88,6 @@ class Control extends BaseObject {
} }
} }
/**
* Creates the element for this control and sets it on the instance.
* @return {HTMLDivElement} Element for this control.
*/
createElement() {
const element = document.createElement('div');
element.style.pointerEvents = 'auto';
this.element = element;
return element;
}
/** /**
* Clean up. * Clean up.
*/ */

View File

@@ -72,9 +72,9 @@ class FullScreen extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
target: options.target, target: options.target,
}); });
this.createElement();
/** /**
* @private * @private

View File

@@ -60,15 +60,16 @@ class MousePosition extends Control {
constructor(opt_options) { constructor(opt_options) {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
const element = document.createElement('div');
element.className =
options.className !== undefined ? options.className : 'ol-mouse-position';
super({ super({
element: element,
render: options.render, render: options.render,
target: options.target, target: options.target,
}); });
const element = this.createElement();
element.className =
options.className !== undefined ? options.className : 'ol-mouse-position';
this.addEventListener( this.addEventListener(
getChangeEventType(PROJECTION), getChangeEventType(PROJECTION),
this.handleProjectionChanged_ this.handleProjectionChanged_

View File

@@ -80,10 +80,10 @@ class OverviewMap extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
render: options.render, render: options.render,
target: options.target, target: options.target,
}); });
this.createElement();
/** /**
* @private * @private

View File

@@ -38,10 +38,10 @@ class Rotate extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
render: options.render, render: options.render,
target: options.target, target: options.target,
}); });
this.createElement();
const className = const className =
options.className !== undefined ? options.className : 'ol-rotate'; options.className !== undefined ? options.className : 'ol-rotate';

View File

@@ -86,10 +86,10 @@ class ScaleLine extends Control {
: 'ol-scale-line'; : 'ol-scale-line';
super({ super({
element: document.createElement('div'),
render: options.render, render: options.render,
target: options.target, target: options.target,
}); });
this.createElement();
/** /**
* @private * @private

View File

@@ -37,9 +37,9 @@ class Zoom extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
target: options.target, target: options.target,
}); });
this.createElement();
const className = const className =
options.className !== undefined ? options.className : 'ol-zoom'; options.className !== undefined ? options.className : 'ol-zoom';

View File

@@ -48,9 +48,9 @@ class ZoomSlider extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
render: options.render, render: options.render,
}); });
this.createElement();
/** /**
* @type {!Array.<import("../events.js").EventsKey>} * @type {!Array.<import("../events.js").EventsKey>}

View File

@@ -33,9 +33,9 @@ class ZoomToExtent extends Control {
const options = opt_options ? opt_options : {}; const options = opt_options ? opt_options : {};
super({ super({
element: document.createElement('div'),
target: options.target, target: options.target,
}); });
this.createElement();
/** /**
* @type {?import("../extent.js").Extent} * @type {?import("../extent.js").Extent}

View File

@@ -27,6 +27,30 @@ describe('ol.control.Control', function () {
}); });
}); });
describe('element', function () {
it('sets `pointer-events: auto` for default target', function () {
const control = new Control({
element: document.createElement('div'),
});
expect(control.element.style.pointerEvents).to.be('auto');
});
it('does not set `pointer-events: auto` for custom target', function () {
const control = new Control({
element: document.createElement('div'),
target: document.createElement('div'),
});
expect(control.element.style.pointerEvents).to.be('');
});
it('does not override `pointer-events` style', function () {
const element = document.createElement('div');
element.style.pointerEvents = 'none';
const control = new Control({
element: element,
});
expect(control.element.style.pointerEvents).to.be('none');
});
});
describe("ol.control.Control's target", function () { describe("ol.control.Control's target", function () {
describe('target as string or element', function () { describe('target as string or element', function () {
it('transforms target from string to element', function () { it('transforms target from string to element', function () {