changed visibility of overlay properties to protected,

added new getOptions function to overlay, to get the original options

remove trailing underscore from protected properties and functions
This commit is contained in:
Jannes Bolling
2017-10-19 13:06:20 +02:00
parent d33e41d322
commit a802f5937b
2 changed files with 101 additions and 85 deletions
+98 -82
View File
@@ -36,32 +36,38 @@ ol.Overlay = function(options) {
ol.Object.call(this); ol.Object.call(this);
/** /**
* @private * @protected
* @type {number|string|undefined} * @type {olx.OverlayOptions}
*/ */
this.id_ = options.id; this.options = options;
/** /**
* @private * @protected
* @type {number|string|undefined}
*/
this.id = options.id;
/**
* @protected
* @type {boolean} * @type {boolean}
*/ */
this.insertFirst_ = options.insertFirst !== undefined ? this.insertFirst = options.insertFirst !== undefined ?
options.insertFirst : true; options.insertFirst : true;
/** /**
* @private * @protected
* @type {boolean} * @type {boolean}
*/ */
this.stopEvent_ = options.stopEvent !== undefined ? options.stopEvent : true; this.stopEvent = options.stopEvent !== undefined ? options.stopEvent : true;
/** /**
* @private * @protected
* @type {Element} * @type {Element}
*/ */
this.element_ = document.createElement('DIV'); this.element = document.createElement('DIV');
this.element_.className = options.className !== undefined ? this.element.className = options.className !== undefined ?
options.className : 'ol-overlay-container ' + ol.css.CLASS_SELECTABLE; options.className : 'ol-overlay-container ' + ol.css.CLASS_SELECTABLE;
this.element_.style.position = 'absolute'; this.element.style.position = 'absolute';
/** /**
* @protected * @protected
@@ -70,28 +76,28 @@ ol.Overlay = function(options) {
this.autoPan = options.autoPan !== undefined ? options.autoPan : false; this.autoPan = options.autoPan !== undefined ? options.autoPan : false;
/** /**
* @private * @protected
* @type {olx.OverlayPanOptions} * @type {olx.OverlayPanOptions}
*/ */
this.autoPanAnimation_ = options.autoPanAnimation || this.autoPanAnimation = options.autoPanAnimation ||
/** @type {olx.OverlayPanOptions} */ ({}); /** @type {olx.OverlayPanOptions} */ ({});
/** /**
* @private * @protected
* @type {number} * @type {number}
*/ */
this.autoPanMargin_ = options.autoPanMargin !== undefined ? this.autoPanMargin = options.autoPanMargin !== undefined ?
options.autoPanMargin : 20; options.autoPanMargin : 20;
/** /**
* @private * @protected
* @type {{bottom_: string, * @type {{bottom_: string,
* left_: string, * left_: string,
* right_: string, * right_: string,
* top_: string, * top_: string,
* visible: boolean}} * visible: boolean}}
*/ */
this.rendered_ = { this.rendered = {
bottom_: '', bottom_: '',
left_: '', left_: '',
right_: '', right_: '',
@@ -100,29 +106,29 @@ ol.Overlay = function(options) {
}; };
/** /**
* @private * @protected
* @type {?ol.EventsKey} * @type {?ol.EventsKey}
*/ */
this.mapPostrenderListenerKey_ = null; this.mapPostrenderListenerKey = null;
ol.events.listen( ol.events.listen(
this, ol.Object.getChangeEventType(ol.Overlay.Property_.ELEMENT), this, ol.Object.getChangeEventType(ol.Overlay.Property.ELEMENT),
this.handleElementChanged, this); this.handleElementChanged, this);
ol.events.listen( ol.events.listen(
this, ol.Object.getChangeEventType(ol.Overlay.Property_.MAP), this, ol.Object.getChangeEventType(ol.Overlay.Property.MAP),
this.handleMapChanged, this); this.handleMapChanged, this);
ol.events.listen( ol.events.listen(
this, ol.Object.getChangeEventType(ol.Overlay.Property_.OFFSET), this, ol.Object.getChangeEventType(ol.Overlay.Property.OFFSET),
this.handleOffsetChanged, this); this.handleOffsetChanged, this);
ol.events.listen( ol.events.listen(
this, ol.Object.getChangeEventType(ol.Overlay.Property_.POSITION), this, ol.Object.getChangeEventType(ol.Overlay.Property.POSITION),
this.handlePositionChanged, this); this.handlePositionChanged, this);
ol.events.listen( ol.events.listen(
this, ol.Object.getChangeEventType(ol.Overlay.Property_.POSITIONING), this, ol.Object.getChangeEventType(ol.Overlay.Property.POSITIONING),
this.handlePositioningChanged, this); this.handlePositioningChanged, this);
if (options.element !== undefined) { if (options.element !== undefined) {
@@ -151,7 +157,7 @@ ol.inherits(ol.Overlay, ol.Object);
*/ */
ol.Overlay.prototype.getElement = function() { ol.Overlay.prototype.getElement = function() {
return /** @type {Element|undefined} */ ( return /** @type {Element|undefined} */ (
this.get(ol.Overlay.Property_.ELEMENT)); this.get(ol.Overlay.Property.ELEMENT));
}; };
@@ -161,7 +167,7 @@ ol.Overlay.prototype.getElement = function() {
* @api * @api
*/ */
ol.Overlay.prototype.getId = function() { ol.Overlay.prototype.getId = function() {
return this.id_; return this.id;
}; };
@@ -173,7 +179,7 @@ ol.Overlay.prototype.getId = function() {
*/ */
ol.Overlay.prototype.getMap = function() { ol.Overlay.prototype.getMap = function() {
return /** @type {ol.PluggableMap|undefined} */ ( return /** @type {ol.PluggableMap|undefined} */ (
this.get(ol.Overlay.Property_.MAP)); this.get(ol.Overlay.Property.MAP));
}; };
@@ -185,7 +191,7 @@ ol.Overlay.prototype.getMap = function() {
*/ */
ol.Overlay.prototype.getOffset = function() { ol.Overlay.prototype.getOffset = function() {
return /** @type {Array.<number>} */ ( return /** @type {Array.<number>} */ (
this.get(ol.Overlay.Property_.OFFSET)); this.get(ol.Overlay.Property.OFFSET));
}; };
@@ -198,7 +204,7 @@ ol.Overlay.prototype.getOffset = function() {
*/ */
ol.Overlay.prototype.getPosition = function() { ol.Overlay.prototype.getPosition = function() {
return /** @type {ol.Coordinate|undefined} */ ( return /** @type {ol.Coordinate|undefined} */ (
this.get(ol.Overlay.Property_.POSITION)); this.get(ol.Overlay.Property.POSITION));
}; };
@@ -211,7 +217,7 @@ ol.Overlay.prototype.getPosition = function() {
*/ */
ol.Overlay.prototype.getPositioning = function() { ol.Overlay.prototype.getPositioning = function() {
return /** @type {ol.OverlayPositioning} */ ( return /** @type {ol.OverlayPositioning} */ (
this.get(ol.Overlay.Property_.POSITIONING)); this.get(ol.Overlay.Property.POSITIONING));
}; };
@@ -219,10 +225,10 @@ ol.Overlay.prototype.getPositioning = function() {
* @protected * @protected
*/ */
ol.Overlay.prototype.handleElementChanged = function() { ol.Overlay.prototype.handleElementChanged = function() {
ol.dom.removeChildren(this.element_); ol.dom.removeChildren(this.element);
var element = this.getElement(); var element = this.getElement();
if (element) { if (element) {
this.element_.appendChild(element); this.element.appendChild(element);
} }
}; };
@@ -231,22 +237,22 @@ ol.Overlay.prototype.handleElementChanged = function() {
* @protected * @protected
*/ */
ol.Overlay.prototype.handleMapChanged = function() { ol.Overlay.prototype.handleMapChanged = function() {
if (this.mapPostrenderListenerKey_) { if (this.mapPostrenderListenerKey) {
ol.dom.removeNode(this.element_); ol.dom.removeNode(this.element);
ol.events.unlistenByKey(this.mapPostrenderListenerKey_); ol.events.unlistenByKey(this.mapPostrenderListenerKey);
this.mapPostrenderListenerKey_ = null; this.mapPostrenderListenerKey = null;
} }
var map = this.getMap(); var map = this.getMap();
if (map) { if (map) {
this.mapPostrenderListenerKey_ = ol.events.listen(map, this.mapPostrenderListenerKey = ol.events.listen(map,
ol.MapEventType.POSTRENDER, this.render, this); ol.MapEventType.POSTRENDER, this.render, this);
this.updatePixelPosition(); this.updatePixelPosition();
var container = this.stopEvent_ ? var container = this.stopEvent ?
map.getOverlayContainerStopEvent() : map.getOverlayContainer(); map.getOverlayContainerStopEvent() : map.getOverlayContainer();
if (this.insertFirst_) { if (this.insertFirst) {
container.insertBefore(this.element_, container.childNodes[0] || null); container.insertBefore(this.element, container.childNodes[0] || null);
} else { } else {
container.appendChild(this.element_); container.appendChild(this.element);
} }
} }
}; };
@@ -273,8 +279,8 @@ ol.Overlay.prototype.handleOffsetChanged = function() {
*/ */
ol.Overlay.prototype.handlePositionChanged = function() { ol.Overlay.prototype.handlePositionChanged = function() {
this.updatePixelPosition(); this.updatePixelPosition();
if (this.get(ol.Overlay.Property_.POSITION) && this.autoPan) { if (this.get(ol.Overlay.Property.POSITION) && this.autoPan) {
this.panIntoView_(); this.panIntoView();
} }
}; };
@@ -294,7 +300,7 @@ ol.Overlay.prototype.handlePositioningChanged = function() {
* @api * @api
*/ */
ol.Overlay.prototype.setElement = function(element) { ol.Overlay.prototype.setElement = function(element) {
this.set(ol.Overlay.Property_.ELEMENT, element); this.set(ol.Overlay.Property.ELEMENT, element);
}; };
@@ -305,7 +311,7 @@ ol.Overlay.prototype.setElement = function(element) {
* @api * @api
*/ */
ol.Overlay.prototype.setMap = function(map) { ol.Overlay.prototype.setMap = function(map) {
this.set(ol.Overlay.Property_.MAP, map); this.set(ol.Overlay.Property.MAP, map);
}; };
@@ -316,7 +322,7 @@ ol.Overlay.prototype.setMap = function(map) {
* @api * @api
*/ */
ol.Overlay.prototype.setOffset = function(offset) { ol.Overlay.prototype.setOffset = function(offset) {
this.set(ol.Overlay.Property_.OFFSET, offset); this.set(ol.Overlay.Property.OFFSET, offset);
}; };
@@ -329,28 +335,28 @@ ol.Overlay.prototype.setOffset = function(offset) {
* @api * @api
*/ */
ol.Overlay.prototype.setPosition = function(position) { ol.Overlay.prototype.setPosition = function(position) {
this.set(ol.Overlay.Property_.POSITION, position); this.set(ol.Overlay.Property.POSITION, position);
}; };
/** /**
* Pan the map so that the overlay is entirely visible in the current viewport * Pan the map so that the overlay is entirely visible in the current viewport
* (if necessary). * (if necessary).
* @private * @protected
*/ */
ol.Overlay.prototype.panIntoView_ = function() { ol.Overlay.prototype.panIntoView = function() {
var map = this.getMap(); var map = this.getMap();
if (!map || !map.getTargetElement()) { if (!map || !map.getTargetElement()) {
return; return;
} }
var mapRect = this.getRect_(map.getTargetElement(), map.getSize()); var mapRect = this.getRect(map.getTargetElement(), map.getSize());
var element = /** @type {!Element} */ (this.getElement()); var element = /** @type {!Element} */ (this.getElement());
var overlayRect = this.getRect_(element, var overlayRect = this.getRect(element,
[ol.dom.outerWidth(element), ol.dom.outerHeight(element)]); [ol.dom.outerWidth(element), ol.dom.outerHeight(element)]);
var margin = this.autoPanMargin_; var margin = this.autoPanMargin;
if (!ol.extent.containsExtent(mapRect, overlayRect)) { if (!ol.extent.containsExtent(mapRect, overlayRect)) {
// the overlay is not completely inside the viewport, so pan the map // the overlay is not completely inside the viewport, so pan the map
var offsetLeft = overlayRect[0] - mapRect[0]; var offsetLeft = overlayRect[0] - mapRect[0];
@@ -384,8 +390,8 @@ ol.Overlay.prototype.panIntoView_ = function() {
map.getView().animate({ map.getView().animate({
center: map.getCoordinateFromPixel(newCenterPx), center: map.getCoordinateFromPixel(newCenterPx),
duration: this.autoPanAnimation_.duration, duration: this.autoPanAnimation.duration,
easing: this.autoPanAnimation_.easing easing: this.autoPanAnimation.easing
}); });
} }
} }
@@ -397,9 +403,9 @@ ol.Overlay.prototype.panIntoView_ = function() {
* @param {Element|undefined} element The element. * @param {Element|undefined} element The element.
* @param {ol.Size|undefined} size The size of the element. * @param {ol.Size|undefined} size The size of the element.
* @return {ol.Extent} The extent. * @return {ol.Extent} The extent.
* @private * @protected
*/ */
ol.Overlay.prototype.getRect_ = function(element, size) { ol.Overlay.prototype.getRect = function(element, size) {
var box = element.getBoundingClientRect(); var box = element.getBoundingClientRect();
var offsetX = box.left + window.pageXOffset; var offsetX = box.left + window.pageXOffset;
var offsetY = box.top + window.pageYOffset; var offsetY = box.top + window.pageYOffset;
@@ -420,7 +426,7 @@ ol.Overlay.prototype.getRect_ = function(element, size) {
* @api * @api
*/ */
ol.Overlay.prototype.setPositioning = function(positioning) { ol.Overlay.prototype.setPositioning = function(positioning) {
this.set(ol.Overlay.Property_.POSITIONING, positioning); this.set(ol.Overlay.Property.POSITIONING, positioning);
}; };
@@ -430,9 +436,9 @@ ol.Overlay.prototype.setPositioning = function(positioning) {
* @protected * @protected
*/ */
ol.Overlay.prototype.setVisible = function(visible) { ol.Overlay.prototype.setVisible = function(visible) {
if (this.rendered_.visible !== visible) { if (this.rendered.visible !== visible) {
this.element_.style.display = visible ? '' : 'none'; this.element.style.display = visible ? '' : 'none';
this.rendered_.visible = visible; this.rendered.visible = visible;
} }
}; };
@@ -461,7 +467,7 @@ ol.Overlay.prototype.updatePixelPosition = function() {
* @protected * @protected
*/ */
ol.Overlay.prototype.updateRenderedPosition = function(pixel, mapSize) { ol.Overlay.prototype.updateRenderedPosition = function(pixel, mapSize) {
var style = this.element_.style; var style = this.element.style;
var offset = this.getOffset(); var offset = this.getOffset();
var positioning = this.getPositioning(); var positioning = this.getPositioning();
@@ -473,59 +479,69 @@ ol.Overlay.prototype.updateRenderedPosition = function(pixel, mapSize) {
if (positioning == ol.OverlayPositioning.BOTTOM_RIGHT || if (positioning == ol.OverlayPositioning.BOTTOM_RIGHT ||
positioning == ol.OverlayPositioning.CENTER_RIGHT || positioning == ol.OverlayPositioning.CENTER_RIGHT ||
positioning == ol.OverlayPositioning.TOP_RIGHT) { positioning == ol.OverlayPositioning.TOP_RIGHT) {
if (this.rendered_.left_ !== '') { if (this.rendered.left_ !== '') {
this.rendered_.left_ = style.left = ''; this.rendered.left_ = style.left = '';
} }
var right = Math.round(mapSize[0] - pixel[0] - offsetX) + 'px'; var right = Math.round(mapSize[0] - pixel[0] - offsetX) + 'px';
if (this.rendered_.right_ != right) { if (this.rendered.right_ != right) {
this.rendered_.right_ = style.right = right; this.rendered.right_ = style.right = right;
} }
} else { } else {
if (this.rendered_.right_ !== '') { if (this.rendered.right_ !== '') {
this.rendered_.right_ = style.right = ''; this.rendered.right_ = style.right = '';
} }
if (positioning == ol.OverlayPositioning.BOTTOM_CENTER || if (positioning == ol.OverlayPositioning.BOTTOM_CENTER ||
positioning == ol.OverlayPositioning.CENTER_CENTER || positioning == ol.OverlayPositioning.CENTER_CENTER ||
positioning == ol.OverlayPositioning.TOP_CENTER) { positioning == ol.OverlayPositioning.TOP_CENTER) {
offsetX -= this.element_.offsetWidth / 2; offsetX -= this.element.offsetWidth / 2;
} }
var left = Math.round(pixel[0] + offsetX) + 'px'; var left = Math.round(pixel[0] + offsetX) + 'px';
if (this.rendered_.left_ != left) { if (this.rendered.left_ != left) {
this.rendered_.left_ = style.left = left; this.rendered.left_ = style.left = left;
} }
} }
if (positioning == ol.OverlayPositioning.BOTTOM_LEFT || if (positioning == ol.OverlayPositioning.BOTTOM_LEFT ||
positioning == ol.OverlayPositioning.BOTTOM_CENTER || positioning == ol.OverlayPositioning.BOTTOM_CENTER ||
positioning == ol.OverlayPositioning.BOTTOM_RIGHT) { positioning == ol.OverlayPositioning.BOTTOM_RIGHT) {
if (this.rendered_.top_ !== '') { if (this.rendered.top_ !== '') {
this.rendered_.top_ = style.top = ''; this.rendered.top_ = style.top = '';
} }
var bottom = Math.round(mapSize[1] - pixel[1] - offsetY) + 'px'; var bottom = Math.round(mapSize[1] - pixel[1] - offsetY) + 'px';
if (this.rendered_.bottom_ != bottom) { if (this.rendered.bottom_ != bottom) {
this.rendered_.bottom_ = style.bottom = bottom; this.rendered.bottom_ = style.bottom = bottom;
} }
} else { } else {
if (this.rendered_.bottom_ !== '') { if (this.rendered.bottom_ !== '') {
this.rendered_.bottom_ = style.bottom = ''; this.rendered.bottom_ = style.bottom = '';
} }
if (positioning == ol.OverlayPositioning.CENTER_LEFT || if (positioning == ol.OverlayPositioning.CENTER_LEFT ||
positioning == ol.OverlayPositioning.CENTER_CENTER || positioning == ol.OverlayPositioning.CENTER_CENTER ||
positioning == ol.OverlayPositioning.CENTER_RIGHT) { positioning == ol.OverlayPositioning.CENTER_RIGHT) {
offsetY -= this.element_.offsetHeight / 2; offsetY -= this.element.offsetHeight / 2;
} }
var top = Math.round(pixel[1] + offsetY) + 'px'; var top = Math.round(pixel[1] + offsetY) + 'px';
if (this.rendered_.top_ != top) { if (this.rendered.top_ != top) {
this.rendered_.top_ = style.top = top; this.rendered.top_ = style.top = top;
} }
} }
}; };
/** /**
* @enum {string} * returns the options this Overlay has been created with
* @private * @public
* @return {olx.OverlayOptions} overlay options
*/ */
ol.Overlay.Property_ = { ol.Overlay.prototype.getOptions = function() {
return this.options;
};
/**
* @enum {string}
* @protected
*/
ol.Overlay.Property = {
ELEMENT: 'element', ELEMENT: 'element',
MAP: 'map', MAP: 'map',
OFFSET: 'offset', OFFSET: 'offset',
+3 -3
View File
@@ -47,7 +47,7 @@ describe('ol.Overlay', function() {
it('can be constructed with className', function() { it('can be constructed with className', function() {
var instance = new ol.Overlay({className: 'my-class'}); var instance = new ol.Overlay({className: 'my-class'});
expect(instance).to.be.an(ol.Overlay); expect(instance).to.be.an(ol.Overlay);
expect(instance.element_.className).to.be('my-class'); expect(instance.element.className).to.be('my-class');
}); });
}); });
@@ -98,9 +98,9 @@ describe('ol.Overlay', function() {
}); });
map.addOverlay(overlay); map.addOverlay(overlay);
map.renderSync(); map.renderSync();
expect(overlay.element_.style.display).not.to.be('none'); expect(overlay.element.style.display).not.to.be('none');
overlay.setVisible(false); overlay.setVisible(false);
expect(overlay.element_.style.display).to.be('none'); expect(overlay.element.style.display).to.be('none');
}); });
}); });