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

View File

@@ -18,6 +18,7 @@ goog.require('ol.layer.Layer');
goog.require('ol.proj');
goog.require('ol.source.Vector');
goog.require('ol.structs.RTree');
goog.require('ol.style');
goog.require('ol.style.Style');
goog.require('ol.style.TextLiteral');
@@ -436,12 +437,11 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
if (!goog.isNull(symbolizers)) {
literals = ol.style.Style.createLiterals(symbolizers, feature);
} else {
if (!goog.isNull(style)) {
// layer style second
literals = style.createLiterals(feature);
} else {
literals = ol.style.Style.defaults.createLiterals(feature);
if (goog.isNull(style)) {
style = ol.style.getDefault();
}
literals = style.createLiterals(feature);
}
numLiterals = literals.length;
for (j = 0; j < numLiterals; ++j) {

View File

@@ -123,13 +123,3 @@ ol.style.FillDefaults = {
color: '#ffffff',
opacity: 0.4
};
/**
* @typedef {{color: (string),
* opacity: (number)}}
*/
ol.style.FillDefaultsSelect = {
color: '#ffffff',
opacity: 0.7
};

View File

@@ -1,7 +1,12 @@
goog.provide('ol.style');
goog.provide('ol.style.Style');
goog.require('goog.object');
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.style.Fill');
goog.require('ol.style.Literal');
@@ -65,18 +70,44 @@ ol.style.Style.prototype.createLiterals = function(feature) {
/**
* The default 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: [
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: [
new ol.style.Shape({
fill: new ol.style.Fill(ol.style.FillDefaultsSelect),
stroke: new ol.style.Stroke(ol.style.StrokeDefaultsSelect)
fill: new ol.style.Fill({
color: '#ffffff',
opacity: 0.7
}),
new ol.style.Fill(ol.style.FillDefaultsSelect),
new ol.style.Stroke(ol.style.StrokeDefaultsSelect)
stroke: new ol.style.Stroke({
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
})
]
})
],
@@ -88,7 +119,21 @@ ol.style.Style.defaults = new ol.style.Style({
new ol.style.Fill(),
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;
};
/**

View File

@@ -122,18 +122,26 @@ describe('ol.style.Style', function() {
});
describe('ol.style.Style.defaults.createLiterals(feature)', function() {
var feature = new ol.Feature();
describe('ol.style.getDefault()', function() {
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() {
expect(ol.style.Style.defaults.createLiterals(feature))
var feature = new ol.Feature();
expect(style.createLiterals(feature))
.to.have.length(0);
});
it('returns an array with the Shape default for points', function() {
var feature = new ol.Feature();
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);
var literal = literals[0];
@@ -147,9 +155,10 @@ describe('ol.style.Style', 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]]));
var literals = ol.style.Style.defaults.createLiterals(feature);
var literals = style.createLiterals(feature);
expect(literals).to.have.length(1);
var literal = literals[0];
@@ -160,9 +169,10 @@ describe('ol.style.Style', 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]]]));
var literals = ol.style.Style.defaults.createLiterals(feature);
var literals = style.createLiterals(feature);
expect(literals).to.have.length(1);
var literal = literals[0];
@@ -176,6 +186,8 @@ describe('ol.style.Style', function() {
});
});
describe('#reduceLiterals_', 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.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.style');
goog.require('ol.style.Fill');
goog.require('ol.style.LineLiteral');
goog.require('ol.style.PolygonLiteral');