Optional polygon stroke or fill

This commit is contained in:
Tim Schaub
2013-03-07 22:28:39 -07:00
parent f93bf2ad77
commit 77355ca634
2 changed files with 84 additions and 30 deletions

View File

@@ -8,9 +8,9 @@ goog.require('ol.style.SymbolizerLiteral');
/** /**
* @typedef {{fillStyle: (string), * @typedef {{fillStyle: (string|undefined),
* strokeStyle: (string), * strokeStyle: (string|undefined),
* strokeWidth: (number), * strokeWidth: (number|undefined),
* opacity: (number)}} * opacity: (number)}}
*/ */
ol.style.PolygonLiteralOptions; ol.style.PolygonLiteralOptions;
@@ -25,17 +25,30 @@ ol.style.PolygonLiteralOptions;
ol.style.PolygonLiteral = function(config) { ol.style.PolygonLiteral = function(config) {
goog.base(this); goog.base(this);
goog.asserts.assertString(config.fillStyle, 'fillStyle must be a string'); /** @type {string|undefined} */
/** @type {string} */
this.fillStyle = config.fillStyle; this.fillStyle = config.fillStyle;
if (goog.isDef(config.fillStyle)) {
goog.asserts.assertString(config.fillStyle, 'fillStyle must be a string');
}
goog.asserts.assertString(config.strokeStyle, 'strokeStyle must be a string'); /** @type {string|undefined} */
/** @type {string} */
this.strokeStyle = config.strokeStyle; this.strokeStyle = config.strokeStyle;
if (goog.isDef(this.strokeStyle)) {
goog.asserts.assertString(
this.strokeStyle, 'strokeStyle must be a string');
}
goog.asserts.assertNumber(config.strokeWidth, 'strokeWidth must be a number'); /** @type {number|undefined} */
/** @type {number} */
this.strokeWidth = config.strokeWidth; this.strokeWidth = config.strokeWidth;
if (goog.isDef(this.strokeWidth)) {
goog.asserts.assertNumber(
this.strokeWidth, 'strokeWidth must be a number');
}
goog.asserts.assert(
goog.isDef(this.fillStyle) ||
(goog.isDef(this.strokeStyle) && goog.isDef(this.strokeWidth)),
'Either fillStyle or strokeStyle and strokeWidth must be set');
goog.asserts.assertNumber(config.opacity, 'opacity must be a number'); goog.asserts.assertNumber(config.opacity, 'opacity must be a number');
/** @type {number} */ /** @type {number} */
@@ -69,28 +82,45 @@ ol.style.Polygon = function(options) {
* @type {ol.Expression} * @type {ol.Expression}
* @private * @private
*/ */
this.fillStyle_ = !goog.isDef(options.fillStyle) ? this.fillStyle_ = !goog.isDefAndNotNull(options.fillStyle) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.fillStyle) : null :
(options.fillStyle instanceof ol.Expression) ? (options.fillStyle instanceof ol.Expression) ?
options.fillStyle : new ol.ExpressionLiteral(options.fillStyle); options.fillStyle : new ol.ExpressionLiteral(options.fillStyle);
/** // stroke handling - if any stroke property is supplied, use defaults
* @type {ol.Expression} var strokeStyle = null,
* @private strokeWidth = null;
*/
this.strokeStyle_ = !goog.isDef(options.strokeStyle) ? if (goog.isDefAndNotNull(options.strokeStyle) ||
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeStyle) : goog.isDefAndNotNull(options.strokeWidth)) {
(options.strokeStyle instanceof ol.Expression) ?
options.strokeStyle : new ol.ExpressionLiteral(options.strokeStyle); strokeStyle = !goog.isDefAndNotNull(options.strokeStyle) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeStyle) :
(options.strokeStyle instanceof ol.Expression) ?
options.strokeStyle : new ol.ExpressionLiteral(options.strokeStyle);
strokeWidth = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
}
/** /**
* @type {ol.Expression} * @type {ol.Expression}
* @private * @private
*/ */
this.strokeWidth_ = !goog.isDef(options.strokeWidth) ? this.strokeStyle_ = strokeStyle;
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ? /**
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth); * @type {ol.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
// one of stroke or fill can be null, both null is user error
goog.asserts.assert(!goog.isNull(this.fillStyle_) ||
!(goog.isNull(this.strokeStyle_) && goog.isNull(this.strokeWidth_)),
'Stroke or fill properties must be provided');
/** /**
* @type {ol.Expression} * @type {ol.Expression}
@@ -116,14 +146,25 @@ ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
attrs = feature.getAttributes(); attrs = feature.getAttributes();
} }
var fillStyle = this.fillStyle_.evaluate(feature, attrs); var fillStyle = goog.isNull(this.fillStyle_) ?
goog.asserts.assertString(fillStyle, 'fillStyle must be a string'); undefined :
/** @type {string} */ (this.fillStyle_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(fillStyle) || goog.isString(fillStyle));
var strokeStyle = this.strokeStyle_.evaluate(feature, attrs); var strokeStyle = goog.isNull(this.strokeStyle_) ?
goog.asserts.assertString(strokeStyle, 'strokeStyle must be a string'); undefined :
/** @type {string} */ (this.strokeStyle_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeStyle) || goog.isString(strokeStyle));
var strokeWidth = this.strokeWidth_.evaluate(feature, attrs); var strokeWidth = goog.isNull(this.strokeWidth_) ?
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number'); undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(feature, attrs));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
goog.asserts.assert(
goog.isDef(fillStyle) ||
(goog.isDef(strokeStyle) && goog.isDef(strokeWidth)),
'either fill style or strokeStyle and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(feature, attrs); var opacity = this.opacity_.evaluate(feature, attrs);
goog.asserts.assertNumber(opacity, 'opacity must be a number'); goog.asserts.assertNumber(opacity, 'opacity must be a number');

View File

@@ -46,7 +46,7 @@ describe('ol.style.Polygon', function() {
it('accepts expressions', function() { it('accepts expressions', function() {
var symbolizer = new ol.style.Polygon({ var symbolizer = new ol.style.Polygon({
opacity: new ol.Expression('value / 100'), opacity: new ol.Expression('value / 100'),
fillStyle: ol.Expression('fillAttr') fillStyle: new ol.Expression('fillAttr')
}); });
expect(symbolizer).toBeA(ol.style.Polygon); expect(symbolizer).toBeA(ol.style.Polygon);
}); });
@@ -70,6 +70,19 @@ describe('ol.style.Polygon', function() {
expect(literal).toBeA(ol.style.PolygonLiteral); expect(literal).toBeA(ol.style.PolygonLiteral);
expect(literal.opacity).toBe(42 / 100); expect(literal.opacity).toBe(42 / 100);
expect(literal.fillStyle).toBe('#ff0000'); expect(literal.fillStyle).toBe('#ff0000');
expect(literal.strokeStyle).toBeUndefined();
});
it('applies default strokeWidth if only strokeStyle is given', function() {
var symbolizer = new ol.style.Polygon({
strokeStyle: '#ff0000'
});
var literal = symbolizer.createLiteral();
expect(literal).toBeA(ol.style.PolygonLiteral);
expect(literal.strokeStyle).toBe('#ff0000');
expect(literal.strokeWidth).toBe(1.5);
expect(literal.fillStyle).toBeUndefined();
}); });
}); });