-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
style_declaration.js
69 lines (59 loc) · 2.29 KB
/
style_declaration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const createFunction = require('../style-spec/function');
const util = require('../util/util');
/**
* A style property declaration
* @private
*/
class StyleDeclaration {
constructor(reference, value) {
this.value = util.clone(value);
this.isFunction = createFunction.isFunctionDefinition(value);
// immutable representation of value. used for comparison
this.json = JSON.stringify(this.value);
this.minimum = reference.minimum;
this.function = createFunction(this.value, reference);
this.isFeatureConstant = this.function.isFeatureConstant;
this.isZoomConstant = this.function.isZoomConstant;
if (!this.isFeatureConstant && !this.isZoomConstant) {
this.stopZoomLevels = [];
const interpolationAmountStops = [];
for (const stop of this.value.stops) {
const zoom = stop[0].zoom;
if (this.stopZoomLevels.indexOf(zoom) < 0) {
this.stopZoomLevels.push(zoom);
interpolationAmountStops.push([zoom, interpolationAmountStops.length]);
}
}
this._functionInterpolationT = createFunction({
type: 'exponential',
stops: interpolationAmountStops,
base: value.base
}, {
type: 'number'
});
} else if (!this.isZoomConstant) {
this.stopZoomLevels = this.value.stops.map(stop => stop[0]);
}
}
calculate(globalProperties, featureProperties) {
const value = this.function(globalProperties && globalProperties.zoom, featureProperties || {});
if (this.minimum !== undefined && value < this.minimum) {
return this.minimum;
}
return value;
}
/**
* Given a zoom level, calculate a possibly-fractional "index" into the
* composite function stops array, intended to be used for interpolating
* between paint values that have been evaluated at the surrounding stop
* values.
*
* Only valid for composite functions.
* @private
*/
calculateInterpolationT(globalProperties) {
return this._functionInterpolationT(globalProperties && globalProperties.zoom, {});
}
}
module.exports = StyleDeclaration;