UNPKG

2.1 kBPlain TextView Raw
1import assert from 'assert';
2import type {Expression} from '../style-spec/expression/expression';
3import type EvaluationContext from '../style-spec/expression/evaluation_context';
4import type {Type} from '../style-spec/expression/types';
5import type {ZoomConstantExpression} from '../style-spec/expression';
6import {NullType} from '../style-spec/expression/types';
7import {PossiblyEvaluatedPropertyValue} from './properties';
8import {register} from '../util/web_worker_transfer';
9
10// This is an internal expression class. It is only used in GL JS and
11// has GL JS dependencies which can break the standalone style-spec module
12export default class FormatSectionOverride<T> implements Expression {
13 type: Type;
14 defaultValue: PossiblyEvaluatedPropertyValue<T>;
15
16 constructor(defaultValue: PossiblyEvaluatedPropertyValue<T>) {
17 assert(defaultValue.property.overrides !== undefined);
18 this.type = defaultValue.property.overrides ? defaultValue.property.overrides.runtimeType : NullType;
19 this.defaultValue = defaultValue;
20 }
21
22 evaluate(ctx: EvaluationContext) {
23 if (ctx.formattedSection) {
24 const overrides = this.defaultValue.property.overrides;
25 if (overrides && overrides.hasOverride(ctx.formattedSection)) {
26 return overrides.getOverride(ctx.formattedSection);
27 }
28 }
29
30 if (ctx.feature && ctx.featureState) {
31 return this.defaultValue.evaluate(ctx.feature, ctx.featureState);
32 }
33
34 return this.defaultValue.property.specification.default;
35 }
36
37 eachChild(fn: (_: Expression) => void) {
38 if (!this.defaultValue.isConstant()) {
39 const expr: ZoomConstantExpression<'source'> = (this.defaultValue.value as any);
40 fn(expr._styleExpression.expression);
41 }
42 }
43
44 // Cannot be statically evaluated, as the output depends on the evaluation context.
45 outputDefined() {
46 return false;
47 }
48
49 serialize() {
50 return null;
51 }
52}
53
54register('FormatSectionOverride', FormatSectionOverride, {omit: ['defaultValue']});