diff --git a/src/ol/Collection.js b/src/ol/Collection.js index a4c97d06b3..d57fc2e831 100644 --- a/src/ol/Collection.js +++ b/src/ol/Collection.js @@ -17,6 +17,33 @@ const Property = { }; +/** + * @classdesc + * Events emitted by {@link ol.Collection} instances are instances of this + * type. + * + * @constructor + * @extends {ol.events.Event} + * @implements {oli.CollectionEvent} + * @param {ol.CollectionEventType} type Type. + * @param {*=} opt_element Element. + */ +export const CollectionEvent = function(type, opt_element) { + + Event.call(this, type); + + /** + * The element that is added to or removed from the collection. + * @type {*} + * @api + */ + this.element = opt_element; + +}; + +inherits(CollectionEvent, Event); + + /** * @typedef {{unique: (boolean|undefined)}} */ @@ -33,7 +60,7 @@ export let CollectionOptions; * * @constructor * @extends {ol.Object} - * @fires ol.Collection.Event + * @fires ol.CollectionEvent * @param {Array.=} opt_array Array. * @param {CollectionOptions=} opt_options Collection options. * @param {boolean|undefined} opt_options.unique Disallow the same item from @@ -162,7 +189,7 @@ Collection.prototype.insertAt = function(index, elem) { this.array_.splice(index, 0, elem); this.updateLength_(); this.dispatchEvent( - new Collection.Event(CollectionEventType.ADD, elem)); + new CollectionEvent(CollectionEventType.ADD, elem)); }; @@ -222,7 +249,7 @@ Collection.prototype.removeAt = function(index) { const prev = this.array_[index]; this.array_.splice(index, 1); this.updateLength_(); - this.dispatchEvent(new Collection.Event(CollectionEventType.REMOVE, prev)); + this.dispatchEvent(new CollectionEvent(CollectionEventType.REMOVE, prev)); return prev; }; @@ -242,9 +269,9 @@ Collection.prototype.setAt = function(index, elem) { const prev = this.array_[index]; this.array_[index] = elem; this.dispatchEvent( - new Collection.Event(CollectionEventType.REMOVE, prev)); + new CollectionEvent(CollectionEventType.REMOVE, prev)); this.dispatchEvent( - new Collection.Event(CollectionEventType.ADD, elem)); + new CollectionEvent(CollectionEventType.ADD, elem)); } else { let j; for (j = n; j < index; ++j) { @@ -276,29 +303,4 @@ Collection.prototype.assertUnique_ = function(elem, opt_except) { } }; -/** - * @classdesc - * Events emitted by {@link ol.Collection} instances are instances of this - * type. - * - * @constructor - * @extends {ol.events.Event} - * @implements {oli.Collection.Event} - * @param {ol.CollectionEventType} type Type. - * @param {*=} opt_element Element. - */ -Collection.Event = function(type, opt_element) { - - Event.call(this, type); - - /** - * The element that is added to or removed from the collection. - * @type {*} - * @api - */ - this.element = opt_element; - -}; -inherits(Collection.Event, Event); - export default Collection; diff --git a/src/ol/CollectionEventType.js b/src/ol/CollectionEventType.js index 1649783d30..a3f93ae83b 100644 --- a/src/ol/CollectionEventType.js +++ b/src/ol/CollectionEventType.js @@ -8,13 +8,13 @@ export default { /** * Triggered when an item is added to the collection. - * @event ol.Collection.Event#add + * @event ol.CollectionEvent#add * @api */ ADD: 'add', /** * Triggered when an item is removed from the collection. - * @event ol.Collection.Event#remove + * @event ol.CollectionEvent#remove * @api */ REMOVE: 'remove' diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 17394eec2b..5cc9b07e4c 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -18,7 +18,7 @@ import ViewHint from './ViewHint.js'; import {assert} from './asserts.js'; import {removeNode} from './dom.js'; import {listen, unlistenByKey, unlisten} from './events.js'; -import Event from './events/Event.js'; +import {stopPropagation} from './events/Event.js'; import EventType from './events/EventType.js'; import {createEmpty, clone, createOrUpdateEmpty, equals, getForViewAndSize, isEmpty} from './extent.js'; import {TRUE} from './functions.js'; @@ -248,8 +248,7 @@ const PluggableMap = function(options) { EventType.WHEEL ]; for (let i = 0, ii = overlayEvents.length; i < ii; ++i) { - listen(this.overlayContainerStopEvent_, overlayEvents[i], - Event.stopPropagation); + listen(this.overlayContainerStopEvent_, overlayEvents[i], stopPropagation); } this.viewport_.appendChild(this.overlayContainerStopEvent_); @@ -371,7 +370,7 @@ const PluggableMap = function(options) { listen(this.controls, CollectionEventType.ADD, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { event.element.setMap(this); @@ -379,7 +378,7 @@ const PluggableMap = function(options) { listen(this.controls, CollectionEventType.REMOVE, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { event.element.setMap(null); @@ -396,7 +395,7 @@ const PluggableMap = function(options) { listen(this.interactions, CollectionEventType.ADD, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { event.element.setMap(this); @@ -404,7 +403,7 @@ const PluggableMap = function(options) { listen(this.interactions, CollectionEventType.REMOVE, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { event.element.setMap(null); @@ -414,7 +413,7 @@ const PluggableMap = function(options) { listen(this.overlays_, CollectionEventType.ADD, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { this.addOverlayInternal_(/** @type {ol.Overlay} */ (event.element)); @@ -422,7 +421,7 @@ const PluggableMap = function(options) { listen(this.overlays_, CollectionEventType.REMOVE, /** - * @param {ol.Collection.Event} event Collection event. + * @param {ol.CollectionEvent} event CollectionEvent. */ function(event) { const overlay = /** @type {ol.Overlay} */ (event.element); diff --git a/src/ol/control/Attribution.js b/src/ol/control/Attribution.js index b7b36c823c..a6633d8ba3 100644 --- a/src/ol/control/Attribution.js +++ b/src/ol/control/Attribution.js @@ -97,11 +97,9 @@ const Attribution = function(opt_options) { element.appendChild(this.ulElement_); element.appendChild(button); - const render = options.render ? options.render : Attribution.render; - Control.call(this, { element: element, - render: render, + render: options.render || render, target: options.target }); @@ -189,9 +187,9 @@ Attribution.prototype.getSourceAttributions_ = function(frameState) { * @this {ol.control.Attribution} * @api */ -Attribution.render = function(mapEvent) { +export function render(mapEvent) { this.updateElement_(mapEvent.frameState); -}; +} /** diff --git a/src/ol/control/FullScreen.js b/src/ol/control/FullScreen.js index 70ed0e3bef..18e40839f7 100644 --- a/src/ol/control/FullScreen.js +++ b/src/ol/control/FullScreen.js @@ -80,7 +80,7 @@ const FullScreen = function(opt_options) { const tipLabel = options.tipLabel ? options.tipLabel : 'Toggle full-screen'; const button = document.createElement('button'); - button.className = this.cssClassName_ + '-' + FullScreen.isFullScreen(); + button.className = this.cssClassName_ + '-' + isFullScreen(); button.setAttribute('type', 'button'); button.title = tipLabel; button.appendChild(this.labelNode_); @@ -90,7 +90,7 @@ const FullScreen = function(opt_options) { const cssClasses = this.cssClassName_ + ' ' + CLASS_UNSELECTABLE + ' ' + CLASS_CONTROL + ' ' + - (!FullScreen.isFullScreenSupported() ? CLASS_UNSUPPORTED : ''); + (!isFullScreenSupported() ? CLASS_UNSUPPORTED : ''); const element = document.createElement('div'); element.className = cssClasses; element.appendChild(button); @@ -131,15 +131,15 @@ FullScreen.prototype.handleClick_ = function(event) { * @private */ FullScreen.prototype.handleFullScreen_ = function() { - if (!FullScreen.isFullScreenSupported()) { + if (!isFullScreenSupported()) { return; } const map = this.getMap(); if (!map) { return; } - if (FullScreen.isFullScreen()) { - FullScreen.exitFullScreen(); + if (isFullScreen()) { + exitFullScreen(); } else { let element; if (this.source_) { @@ -150,10 +150,10 @@ FullScreen.prototype.handleFullScreen_ = function() { element = map.getTargetElement(); } if (this.keys_) { - FullScreen.requestFullScreenWithKeys(element); + requestFullScreenWithKeys(element); } else { - FullScreen.requestFullScreen(element); + requestFullScreen(element); } } }; @@ -165,7 +165,7 @@ FullScreen.prototype.handleFullScreen_ = function() { FullScreen.prototype.handleFullScreenChange_ = function() { const button = this.element.firstElementChild; const map = this.getMap(); - if (FullScreen.isFullScreen()) { + if (isFullScreen()) { button.className = this.cssClassName_ + '-true'; replaceNode(this.labelActiveNode_, this.labelNode_); } else { @@ -195,7 +195,7 @@ FullScreen.prototype.setMap = function(map) { /** * @return {boolean} Fullscreen is supported by the current platform. */ -FullScreen.isFullScreenSupported = function() { +function isFullScreenSupported() { const body = document.body; return !!( body.webkitRequestFullscreen || @@ -203,23 +203,23 @@ FullScreen.isFullScreenSupported = function() { (body.msRequestFullscreen && document.msFullscreenEnabled) || (body.requestFullscreen && document.fullscreenEnabled) ); -}; +} /** * @return {boolean} Element is currently in fullscreen. */ -FullScreen.isFullScreen = function() { +function isFullScreen() { return !!( document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement ); -}; +} /** * Request to fullscreen an element. * @param {Node} element Element to request fullscreen */ -FullScreen.requestFullScreen = function(element) { +function requestFullScreen(element) { if (element.requestFullscreen) { element.requestFullscreen(); } else if (element.msRequestFullscreen) { @@ -229,26 +229,26 @@ FullScreen.requestFullScreen = function(element) { } else if (element.webkitRequestFullscreen) { element.webkitRequestFullscreen(); } -}; +} /** * Request to fullscreen an element with keyboard input. * @param {Node} element Element to request fullscreen */ -FullScreen.requestFullScreenWithKeys = function(element) { +function requestFullScreenWithKeys(element) { if (element.mozRequestFullScreenWithKeys) { element.mozRequestFullScreenWithKeys(); } else if (element.webkitRequestFullscreen) { element.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); } else { - FullScreen.requestFullScreen(element); + requestFullScreen(element); } -}; +} /** * Exit fullscreen. */ -FullScreen.exitFullScreen = function() { +function exitFullScreen() { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.msExitFullscreen) { @@ -258,6 +258,6 @@ FullScreen.exitFullScreen = function() { } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } -}; +} export default FullScreen; diff --git a/src/ol/control/MousePosition.js b/src/ol/control/MousePosition.js index 4cacf7de63..d7311769f9 100644 --- a/src/ol/control/MousePosition.js +++ b/src/ol/control/MousePosition.js @@ -41,12 +41,9 @@ const MousePosition = function(opt_options) { const element = document.createElement('DIV'); element.className = options.className !== undefined ? options.className : 'ol-mouse-position'; - const render = options.render ? - options.render : MousePosition.render; - Control.call(this, { element: element, - render: render, + render: options.render || render, target: options.target }); @@ -102,7 +99,7 @@ inherits(MousePosition, Control); * @this {ol.control.MousePosition} * @api */ -MousePosition.render = function(mapEvent) { +export function render(mapEvent) { const frameState = mapEvent.frameState; if (!frameState) { this.mapProjection_ = null; @@ -113,7 +110,7 @@ MousePosition.render = function(mapEvent) { } } this.updateHTML_(this.lastMouseMovePixel_); -}; +} /** diff --git a/src/ol/control/OverviewMap.js b/src/ol/control/OverviewMap.js index a0042c2064..e052e0d6bb 100644 --- a/src/ol/control/OverviewMap.js +++ b/src/ol/control/OverviewMap.js @@ -157,11 +157,9 @@ const OverviewMap = function(opt_options) { element.appendChild(this.ovmapDiv_); element.appendChild(button); - const render = options.render ? options.render : OverviewMap.render; - Control.call(this, { element: element, - render: render, + render: options.render || render, target: options.target }); @@ -306,10 +304,10 @@ OverviewMap.prototype.handleRotationChanged_ = function() { * @this {ol.control.OverviewMap} * @api */ -OverviewMap.render = function(mapEvent) { +export function render(mapEvent) { this.validateExtent_(); this.updateBox_(); -}; +} /** diff --git a/src/ol/control/Rotate.js b/src/ol/control/Rotate.js index bb702b2748..328d388902 100644 --- a/src/ol/control/Rotate.js +++ b/src/ol/control/Rotate.js @@ -59,13 +59,11 @@ const Rotate = function(opt_options) { element.className = cssClasses; element.appendChild(button); - const render = options.render ? options.render : Rotate.render; - this.callResetNorth_ = options.resetNorth ? options.resetNorth : undefined; Control.call(this, { element: element, - render: render, + render: options.render || render, target: options.target }); @@ -141,7 +139,7 @@ Rotate.prototype.resetNorth_ = function() { * @this {ol.control.Rotate} * @api */ -Rotate.render = function(mapEvent) { +export function render(mapEvent) { const frameState = mapEvent.frameState; if (!frameState) { return; @@ -162,5 +160,6 @@ Rotate.render = function(mapEvent) { this.label_.style.transform = transform; } this.rotation_ = rotation; -}; +} + export default Rotate; diff --git a/src/ol/control/ScaleLine.js b/src/ol/control/ScaleLine.js index 404c431b57..00091a8eb0 100644 --- a/src/ol/control/ScaleLine.js +++ b/src/ol/control/ScaleLine.js @@ -91,11 +91,9 @@ const ScaleLine = function(opt_options) { */ this.renderedHTML_ = ''; - const render = options.render ? options.render : ScaleLine.render; - Control.call(this, { element: this.element_, - render: render, + render: options.render || render, target: options.target }); @@ -129,7 +127,7 @@ ScaleLine.prototype.getUnits = function() { * @this {ol.control.ScaleLine} * @api */ -ScaleLine.render = function(mapEvent) { +export function render(mapEvent) { const frameState = mapEvent.frameState; if (!frameState) { this.viewState_ = null; @@ -137,7 +135,7 @@ ScaleLine.render = function(mapEvent) { this.viewState_ = frameState.viewState; } this.updateElement_(); -}; +} /** diff --git a/src/ol/control/ZoomSlider.js b/src/ol/control/ZoomSlider.js index 26556cceaa..b5b2ff460e 100644 --- a/src/ol/control/ZoomSlider.js +++ b/src/ol/control/ZoomSlider.js @@ -7,7 +7,7 @@ import Control from '../control/Control.js'; import {CLASS_CONTROL, CLASS_UNSELECTABLE} from '../css.js'; import {easeOut} from '../easing.js'; import {listen} from '../events.js'; -import Event from '../events/Event.js'; +import {stopPropagation} from '../events/Event.js'; import EventType from '../events/EventType.js'; import {clamp} from '../math.js'; import PointerEventType from '../pointer/EventType.js'; @@ -130,16 +130,12 @@ const ZoomSlider = function(opt_options) { listen(this.dragger_, PointerEventType.POINTERUP, this.handleDraggerEnd_, this); - listen(containerElement, EventType.CLICK, - this.handleContainerClick_, this); - listen(thumbElement, EventType.CLICK, - Event.stopPropagation); - - const render = options.render ? options.render : ZoomSlider.render; + listen(containerElement, EventType.CLICK, this.handleContainerClick_, this); + listen(thumbElement, EventType.CLICK, stopPropagation); Control.call(this, { element: containerElement, - render: render + render: options.render || render }); }; @@ -206,7 +202,7 @@ ZoomSlider.prototype.initSlider_ = function() { * @this {ol.control.ZoomSlider} * @api */ -ZoomSlider.render = function(mapEvent) { +export function render(mapEvent) { if (!mapEvent.frameState) { return; } @@ -218,7 +214,7 @@ ZoomSlider.render = function(mapEvent) { this.currentResolution_ = res; this.setThumbPosition_(res); } -}; +} /** diff --git a/src/ol/events/Event.js b/src/ol/events/Event.js index 8756c1bca7..d998e624d1 100644 --- a/src/ol/events/Event.js +++ b/src/ol/events/Event.js @@ -61,15 +61,16 @@ Event.prototype.preventDefault = /** * @param {Event|ol.events.Event} evt Event */ -Event.stopPropagation = function(evt) { +export function stopPropagation(evt) { evt.stopPropagation(); -}; +} /** * @param {Event|ol.events.Event} evt Event */ -Event.preventDefault = function(evt) { +export function preventDefault(evt) { evt.preventDefault(); -}; +} + export default Event; diff --git a/src/ol/interaction/Modify.js b/src/ol/interaction/Modify.js index d58c416b8c..e01300603e 100644 --- a/src/ol/interaction/Modify.js +++ b/src/ol/interaction/Modify.js @@ -355,7 +355,7 @@ Modify.prototype.handleSourceRemove_ = function(event) { /** - * @param {ol.Collection.Event} evt Event. + * @param {ol.CollectionEvent} evt Event. * @private */ Modify.prototype.handleFeatureAdd_ = function(evt) { @@ -377,7 +377,7 @@ Modify.prototype.handleFeatureChange_ = function(evt) { /** - * @param {ol.Collection.Event} evt Event. + * @param {ol.CollectionEvent} evt Event. * @private */ Modify.prototype.handleFeatureRemove_ = function(evt) { diff --git a/src/ol/interaction/Select.js b/src/ol/interaction/Select.js index 593d4f3a7e..df425c54de 100644 --- a/src/ol/interaction/Select.js +++ b/src/ol/interaction/Select.js @@ -349,7 +349,7 @@ Select.getDefaultStyleFunction = function() { /** - * @param {ol.Collection.Event} evt Event. + * @param {ol.CollectionEvent} evt Event. * @private */ Select.prototype.addFeature_ = function(evt) { @@ -361,7 +361,7 @@ Select.prototype.addFeature_ = function(evt) { /** - * @param {ol.Collection.Event} evt Event. + * @param {ol.CollectionEvent} evt Event. * @private */ Select.prototype.removeFeature_ = function(evt) { diff --git a/src/ol/interaction/Snap.js b/src/ol/interaction/Snap.js index 6c5fdf6663..0aaca4e09d 100644 --- a/src/ol/interaction/Snap.js +++ b/src/ol/interaction/Snap.js @@ -2,7 +2,7 @@ * @module ol/interaction/Snap */ import {getUid, inherits} from '../index.js'; -import Collection from '../Collection.js'; +import {CollectionEvent} from '../Collection.js'; import CollectionEventType from '../CollectionEventType.js'; import {distance as coordinateDistance, squaredDistance as squaredCoordinateDistance, closestOnCircle, closestOnSegment, squaredDistanceToSegment} from '../coordinate.js'; import {listen, unlistenByKey} from '../events.js'; @@ -214,14 +214,14 @@ Snap.prototype.getFeatures_ = function() { /** - * @param {ol.source.Vector.Event|ol.Collection.Event} evt Event. + * @param {ol.source.Vector.Event|ol.CollectionEvent} evt Event. * @private */ Snap.prototype.handleFeatureAdd_ = function(evt) { let feature; if (evt instanceof VectorSource.Event) { feature = evt.feature; - } else if (evt instanceof Collection.Event) { + } else if (evt instanceof CollectionEvent) { feature = evt.element; } this.addFeature(/** @type {ol.Feature} */ (feature)); @@ -229,14 +229,14 @@ Snap.prototype.handleFeatureAdd_ = function(evt) { /** - * @param {ol.source.Vector.Event|ol.Collection.Event} evt Event. + * @param {ol.source.Vector.Event|ol.CollectionEvent} evt Event. * @private */ Snap.prototype.handleFeatureRemove_ = function(evt) { let feature; if (evt instanceof VectorSource.Event) { feature = evt.feature; - } else if (evt instanceof Collection.Event) { + } else if (evt instanceof CollectionEvent) { feature = evt.element; } this.removeFeature(/** @type {ol.Feature} */ (feature)); diff --git a/src/ol/layer/Group.js b/src/ol/layer/Group.js index a986ee894c..3a4c516e53 100644 --- a/src/ol/layer/Group.js +++ b/src/ol/layer/Group.js @@ -121,7 +121,7 @@ LayerGroup.prototype.handleLayersChanged_ = function(event) { /** - * @param {ol.Collection.Event} collectionEvent Collection event. + * @param {ol.CollectionEvent} collectionEvent CollectionEvent. * @private */ LayerGroup.prototype.handleLayersAdd_ = function(collectionEvent) { @@ -136,7 +136,7 @@ LayerGroup.prototype.handleLayersAdd_ = function(collectionEvent) { /** - * @param {ol.Collection.Event} collectionEvent Collection event. + * @param {ol.CollectionEvent} collectionEvent CollectionEvent. * @private */ LayerGroup.prototype.handleLayersRemove_ = function(collectionEvent) { diff --git a/test/spec/ol/control/scaleline.test.js b/test/spec/ol/control/scaleline.test.js index f8396f2eb9..5ae93b2f44 100644 --- a/test/spec/ol/control/scaleline.test.js +++ b/test/spec/ol/control/scaleline.test.js @@ -1,6 +1,6 @@ import Map from '../../../../src/ol/Map.js'; import View from '../../../../src/ol/View.js'; -import ScaleLine from '../../../../src/ol/control/ScaleLine.js'; +import ScaleLine, {render} from '../../../../src/ol/control/ScaleLine.js'; import {fromLonLat} from '../../../../src/ol/proj.js'; import Projection from '../../../../src/ol/proj/Projection.js'; @@ -67,12 +67,10 @@ describe('ol.control.ScaleLine', function() { describe('render', function() { it('defaults to `ol.control.ScaleLine.render`', function() { const ctrl = new ScaleLine(); - expect(ctrl.render).to.be(ScaleLine.render); + expect(ctrl.render).to.be(render); }); it('can be configured', function() { - const myRender = function() { - - }; + const myRender = function() {}; const ctrl = new ScaleLine({ render: myRender }); diff --git a/test/spec/ol/events/event.test.js b/test/spec/ol/events/event.test.js index 7ac8974fe3..c308dedb88 100644 --- a/test/spec/ol/events/event.test.js +++ b/test/spec/ol/events/event.test.js @@ -1,4 +1,4 @@ -import Event from '../../../../src/ol/events/Event.js'; +import Event, {preventDefault, stopPropagation} from '../../../../src/ol/events/Event.js'; describe('ol.events.Event', function() { @@ -30,7 +30,7 @@ describe('ol.events.Event', function() { const event = { preventDefault: sinon.spy() }; - Event.preventDefault(event); + preventDefault(event); expect(event.preventDefault.called).to.be(true); }); }); @@ -40,7 +40,7 @@ describe('ol.events.Event', function() { const event = { stopPropagation: sinon.spy() }; - Event.stopPropagation(event); + stopPropagation(event); expect(event.stopPropagation.called).to.be(true); }); });