No need to have a mutable_ flag

Instead, educate users to call setStyle.
This commit is contained in:
Andreas Hocevar
2014-09-04 11:47:32 -06:00
parent 13d84e75ad
commit 0c36d7606b
8 changed files with 76 additions and 221 deletions
-15
View File
@@ -172,21 +172,6 @@ ol.style.Circle.prototype.getStroke = function() {
}; };
/**
* @protected
* @param {boolean} mutable Mutable.
*/
ol.style.Circle.prototype.setMutable = function(mutable) {
if (!goog.isNull(this.stroke_)) {
this.stroke_.setMutable(mutable);
}
if (!goog.isNull(this.fill_)) {
this.fill_.setMutable(mutable);
}
goog.base(this, 'setMutable', mutable);
};
/** /**
* @inheritDoc * @inheritDoc
*/ */
+3 -18
View File
@@ -1,7 +1,5 @@
goog.provide('ol.style.Fill'); goog.provide('ol.style.Fill');
goog.require('goog.asserts');
/** /**
@@ -21,13 +19,6 @@ ol.style.Fill = function(opt_options) {
* @type {ol.Color|string} * @type {ol.Color|string}
*/ */
this.color_ = goog.isDef(options.color) ? options.color : null; this.color_ = goog.isDef(options.color) ? options.color : null;
/**
* @private
* @type {boolean}
*/
this.mutable_ = true;
}; };
@@ -41,18 +32,12 @@ ol.style.Fill.prototype.getColor = function() {
/** /**
* Set the color. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {ol.Color|string} color Color. * @param {ol.Color|string} color Color.
* @api * @api
*/ */
ol.style.Fill.prototype.setColor = function(color) { ol.style.Fill.prototype.setColor = function(color) {
goog.asserts.assert(this.mutable_);
this.color_ = color; this.color_ = color;
}; };
/**
* @param {boolean} mutable Mutable.
*/
ol.style.Fill.prototype.setMutable = function(mutable) {
this.mutable_ = mutable;
};
+15 -21
View File
@@ -1,8 +1,6 @@
goog.provide('ol.style.Image'); goog.provide('ol.style.Image');
goog.provide('ol.style.ImageState'); goog.provide('ol.style.ImageState');
goog.require('goog.asserts');
/** /**
* @enum {number} * @enum {number}
@@ -65,12 +63,6 @@ ol.style.Image = function(options) {
*/ */
this.snapToPixel_ = options.snapToPixel; this.snapToPixel_ = options.snapToPixel;
/**
* @private
* @type {boolean}
*/
this.mutable_ = true;
}; };
@@ -159,60 +151,62 @@ ol.style.Image.prototype.getSize = goog.abstractMethod;
/** /**
* Set the zIndex. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number} opacity Opacity. * @param {number} opacity Opacity.
*/ */
ol.style.Image.prototype.setOpacity = function(opacity) { ol.style.Image.prototype.setOpacity = function(opacity) {
goog.asserts.assert(this.mutable_);
this.opacity_ = opacity; this.opacity_ = opacity;
}; };
/** /**
* Set whether to rotate the style with the view. Use `setStyle()` on the
* feature, layer or feature overlay for changes to take effect.
*
* @param {boolean} rotateWithView Rotate with map. * @param {boolean} rotateWithView Rotate with map.
*/ */
ol.style.Image.prototype.setRotateWithView = function(rotateWithView) { ol.style.Image.prototype.setRotateWithView = function(rotateWithView) {
goog.asserts.assert(this.mutable_);
this.rotateWithView_ = rotateWithView; this.rotateWithView_ = rotateWithView;
}; };
/** /**
* Set the rotation. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number} rotation Rotation. * @param {number} rotation Rotation.
* @api * @api
*/ */
ol.style.Image.prototype.setRotation = function(rotation) { ol.style.Image.prototype.setRotation = function(rotation) {
goog.asserts.assert(this.mutable_);
this.rotation_ = rotation; this.rotation_ = rotation;
}; };
/** /**
* Set the scale. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number} scale Scale. * @param {number} scale Scale.
* @api * @api
*/ */
ol.style.Image.prototype.setScale = function(scale) { ol.style.Image.prototype.setScale = function(scale) {
goog.asserts.assert(this.mutable_);
this.scale_ = scale; this.scale_ = scale;
}; };
/** /**
* Set whether to snap the image to the closest pixel. Use `setStyle()` on the
* feature, layer or feature overlay for changes to take effect.
*
* @param {boolean} snapToPixel Snap to pixel? * @param {boolean} snapToPixel Snap to pixel?
*/ */
ol.style.Image.prototype.setSnapToPixel = function(snapToPixel) { ol.style.Image.prototype.setSnapToPixel = function(snapToPixel) {
goog.asserts.assert(this.mutable_);
this.snapToPixel_ = snapToPixel; this.snapToPixel_ = snapToPixel;
}; };
/**
* @param {boolean} mutable Mutable.
*/
ol.style.Image.prototype.setMutable = function(mutable) {
this.mutable_ = mutable;
};
/** /**
* @param {function(this: T, goog.events.Event)} listener Listener function. * @param {function(this: T, goog.events.Event)} listener Listener function.
* @param {T} thisArg Value to use as `this` when executing `listener`. * @param {T} thisArg Value to use as `this` when executing `listener`.
+18 -23
View File
@@ -1,7 +1,5 @@
goog.provide('ol.style.Stroke'); goog.provide('ol.style.Stroke');
goog.require('goog.asserts');
/** /**
@@ -54,13 +52,6 @@ ol.style.Stroke = function(opt_options) {
* @type {number|undefined} * @type {number|undefined}
*/ */
this.width_ = options.width; this.width_ = options.width;
/**
* @private
* @type {boolean}
*/
this.mutable_ = true;
}; };
@@ -119,68 +110,72 @@ ol.style.Stroke.prototype.getWidth = function() {
/** /**
* Set the color. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {ol.Color|string} color Color. * @param {ol.Color|string} color Color.
* @api * @api
*/ */
ol.style.Stroke.prototype.setColor = function(color) { ol.style.Stroke.prototype.setColor = function(color) {
goog.asserts.assert(this.mutable_);
this.color_ = color; this.color_ = color;
}; };
/** /**
* Set the line cap. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {string|undefined} lineCap Line cap. * @param {string|undefined} lineCap Line cap.
* @api * @api
*/ */
ol.style.Stroke.prototype.setLineCap = function(lineCap) { ol.style.Stroke.prototype.setLineCap = function(lineCap) {
goog.asserts.assert(this.mutable_);
this.lineCap_ = lineCap; this.lineCap_ = lineCap;
}; };
/** /**
* Set the line dash. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {Array.<number>} lineDash Line dash. * @param {Array.<number>} lineDash Line dash.
* @api * @api
*/ */
ol.style.Stroke.prototype.setLineDash = function(lineDash) { ol.style.Stroke.prototype.setLineDash = function(lineDash) {
goog.asserts.assert(this.mutable_);
this.lineDash_ = lineDash; this.lineDash_ = lineDash;
}; };
/** /**
* Set the line join. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {string|undefined} lineJoin Line join. * @param {string|undefined} lineJoin Line join.
* @api * @api
*/ */
ol.style.Stroke.prototype.setLineJoin = function(lineJoin) { ol.style.Stroke.prototype.setLineJoin = function(lineJoin) {
goog.asserts.assert(this.mutable_);
this.lineJoin_ = lineJoin; this.lineJoin_ = lineJoin;
}; };
/** /**
* Set the miter limit. Use `setStyle()` on the feature, layer or feature
* overlay for changes to take effect.
*
* @param {number|undefined} miterLimit Miter limit. * @param {number|undefined} miterLimit Miter limit.
* @api * @api
*/ */
ol.style.Stroke.prototype.setMiterLimit = function(miterLimit) { ol.style.Stroke.prototype.setMiterLimit = function(miterLimit) {
goog.asserts.assert(this.mutable_);
this.miterLimit_ = miterLimit; this.miterLimit_ = miterLimit;
}; };
/** /**
* Set the width. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number|undefined} width Width. * @param {number|undefined} width Width.
* @api * @api
*/ */
ol.style.Stroke.prototype.setWidth = function(width) { ol.style.Stroke.prototype.setWidth = function(width) {
goog.asserts.assert(this.mutable_);
this.width_ = width; this.width_ = width;
}; };
/**
* @param {boolean} mutable Mutable.
*/
ol.style.Stroke.prototype.setMutable = function(mutable) {
this.mutable_ = mutable;
};
+4 -32
View File
@@ -12,9 +12,7 @@ goog.require('ol.style.Stroke');
/** /**
* @classdesc * @classdesc
* Container for vector feature rendering styles. Node that styles are only * Container for vector feature rendering styles.
* mutable as long as they are not assigned to layers, feature overlays or
* features using the respective `setStyle()` methods.
* *
* @constructor * @constructor
* @param {olx.style.StyleOptions=} opt_options Style options. * @param {olx.style.StyleOptions=} opt_options Style options.
@@ -54,11 +52,6 @@ ol.style.Style = function(opt_options) {
*/ */
this.zIndex_ = options.zIndex; this.zIndex_ = options.zIndex;
/**
* @private
* @type {boolean}
*/
this.mutable_ = true;
}; };
@@ -108,35 +101,17 @@ ol.style.Style.prototype.getZIndex = function() {
/** /**
* Set the zIndex. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number|undefined} zIndex ZIndex. * @param {number|undefined} zIndex ZIndex.
* @api * @api
*/ */
ol.style.Style.prototype.setZIndex = function(zIndex) { ol.style.Style.prototype.setZIndex = function(zIndex) {
goog.asserts.assert(this.mutable_);
this.zIndex_ = zIndex; this.zIndex_ = zIndex;
}; };
/**
* @param {boolean} mutable Mutable.
*/
ol.style.Style.prototype.setMutable = function(mutable) {
if (!goog.isNull(this.fill_)) {
this.fill_.setMutable(mutable);
}
if (!goog.isNull(this.image_)) {
this.image_.setMutable(mutable);
}
if (!goog.isNull(this.stroke_)) {
this.stroke_.setMutable(mutable);
}
if (!goog.isNull(this.text_)) {
this.text_.setMutable(mutable);
}
this.mutable_ = mutable;
};
/** /**
* A function that takes an {@link ol.Feature} and a `{number}` representing * A function that takes an {@link ol.Feature} and a `{number}` representing
* the view's resolution. The function should return an array of * the view's resolution. The function should return an array of
@@ -175,9 +150,6 @@ ol.style.createStyleFunction = function(obj) {
goog.asserts.assertInstanceof(obj, ol.style.Style); goog.asserts.assertInstanceof(obj, ol.style.Style);
styles = [obj]; styles = [obj];
} }
for (var i = styles.length - 1; i >= 0; --i) {
styles[i].setMutable(false);
}
styleFunction = goog.functions.constant(styles); styleFunction = goog.functions.constant(styles);
} }
return styleFunction; return styleFunction;
+30 -33
View File
@@ -1,7 +1,5 @@
goog.provide('ol.style.Text'); goog.provide('ol.style.Text');
goog.require('goog.asserts');
/** /**
@@ -75,13 +73,6 @@ ol.style.Text = function(opt_options) {
* @type {number} * @type {number}
*/ */
this.offsetY_ = goog.isDef(options.offsetY) ? options.offsetY : 0; this.offsetY_ = goog.isDef(options.offsetY) ? options.offsetY : 0;
/**
* @private
* @type {boolean}
*/
this.mutable_ = true;
}; };
@@ -174,112 +165,118 @@ ol.style.Text.prototype.getTextBaseline = function() {
/** /**
* Set the font. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {string|undefined} font Font. * @param {string|undefined} font Font.
* @api * @api
*/ */
ol.style.Text.prototype.setFont = function(font) { ol.style.Text.prototype.setFont = function(font) {
goog.asserts.assert(this.mutable_);
this.font_ = font; this.font_ = font;
}; };
/** /**
* Set the x offset. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number} offsetX Horizontal text offset. * @param {number} offsetX Horizontal text offset.
*/ */
ol.style.Text.prototype.setOffsetX = function(offsetX) { ol.style.Text.prototype.setOffsetX = function(offsetX) {
goog.asserts.assert(this.mutable_);
this.offsetX_ = offsetX; this.offsetX_ = offsetX;
}; };
/** /**
* Set the y offset. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number} offsetY Vertical text offset. * @param {number} offsetY Vertical text offset.
*/ */
ol.style.Text.prototype.setOffsetY = function(offsetY) { ol.style.Text.prototype.setOffsetY = function(offsetY) {
goog.asserts.assert(this.mutable_);
this.offsetY_ = offsetY; this.offsetY_ = offsetY;
}; };
/** /**
* Set the fill. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {ol.style.Fill} fill Fill style. * @param {ol.style.Fill} fill Fill style.
* @api * @api
*/ */
ol.style.Text.prototype.setFill = function(fill) { ol.style.Text.prototype.setFill = function(fill) {
goog.asserts.assert(this.mutable_);
this.fill_ = fill; this.fill_ = fill;
}; };
/** /**
* Set the rotation. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number|undefined} rotation Rotation. * @param {number|undefined} rotation Rotation.
* @api * @api
*/ */
ol.style.Text.prototype.setRotation = function(rotation) { ol.style.Text.prototype.setRotation = function(rotation) {
goog.asserts.assert(this.mutable_);
this.rotation_ = rotation; this.rotation_ = rotation;
}; };
/** /**
* Set the scale. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {number|undefined} scale Scale. * @param {number|undefined} scale Scale.
* @api * @api
*/ */
ol.style.Text.prototype.setScale = function(scale) { ol.style.Text.prototype.setScale = function(scale) {
goog.asserts.assert(this.mutable_);
this.scale_ = scale; this.scale_ = scale;
}; };
/** /**
* Set the stroke. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {ol.style.Stroke} stroke Stroke style. * @param {ol.style.Stroke} stroke Stroke style.
* @api * @api
*/ */
ol.style.Text.prototype.setStroke = function(stroke) { ol.style.Text.prototype.setStroke = function(stroke) {
goog.asserts.assert(this.mutable_);
this.stroke_ = stroke; this.stroke_ = stroke;
}; };
/** /**
* Set the text. Use `setStyle()` on the feature, layer or feature overlay
* for changes to take effect.
*
* @param {string|undefined} text Text. * @param {string|undefined} text Text.
* @api * @api
*/ */
ol.style.Text.prototype.setText = function(text) { ol.style.Text.prototype.setText = function(text) {
goog.asserts.assert(this.mutable_);
this.text_ = text; this.text_ = text;
}; };
/** /**
* Set the text alignment. Use `setStyle()` on the feature, layer or feature
* overlay for changes to take effect.
*
* @param {string|undefined} textAlign Text align. * @param {string|undefined} textAlign Text align.
* @api * @api
*/ */
ol.style.Text.prototype.setTextAlign = function(textAlign) { ol.style.Text.prototype.setTextAlign = function(textAlign) {
goog.asserts.assert(this.mutable_);
this.textAlign_ = textAlign; this.textAlign_ = textAlign;
}; };
/** /**
* Set the text baseline. Use `setStyle()` on the feature, layer or feature
* overlay for changes to take effect.
*
* @param {string|undefined} textBaseline Text baseline. * @param {string|undefined} textBaseline Text baseline.
* @api * @api
*/ */
ol.style.Text.prototype.setTextBaseline = function(textBaseline) { ol.style.Text.prototype.setTextBaseline = function(textBaseline) {
goog.asserts.assert(this.mutable_);
this.textBaseline_ = textBaseline; this.textBaseline_ = textBaseline;
}; };
/**
* @param {boolean} mutable Mutable.
*/
ol.style.Text.prototype.setMutable = function(mutable) {
if (!goog.isNull(this.stroke_)) {
this.stroke_.setMutable(mutable);
}
if (!goog.isNull(this.fill_)) {
this.fill_.setMutable(mutable);
}
this.mutable_ = mutable;
};
+1 -2
View File
@@ -16,12 +16,11 @@ describe('ol.FeatureOverlay', function() {
expect(featureOverlay.getFeatures().getLength()).to.be(1); expect(featureOverlay.getFeatures().getLength()).to.be(1);
}); });
it('takes a style and makes it immutable', function() { it('takes a style', function() {
var style = [new ol.style.Style()]; var style = [new ol.style.Style()];
var featureOverlay = new ol.FeatureOverlay({ var featureOverlay = new ol.FeatureOverlay({
style: [new ol.style.Style()] style: [new ol.style.Style()]
}); });
style[0].setMutable(false);
expect(featureOverlay.getStyle()).to.eql(style); expect(featureOverlay.getStyle()).to.eql(style);
expect(featureOverlay.getStyleFunction()()).to.eql(style); expect(featureOverlay.getStyleFunction()()).to.eql(style);
}); });
+5 -77
View File
@@ -2,50 +2,13 @@ goog.provide('ol.test.style.Style');
describe('ol.style.Style', function() { describe('ol.style.Style', function() {
describe('#setMutable', function() { describe('#setZIndex', function() {
it('recursively sets the mutable flag', function() { it('sets the zIndex', function() {
var style = new ol.style.Style({ var style = new ol.style.Style();
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.6)'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 3
})
})
});
expect(function() { style.setZIndex(0.7);
style.setZIndex(1); expect(style.getZIndex()).to.be(0.7);
}).to.not.throwException();
expect(style.mutable_).to.be(true);
expect(style.getFill().mutable_).to.be(true);
expect(style.getStroke().mutable_).to.be(true);
expect(style.getText().mutable_).to.be(true);
expect(style.getText().getStroke().mutable_).to.be(true);
style.setMutable(false);
expect(function() {
style.setZIndex();
}).to.throwException();
expect(style.mutable_).to.be(false);
expect(style.getFill().mutable_).to.be(false);
expect(style.getStroke().mutable_).to.be(false);
expect(style.getText().mutable_).to.be(false);
expect(style.getText().getStroke().mutable_).to.be(false);
}); });
}); });
}); });
@@ -77,41 +40,6 @@ describe('ol.style.createStyleFunction()', function() {
}).to.throwException(); }).to.throwException();
}); });
it('makes styles immutable', function() {
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.6)'
}),
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 3
})
})
});
expect(function() {
style.getFill().setColor('white');
}).to.not.throwException();
ol.style.createStyleFunction(style);
expect(function() {
style.getFill().setColor('black');
}).to.throwException();
});
}); });
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style'); goog.require('ol.style.Style');
goog.require('ol.style.Text');