|
| 1 | +<template> |
| 2 | + <v-form |
| 3 | + ref="form" |
| 4 | + v-model="is_form_valid" |
| 5 | + @submit.prevent="saveEditedParam(false)" |
| 6 | + > |
| 7 | + <template v-if="!custom_input && param?.bitmask"> |
| 8 | + <v-checkbox |
| 9 | + v-for="(key, keyvalue) in param?.bitmask" |
| 10 | + :key="keyvalue" |
| 11 | + v-model="selected_bitflags" |
| 12 | + dense |
| 13 | + hide-details |
| 14 | + :loading="waiting_for_param_update" |
| 15 | + :label="key" |
| 16 | + :value="2 ** keyvalue" |
| 17 | + /> |
| 18 | + </template> |
| 19 | + <v-autocomplete |
| 20 | + v-else-if="!custom_input && Object.entries(param?.options ?? []).length > 10" |
| 21 | + v-model.number="new_value" |
| 22 | + variant="solo" |
| 23 | + :items="as_select_items" |
| 24 | + /> |
| 25 | + <v-select |
| 26 | + v-else-if="!custom_input && param?.options" |
| 27 | + v-model.number="new_value" |
| 28 | + dense |
| 29 | + :items="as_select_items" |
| 30 | + :rules="[() => true]" |
| 31 | + :indeterminate="waiting_for_param_update" |
| 32 | + @change="updateVariables" |
| 33 | + /> |
| 34 | + <v-text-field |
| 35 | + v-if=" |
| 36 | + custom_input |
| 37 | + || (param |
| 38 | + && !param.options |
| 39 | + && !param.bitmask |
| 40 | + ) |
| 41 | + " |
| 42 | + v-model.number="new_value" |
| 43 | + dense |
| 44 | + type="number" |
| 45 | + :step="param.increment ?? 0.01" |
| 46 | + :suffix="param.units" |
| 47 | + :rules="forcing_input ? [] : [isInRange, isValidType]" |
| 48 | + :loading="waiting_for_param_update" |
| 49 | + @blur="updateVariables" |
| 50 | + /> |
| 51 | + |
| 52 | + <v-checkbox |
| 53 | + v-if="show_advanced_checkbox" |
| 54 | + v-model="forcing_input" |
| 55 | + dense |
| 56 | + :label="'Force'" |
| 57 | + /> |
| 58 | + <v-checkbox |
| 59 | + v-if="show_custom_checkbox" |
| 60 | + v-model="custom_input" |
| 61 | + dense |
| 62 | + :label="'Custom'" |
| 63 | + /> |
| 64 | + </v-form> |
| 65 | +</template> |
| 66 | + |
| 67 | +<script lang="ts"> |
| 68 | +import Vue, { PropType } from 'vue' |
| 69 | +
|
| 70 | +import mavlink2rest from '@/libs/MAVLink2Rest' |
| 71 | +import autopilot_data from '@/store/autopilot' |
| 72 | +import Parameter from '@/types/autopilot/parameter' |
| 73 | +
|
| 74 | +export default Vue.extend({ |
| 75 | + name: 'InlineParameterEditor', |
| 76 | + props: { |
| 77 | + param: { |
| 78 | + type: Object as PropType<Parameter> | undefined, |
| 79 | + default: undefined, |
| 80 | + }, |
| 81 | + allowCustom: { |
| 82 | + type: Boolean, |
| 83 | + default: false, |
| 84 | + }, |
| 85 | + }, |
| 86 | + data() { |
| 87 | + return { |
| 88 | + custom_input: false, |
| 89 | + forcing_input: false, |
| 90 | + // Form can't be computed correctly, so we save it's state under data |
| 91 | + is_form_valid: false, |
| 92 | + new_value: 0, |
| 93 | + selected_bitflags: [] as number[], |
| 94 | + } |
| 95 | + }, |
| 96 | + computed: { |
| 97 | + as_select_items() { |
| 98 | + const entries = Object.entries(this.param?.options ?? []) |
| 99 | + return entries.map(([value, name]) => ({ text: name, value: parseFloat(value), disabled: false })) |
| 100 | + }, |
| 101 | + edited_bitmask_value(): number { |
| 102 | + return this.selected_bitflags.reduce((accumulator, current) => accumulator + current, 0) |
| 103 | + }, |
| 104 | + show_advanced_checkbox(): boolean { |
| 105 | + return typeof this.isInRange(this.new_value ?? 0) === 'string' |
| 106 | + }, |
| 107 | + show_custom_checkbox(): boolean { |
| 108 | + return !!(this.param?.options || this.param?.bitmask) && this.allowCustom |
| 109 | + }, |
| 110 | + waiting_for_param_update(): boolean { |
| 111 | + return this.param.value !== this.new_value |
| 112 | + }, |
| 113 | + param_value() { |
| 114 | + return this.param?.value ?? 0 |
| 115 | + }, |
| 116 | + }, |
| 117 | + watch: { |
| 118 | + is_form_valid(valid) { |
| 119 | + if (!valid) { |
| 120 | + this.forcing_input = true |
| 121 | + } |
| 122 | + }, |
| 123 | + selected_bitflags() { |
| 124 | + this.new_value = this.edited_bitmask_value |
| 125 | + this.updateVariables() |
| 126 | + }, |
| 127 | + param_value() { |
| 128 | + this.updateSelectedFlags() |
| 129 | + }, |
| 130 | + }, |
| 131 | + mounted() { |
| 132 | + this.new_value = this.param?.value ?? 0 |
| 133 | + this.updateVariables() |
| 134 | + this.updateSelectedFlags() |
| 135 | + }, |
| 136 | + methods: { |
| 137 | + isInRange(input: number | string): boolean | string { |
| 138 | + // The input value is an empty string when the field is empty |
| 139 | + if (typeof input === 'string' && input?.trim().length === 0) { |
| 140 | + return 'This should be a number between min and max' |
| 141 | + } |
| 142 | + input = Number(input) |
| 143 | +
|
| 144 | + if (!this.param?.range) { |
| 145 | + return true |
| 146 | + } |
| 147 | +
|
| 148 | + if (this.param?.bitmask && input < 0) { |
| 149 | + return 'Value should be greater or equal to 0' |
| 150 | + } |
| 151 | +
|
| 152 | + if (input > this.param.range.high) { |
| 153 | + return `Value should be smaller than ${this.param.range.high}` |
| 154 | + } |
| 155 | + if (input < this.param.range.low) { |
| 156 | + return `Value should be greater than ${this.param.range.low}` |
| 157 | + } |
| 158 | + return true |
| 159 | + }, |
| 160 | + isValidType(input: number): boolean | string { |
| 161 | + if (this.param?.paramType.type.includes('UINT')) { |
| 162 | + if (input < 0) { |
| 163 | + return 'This parameter must be a positive Integer' |
| 164 | + } |
| 165 | + } |
| 166 | + if (this.param?.paramType.type.includes('INT')) { |
| 167 | + if (!Number.isInteger(input)) { |
| 168 | + return 'This parameter must be an Integer' |
| 169 | + } |
| 170 | + } |
| 171 | + return true |
| 172 | + }, |
| 173 | + updateSelectedFlags(): void { |
| 174 | + const value = this.param.value ?? 0 |
| 175 | + if (value < 0) { |
| 176 | + // No bitmask checking for negative values |
| 177 | + return |
| 178 | + } |
| 179 | +
|
| 180 | + const output = [] |
| 181 | + for (let bit = 0; bit < 64; bit += 1) { |
| 182 | + const bitmask_value = 2 ** bit |
| 183 | + // eslint-disable-next-line no-bitwise |
| 184 | + if (value & bitmask_value) { |
| 185 | + output.push(bitmask_value) |
| 186 | + } |
| 187 | + } |
| 188 | + this.selected_bitflags = output |
| 189 | + }, |
| 190 | +
|
| 191 | + async saveEditedParam() { |
| 192 | + if (this.param_value === this.new_value) { |
| 193 | + return |
| 194 | + } |
| 195 | + if (!this.forcing_input && !this.is_form_valid) { |
| 196 | + return |
| 197 | + } |
| 198 | + if (this.param == null) { |
| 199 | + return |
| 200 | + } |
| 201 | + if (!this.custom_input && this.param?.bitmask !== undefined) { |
| 202 | + this.new_value = this.edited_bitmask_value |
| 203 | + } |
| 204 | + let value = 0 |
| 205 | + if (typeof this.new_value === 'string') { |
| 206 | + value = parseFloat(this.new_value) |
| 207 | + } else { |
| 208 | + value = this.new_value |
| 209 | + } |
| 210 | + if (this.param?.rebootRequired) { |
| 211 | + autopilot_data.setRebootRequired(false) |
| 212 | + } |
| 213 | + mavlink2rest.setParam(this.param.name, value, autopilot_data.system_id, this.param.paramType.type) |
| 214 | + }, |
| 215 | + updateVariables(): void { |
| 216 | + // Select custom input if value is outside of possible options |
| 217 | + // Remove custom once value is known |
| 218 | + if (this.custom_input) { |
| 219 | + this.custom_input = !Object.keys(this.param?.options ?? []) |
| 220 | + .map((value) => parseFloat(value)) |
| 221 | + .includes(this.new_value) |
| 222 | + } |
| 223 | +
|
| 224 | + this.saveEditedParam() |
| 225 | + if (!this.is_form_valid) { |
| 226 | + this.forcing_input = true |
| 227 | + } |
| 228 | + }, |
| 229 | + }, |
| 230 | +}) |
| 231 | +</script> |
0 commit comments