Move ol.Collection.create to ol3.collection

This commit is contained in:
Tom Payne
2012-08-16 21:47:12 +02:00
parent a30b2f1795
commit b8edf651e9
4 changed files with 53 additions and 60 deletions
+26 -24
View File
@@ -2,6 +2,7 @@ goog.provide('ol3');
goog.provide('ol3.layer'); goog.provide('ol3.layer');
goog.require('goog.dom'); goog.require('goog.dom');
goog.require('ol.Collection');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.Layer'); goog.require('ol.Layer');
goog.require('ol.Map'); goog.require('ol.Map');
@@ -14,21 +15,21 @@ goog.require('ol.layer.OpenStreetMap');
goog.exportSymbol('ol3', ol3); goog.exportSymbol('ol3', ol3);
/**
* @typedef {Array|ol.Collection}
*/
ol3.Collection;
/** /**
* @typedef {Array.<number>|ol.Coordinate|{x: number, y: number}} * @typedef {Array.<number>|ol.Coordinate|{x: number, y: number}}
*/ */
ol3.Coordinate; ol3.Coordinate;
/**
* @typedef {Array.<ol.Layer>|ol.Collection}
*/
ol3.Layers;
/** /**
* @typedef {{center: (ol3.Coordinate|undefined), * @typedef {{center: (ol3.Coordinate|undefined),
* layers: (ol3.Layers|undefined), * layers: (ol3.Collection|undefined),
* renderTo: (Element|string|undefined), * renderTo: (Element|string|undefined),
* resolution: (number|undefined), * resolution: (number|undefined),
* zoom: (number|undefined)}} * zoom: (number|undefined)}}
@@ -48,6 +49,23 @@ ol3.Object;
ol3.Projection; 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. * @param {ol3.Coordinate} coordinate Coordinate.
* @return {ol.Coordinate} Coordinate. * @return {ol.Coordinate} Coordinate.
@@ -80,22 +98,6 @@ ol3.layer.osm = function() {
goog.exportProperty(ol3.layer, 'osm', ol3.layer.osm); 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. * @param {ol3.MapOptions=} opt_mapOptions Options.
* @return {ol.Map} Map. * @return {ol.Map} Map.
@@ -104,7 +106,7 @@ ol3.map = function(opt_mapOptions) {
var options = opt_mapOptions || {}; var options = opt_mapOptions || {};
var center = ol3.coordinate(/** @type {ol3.Coordinate} */ var center = ol3.coordinate(/** @type {ol3.Coordinate} */
(goog.object.get(options, 'center', null))); (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))); (goog.object.get(options, 'layers', null)));
var projection = ol3.projection(/** @type {ol3.Projection} */ var projection = ol3.projection(/** @type {ol3.Projection} */
(goog.object.get(options, 'projection', 'EPSG:3857'))); (goog.object.get(options, 'projection', 'EPSG:3857')));
+22
View File
@@ -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);
}
-13
View File
@@ -90,19 +90,6 @@ ol.Collection = function(opt_array) {
goog.inherits(ol.Collection, ol.Object); 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() { ol.Collection.prototype.clear = function() {
+5 -23
View File
@@ -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() { function testLengthChangeInsertAt() {
var collection = ol.Collection.create([0, 1, 2]); var collection = new ol.Collection([0, 1, 2]);
var lengthEventDispatched; var lengthEventDispatched;
goog.events.listen(collection, 'length_changed', function() { goog.events.listen(collection, 'length_changed', function() {
lengthEventDispatched = true; lengthEventDispatched = true;
@@ -173,7 +155,7 @@ function testLengthChangeInsertAt() {
function testLengthChangeRemoveAt() { function testLengthChangeRemoveAt() {
var collection = ol.Collection.create([0, 1, 2]); var collection = new ol.Collection([0, 1, 2]);
var lengthEventDispatched; var lengthEventDispatched;
goog.events.listen(collection, 'length_changed', function() { goog.events.listen(collection, 'length_changed', function() {
lengthEventDispatched = true; lengthEventDispatched = true;
@@ -184,7 +166,7 @@ function testLengthChangeRemoveAt() {
function testLengthChangeSetAt() { function testLengthChangeSetAt() {
var collection = ol.Collection.create([0, 1, 2]); var collection = new ol.Collection([0, 1, 2]);
var lengthEventDispatched; var lengthEventDispatched;
goog.events.listen(collection, 'length_changed', function() { goog.events.listen(collection, 'length_changed', function() {
lengthEventDispatched = true; lengthEventDispatched = true;
@@ -195,7 +177,7 @@ function testLengthChangeSetAt() {
function testForEach() { function testForEach() {
var collection = ol.Collection.create([1, 2, 4]); var collection = new ol.Collection([1, 2, 4]);
var sum = 0; var sum = 0;
collection.forEach(function(elem) { collection.forEach(function(elem) {
sum += elem; sum += elem;
@@ -205,7 +187,7 @@ function testForEach() {
function testForEachScope() { function testForEachScope() {
var collection = ol.Collection.create([0]); var collection = new ol.Collection([0]);
var that; var that;
var uniqueObj = {}; var uniqueObj = {};
collection.forEach(function(elem) { collection.forEach(function(elem) {