Permalink
Browse files

autoSize getter/setter

  • Loading branch information...
1 parent 53fd0fc commit d7061c5d13956f5080ef4c38b310766cfde09935 @tmcw tmcw committed Jan 25, 2012
Showing with 78 additions and 79 deletions.
  1. +39 −41 modestmaps.js
  2. +1 −1 modestmaps.min.js
  3. +38 −37 src/map.js
View
@@ -2292,11 +2292,9 @@ var MM = com.modestmaps = {
if (!dimensions) {
dimensions = new MM.Point(this.parent.offsetWidth,
this.parent.offsetHeight);
- this.autoSize = true;
- // use destroy to get rid of this handler from the DOM
- MM.addEvent(window, 'resize', this.windowResize());
+ this.autoSize(true);
} else {
- this.autoSize = false;
+ this.autoSize(false);
// don't call setSize here because it calls draw()
this.parent.style.width = Math.round(dimensions.x) + 'px';
this.parent.style.height = Math.round(dimensions.y) + 'px';
@@ -2346,7 +2344,25 @@ var MM = com.modestmaps = {
eventHandlers: null, // Array of interaction handlers, just a MM.MouseHandler by default
- autoSize: null, // Boolean, true if we have a window resize listener
+ // Boolean, true if we have a window resize listener
+ _autoSize: null,
+
+ autoSize: function(x) {
+ // accessor
+ if (x === undefined) return this._autoSize;
+ // no change to setting
+ if (x === this._autoSize) return;
+ // setter
+ if (!x) {
+ if (this._autoSize === true) {
+ MM.removeEvent(window, 'resize', this.windowResize());
+ }
+ this._autoSize = false;
+ } else {
+ MM.addEvent(window, 'resize', this.windowResize());
+ this._autoSize = true;
+ }
+ },
toString: function() {
return 'Map(#' + this.parent.id + ')';
@@ -2528,10 +2544,7 @@ var MM = com.modestmaps = {
this.dimensions = new MM.Point(dimensions.x, dimensions.y);
this.parent.style.width = Math.round(this.dimensions.x) + 'px';
this.parent.style.height = Math.round(this.dimensions.y) + 'px';
- if (this.autoSize) {
- MM.removeEvent(window, 'resize', this.windowResize());
- this.autoSize = false;
- }
+ this.autoSize(false);
this.draw(); // draw calls enforceLimits
// (if you switch to getFrame, call enforceLimits first)
this.dispatchCallback('resized', this.dimensions);
@@ -2593,11 +2606,8 @@ var MM = com.modestmaps = {
},
extent: function(locations, precise) {
- if (locations) {
- return this.setExtent(locations, precise);
- } else {
- return this.getExtent();
- }
+ return (location !== undefined) ? this.setExtent(locations, precise) :
+ this.getExtent();
},
// Get the current centerpoint of the map, returning a `Location`
@@ -2606,11 +2616,7 @@ var MM = com.modestmaps = {
},
center: function(location) {
- if (location) {
- return this.setCenter(location);
- } else {
- return this.getCenter();
- }
+ return (location !== undefined) ? this.setCenter(location) : this.getCenter();
},
// Get the current zoom level of the map, returning a number
@@ -2619,11 +2625,7 @@ var MM = com.modestmaps = {
},
zoom: function(zoom) {
- if (zoom !== undefined) {
- return this.setZoom(zoom);
- } else {
- return this.getZoom();
- }
+ return (zoom !== undefined) ? this.setZoom(zoom) : this.getZoom();
},
// layers
@@ -2656,10 +2658,7 @@ var MM = com.modestmaps = {
addLayer: function(layer) {
layer = this.coerceLayer(layer);
this.layers.push(layer);
- // make sure layer.parent doesn't already have a parentNode
- if (!layer.parent.parentNode) {
- this.parent.appendChild(layer.parent);
- }
+ this.parent.appendChild(layer.parent);
layer.map = this; // TODO: remove map property from MM.Layer?
return this;
},
@@ -2711,7 +2710,7 @@ var MM = com.modestmaps = {
layer = this.coerceLayer(layer);
- if(index == this.layers.length) {
+ if (index == this.layers.length) {
// it just gets tacked on to the end
this.layers.push(layer);
this.parent.appendChild(layer.parent);
@@ -2780,8 +2779,7 @@ var MM = com.modestmaps = {
var maxZoom = limits[1].zoom;
if (coord.zoom < minZoom) {
coord = coord.zoomTo(minZoom);
- }
- else if (coord.zoom > maxZoom) {
+ } else if (coord.zoom > maxZoom) {
coord = coord.zoomTo(maxZoom);
}
}
@@ -2797,15 +2795,16 @@ var MM = com.modestmaps = {
coord = coord.copy();
// clamp pan:
- var topLeftLimit = limits[0].zoomTo(coord.zoom);
- var bottomRightLimit = limits[1].zoomTo(coord.zoom);
- var currentTopLeft = this.pointCoordinate(new MM.Point(0,0));
- var currentBottomRight = this.pointCoordinate(this.dimensions);
+ var topLeftLimit = limits[0].zoomTo(coord.zoom),
+ bottomRightLimit = limits[1].zoomTo(coord.zoom),
+ currentTopLeft = this.pointCoordinate(new MM.Point(0,0)),
+ currentBottomRight = this.pointCoordinate(this.dimensions);
// this handles infinite limits:
// (Infinity - Infinity) is Nan
// NaN is never less than anything
- if (bottomRightLimit.row - topLeftLimit.row < currentBottomRight.row - currentTopLeft.row) {
+ if (bottomRightLimit.row - topLeftLimit.row <
+ currentBottomRight.row - currentTopLeft.row) {
// if the limit is smaller than the current view center it
coord.row = (bottomRightLimit.row + topLeftLimit.row) / 2;
}
@@ -2817,7 +2816,8 @@ var MM = com.modestmaps = {
coord.row -= currentBottomRight.row - bottomRightLimit.row;
}
}
- if (bottomRightLimit.column - topLeftLimit.column < currentBottomRight.column - currentTopLeft.column) {
+ if (bottomRightLimit.column - topLeftLimit.column <
+ currentBottomRight.column - currentTopLeft.column) {
// if the limit is smaller than the current view, center it
coord.column = (bottomRightLimit.column + topLeftLimit.column) / 2;
}
@@ -2848,7 +2848,7 @@ var MM = com.modestmaps = {
// if we don't have dimensions, check the parent size
if (this.dimensions.x <= 0 || this.dimensions.y <= 0) {
- if (this.autoSize) {
+ if (this.autoSize()) {
// maybe the parent size has changed?
var w = this.parent.offsetWidth,
h = this.parent.offsetHeight;
@@ -2908,9 +2908,7 @@ var MM = com.modestmaps = {
for (var i = 0; i < this.eventHandlers.length; i++) {
this.eventHandlers[i].remove();
}
- if (this.autoSize) {
- MM.removeEvent(window, 'resize', this.windowResize());
- }
+ this.autoSize(false);
}
};
if (typeof module !== 'undefined' && module.exports) {
Oops, something went wrong.

0 comments on commit d7061c5

Please sign in to comment.