From ffe3565166cff3a907aa120e26400cf75457396d Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 6 Aug 2016 10:21:19 -0600 Subject: [PATCH] Single provide from ol/collection.js --- changelog/upgrade-notes.md | 7 ++ externs/oli.js | 4 +- src/ol/collection.js | 102 ++++++++++++------------ src/ol/interaction/modifyinteraction.js | 9 +-- src/ol/interaction/selectinteraction.js | 10 +-- src/ol/interaction/snapinteraction.js | 14 ++-- src/ol/layer/layergroup.js | 10 +-- src/ol/map.js | 25 +++--- src/ol/source/vectorsource.js | 5 +- test/spec/ol/collection.test.js | 23 +++--- 10 files changed, 103 insertions(+), 106 deletions(-) diff --git a/changelog/upgrade-notes.md b/changelog/upgrade-notes.md index b9dd996646..e386e0399c 100644 --- a/changelog/upgrade-notes.md +++ b/changelog/upgrade-notes.md @@ -12,6 +12,13 @@ This option was previously needed to use named colors with the WebGL renderer bu The URL constructor is supported by all modern browsers, but not by older ones, such as IE. To use the KML format in such older browsers, a URL polyfill will have to be loaded before use. +#### Changes only relevant to those who compile their applications together with the Closure Compiler + +A number of internal types have been renamed. This will not affect those who use the API provided by the library, but if you are compiling your application together with OpenLayers and using type names, you'll need to do the following: + + * rename `ol.CollectionEvent` to `ol.Collection.Event` + * rename `ol.CollectionEventType` to `ol.Collection.EventType` + ### v3.17.0 #### `ol.source.MapQuest` removal diff --git a/externs/oli.js b/externs/oli.js index d7cdcce0eb..305b8fe033 100644 --- a/externs/oli.js +++ b/externs/oli.js @@ -52,13 +52,13 @@ oli.events.Event.prototype.stopPropagation = function() {}; /** * @interface */ -oli.CollectionEvent = function() {}; +oli.Collection.Event = function() {}; /** * @type {*} */ -oli.CollectionEvent.prototype.element; +oli.Collection.Event.prototype.element; /** diff --git a/src/ol/collection.js b/src/ol/collection.js index 5ee4271eae..191b389a8d 100644 --- a/src/ol/collection.js +++ b/src/ol/collection.js @@ -4,58 +4,11 @@ */ goog.provide('ol.Collection'); -goog.provide('ol.CollectionEvent'); -goog.provide('ol.CollectionEventType'); goog.require('ol.events.Event'); goog.require('ol.Object'); -/** - * @enum {string} - */ -ol.CollectionEventType = { - /** - * Triggered when an item is added to the collection. - * @event ol.CollectionEvent#add - * @api stable - */ - ADD: 'add', - /** - * Triggered when an item is removed from the collection. - * @event ol.CollectionEvent#remove - * @api stable - */ - REMOVE: 'remove' -}; - - -/** - * @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. - */ -ol.CollectionEvent = function(type, opt_element) { - - ol.events.Event.call(this, type); - - /** - * The element that is added to or removed from the collection. - * @type {*} - * @api stable - */ - this.element = opt_element; - -}; -ol.inherits(ol.CollectionEvent, ol.events.Event); - - /** * @enum {string} */ @@ -74,7 +27,7 @@ ol.CollectionProperty = { * * @constructor * @extends {ol.Object} - * @fires ol.CollectionEvent + * @fires ol.Collection.Event * @param {!Array.=} opt_array Array. * @template T * @api stable @@ -181,7 +134,7 @@ ol.Collection.prototype.insertAt = function(index, elem) { this.array_.splice(index, 0, elem); this.updateLength_(); this.dispatchEvent( - new ol.CollectionEvent(ol.CollectionEventType.ADD, elem)); + new ol.Collection.Event(ol.Collection.EventType.ADD, elem)); }; @@ -239,7 +192,7 @@ ol.Collection.prototype.removeAt = function(index) { this.array_.splice(index, 1); this.updateLength_(); this.dispatchEvent( - new ol.CollectionEvent(ol.CollectionEventType.REMOVE, prev)); + new ol.Collection.Event(ol.Collection.EventType.REMOVE, prev)); return prev; }; @@ -256,9 +209,9 @@ ol.Collection.prototype.setAt = function(index, elem) { var prev = this.array_[index]; this.array_[index] = elem; this.dispatchEvent( - new ol.CollectionEvent(ol.CollectionEventType.REMOVE, prev)); + new ol.Collection.Event(ol.Collection.EventType.REMOVE, prev)); this.dispatchEvent( - new ol.CollectionEvent(ol.CollectionEventType.ADD, elem)); + new ol.Collection.Event(ol.Collection.EventType.ADD, elem)); } else { var j; for (j = n; j < index; ++j) { @@ -275,3 +228,48 @@ ol.Collection.prototype.setAt = function(index, elem) { ol.Collection.prototype.updateLength_ = function() { this.set(ol.CollectionProperty.LENGTH, this.array_.length); }; + + +/** + * @enum {string} + */ +ol.Collection.EventType = { + /** + * Triggered when an item is added to the collection. + * @event ol.Collection.Event#add + * @api stable + */ + ADD: 'add', + /** + * Triggered when an item is removed from the collection. + * @event ol.Collection.Event#remove + * @api stable + */ + REMOVE: 'remove' +}; + + +/** + * @classdesc + * Events emitted by {@link ol.Collection} instances are instances of this + * type. + * + * @constructor + * @extends {ol.events.Event} + * @implements {oli.Collection.Event} + * @param {ol.Collection.EventType} type Type. + * @param {*=} opt_element Element. + */ +ol.Collection.Event = function(type, opt_element) { + + ol.events.Event.call(this, type); + + /** + * The element that is added to or removed from the collection. + * @type {*} + * @api stable + */ + this.element = opt_element; + +}; +ol.inherits(ol.Collection.Event, ol.events.Event); diff --git a/src/ol/interaction/modifyinteraction.js b/src/ol/interaction/modifyinteraction.js index 542bcf2634..c934d14c08 100644 --- a/src/ol/interaction/modifyinteraction.js +++ b/src/ol/interaction/modifyinteraction.js @@ -6,7 +6,6 @@ goog.require('ol.events.Event'); goog.require('ol.events.EventType'); goog.require('ol'); goog.require('ol.Collection'); -goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); goog.require('ol.MapBrowserEvent.EventType'); goog.require('ol.MapBrowserPointerEvent'); @@ -232,9 +231,9 @@ ol.interaction.Modify = function(options) { this.features_ = options.features; this.features_.forEach(this.addFeature_, this); - ol.events.listen(this.features_, ol.CollectionEventType.ADD, + ol.events.listen(this.features_, ol.Collection.EventType.ADD, this.handleFeatureAdd_, this); - ol.events.listen(this.features_, ol.CollectionEventType.REMOVE, + ol.events.listen(this.features_, ol.Collection.EventType.REMOVE, this.handleFeatureRemove_, this); /** @@ -327,7 +326,7 @@ ol.interaction.Modify.prototype.setMap = function(map) { /** - * @param {ol.CollectionEvent} evt Event. + * @param {ol.Collection.Event} evt Event. * @private */ ol.interaction.Modify.prototype.handleFeatureAdd_ = function(evt) { @@ -349,7 +348,7 @@ ol.interaction.Modify.prototype.handleFeatureChange_ = function(evt) { /** - * @param {ol.CollectionEvent} evt Event. + * @param {ol.Collection.Event} evt Event. * @private */ ol.interaction.Modify.prototype.handleFeatureRemove_ = function(evt) { diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index f2e739d2ad..56e11868f8 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -3,7 +3,7 @@ goog.provide('ol.interaction.SelectEvent'); goog.provide('ol.interaction.SelectEventType'); goog.require('ol.functions'); -goog.require('ol.CollectionEventType'); +goog.require('ol.Collection'); goog.require('ol.Feature'); goog.require('ol.array'); goog.require('ol.events'); @@ -185,9 +185,9 @@ ol.interaction.Select = function(opt_options) { this.featureLayerAssociation_ = {}; var features = this.featureOverlay_.getSource().getFeaturesCollection(); - ol.events.listen(features, ol.CollectionEventType.ADD, + ol.events.listen(features, ol.Collection.EventType.ADD, this.addFeature_, this); - ol.events.listen(features, ol.CollectionEventType.REMOVE, + ol.events.listen(features, ol.Collection.EventType.REMOVE, this.removeFeature_, this); }; @@ -355,7 +355,7 @@ ol.interaction.Select.getDefaultStyleFunction = function() { /** - * @param {ol.CollectionEvent} evt Event. + * @param {ol.Collection.Event} evt Event. * @private */ ol.interaction.Select.prototype.addFeature_ = function(evt) { @@ -367,7 +367,7 @@ ol.interaction.Select.prototype.addFeature_ = function(evt) { /** - * @param {ol.CollectionEvent} evt Event. + * @param {ol.Collection.Event} evt Event. * @private */ ol.interaction.Select.prototype.removeFeature_ = function(evt) { diff --git a/src/ol/interaction/snapinteraction.js b/src/ol/interaction/snapinteraction.js index 8276c9d638..fcbb248864 100644 --- a/src/ol/interaction/snapinteraction.js +++ b/src/ol/interaction/snapinteraction.js @@ -3,8 +3,6 @@ goog.provide('ol.interaction.SnapProperty'); goog.require('ol'); goog.require('ol.Collection'); -goog.require('ol.CollectionEvent'); -goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); goog.require('ol.Object'); goog.require('ol.Observable'); @@ -232,14 +230,14 @@ ol.interaction.Snap.prototype.getFeatures_ = function() { /** - * @param {ol.source.VectorEvent|ol.CollectionEvent} evt Event. + * @param {ol.source.VectorEvent|ol.Collection.Event} evt Event. * @private */ ol.interaction.Snap.prototype.handleFeatureAdd_ = function(evt) { var feature; if (evt instanceof ol.source.VectorEvent) { feature = evt.feature; - } else if (evt instanceof ol.CollectionEvent) { + } else if (evt instanceof ol.Collection.Event) { feature = evt.element; } this.addFeature(/** @type {ol.Feature} */ (feature)); @@ -247,14 +245,14 @@ ol.interaction.Snap.prototype.handleFeatureAdd_ = function(evt) { /** - * @param {ol.source.VectorEvent|ol.CollectionEvent} evt Event. + * @param {ol.source.VectorEvent|ol.Collection.Event} evt Event. * @private */ ol.interaction.Snap.prototype.handleFeatureRemove_ = function(evt) { var feature; if (evt instanceof ol.source.VectorEvent) { feature = evt.feature; - } else if (evt instanceof ol.CollectionEvent) { + } else if (evt instanceof ol.Collection.Event) { feature = evt.element; } this.removeFeature(/** @type {ol.Feature} */ (feature)); @@ -342,9 +340,9 @@ ol.interaction.Snap.prototype.setMap = function(map) { if (map) { if (this.features_) { keys.push( - ol.events.listen(this.features_, ol.CollectionEventType.ADD, + ol.events.listen(this.features_, ol.Collection.EventType.ADD, this.handleFeatureAdd_, this), - ol.events.listen(this.features_, ol.CollectionEventType.REMOVE, + ol.events.listen(this.features_, ol.Collection.EventType.REMOVE, this.handleFeatureRemove_, this) ); } else if (this.source_) { diff --git a/src/ol/layer/layergroup.js b/src/ol/layer/layergroup.js index 87e6167b5a..c63b6b4cda 100644 --- a/src/ol/layer/layergroup.js +++ b/src/ol/layer/layergroup.js @@ -1,8 +1,6 @@ goog.provide('ol.layer.Group'); goog.require('ol.Collection'); -goog.require('ol.CollectionEvent'); -goog.require('ol.CollectionEventType'); goog.require('ol.Object'); goog.require('ol.ObjectEventType'); goog.require('ol.events'); @@ -97,9 +95,9 @@ ol.layer.Group.prototype.handleLayersChanged_ = function(event) { var layers = this.getLayers(); this.layersListenerKeys_.push( - ol.events.listen(layers, ol.CollectionEventType.ADD, + ol.events.listen(layers, ol.Collection.EventType.ADD, this.handleLayersAdd_, this), - ol.events.listen(layers, ol.CollectionEventType.REMOVE, + ol.events.listen(layers, ol.Collection.EventType.REMOVE, this.handleLayersRemove_, this)); for (var id in this.listenerKeys_) { @@ -124,7 +122,7 @@ ol.layer.Group.prototype.handleLayersChanged_ = function(event) { /** - * @param {ol.CollectionEvent} collectionEvent Collection event. + * @param {ol.Collection.Event} collectionEvent Collection event. * @private */ ol.layer.Group.prototype.handleLayersAdd_ = function(collectionEvent) { @@ -143,7 +141,7 @@ ol.layer.Group.prototype.handleLayersAdd_ = function(collectionEvent) { /** - * @param {ol.CollectionEvent} collectionEvent Collection event. + * @param {ol.Collection.Event} collectionEvent Collection event. * @private */ ol.layer.Group.prototype.handleLayersRemove_ = function(collectionEvent) { diff --git a/src/ol/map.js b/src/ol/map.js index 62e2ca4009..c5b1f2864e 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -7,7 +7,6 @@ goog.provide('ol.MapProperty'); goog.require('goog.async.nextTick'); goog.require('ol.Collection'); -goog.require('ol.CollectionEventType'); goog.require('ol.MapBrowserEvent'); goog.require('ol.MapBrowserEvent.EventType'); goog.require('ol.MapBrowserEventHandler'); @@ -404,17 +403,17 @@ ol.Map = function(options) { control.setMap(this); }, this); - ol.events.listen(this.controls_, ol.CollectionEventType.ADD, + ol.events.listen(this.controls_, ol.Collection.EventType.ADD, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { event.element.setMap(this); }, this); - ol.events.listen(this.controls_, ol.CollectionEventType.REMOVE, + ol.events.listen(this.controls_, ol.Collection.EventType.REMOVE, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { event.element.setMap(null); @@ -429,17 +428,17 @@ ol.Map = function(options) { interaction.setMap(this); }, this); - ol.events.listen(this.interactions_, ol.CollectionEventType.ADD, + ol.events.listen(this.interactions_, ol.Collection.EventType.ADD, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { event.element.setMap(this); }, this); - ol.events.listen(this.interactions_, ol.CollectionEventType.REMOVE, + ol.events.listen(this.interactions_, ol.Collection.EventType.REMOVE, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { event.element.setMap(null); @@ -447,17 +446,17 @@ ol.Map = function(options) { this.overlays_.forEach(this.addOverlayInternal_, this); - ol.events.listen(this.overlays_, ol.CollectionEventType.ADD, + ol.events.listen(this.overlays_, ol.Collection.EventType.ADD, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { this.addOverlayInternal_(/** @type {ol.Overlay} */ (event.element)); }, this); - ol.events.listen(this.overlays_, ol.CollectionEventType.REMOVE, + ol.events.listen(this.overlays_, ol.Collection.EventType.REMOVE, /** - * @param {ol.CollectionEvent} event Collection event. + * @param {ol.Collection.Event} event Collection event. */ function(event) { var id = event.element.getId(); diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index 4a0974515c..4cb81e3527 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -7,7 +7,6 @@ goog.provide('ol.source.VectorEventType'); goog.require('ol'); goog.require('ol.Collection'); -goog.require('ol.CollectionEventType'); goog.require('ol.Feature'); goog.require('ol.ObjectEventType'); goog.require('ol.array'); @@ -348,7 +347,7 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) { modifyingCollection = false; } }); - ol.events.listen(collection, ol.CollectionEventType.ADD, + ol.events.listen(collection, ol.Collection.EventType.ADD, function(evt) { if (!modifyingCollection) { modifyingCollection = true; @@ -356,7 +355,7 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) { modifyingCollection = false; } }, this); - ol.events.listen(collection, ol.CollectionEventType.REMOVE, + ol.events.listen(collection, ol.Collection.EventType.REMOVE, function(evt) { if (!modifyingCollection) { modifyingCollection = true; diff --git a/test/spec/ol/collection.test.js b/test/spec/ol/collection.test.js index 94fd5443e9..468f0eecae 100644 --- a/test/spec/ol/collection.test.js +++ b/test/spec/ol/collection.test.js @@ -114,7 +114,7 @@ describe('ol.collection', function() { it('fires a remove event', function() { var collection = new ol.Collection([0, 1, 2]); var cb = sinon.spy(); - ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb); + ol.events.listen(collection, ol.Collection.EventType.REMOVE, cb); expect(collection.remove(1)).to.eql(1); expect(cb).to.be.called(); expect(cb.lastCall.args[0].element).to.eql(1); @@ -137,11 +137,11 @@ describe('ol.collection', function() { it('does dispatch events', function() { var collection = new ol.Collection(['a', 'b']); var added, removed; - ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) { + ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) { added = e.element; }); ol.events.listen( - collection, ol.CollectionEventType.REMOVE, function(e) { + collection, ol.Collection.EventType.REMOVE, function(e) { removed = e.element; }); collection.setAt(1, 1); @@ -155,7 +155,7 @@ describe('ol.collection', function() { var collection = new ol.Collection(['a']); var removed; ol.events.listen( - collection, ol.CollectionEventType.REMOVE, function(e) { + collection, ol.Collection.EventType.REMOVE, function(e) { removed = e.element; }); collection.pop(); @@ -168,7 +168,7 @@ describe('ol.collection', function() { var collection = new ol.Collection([0, 2]); var added; ol.events.listen( - collection, ol.CollectionEventType.ADD, function(e) { + collection, ol.Collection.EventType.ADD, function(e) { added = e.element; }); collection.insertAt(1, 1); @@ -180,7 +180,7 @@ describe('ol.collection', function() { it('triggers events properly', function() { var added = []; ol.events.listen( - collection, ol.CollectionEventType.ADD, function(e) { + collection, ol.Collection.EventType.ADD, function(e) { added.push(e.element); }); collection.setAt(2, 0); @@ -229,7 +229,7 @@ describe('ol.collection', function() { it('triggers add when pushing', function() { var collection = new ol.Collection(); var elem; - ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) { + ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) { elem = e.element; }); collection.push(1); @@ -246,8 +246,8 @@ describe('ol.collection', function() { }); describe('setAt', function() { it('triggers remove', function() { - ol.events.listen(collection, ol.CollectionEventType.ADD, cb1); - ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb2); + ol.events.listen(collection, ol.Collection.EventType.ADD, cb1); + ol.events.listen(collection, ol.Collection.EventType.REMOVE, cb2); collection.setAt(0, 2); expect(cb2.lastCall.args[0].element).to.eql(1); expect(cb1.lastCall.args[0].element).to.eql(2); @@ -255,7 +255,7 @@ describe('ol.collection', function() { }); describe('pop', function() { it('triggers remove', function() { - ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb1); + ol.events.listen(collection, ol.Collection.EventType.REMOVE, cb1); collection.pop(); expect(cb1.lastCall.args[0].element).to.eql(1); }); @@ -273,7 +273,7 @@ describe('ol.collection', function() { it('fires events', function() { var collection = new ol.Collection(); var elems = []; - ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) { + ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) { elems.push(e.element); }); collection.extend([1, 2]); @@ -286,4 +286,3 @@ describe('ol.collection', function() { goog.require('ol.events'); goog.require('ol.Collection'); -goog.require('ol.CollectionEventType');