From ef83910c3d35556c1d1f664487e8f1ae934c7d33 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 19 Jun 2012 12:33:59 +0200 Subject: [PATCH] Make more Loc tests pass. --- src/api/loc.js | 62 +++++++++++++++++++--------------------- src/ol/Map.js | 24 ++++++++++++++++ test/spec/ol/Loc.test.js | 9 +++++- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/api/loc.js b/src/api/loc.js index 74138b049a..18da81d568 100644 --- a/src/api/loc.js +++ b/src/api/loc.js @@ -1,6 +1,7 @@ goog.provide('ol.loc'); goog.require('ol.Loc'); +goog.require('ol.projection'); /** @@ -12,13 +13,13 @@ ol.LocLike; /** * @export - * @param {ol.LocLike} loc Location. + * @param {ol.LocLike} opt_arg Location. * @return {ol.Loc} Location. */ -ol.loc = function(loc){ +ol.loc = function(opt_arg){ - if (loc instanceof ol.Loc) { - return loc; + if (opt_arg instanceof ol.Loc) { + return opt_arg; } var x = 0; @@ -26,37 +27,32 @@ ol.loc = function(loc){ var z; var projection; - if (goog.isArray(loc)) { - if (loc.length >= 1) { - x = loc[0]; - if (loc.length >= 2) { - y = loc[1]; - if (loc.length >= 3) { - z = loc[2]; - } - } - } - } - else - if (goog.isObject(loc)) { - if (goog.isDef(loc.x)) { - x = loc.x; - } - if (goog.isDef(loc.y)) { - y = loc.y; - } - if (goog.isDef(loc.z)) { - z = loc.z; - } - if (goog.isDef(loc.projection)) { - projection = loc.projection; - } - } - else { + if (arguments.length == 1 && goog.isDef(opt_arg)) { + if (goog.isArray(opt_arg)) { + x = opt_arg[0]; + y = opt_arg[1]; + z = opt_arg[2]; + projection = opt_arg[3]; + } else if (goog.isObject(opt_arg)) { + x = opt_arg['x']; + y = opt_arg['y']; + z = opt_arg['z']; + projection = opt_arg['projection']; + } else { throw new Error('ol.loc'); } + } + + if (goog.isDef(projection)) { + projection = ol.projection(projection); + } - return new ol.Loc(x, y, z, projection); + var loc = new ol.Loc(); + loc.setX(x); + loc.setY(y); + loc.setZ(z); + loc.setProjection(projection); + return loc; }; @@ -68,7 +64,7 @@ ol.loc = function(loc){ */ ol.Loc.prototype.projection = function(opt_arg){ if (arguments.length == 1 && goog.isDef(opt_arg)) { - return this.setProjection(opt_arg); + return this.setProjection(ol.projection(opt_arg)); } else { return this.getProjection(); diff --git a/src/ol/Map.js b/src/ol/Map.js index e5e847f366..d480d46033 100644 --- a/src/ol/Map.js +++ b/src/ol/Map.js @@ -16,6 +16,12 @@ ol.Map = function() { */ this.projection_ = new ol.Projection(); + /** + * @private + * @type {ol.Projection} + */ + this.userProjection_ = new ol.Projection(); + /** * @private * @type {ol.Loc} @@ -47,6 +53,14 @@ ol.Map.prototype.getProjection = function() { }; +/** + * @return {ol.Projection} User projection. + */ +ol.Map.prototype.getUserProjection = function() { + return this.userProjection_; +}; + + /** * @return {number} Zoom. */ @@ -75,6 +89,16 @@ ol.Map.prototype.setProjection = function(projection) { }; +/** + * @param {ol.Projection} userProjection User projection. + * @return {ol.Map} This. + */ +ol.Map.prototype.setProjection = function(userProjection) { + this.userProjection_ = userProjection; + return this; +}; + + /** * @param {number} zoom Zoom. * @return {ol.Map} This. diff --git a/test/spec/ol/Loc.test.js b/test/spec/ol/Loc.test.js index d6d3306e33..2f50be703e 100644 --- a/test/spec/ol/Loc.test.js +++ b/test/spec/ol/Loc.test.js @@ -1,18 +1,25 @@ describe("ol.Loc", function() { - it("allows flexible construction", function() { + it("allows empty construction", function() { var loc; // nowhere loc = ol.loc(); expect(loc instanceof ol.Loc).toBe(true); + }); + it("allows construction from an obj config", function() { + var loc; + // obj config loc = ol.loc({x: 10, y: 20}); expect(loc.x()).toBe(10); expect(loc.y()).toBe(20); + }); + it("allows construction from an array config", function() { + var loc; // array config loc = ol.loc([30, 40]);