-
-
Notifications
You must be signed in to change notification settings - Fork 948
/
Copy pathFeatures.js
234 lines (191 loc) · 8.51 KB
/
Features.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { bit_check, bit_set, bit_clear } from "./bit";
import { API_VERSION_1_44, API_VERSION_1_45, API_VERSION_1_46 } from './data_storage';
import semver from "semver";
import { tracking } from "./Analytics";
import $ from 'jquery';
const Features = function (config) {
const self = this;
const features = [
{bit: 0, group: 'rxMode', mode: 'select', name: 'RX_PPM'},
{bit: 2, group: 'other', name: 'INFLIGHT_ACC_CAL'},
{bit: 3, group: 'rxMode', mode: 'select', name: 'RX_SERIAL'},
{bit: 4, group: 'escMotorStop', name: 'MOTOR_STOP'},
{bit: 5, group: 'other', name: 'SERVO_TILT', haveTip: true, dependsOn: 'SERVOS'},
{bit: 6, group: 'other', name: 'SOFTSERIAL', haveTip: true},
{bit: 7, group: 'other', name: 'GPS', haveTip: true, dependsOn: 'GPS'},
{bit: 9, group: 'other', name: 'SONAR', haveTip: true, dependsOn: 'RANGEFINDER'},
{bit: 10, group: 'telemetry', name: 'TELEMETRY', haveTip: true, dependsOn: 'TELEMETRY'},
{bit: 12, group: '3D', name: '3D', haveTip: true},
{bit: 13, group: 'rxMode', mode: 'select', name: 'RX_PARALLEL_PWM'},
{bit: 14, group: 'rxMode', mode: 'select', name: 'RX_MSP'},
{bit: 15, group: 'rssi', name: 'RSSI_ADC'},
{bit: 16, group: 'other', name: 'LED_STRIP', haveTip: true, dependsOn: 'LED_STRIP'},
{bit: 17, group: 'other', name: 'DISPLAY', haveTip: true, dependsOn: 'DASHBOARD'},
{bit: 18, group: 'other', name: 'OSD', haveTip: true, dependsOn: 'OSD'},
{bit: 20, group: 'other', name: 'CHANNEL_FORWARDING', dependsOn: 'SERVOS'},
{bit: 21, group: 'other', name: 'TRANSPONDER', haveTip: true, dependsOn: 'TRANSPONDER'},
{bit: 22, group: 'other', name: 'AIRMODE'},
{bit: 25, group: 'rxMode', mode: 'select', name: 'RX_SPI'},
{bit: 27, group: 'escSensor', name: 'ESC_SENSOR'},
{bit: 28, group: 'antiGravity', name: 'ANTI_GRAVITY', haveTip: true, hideName: true},
];
if (semver.lt(config.apiVersion, API_VERSION_1_44)) { // DYNAMIC_FILTER got removed from FEATURES in BF 4.3 / API 1.44
features.push(
{bit: 29, group: 'other', name: 'DYNAMIC_FILTER'},
);
}
self._features = features;
if (config.buildOptions?.length) {
// Filter features based on build options
if (semver.gte(config.apiVersion, API_VERSION_1_45)) {
self._features = [];
for (const feature of features) {
if (config.buildOptions.some(opt => opt.includes(feature.dependsOn)) || feature.dependsOn === undefined) {
self._features.push(feature);
}
}
}
// Add TELEMETRY feature if any of the following protocols are used: CRSF, GHST, FPORT
if (semver.gte(config.apiVersion, API_VERSION_1_46)) {
let enableTelemetry = false;
if (config.buildOptions.some(opt => opt.includes('CRSF') || opt.includes('GHST') || opt.includes('FPORT'))) {
enableTelemetry = true;
}
const telemetryFeature = self._features.filter(f => f.name === 'TELEMETRY')?.[0];
if (enableTelemetry && !telemetryFeature) {
self._features.push({bit: 10, group: 'telemetry', name: 'TELEMETRY', haveTip: true, dependsOn: 'TELEMETRY'});
}
}
}
self._features.sort((a, b) => a.name.localeCompare(b.name, window.navigator.language, { ignorePunctuation: true }));
self._featureMask = 0;
self._analyticsChanges = {};
};
Features.prototype.getMask = function () {
const self = this;
tracking.sendChangeEvents(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, self._analyticsChanges);
self._analyticsChanges = {};
return self._featureMask;
};
Features.prototype.setMask = function (featureMask) {
const self = this;
self._featureMask = featureMask;
};
Features.prototype.isEnabled = function (featureName) {
const self = this;
for (const element of self._features) {
if (element.name === featureName && bit_check(self._featureMask, element.bit)) {
return true;
}
}
return false;
};
Features.prototype.enable = function (featureName) {
const self = this;
for (const element of self._features) {
if (element.name === featureName) {
self._featureMask = bit_set(self._featureMask, element.bit);
}
}
};
Features.prototype.disable = function (featureName) {
const self = this;
for (const element of self._features) {
if (element.name === featureName) {
self._featureMask = bit_clear(self._featureMask, element.bit);
}
}
};
Features.prototype.generateElements = function (featuresElements) {
const self = this;
self._featureChanges = {};
const listElements = [];
for (const feature of self._features) {
let feature_tip_html = '';
const featureName = feature.name;
const featureBit = feature.bit;
if (feature.haveTip) {
feature_tip_html = `<div class="helpicon cf_tip" i18n_title="feature${featureName}Tip"></div>`;
}
const newElements = [];
if (feature.mode === 'select') {
if (listElements.length === 0) {
newElements.push($('<option class="feature" value="-1" i18n="featureNone" />'));
}
const newElement = $(`<option class="feature" id="feature${featureBit}" name="${featureName}" value="${featureBit}" i18n="feature${featureName}" />`);
newElements.push(newElement);
listElements.push(newElement);
} else {
let newFeatureName = '';
if (!feature.hideName) {
newFeatureName = `<td><div>${featureName}</div></td>`;
}
let element = `<tr><td><input class="feature toggle" id="feature${featureBit}"`;
element += `name="${featureName}" title="${featureName}"`;
element += `type="checkbox"/></td><td><div>${newFeatureName}</div>`;
element += `<span class="xs" i18n="feature${featureName}"></span></td>`;
element += `<td><span class="sm-min" i18n="feature${featureName}"></span>`;
if (feature.haveTip) {
element += feature_tip_html;
}
element += '</td></tr>';
const newElement = $(element);
const featureElement = newElement.find('input.feature');
featureElement.prop('checked', bit_check(self._featureMask, featureBit));
featureElement.data('bit', featureBit);
newElements.push(newElement);
}
featuresElements.each(function () {
if ($(this).hasClass(feature.group)) {
$(this).append(newElements);
}
});
}
for (const element of listElements) {
const bit = parseInt(element.attr('value'));
const state = bit_check(self._featureMask, bit);
element.prop('selected', state);
}
};
Features.prototype.findFeatureByBit = function (bit) {
const self = this;
for (const feature of self._features) {
if (feature.bit === bit) {
return feature;
}
}
};
Features.prototype.updateData = function (featureElement) {
const self = this;
if (featureElement.attr('type') === 'checkbox') {
const bit = featureElement.data('bit');
let featureValue;
if (featureElement.is(':checked')) {
self._featureMask = bit_set(self._featureMask, bit);
featureValue = 'On';
} else {
self._featureMask = bit_clear(self._featureMask, bit);
featureValue = 'Off';
}
self._analyticsChanges[`Feature${self.findFeatureByBit(bit).name}`] = featureValue;
} else if (featureElement.prop('localName') === 'select') {
const controlElements = featureElement.children();
const selectedBit = featureElement.val();
if (selectedBit !== -1) {
let selectedFeature;
for (const controlElement of controlElements) {
const bit = controlElement.value;
if (selectedBit === bit) {
self._featureMask = bit_set(self._featureMask, bit);
selectedFeature = self.findFeatureByBit(bit);
} else {
self._featureMask = bit_clear(self._featureMask, bit);
}
}
if (selectedFeature) {
self._analyticsChanges[`FeatureGroup-${selectedFeature.group}`] = selectedFeature.name;
}
}
}
};
export default Features;