From bc0bfd8385fc3b33512656ea45c2ecc928370e49 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 2 Aug 2012 16:24:24 +0200 Subject: [PATCH] Add add and remove events --- src/ol/base/collection.js | 33 ++++++++++++++++++++++------- src/ol/base/collection_test.js | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/ol/base/collection.js b/src/ol/base/collection.js index bd151d3d1e..424c6ebc25 100644 --- a/src/ol/base/collection.js +++ b/src/ol/base/collection.js @@ -18,7 +18,9 @@ goog.require('ol.Object'); * @enum {string} */ ol.CollectionEventType = { + ADD: 'add', INSERT_AT: 'insert_at', + REMOVE: 'remove', REMOVE_AT: 'remove_at', SET_AT: 'set_at' }; @@ -29,18 +31,25 @@ ol.CollectionEventType = { * @constructor * @extends {goog.events.Event} * @param {ol.CollectionEventType} type Type. - * @param {number} index Index. + * @param {*=} opt_elem Element. + * @param {number=} opt_index Index. * @param {*=} opt_prev Value. * @param {Object=} opt_target Target. */ -ol.CollectionEvent = function(type, index, opt_prev, opt_target) { +ol.CollectionEvent = + function(type, opt_elem, opt_index, opt_prev, opt_target) { goog.base(this, type, opt_target); /** - * @type {number} + * @type {*} */ - this.index = index; + this.elem = opt_elem; + + /** + * @type {number|undefined} + */ + this.index = opt_index; /** * @type {*} @@ -145,7 +154,9 @@ ol.Collection.prototype.insertAt = function(index, elem) { goog.array.insertAt(this.array_, elem, index); this.updateLength_(); this.dispatchEvent(new ol.CollectionEvent( - ol.CollectionEventType.INSERT_AT, index, undefined, this)); + ol.CollectionEventType.ADD, elem, undefined, undefined, this)); + this.dispatchEvent(new ol.CollectionEvent( + ol.CollectionEventType.INSERT_AT, elem, index, undefined, this)); if (this[ol.CollectionEventType.INSERT_AT]) { this[ol.CollectionEventType.INSERT_AT](index); } @@ -179,8 +190,10 @@ ol.Collection.prototype.removeAt = function(index) { var prev = this.array_[index]; goog.array.removeAt(this.array_, index); this.updateLength_(); + this.dispatchEvent(new ol.CollectionEvent( + ol.CollectionEventType.REMOVE, prev, undefined, undefined, this)); this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.REMOVE_AT, - index, prev, this)); + undefined, index, prev, this)); if (this[ol.CollectionEventType.REMOVE_AT]) { this[ol.CollectionEventType.REMOVE_AT](index); } @@ -198,7 +211,11 @@ ol.Collection.prototype.setAt = function(index, elem) { var prev = this.array_[index]; this.array_[index] = elem; this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.SET_AT, - index, prev, this)); + elem, index, prev, this)); + this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.REMOVE, + prev, undefined, undefined, this)); + this.dispatchEvent(new ol.CollectionEvent(ol.CollectionEventType.ADD, + elem, undefined, undefined, this)); if (this[ol.CollectionEventType.SET_AT]) { this[ol.CollectionEventType.SET_AT](index, prev); } @@ -216,5 +233,5 @@ ol.Collection.prototype.setAt = function(index, elem) { * @private */ ol.Collection.prototype.updateLength_ = function() { - this.set('length', this.array_.length); + this.set(ol.CollectionProperty.LENGTH, this.array_.length); }; diff --git a/src/ol/base/collection_test.js b/src/ol/base/collection_test.js index db8d103a8a..24b0c63228 100644 --- a/src/ol/base/collection_test.js +++ b/src/ol/base/collection_test.js @@ -213,3 +213,41 @@ function testForEachScope() { }, uniqueObj); assertTrue(that === uniqueObj); } + + +function testAddEvent() { + var collection = new ol.Collection(); + var elem; + goog.events.listen(collection, ol.CollectionEventType.ADD, function(e) { + elem = e.elem; + }); + collection.push(1); + assertEquals(1, elem); +} + + +function testAddRemoveEvent() { + var collection = new ol.Collection([1]); + var addedElem; + goog.events.listen(collection, ol.CollectionEventType.ADD, function(e) { + addedElem = e.elem; + }); + var removedElem; + goog.events.listen(collection, ol.CollectionEventType.REMOVE, function(e) { + removedElem = e.elem; + }); + collection.setAt(0, 2); + assertEquals(1, removedElem); + assertEquals(2, addedElem); +} + + +function testRemove() { + var collection = new ol.Collection([1]); + var elem; + goog.events.listen(collection, ol.CollectionEventType.REMOVE, function(e) { + elem = e.elem; + }); + collection.pop(); + assertEquals(1, elem); +}