Skip to content

Commit 931a53d

Browse files
committed
Add "show in legend" option to dropdown/checkbox fields (#330)
1 parent ba42784 commit 931a53d

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

docs/src/developers/client/types.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ their `idx` property.
114114
different object properties
115115
* `colourFixed`, `sizeFixed`, `iconFixed`, `shapeFixed`, `widthFixed`, `strokeFixed`, `modeFixed` (boolean): Whether those values are fixed and
116116
cannot be changed for an individual object
117-
* `fields` ([object]): The form fields for this type. Each field has the following properties:
117+
* `fields` (object): The form fields for this type. Each field has the following properties:
118118
* `name` (string): The name of the field. This is at the same time the key in the `data` properties of markers and lines. Note that the if the name is "Description", the FacilMap UI will translate the name to other languages even though the underlying name is in English.
119119
* `oldName` (string): When renaming a field (using [`editType(data)`](./methods.md#edittype-data)), specify the former name here
120120
* `type` (string): The type of field, one of `textarea`, `dropdown`, `checkbox`, `input`
121-
* `controlColour`, `controlSize`, `controlIcon`, `controlShape`, `controlWidth`, `controlStroke` (boolean): If this field is a dropdown, whether the different options set a specific property on the object
121+
* `controlColour`, `controlSize`, `controlIcon`, `controlShape`, `controlWidth`, `controlStroke` (boolean): If this field is a dropdown or checkbox, whether the different options set a specific property on the object
122+
* `showInLegend` (boolean): If this field is a dropdown or checkbox, whether items for its options should be shown in the legend. Only has an effect if the type itself is shown in the legend. If set to undefined, it is shown in the legend only if the field controls the colour, marker icon or shape, or line width or stroke.
122123
* `default` (string/boolean): The default value of this field
123-
* `options` ([object]): If this field is a dropdown or a checkbox, an array of objects with the following properties. For a checkbox, the array has to have 2 items, the first representing the unchecked and the second the checked state.
124+
* `options` (object): If this field is a dropdown or a checkbox, an array of objects with the following properties. For a checkbox, the array has to have 2 items, the first representing the unchecked and the second the checked state.
124125
* `value` (string): The value of this option.
125126
* `oldValue` (string): When renaming a dropdown option (using [`editType(data)`](./methods.md#edittype-data)), specify the former value here
126127
* `colour`, `size`, `shape`, `icon`, `width`, `stroke` (string/number): The property value if this field controls that property

frontend/src/i18n/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
"control-shape": "Form {{type}} kontrollieren",
231231
"control-width": "Dicke {{type}} kontrollieren",
232232
"control-stroke": "Kontur {{type}} kontrollieren",
233+
"show-in-legend": "In der Legende anzeigen",
233234
"option": "Option",
234235
"label": "Bezeichnung (für Legende)",
235236
"colour": "Farbe",

frontend/src/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
"control-shape": "Control {{type}} shape",
233233
"control-width": "Control {{type}} width",
234234
"control-stroke": "Control {{type}} stroke",
235+
"show-in-legend": "Show in legend",
235236
"option": "Option",
236237
"label": "Label (for legend)",
237238
"colour": "Colour",

frontend/src/lib/components/edit-type-dialog/edit-type-dropdown-dialog.vue

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import type { CRU, Field, FieldOptionUpdate, FieldUpdate, Type } from "facilmap-types";
3-
import { canControl, formatCheckboxValue, mergeObject } from "facilmap-utils";
3+
import { canControl, formatCheckboxValue, getDefaultFieldShowInLegend, mergeObject } from "facilmap-utils";
44
import { getUniqueId } from "../../utils/utils";
55
import { cloneDeep, isEqual } from "lodash-es";
66
import ColourPicker from "../ui/colour-picker.vue";
@@ -124,6 +124,23 @@
124124
125125
const columns = computed(() => controlNumber.value + (fieldValue.value.type === "checkbox" ? 2 : 3));
126126
127+
const defaultShowInLegend = computed(() => getDefaultFieldShowInLegend(props.type, fieldValue.value));
128+
129+
const showInLegend = computed({
130+
get: () => fieldValue.value.showInLegend ?? defaultShowInLegend.value,
131+
set: (v) => {
132+
if (v === defaultShowInLegend.value) {
133+
delete fieldValue.value.showInLegend;
134+
} else {
135+
fieldValue.value.showInLegend = v;
136+
}
137+
}
138+
});
139+
140+
watch(defaultShowInLegend, () => {
141+
delete fieldValue.value.showInLegend;
142+
});
143+
127144
function validateOptionValue(value: string): string | undefined {
128145
if (fieldValue.value.type !== "checkbox" && fieldValue.value.options!.filter((op) => op.value === value).length > 1) {
129146
return i18n.t("edit-type-dropdown-dialog.unique-value-error");
@@ -257,13 +274,28 @@
257274
{{i18n.t("edit-type-dropdown-dialog.control-stroke", { type: typeInterpolation })}}
258275
</label>
259276
</div>
277+
278+
<div v-if="type.showInLegend" class="form-check">
279+
<input
280+
:id="`${id}-showInLegend`"
281+
class="form-check-input"
282+
type="checkbox"
283+
v-model="showInLegend"
284+
/>
285+
<label
286+
class="form-check-label"
287+
:for="`${id}-showInLegend`"
288+
>
289+
{{i18n.t("edit-type-dropdown-dialog.show-in-legend")}}
290+
</label>
291+
</div>
260292
</div>
261293
</div>
262-
<table v-if="fieldValue.type != 'checkbox' || controlNumber > 0" class="table table-striped table-hover">
294+
<table v-if="fieldValue.type != 'checkbox' || controlNumber > 0 || showInLegend" class="table table-striped table-hover">
263295
<thead>
264296
<tr>
265297
<th>{{i18n.t("edit-type-dropdown-dialog.option")}}</th>
266-
<th v-if="fieldValue.type == 'checkbox'">{{i18n.t("edit-type-dropdown-dialog.label")}}</th>
298+
<th v-if="fieldValue.type == 'checkbox' && showInLegend">{{i18n.t("edit-type-dropdown-dialog.label")}}</th>
267299
<th v-if="fieldValue.controlColour">{{i18n.t("edit-type-dropdown-dialog.colour")}}</th>
268300
<th v-if="fieldValue.controlSize">{{i18n.t("edit-type-dropdown-dialog.size")}}</th>
269301
<th v-if="fieldValue.controlIcon">{{i18n.t("edit-type-dropdown-dialog.icon")}}</th>
@@ -286,6 +318,7 @@
286318
<strong>{{formatCheckboxValue(idx === 0 ? "0" : "1")}}</strong>
287319
</td>
288320
<ValidatedField
321+
v-if="fieldValue.type !== 'checkbox' || showInLegend"
289322
tag="td"
290323
class="field position-relative"
291324
:value="option.value"

frontend/src/lib/components/legend/legend-utils.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ID, Shape, Stroke, Icon, Type } from "facilmap-types";
22
import { iconList } from "facilmap-leaflet";
3-
import { formatTypeName, getOrderedTypes, isBright } from "facilmap-utils";
3+
import { formatTypeName, getDefaultFieldShowInLegend, getOrderedTypes, isBright } from "facilmap-utils";
44
import type { FacilMapContext } from "../facil-map-context-provider/facil-map-context";
55
import { requireClientContext, requireMapContext } from "../facil-map-context-provider/facil-map-context-provider.vue";
66

@@ -80,13 +80,7 @@ export function getLegendItems(context: FacilMapContext): LegendType[] {
8080
for (const field of type.fields) {
8181
if (
8282
(field.type != "dropdown" && field.type != "checkbox") ||
83-
(
84-
!field.controlColour &&
85-
!(type.type === "marker" && field.controlIcon) &&
86-
!(type.type === "marker" && field.controlShape) &&
87-
!(type.type === "line" && field.controlWidth) &&
88-
!(type.type === "line" && field.controlStroke)
89-
)
83+
!(field.showInLegend ?? getDefaultFieldShowInLegend(type, field))
9084
)
9185
continue;
9286

types/src/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export const fieldValidator = cruValidator({
7070
controlShape: z.boolean().optional(),
7171
controlWidth: z.boolean().optional(),
7272
controlStroke: z.boolean().optional(),
73+
showInLegend: z.boolean().optional(),
7374

7475
options: {
7576
read: fieldOptionsValidator.read.optional(),

utils/src/objects.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export function canControl<T extends Marker | Line = Marker | Line>(type: Type<C
2525
}) as Array<T extends any ? keyof T : never>;
2626
}
2727

28+
export function getDefaultFieldShowInLegend(type: Type<CRU.READ | CRU.CREATE_VALIDATED>, field: Field): boolean {
29+
return !!(
30+
field.controlColour ||
31+
(type.type === "marker" && (field.controlIcon || field.controlShape)) ||
32+
(type.type === "line" && (field.controlWidth || field.controlStroke))
33+
);
34+
}
35+
2836
export function getSelectedOption(field: Field, value: string | undefined, ignoreDefault = false): FieldOption | undefined {
2937
const get = (val: string) => {
3038
if (field.type === "dropdown") {

0 commit comments

Comments
 (0)