Dedicated module for collection event type enum
This commit is contained in:
@@ -6,8 +6,9 @@
|
||||
goog.provide('ol.Collection');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.events.Event');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.events.Event');
|
||||
|
||||
|
||||
/**
|
||||
@@ -127,7 +128,7 @@ ol.Collection.prototype.insertAt = function(index, elem) {
|
||||
this.array_.splice(index, 0, elem);
|
||||
this.updateLength_();
|
||||
this.dispatchEvent(
|
||||
new ol.Collection.Event(ol.Collection.EventType.ADD, elem));
|
||||
new ol.Collection.Event(ol.CollectionEventType.ADD, elem));
|
||||
};
|
||||
|
||||
|
||||
@@ -185,7 +186,7 @@ ol.Collection.prototype.removeAt = function(index) {
|
||||
this.array_.splice(index, 1);
|
||||
this.updateLength_();
|
||||
this.dispatchEvent(
|
||||
new ol.Collection.Event(ol.Collection.EventType.REMOVE, prev));
|
||||
new ol.Collection.Event(ol.CollectionEventType.REMOVE, prev));
|
||||
return prev;
|
||||
};
|
||||
|
||||
@@ -202,9 +203,9 @@ ol.Collection.prototype.setAt = function(index, elem) {
|
||||
var prev = this.array_[index];
|
||||
this.array_[index] = elem;
|
||||
this.dispatchEvent(
|
||||
new ol.Collection.Event(ol.Collection.EventType.REMOVE, prev));
|
||||
new ol.Collection.Event(ol.CollectionEventType.REMOVE, prev));
|
||||
this.dispatchEvent(
|
||||
new ol.Collection.Event(ol.Collection.EventType.ADD, elem));
|
||||
new ol.Collection.Event(ol.CollectionEventType.ADD, elem));
|
||||
} else {
|
||||
var j;
|
||||
for (j = n; j < index; ++j) {
|
||||
@@ -231,25 +232,6 @@ ol.Collection.Property = {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
@@ -258,7 +240,7 @@ ol.Collection.EventType = {
|
||||
* @constructor
|
||||
* @extends {ol.events.Event}
|
||||
* @implements {oli.Collection.Event}
|
||||
* @param {ol.Collection.EventType} type Type.
|
||||
* @param {ol.CollectionEventType} type Type.
|
||||
* @param {*=} opt_element Element.
|
||||
*/
|
||||
ol.Collection.Event = function(type, opt_element) {
|
||||
|
||||
19
src/ol/collectioneventtype.js
Normal file
19
src/ol/collectioneventtype.js
Normal file
@@ -0,0 +1,19 @@
|
||||
goog.provide('ol.CollectionEventType');
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
*/
|
||||
ol.CollectionEventType = {
|
||||
/**
|
||||
* 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'
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
goog.provide('ol.interaction.Modify');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.MapBrowserPointerEvent');
|
||||
@@ -173,9 +173,9 @@ ol.interaction.Modify = function(options) {
|
||||
this.features_ = options.features;
|
||||
|
||||
this.features_.forEach(this.addFeature_, this);
|
||||
ol.events.listen(this.features_, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(this.features_, ol.CollectionEventType.ADD,
|
||||
this.handleFeatureAdd_, this);
|
||||
ol.events.listen(this.features_, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(this.features_, ol.CollectionEventType.REMOVE,
|
||||
this.handleFeatureRemove_, this);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
goog.provide('ol.interaction.Select');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.functions');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.array');
|
||||
goog.require('ol.events');
|
||||
goog.require('ol.events.Event');
|
||||
goog.require('ol.events.condition');
|
||||
goog.require('ol.functions');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.interaction.Interaction');
|
||||
goog.require('ol.layer.Vector');
|
||||
@@ -136,9 +136,9 @@ ol.interaction.Select = function(opt_options) {
|
||||
this.featureLayerAssociation_ = {};
|
||||
|
||||
var features = this.featureOverlay_.getSource().getFeaturesCollection();
|
||||
ol.events.listen(features, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(features, ol.CollectionEventType.ADD,
|
||||
this.addFeature_, this);
|
||||
ol.events.listen(features, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(features, ol.CollectionEventType.REMOVE,
|
||||
this.removeFeature_, this);
|
||||
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@ goog.provide('ol.interaction.Snap');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.Observable');
|
||||
goog.require('ol.coordinate');
|
||||
@@ -335,9 +336,9 @@ ol.interaction.Snap.prototype.setMap = function(map) {
|
||||
if (map) {
|
||||
if (this.features_) {
|
||||
keys.push(
|
||||
ol.events.listen(this.features_, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(this.features_, ol.CollectionEventType.ADD,
|
||||
this.handleFeatureAdd_, this),
|
||||
ol.events.listen(this.features_, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(this.features_, ol.CollectionEventType.REMOVE,
|
||||
this.handleFeatureRemove_, this)
|
||||
);
|
||||
} else if (this.source_) {
|
||||
|
||||
@@ -3,6 +3,7 @@ goog.provide('ol.layer.Group');
|
||||
goog.require('ol');
|
||||
goog.require('ol.asserts');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.events');
|
||||
goog.require('ol.events.EventType');
|
||||
@@ -88,9 +89,9 @@ ol.layer.Group.prototype.handleLayersChanged_ = function(event) {
|
||||
|
||||
var layers = this.getLayers();
|
||||
this.layersListenerKeys_.push(
|
||||
ol.events.listen(layers, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(layers, ol.CollectionEventType.ADD,
|
||||
this.handleLayersAdd_, this),
|
||||
ol.events.listen(layers, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(layers, ol.CollectionEventType.REMOVE,
|
||||
this.handleLayersRemove_, this));
|
||||
|
||||
for (var id in this.listenerKeys_) {
|
||||
|
||||
@@ -6,6 +6,7 @@ goog.provide('ol.Map');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.MapBrowserEventHandler');
|
||||
goog.require('ol.MapEvent');
|
||||
@@ -391,7 +392,7 @@ ol.Map = function(options) {
|
||||
control.setMap(this);
|
||||
}, this);
|
||||
|
||||
ol.events.listen(this.controls_, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(this.controls_, ol.CollectionEventType.ADD,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
@@ -399,7 +400,7 @@ ol.Map = function(options) {
|
||||
event.element.setMap(this);
|
||||
}, this);
|
||||
|
||||
ol.events.listen(this.controls_, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(this.controls_, ol.CollectionEventType.REMOVE,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
@@ -416,7 +417,7 @@ ol.Map = function(options) {
|
||||
interaction.setMap(this);
|
||||
}, this);
|
||||
|
||||
ol.events.listen(this.interactions_, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(this.interactions_, ol.CollectionEventType.ADD,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
@@ -424,7 +425,7 @@ ol.Map = function(options) {
|
||||
event.element.setMap(this);
|
||||
}, this);
|
||||
|
||||
ol.events.listen(this.interactions_, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(this.interactions_, ol.CollectionEventType.REMOVE,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
@@ -434,7 +435,7 @@ ol.Map = function(options) {
|
||||
|
||||
this.overlays_.forEach(this.addOverlayInternal_, this);
|
||||
|
||||
ol.events.listen(this.overlays_, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(this.overlays_, ol.CollectionEventType.ADD,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
@@ -442,7 +443,7 @@ ol.Map = function(options) {
|
||||
this.addOverlayInternal_(/** @type {ol.Overlay} */ (event.element));
|
||||
}, this);
|
||||
|
||||
ol.events.listen(this.overlays_, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(this.overlays_, ol.CollectionEventType.REMOVE,
|
||||
/**
|
||||
* @param {ol.Collection.Event} event Collection event.
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ goog.provide('ol.source.Vector');
|
||||
|
||||
goog.require('ol');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.array');
|
||||
goog.require('ol.asserts');
|
||||
@@ -316,7 +317,7 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) {
|
||||
modifyingCollection = false;
|
||||
}
|
||||
});
|
||||
ol.events.listen(collection, ol.Collection.EventType.ADD,
|
||||
ol.events.listen(collection, ol.CollectionEventType.ADD,
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
modifyingCollection = true;
|
||||
@@ -324,7 +325,7 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) {
|
||||
modifyingCollection = false;
|
||||
}
|
||||
}, this);
|
||||
ol.events.listen(collection, ol.Collection.EventType.REMOVE,
|
||||
ol.events.listen(collection, ol.CollectionEventType.REMOVE,
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
modifyingCollection = true;
|
||||
|
||||
@@ -2,6 +2,7 @@ goog.provide('ol.test.Collection');
|
||||
|
||||
goog.require('ol.events');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.CollectionEventType');
|
||||
|
||||
|
||||
describe('ol.collection', function() {
|
||||
@@ -130,7 +131,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.Collection.EventType.REMOVE, cb);
|
||||
ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb);
|
||||
expect(collection.remove(1)).to.eql(1);
|
||||
expect(cb).to.be.called();
|
||||
expect(cb.lastCall.args[0].element).to.eql(1);
|
||||
@@ -153,11 +154,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.Collection.EventType.ADD, function(e) {
|
||||
ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) {
|
||||
added = e.element;
|
||||
});
|
||||
ol.events.listen(
|
||||
collection, ol.Collection.EventType.REMOVE, function(e) {
|
||||
collection, ol.CollectionEventType.REMOVE, function(e) {
|
||||
removed = e.element;
|
||||
});
|
||||
collection.setAt(1, 1);
|
||||
@@ -171,7 +172,7 @@ describe('ol.collection', function() {
|
||||
var collection = new ol.Collection(['a']);
|
||||
var removed;
|
||||
ol.events.listen(
|
||||
collection, ol.Collection.EventType.REMOVE, function(e) {
|
||||
collection, ol.CollectionEventType.REMOVE, function(e) {
|
||||
removed = e.element;
|
||||
});
|
||||
collection.pop();
|
||||
@@ -184,7 +185,7 @@ describe('ol.collection', function() {
|
||||
var collection = new ol.Collection([0, 2]);
|
||||
var added;
|
||||
ol.events.listen(
|
||||
collection, ol.Collection.EventType.ADD, function(e) {
|
||||
collection, ol.CollectionEventType.ADD, function(e) {
|
||||
added = e.element;
|
||||
});
|
||||
collection.insertAt(1, 1);
|
||||
@@ -196,7 +197,7 @@ describe('ol.collection', function() {
|
||||
it('triggers events properly', function() {
|
||||
var added = [];
|
||||
ol.events.listen(
|
||||
collection, ol.Collection.EventType.ADD, function(e) {
|
||||
collection, ol.CollectionEventType.ADD, function(e) {
|
||||
added.push(e.element);
|
||||
});
|
||||
collection.setAt(2, 0);
|
||||
@@ -245,7 +246,7 @@ describe('ol.collection', function() {
|
||||
it('triggers add when pushing', function() {
|
||||
var collection = new ol.Collection();
|
||||
var elem;
|
||||
ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) {
|
||||
ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) {
|
||||
elem = e.element;
|
||||
});
|
||||
var length = collection.push(1);
|
||||
@@ -262,8 +263,8 @@ describe('ol.collection', function() {
|
||||
});
|
||||
describe('setAt', function() {
|
||||
it('triggers remove', function() {
|
||||
ol.events.listen(collection, ol.Collection.EventType.ADD, cb1);
|
||||
ol.events.listen(collection, ol.Collection.EventType.REMOVE, cb2);
|
||||
ol.events.listen(collection, ol.CollectionEventType.ADD, cb1);
|
||||
ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb2);
|
||||
collection.setAt(0, 2);
|
||||
expect(cb2.lastCall.args[0].element).to.eql(1);
|
||||
expect(cb1.lastCall.args[0].element).to.eql(2);
|
||||
@@ -271,7 +272,7 @@ describe('ol.collection', function() {
|
||||
});
|
||||
describe('pop', function() {
|
||||
it('triggers remove', function() {
|
||||
ol.events.listen(collection, ol.Collection.EventType.REMOVE, cb1);
|
||||
ol.events.listen(collection, ol.CollectionEventType.REMOVE, cb1);
|
||||
collection.pop();
|
||||
expect(cb1.lastCall.args[0].element).to.eql(1);
|
||||
});
|
||||
@@ -289,7 +290,7 @@ describe('ol.collection', function() {
|
||||
it('fires events', function() {
|
||||
var collection = new ol.Collection();
|
||||
var elems = [];
|
||||
ol.events.listen(collection, ol.Collection.EventType.ADD, function(e) {
|
||||
ol.events.listen(collection, ol.CollectionEventType.ADD, function(e) {
|
||||
elems.push(e.element);
|
||||
});
|
||||
collection.extend([1, 2]);
|
||||
|
||||
Reference in New Issue
Block a user