Accept an object literal for static layer styling

This commit is contained in:
Tim Schaub
2022-07-28 18:13:13 -06:00
parent d5086a7ab5
commit ab48290bd7
22 changed files with 705 additions and 174 deletions

View File

@@ -3,10 +3,11 @@
*/
import Layer from './Layer.js';
import RBush from 'rbush';
import {
import Style, {
createDefaultStyle,
toFunction as toStyleFunction,
} from '../style/Style.js';
import {toStyle} from '../style/flat.js';
/**
* @template {import("../source/Vector.js").default|import("../source/VectorTile.js").default} VectorSourceType
@@ -49,7 +50,7 @@ import {
* the fill and stroke styles of all of those layers regardless of z-index. To opt out of this
* behavior and place declutterd features with their own layer configure the layer with a `className`
* other than `ol-layer`.
* @property {import("../style/Style.js").StyleLike|null} [style] Layer style. When set to `null`, only
* @property {import("../style/Style.js").StyleLike|import("../style/flat.js").FlatStyleLike|null} [style] Layer style. When set to `null`, only
* features that have their own style will be rendered. See {@link module:ol/style/Style~Style} for the default style
* which will be used if this is not set.
* @property {import("./Base.js").BackgroundColor} [background] Background color for the layer. If not specified, no background
@@ -249,12 +250,51 @@ class BaseVectorLayer extends Layer {
* an array of styles. If set to `null`, the layer has no style (a `null` style),
* so only features that have their own styles will be rendered in the layer. Call
* `setStyle()` without arguments to reset to the default style. See
* {@link module:ol/style/Style~Style} for information on the default style.
* @param {import("../style/Style.js").StyleLike|null} [opt_style] Layer style.
* [the ol/style/Style module]{@link module:ol/style/Style~Style} for information on the default style.
*
* If your layer has a static style, you can use "flat" style object literals instead of
* using the `Style` and symbolizer constructors (`Fill`, `Stroke`, etc.). See the documentation
* for the [flat style types]{@link module:ol/style/flat~FlatStyle} to see what properties are supported.
*
* @param {import("../style/Style.js").StyleLike|import("../style/flat.js").FlatStyleLike|null} [opt_style] Layer style.
* @api
*/
setStyle(opt_style) {
this.style_ = opt_style !== undefined ? opt_style : createDefaultStyle;
/**
* @type {import("../style/Style.js").StyleLike|null}
*/
let style;
if (opt_style === undefined) {
style = createDefaultStyle;
} else if (opt_style === null) {
style = null;
} else if (typeof opt_style === 'function') {
style = opt_style;
} else if (opt_style instanceof Style) {
style = opt_style;
} else if (Array.isArray(opt_style)) {
const len = opt_style.length;
/**
* @type {Array<Style>}
*/
const styles = new Array(len);
for (let i = 0; i < len; ++i) {
const s = opt_style[i];
if (s instanceof Style) {
styles[i] = s;
} else {
styles[i] = toStyle(s);
}
}
style = styles;
} else {
style = toStyle(opt_style);
}
this.style_ = style;
this.styleFunction_ =
opt_style === null ? undefined : toStyleFunction(this.style_);
this.changed();