Lazily create default style
This commit is contained in:
@@ -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
|
if (goog.isNull(style)) {
|
||||||
literals = style.createLiterals(feature);
|
style = ol.style.getDefault();
|
||||||
} else {
|
|
||||||
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) {
|
||||||
|
|||||||
@@ -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
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -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,30 +70,70 @@ 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;
|
||||||
rules: [
|
|
||||||
new ol.style.Rule({
|
|
||||||
filter: 'renderIntent("select")',
|
/**
|
||||||
|
* 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: 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({
|
||||||
|
color: '#ffffff',
|
||||||
|
opacity: 0.7
|
||||||
|
}),
|
||||||
|
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
|
||||||
|
})
|
||||||
|
]
|
||||||
|
})
|
||||||
|
],
|
||||||
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)
|
stroke: new ol.style.Stroke()
|
||||||
}),
|
}),
|
||||||
new ol.style.Fill(ol.style.FillDefaultsSelect),
|
new ol.style.Fill(),
|
||||||
new ol.style.Stroke(ol.style.StrokeDefaultsSelect)
|
new ol.style.Stroke()
|
||||||
]
|
]
|
||||||
})
|
});
|
||||||
],
|
}
|
||||||
symbolizers: [
|
return ol.style.default_;
|
||||||
new ol.style.Shape({
|
};
|
||||||
fill: new ol.style.Fill(),
|
|
||||||
stroke: new ol.style.Stroke()
|
|
||||||
}),
|
/**
|
||||||
new ol.style.Fill(),
|
* Set the default style.
|
||||||
new ol.style.Stroke()
|
* @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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -122,56 +122,68 @@ 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('returns an empty array for features without geometry', function() {
|
it('is a ol.style.Style instance', function() {
|
||||||
expect(ol.style.Style.defaults.createLiterals(feature))
|
expect(style).to.be.a(ol.style.Style);
|
||||||
.to.have.length(0);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns an array with the Shape default for points', function() {
|
describe('#createLiterals()', function() {
|
||||||
feature.setGeometry(new ol.geom.Point([0, 0]));
|
|
||||||
|
|
||||||
var literals = ol.style.Style.defaults.createLiterals(feature);
|
it('returns an empty array for features without geometry', function() {
|
||||||
expect(literals).to.have.length(1);
|
var feature = new ol.Feature();
|
||||||
|
expect(style.createLiterals(feature))
|
||||||
|
.to.have.length(0);
|
||||||
|
});
|
||||||
|
|
||||||
var literal = literals[0];
|
it('returns an array with the Shape default for points', function() {
|
||||||
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
var feature = new ol.Feature();
|
||||||
expect(literal.type).to.be(ol.style.ShapeDefaults.type);
|
feature.setGeometry(new ol.geom.Point([0, 0]));
|
||||||
expect(literal.fillColor).to.be(ol.style.FillDefaults.color);
|
|
||||||
expect(literal.fillOpacity).to.be(ol.style.FillDefaults.opacity);
|
|
||||||
expect(literal.strokeColor).to.be(ol.style.StrokeDefaults.color);
|
|
||||||
expect(literal.strokeOpacity).to.be(ol.style.StrokeDefaults.opacity);
|
|
||||||
expect(literal.strokeWidth).to.be(ol.style.StrokeDefaults.width);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns an array with the Line default for lines', function() {
|
var literals = style.createLiterals(feature);
|
||||||
feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]]));
|
expect(literals).to.have.length(1);
|
||||||
|
|
||||||
var literals = ol.style.Style.defaults.createLiterals(feature);
|
var literal = literals[0];
|
||||||
expect(literals).to.have.length(1);
|
expect(literal).to.be.a(ol.style.ShapeLiteral);
|
||||||
|
expect(literal.type).to.be(ol.style.ShapeDefaults.type);
|
||||||
|
expect(literal.fillColor).to.be(ol.style.FillDefaults.color);
|
||||||
|
expect(literal.fillOpacity).to.be(ol.style.FillDefaults.opacity);
|
||||||
|
expect(literal.strokeColor).to.be(ol.style.StrokeDefaults.color);
|
||||||
|
expect(literal.strokeOpacity).to.be(ol.style.StrokeDefaults.opacity);
|
||||||
|
expect(literal.strokeWidth).to.be(ol.style.StrokeDefaults.width);
|
||||||
|
});
|
||||||
|
|
||||||
var literal = literals[0];
|
it('returns an array with the Line default for lines', function() {
|
||||||
expect(literal).to.be.a(ol.style.LineLiteral);
|
var feature = new ol.Feature();
|
||||||
expect(literal.color).to.be(ol.style.StrokeDefaults.color);
|
feature.setGeometry(new ol.geom.LineString([[0, 0], [1, 1]]));
|
||||||
expect(literal.opacity).to.be(ol.style.StrokeDefaults.opacity);
|
|
||||||
expect(literal.width).to.be(ol.style.StrokeDefaults.width);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns an array with the Polygon default for polygons', function() {
|
var literals = style.createLiterals(feature);
|
||||||
feature.setGeometry(new ol.geom.Polygon([[[0, 0], [1, 1], [0, 0]]]));
|
expect(literals).to.have.length(1);
|
||||||
|
|
||||||
var literals = ol.style.Style.defaults.createLiterals(feature);
|
var literal = literals[0];
|
||||||
expect(literals).to.have.length(1);
|
expect(literal).to.be.a(ol.style.LineLiteral);
|
||||||
|
expect(literal.color).to.be(ol.style.StrokeDefaults.color);
|
||||||
|
expect(literal.opacity).to.be(ol.style.StrokeDefaults.opacity);
|
||||||
|
expect(literal.width).to.be(ol.style.StrokeDefaults.width);
|
||||||
|
});
|
||||||
|
|
||||||
|
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 = style.createLiterals(feature);
|
||||||
|
expect(literals).to.have.length(1);
|
||||||
|
|
||||||
|
var literal = literals[0];
|
||||||
|
expect(literal).to.be.a(ol.style.PolygonLiteral);
|
||||||
|
expect(literal.fillColor).to.be(ol.style.FillDefaults.color);
|
||||||
|
expect(literal.fillOpacity).to.be(ol.style.FillDefaults.opacity);
|
||||||
|
expect(literal.strokeColor).to.be(ol.style.StrokeDefaults.color);
|
||||||
|
expect(literal.strokeOpacity).to.be(ol.style.StrokeDefaults.opacity);
|
||||||
|
expect(literal.strokeWidth).to.be(ol.style.StrokeDefaults.width);
|
||||||
|
});
|
||||||
|
|
||||||
var literal = literals[0];
|
|
||||||
expect(literal).to.be.a(ol.style.PolygonLiteral);
|
|
||||||
expect(literal.fillColor).to.be(ol.style.FillDefaults.color);
|
|
||||||
expect(literal.fillOpacity).to.be(ol.style.FillDefaults.opacity);
|
|
||||||
expect(literal.strokeColor).to.be(ol.style.StrokeDefaults.color);
|
|
||||||
expect(literal.strokeOpacity).to.be(ol.style.StrokeDefaults.opacity);
|
|
||||||
expect(literal.strokeWidth).to.be(ol.style.StrokeDefaults.width);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -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');
|
||||||
|
|||||||
Reference in New Issue
Block a user