Lazily create default style

This commit is contained in:
Tim Schaub
2013-09-04 07:17:08 -06:00
parent cc11efa420
commit 13a937fad7
4 changed files with 121 additions and 73 deletions
+4 -4
View File
@@ -18,6 +18,7 @@ goog.require('ol.layer.Layer');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.Vector'); goog.require('ol.source.Vector');
goog.require('ol.structs.RTree'); goog.require('ol.structs.RTree');
goog.require('ol.style');
goog.require('ol.style.Style'); goog.require('ol.style.Style');
goog.require('ol.style.TextLiteral'); goog.require('ol.style.TextLiteral');
@@ -436,12 +437,11 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
if (!goog.isNull(symbolizers)) { if (!goog.isNull(symbolizers)) {
literals = ol.style.Style.createLiterals(symbolizers, feature); literals = ol.style.Style.createLiterals(symbolizers, feature);
} else { } else {
if (!goog.isNull(style)) {
// layer style second // layer style second
literals = style.createLiterals(feature); if (goog.isNull(style)) {
} else { style = ol.style.getDefault();
literals = ol.style.Style.defaults.createLiterals(feature);
} }
literals = style.createLiterals(feature);
} }
numLiterals = literals.length; numLiterals = literals.length;
for (j = 0; j < numLiterals; ++j) { for (j = 0; j < numLiterals; ++j) {
-10
View File
@@ -123,13 +123,3 @@ ol.style.FillDefaults = {
color: '#ffffff', color: '#ffffff',
opacity: 0.4 opacity: 0.4
}; };
/**
* @typedef {{color: (string),
* opacity: (number)}}
*/
ol.style.FillDefaultsSelect = {
color: '#ffffff',
opacity: 0.7
};
+51 -6
View File
@@ -1,7 +1,12 @@
goog.provide('ol.style');
goog.provide('ol.style.Style'); goog.provide('ol.style.Style');
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.expr.Call');
goog.require('ol.expr.Identifier');
goog.require('ol.expr.Literal');
goog.require('ol.expr.functions');
goog.require('ol.geom.GeometryType'); goog.require('ol.geom.GeometryType');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
goog.require('ol.style.Literal'); goog.require('ol.style.Literal');
@@ -65,18 +70,44 @@ ol.style.Style.prototype.createLiterals = function(feature) {
/** /**
* The default style. * The default style.
* @type {ol.style.Style} * @type {ol.style.Style}
* @private
*/ */
ol.style.Style.defaults = new ol.style.Style({ ol.style.default_ = null;
/**
* Get the default style.
* @return {ol.style.Style} The default style.
*/
ol.style.getDefault = function() {
if (goog.isNull(ol.style.default_)) {
ol.style.default_ = new ol.style.Style({
rules: [ rules: [
new ol.style.Rule({ new ol.style.Rule({
filter: 'renderIntent("select")', filter: new ol.expr.Call(
new ol.expr.Identifier(ol.expr.functions.RENDER_INTENT),
[new ol.expr.Literal('select')]),
symbolizers: [ symbolizers: [
new ol.style.Shape({ new ol.style.Shape({
fill: new ol.style.Fill(ol.style.FillDefaultsSelect), fill: new ol.style.Fill({
stroke: new ol.style.Stroke(ol.style.StrokeDefaultsSelect) color: '#ffffff',
opacity: 0.7
}), }),
new ol.style.Fill(ol.style.FillDefaultsSelect), stroke: new ol.style.Stroke({
new ol.style.Stroke(ol.style.StrokeDefaultsSelect) color: '#696969',
opacity: 0.9,
width: 2.0
})
}),
new ol.style.Fill({
color: '#ffffff',
opacity: 0.7
}),
new ol.style.Stroke({
color: '#696969',
opacity: 0.9,
width: 2.0
})
] ]
}) })
], ],
@@ -89,6 +120,20 @@ ol.style.Style.defaults = new ol.style.Style({
new ol.style.Stroke() new ol.style.Stroke()
] ]
}); });
}
return ol.style.default_;
};
/**
* Set the default style.
* @param {ol.style.Style} style The new default style.
* @return {ol.style.Style} The default style.
*/
ol.style.setDefault = function(style) {
ol.style.default_ = style;
return style;
};
/** /**
+19 -6
View File
@@ -122,18 +122,26 @@ describe('ol.style.Style', function() {
}); });
describe('ol.style.Style.defaults.createLiterals(feature)', function() { describe('ol.style.getDefault()', function() {
var feature = new ol.Feature(); var style = ol.style.getDefault();
it('is a ol.style.Style instance', function() {
expect(style).to.be.a(ol.style.Style);
});
describe('#createLiterals()', function() {
it('returns an empty array for features without geometry', function() { it('returns an empty array for features without geometry', function() {
expect(ol.style.Style.defaults.createLiterals(feature)) var feature = new ol.Feature();
expect(style.createLiterals(feature))
.to.have.length(0); .to.have.length(0);
}); });
it('returns an array with the Shape default for points', function() { it('returns an array with the Shape default for points', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.Point([0, 0])); feature.setGeometry(new ol.geom.Point([0, 0]));
var literals = ol.style.Style.defaults.createLiterals(feature); var literals = style.createLiterals(feature);
expect(literals).to.have.length(1); expect(literals).to.have.length(1);
var literal = literals[0]; var literal = literals[0];
@@ -147,9 +155,10 @@ describe('ol.style.Style', function() {
}); });
it('returns an array with the Line default for lines', function() { it('returns an array with the Line default for lines', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]])); feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]]));
var literals = ol.style.Style.defaults.createLiterals(feature); var literals = style.createLiterals(feature);
expect(literals).to.have.length(1); expect(literals).to.have.length(1);
var literal = literals[0]; var literal = literals[0];
@@ -160,9 +169,10 @@ describe('ol.style.Style', function() {
}); });
it('returns an array with the Polygon default for polygons', function() { it('returns an array with the Polygon default for polygons', function() {
var feature = new ol.Feature();
feature.setGeometry(new ol.geom.Polygon([[[0, 0], [1, 1], [0, 0]]])); feature.setGeometry(new ol.geom.Polygon([[[0, 0], [1, 1], [0, 0]]]));
var literals = ol.style.Style.defaults.createLiterals(feature); var literals = style.createLiterals(feature);
expect(literals).to.have.length(1); expect(literals).to.have.length(1);
var literal = literals[0]; var literal = literals[0];
@@ -176,6 +186,8 @@ describe('ol.style.Style', function() {
}); });
});
describe('#reduceLiterals_', function() { describe('#reduceLiterals_', function() {
it('collapses stroke or fill only literals where possible', function() { it('collapses stroke or fill only literals where possible', function() {
@@ -284,6 +296,7 @@ goog.require('ol.expr');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.style');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
goog.require('ol.style.LineLiteral'); goog.require('ol.style.LineLiteral');
goog.require('ol.style.PolygonLiteral'); goog.require('ol.style.PolygonLiteral');