From b8edf651e971319e2f1bae92152bd35d9bf282bd Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 16 Aug 2012 21:47:12 +0200 Subject: [PATCH] Move ol.Collection.create to ol3.collection --- src/api/api.js | 50 ++++++++++++++++++---------------- src/api/collection_test.js | 22 +++++++++++++++ src/ol/base/collection.js | 13 --------- src/ol/base/collection_test.js | 28 ++++--------------- 4 files changed, 53 insertions(+), 60 deletions(-) create mode 100644 src/api/collection_test.js diff --git a/src/api/api.js b/src/api/api.js index 5aebd36da4..2fce523118 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -2,6 +2,7 @@ goog.provide('ol3'); goog.provide('ol3.layer'); goog.require('goog.dom'); +goog.require('ol.Collection'); goog.require('ol.Coordinate'); goog.require('ol.Layer'); goog.require('ol.Map'); @@ -14,21 +15,21 @@ goog.require('ol.layer.OpenStreetMap'); goog.exportSymbol('ol3', ol3); +/** + * @typedef {Array|ol.Collection} + */ +ol3.Collection; + + /** * @typedef {Array.|ol.Coordinate|{x: number, y: number}} */ ol3.Coordinate; -/** - * @typedef {Array.|ol.Collection} - */ -ol3.Layers; - - /** * @typedef {{center: (ol3.Coordinate|undefined), - * layers: (ol3.Layers|undefined), + * layers: (ol3.Collection|undefined), * renderTo: (Element|string|undefined), * resolution: (number|undefined), * zoom: (number|undefined)}} @@ -48,6 +49,23 @@ ol3.Object; ol3.Projection; +/** + * @param {ol3.Collection} collection Collection. + * @return {ol.Collection} Collection. + */ +ol3.collection = function(collection) { + if (collection instanceof ol.Collection) { + return collection; + } else if (goog.isArray(collection)) { + var array = /** @type {Array} */ collection; + return new ol.Collection(collection); + } else { + return null; + } +}; +goog.exportProperty(ol3, 'collection', ol3.collection); + + /** * @param {ol3.Coordinate} coordinate Coordinate. * @return {ol.Coordinate} Coordinate. @@ -80,22 +98,6 @@ ol3.layer.osm = function() { goog.exportProperty(ol3.layer, 'osm', ol3.layer.osm); -/** - * @param {ol3.Layers} layers Layers. - * @return {ol.Collection} Layers. - */ -ol3.layers = function(layers) { - if (layers instanceof ol.Collection) { - return layers; - } else if (goog.isArray(layers)) { - return new ol.Collection(layers); - } else { - return null; - } -}; -goog.exportProperty(ol3, 'layers', ol3.layers); - - /** * @param {ol3.MapOptions=} opt_mapOptions Options. * @return {ol.Map} Map. @@ -104,7 +106,7 @@ ol3.map = function(opt_mapOptions) { var options = opt_mapOptions || {}; var center = ol3.coordinate(/** @type {ol3.Coordinate} */ (goog.object.get(options, 'center', null))); - var layers = ol3.layers(/** @type {ol3.Layers} */ + var layers = ol3.collection(/** @type {ol3.Collection} */ (goog.object.get(options, 'layers', null))); var projection = ol3.projection(/** @type {ol3.Projection} */ (goog.object.get(options, 'projection', 'EPSG:3857'))); diff --git a/src/api/collection_test.js b/src/api/collection_test.js new file mode 100644 index 0000000000..cb4a7fa2c0 --- /dev/null +++ b/src/api/collection_test.js @@ -0,0 +1,22 @@ +goog.require('goog.testing.jsunit'); +goog.require('ol.Collection'); +goog.require('ol3'); + + +function testCreateFromArray() { + var array = [0, 1, 2]; + var collection = ol3.collection(array); + assertTrue(collection instanceof ol.Collection); + assertEquals(3, collection.getLength()); + assertEquals(0, collection.getAt(0)); + assertEquals(1, collection.getAt(1)); + assertEquals(2, collection.getAt(2)); +} + + +function testCreateFromCollection() { + var collection1 = new ol.Collection(); + var collection2 = ol3.collection(collection1); + assertTrue(collection1 === collection2); +} + diff --git a/src/ol/base/collection.js b/src/ol/base/collection.js index bec2f650c3..734900c2d4 100644 --- a/src/ol/base/collection.js +++ b/src/ol/base/collection.js @@ -90,19 +90,6 @@ ol.Collection = function(opt_array) { goog.inherits(ol.Collection, ol.Object); -/** - * @param {ol.Collection|Array} arg Argument. - * @return {ol.Collection} Collection. - */ -ol.Collection.create = function(arg) { - if (arg instanceof ol.Collection) { - return arg; - } else { - return new ol.Collection(arg); - } -}; - - /** */ ol.Collection.prototype.clear = function() { diff --git a/src/ol/base/collection_test.js b/src/ol/base/collection_test.js index 020cbdfa7d..ebc853c733 100644 --- a/src/ol/base/collection_test.js +++ b/src/ol/base/collection_test.js @@ -143,26 +143,8 @@ function testSetAtBeyondEnd() { } -function testCreateFromArray() { - var array = [0, 1, 2]; - var collection = ol.Collection.create(array); - assertTrue(collection instanceof ol.Collection); - assertEquals(3, collection.getLength()); - assertEquals(0, collection.getAt(0)); - assertEquals(1, collection.getAt(1)); - assertEquals(2, collection.getAt(2)); -} - - -function testCreateFromCollection() { - var collection1 = new ol.Collection(); - var collection2 = ol.Collection.create(collection1); - assertTrue(collection1 === collection2); -} - - function testLengthChangeInsertAt() { - var collection = ol.Collection.create([0, 1, 2]); + var collection = new ol.Collection([0, 1, 2]); var lengthEventDispatched; goog.events.listen(collection, 'length_changed', function() { lengthEventDispatched = true; @@ -173,7 +155,7 @@ function testLengthChangeInsertAt() { function testLengthChangeRemoveAt() { - var collection = ol.Collection.create([0, 1, 2]); + var collection = new ol.Collection([0, 1, 2]); var lengthEventDispatched; goog.events.listen(collection, 'length_changed', function() { lengthEventDispatched = true; @@ -184,7 +166,7 @@ function testLengthChangeRemoveAt() { function testLengthChangeSetAt() { - var collection = ol.Collection.create([0, 1, 2]); + var collection = new ol.Collection([0, 1, 2]); var lengthEventDispatched; goog.events.listen(collection, 'length_changed', function() { lengthEventDispatched = true; @@ -195,7 +177,7 @@ function testLengthChangeSetAt() { function testForEach() { - var collection = ol.Collection.create([1, 2, 4]); + var collection = new ol.Collection([1, 2, 4]); var sum = 0; collection.forEach(function(elem) { sum += elem; @@ -205,7 +187,7 @@ function testForEach() { function testForEachScope() { - var collection = ol.Collection.create([0]); + var collection = new ol.Collection([0]); var that; var uniqueObj = {}; collection.forEach(function(elem) {