forked from ngageoint/opensphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatureopacitycmd.js
157 lines (133 loc) · 4.8 KB
/
featureopacitycmd.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
goog.provide('os.command.FeatureOpacity');
goog.require('goog.asserts');
goog.require('os.command.AbstractFeatureStyle');
goog.require('os.command.style');
goog.require('os.events.PropertyChangeEvent');
goog.require('os.metrics');
goog.require('os.style');
/**
* Changes the opacity of a feature
*
* @extends {os.command.AbstractFeatureStyle}
* @param {string} layerId
* @param {string} featureId
* @param {number} opacity
* @param {number|null=} opt_oldOpacity
* @param {os.command.style.ColorChangeType=} opt_changeMode
* @constructor
*/
os.command.FeatureOpacity = function(layerId, featureId, opacity, opt_oldOpacity, opt_changeMode) {
goog.asserts.assert(opacity != null, 'opacity must be defined');
/**
* The opacity change mode. Determines how the config opacity is set.
* @type {os.command.style.ColorChangeType}
* @protected
*/
this.changeMode = opt_changeMode || os.command.style.ColorChangeType.COMBINED;
// intentionally called after changeMode is set so getOldValue has the correct value
os.command.FeatureOpacity.base(this, 'constructor', layerId, featureId, opacity, opt_oldOpacity);
switch (this.changeMode) {
case os.command.style.ColorChangeType.FILL:
this.title = 'Change Feature Fill Opacity';
this.metricKey = os.metrics.Layer.FEATURE_FILL_OPACITY;
break;
case os.command.style.ColorChangeType.STROKE:
this.title = 'Change Feature Opacity';
this.metricKey = os.metrics.Layer.FEATURE_OPACITY;
break;
case os.command.style.ColorChangeType.COMBINED:
default:
this.title = 'Change Feature Opacity';
this.metricKey = os.metrics.Layer.FEATURE_OPACITY;
break;
}
};
goog.inherits(os.command.FeatureOpacity, os.command.AbstractFeatureStyle);
/**
* @inheritDoc
*/
os.command.FeatureOpacity.prototype.getOldValue = function() {
var ret;
var feature = /** @type {ol.Feature} */ (this.getFeature());
var config = /** @type {Array<Object>|Object|undefined} */ (this.getFeatureConfigs(feature));
if (Array.isArray(config)) {
config = config[0];
}
if (config) {
var color;
switch (this.changeMode) {
case os.command.style.ColorChangeType.FILL:
color = os.style.getConfigColor(config, true, os.style.StyleField.FILL);
ret = color && color.length === 4 ? color[3] : os.style.DEFAULT_FILL_ALPHA;
break;
case os.command.style.ColorChangeType.STROKE:
color = os.style.getConfigColor(config, true, os.style.StyleField.STROKE);
ret = color && color.length === 4 ? color[3] : os.style.DEFAULT_ALPHA;
break;
case os.command.style.ColorChangeType.COMBINED:
default:
color = os.style.getConfigColor(config, true);
ret = color && color.length === 4 ? color[3] : os.style.DEFAULT_ALPHA;
break;
}
}
return ret;
};
/**
* @inheritDoc
*/
os.command.FeatureOpacity.prototype.applyValue = function(configs, value) {
var color;
var colorValue;
var i;
switch (this.changeMode) {
case os.command.style.ColorChangeType.FILL:
for (i = 0; i < configs.length; i++) {
color = os.style.getConfigColor(configs[i], true, os.style.StyleField.FILL) ||
os.style.getConfigColor(configs[i], true);
if (color) {
color[3] = value;
colorValue = os.style.toRgbaString(color);
os.style.setFillColor(configs[i], colorValue);
}
}
break;
case os.command.style.ColorChangeType.STROKE:
for (i = 0; i < configs.length; i++) {
color = os.style.getConfigColor(configs[i], true, os.style.StyleField.STROKE) ||
os.style.getConfigColor(configs[i], true);
if (color) {
var fillColor = os.style.getConfigColor(configs[i], true, os.style.StyleField.FILL);
color[3] = value;
colorValue = os.style.toRgbaString(color);
os.style.setConfigColor(configs[i], colorValue);
// preserve the original fill color when changing the stroke
if (fillColor) {
os.style.setFillColor(configs[i], fillColor);
}
}
}
break;
case os.command.style.ColorChangeType.COMBINED:
default:
for (i = 0; i < configs.length; i++) {
color = os.style.getConfigColor(configs[i], true);
if (color) {
color[3] = value;
colorValue = os.style.toRgbaString(color);
os.style.setConfigColor(configs[i], colorValue);
}
}
break;
}
os.command.FeatureOpacity.base(this, 'applyValue', configs, value);
};
/**
* @inheritDoc
*/
os.command.FeatureOpacity.prototype.finish = function(configs) {
// dispatch the color change event on the source for the histogram
var feature = this.getFeature();
feature.dispatchEvent(new os.events.PropertyChangeEvent('colors'));
os.command.FeatureOpacity.base(this, 'finish', configs);
};