Skip to content

Commit b64e46f

Browse files
feat(conditional): allow branches on linear scale and color questions
A linear scale question opens a branch when its value equals, does not equal, lies within a range, or is above a minimum or below a maximum. A color question opens a branch when the color matches, ignoring case. The color button names the selected color for screen readers. The code comes from the original pull request #3123 by Micke Nordin and TimedIn, with the review fixes applied. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent b8de1b0 commit b64e46f

9 files changed

Lines changed: 347 additions & 0 deletions

File tree

‎docs/DataStructure.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,11 @@ Follow-up questions (subquestions) are questions of their own with additional pr
304304
| `multiple_unique` | `option_selected` | Radio buttons - the selected option |
305305
| `dropdown` | `option_selected` | Dropdown - the selected option |
306306
| `multiple` | `options_combination` | Checkboxes - all options of a combination |
307+
| `linearscale` | `value_equals`, `value_not_equals`, `value_range`, `value_min`, `value_max` | Linear scale - numeric value matching |
307308
| `date` | `date_range` | Date - range including its bounds (`YYYY-MM-DD`) |
308309
| `datetime` | `date_range` | Deprecated date and time (`YYYY-MM-DD HH:mm`) |
309310
| `time` | `date_range` | Time - range including its bounds (`HH:mm`) |
311+
| `color` | `value_equals` | Color - exact value, ignoring case |
310312
| `file` | `file_uploaded` | File - whether a file was uploaded |
311313

312314
A branch is shown when any of its conditions matches. Several branches can be shown at once.
@@ -357,6 +359,24 @@ Conditions determine when a branch is activated. The structure depends on the ty
357359

358360
All options in `optionIds` must be selected for the condition to match.
359361

362+
#### value_equals, value_not_equals (for linearscale), value_equals (for color)
363+
364+
```json
365+
{ "type": "value_equals", "value": 3 }
366+
```
367+
368+
```json
369+
{ "type": "value_equals", "value": "#ff0000" }
370+
```
371+
372+
#### value_range, value_min, value_max (for linearscale)
373+
374+
```json
375+
{ "type": "value_range", "min": 3, "max": 5 }
376+
```
377+
378+
`value_min` only uses `min`, `value_max` only uses `max`.
379+
360380
#### date_range (for date, datetime, time)
361381

362382
```json

‎lib/Constants.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ class Constants {
244244
*/
245245
public const CONDITION_TYPE_OPTION_SELECTED = 'option_selected';
246246
public const CONDITION_TYPE_OPTIONS_COMBINATION = 'options_combination';
247+
public const CONDITION_TYPE_VALUE_EQUALS = 'value_equals';
248+
public const CONDITION_TYPE_VALUE_NOT_EQUALS = 'value_not_equals';
249+
public const CONDITION_TYPE_VALUE_RANGE = 'value_range';
250+
public const CONDITION_TYPE_VALUE_MIN = 'value_min';
251+
public const CONDITION_TYPE_VALUE_MAX = 'value_max';
247252
public const CONDITION_TYPE_DATE_RANGE = 'date_range';
248253
public const CONDITION_TYPE_FILE_UPLOADED = 'file_uploaded';
249254

@@ -257,9 +262,11 @@ class Constants {
257262
self::ANSWER_TYPE_MULTIPLEUNIQUE => [self::CONDITION_TYPE_OPTION_SELECTED],
258263
self::ANSWER_TYPE_DROPDOWN => [self::CONDITION_TYPE_OPTION_SELECTED],
259264
self::ANSWER_TYPE_MULTIPLE => [self::CONDITION_TYPE_OPTIONS_COMBINATION],
265+
self::ANSWER_TYPE_LINEARSCALE => [self::CONDITION_TYPE_VALUE_EQUALS, self::CONDITION_TYPE_VALUE_NOT_EQUALS, self::CONDITION_TYPE_VALUE_RANGE, self::CONDITION_TYPE_VALUE_MIN, self::CONDITION_TYPE_VALUE_MAX],
260266
self::ANSWER_TYPE_DATE => [self::CONDITION_TYPE_DATE_RANGE],
261267
self::ANSWER_TYPE_DATETIME => [self::CONDITION_TYPE_DATE_RANGE],
262268
self::ANSWER_TYPE_TIME => [self::CONDITION_TYPE_DATE_RANGE],
269+
self::ANSWER_TYPE_COLOR => [self::CONDITION_TYPE_VALUE_EQUALS],
263270
self::ANSWER_TYPE_FILE => [self::CONDITION_TYPE_FILE_UPLOADED],
264271
];
265272

‎lib/Service/SubmissionService.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,45 @@ private function evaluateBranchConditions(string $triggerType, array $triggerAns
10771077
}
10781078
}
10791079
return false;
1080+
case Constants::ANSWER_TYPE_LINEARSCALE:
1081+
$numValue = (float)($triggerAnswer[0] ?? 0);
1082+
foreach ($conditions as $condition) {
1083+
$type = $condition['type'] ?? Constants::CONDITION_TYPE_VALUE_EQUALS;
1084+
if ($type === Constants::CONDITION_TYPE_VALUE_EQUALS) {
1085+
if ($numValue == (float)($condition['value'] ?? 0)) {
1086+
return true;
1087+
}
1088+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_NOT_EQUALS) {
1089+
if ($numValue != (float)($condition['value'] ?? 0)) {
1090+
return true;
1091+
}
1092+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_RANGE) {
1093+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1094+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1095+
if ($numValue >= $min && $numValue <= $max) {
1096+
return true;
1097+
}
1098+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MIN) {
1099+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1100+
if ($numValue >= $min) {
1101+
return true;
1102+
}
1103+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MAX) {
1104+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1105+
if ($numValue <= $max) {
1106+
return true;
1107+
}
1108+
}
1109+
}
1110+
return false;
1111+
case Constants::ANSWER_TYPE_COLOR:
1112+
$colorValue = is_scalar($triggerAnswer[0] ?? null) ? (string)$triggerAnswer[0] : '';
1113+
foreach ($conditions as $condition) {
1114+
if (strcasecmp($colorValue, $condition['value'] ?? '') === 0) {
1115+
return true;
1116+
}
1117+
}
1118+
return false;
10801119
case Constants::ANSWER_TYPE_FILE:
10811120
$hasFile = !empty($triggerAnswer);
10821121
foreach ($conditions as $condition) {

‎src/components/Conditional/BranchConditionEditor.vue‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,80 @@
3232
</div>
3333
</template>
3434

35+
<!-- Value-based conditions (linearscale, color) -->
36+
<template v-else-if="isValueBasedTrigger">
37+
<div class="condition-row">
38+
<template v-if="triggerType === 'linearscale'">
39+
<NcSelect
40+
v-model="conditionType"
41+
:options="valueConditionTypes"
42+
:placeholder="t('forms', 'Condition type')"
43+
:ariaLabelCombobox="t('forms', 'Condition type')"
44+
label="label"
45+
:reduce="(opt) => opt.value"
46+
class="condition-type-select" />
47+
<template
48+
v-if="
49+
conditionType === 'value_equals'
50+
|| conditionType === 'value_not_equals'
51+
">
52+
<NcTextField
53+
v-model.number="conditionValue"
54+
type="number"
55+
:label="t('forms', 'Value')"
56+
class="condition-value-input" />
57+
</template>
58+
<template v-else-if="conditionType === 'value_range'">
59+
<NcTextField
60+
v-model.number="conditionMin"
61+
type="number"
62+
:label="t('forms', 'Minimum value')"
63+
class="condition-range-input" />
64+
<span class="condition-range-separator">-</span>
65+
<NcTextField
66+
v-model.number="conditionMax"
67+
type="number"
68+
:label="t('forms', 'Maximum value')"
69+
class="condition-range-input" />
70+
</template>
71+
<template v-else-if="conditionType === 'value_min'">
72+
<NcTextField
73+
v-model.number="conditionMin"
74+
type="number"
75+
:label="t('forms', 'Minimum value')"
76+
class="condition-value-input" />
77+
</template>
78+
<template v-else-if="conditionType === 'value_max'">
79+
<NcTextField
80+
v-model.number="conditionMax"
81+
type="number"
82+
:label="t('forms', 'Maximum value')"
83+
class="condition-value-input" />
84+
</template>
85+
</template>
86+
<template v-else-if="triggerType === 'color'">
87+
<NcColorPicker v-model="conditionValue">
88+
<NcButton
89+
:aria-label="
90+
t('forms', 'Select color, current: {color}', {
91+
color: conditionValue || t('forms', 'none'),
92+
})
93+
">
94+
<template #icon>
95+
<div
96+
class="color-preview"
97+
aria-hidden="true"
98+
:style="{
99+
backgroundColor: conditionValue || '#000000',
100+
}" />
101+
</template>
102+
{{ conditionValue || t('forms', 'Select color') }}
103+
</NcButton>
104+
</NcColorPicker>
105+
</template>
106+
</div>
107+
</template>
108+
35109
<!-- Date/time-based conditions -->
36110
<template v-else-if="isDateBasedTrigger">
37111
<div class="condition-row">
@@ -67,17 +141,23 @@
67141
<script>
68142
import { t } from '@nextcloud/l10n'
69143
import moment from '@nextcloud/moment'
144+
import NcButton from '@nextcloud/vue/components/NcButton'
70145
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
146+
import NcColorPicker from '@nextcloud/vue/components/NcColorPicker'
71147
import NcDateTimePicker from '@nextcloud/vue/components/NcDateTimePicker'
72148
import NcSelect from '@nextcloud/vue/components/NcSelect'
149+
import NcTextField from '@nextcloud/vue/components/NcTextField'
73150
74151
export default {
75152
name: 'BranchConditionEditor',
76153
77154
components: {
155+
NcButton,
78156
NcCheckboxRadioSwitch,
157+
NcColorPicker,
79158
NcDateTimePicker,
80159
NcSelect,
160+
NcTextField,
81161
},
82162
83163
props: {
@@ -125,6 +205,13 @@ export default {
125205
return ['multiple_unique', 'dropdown'].includes(this.triggerType)
126206
},
127207
208+
/**
209+
* Check if trigger is value-based (linearscale, color)
210+
*/
211+
isValueBasedTrigger() {
212+
return ['linearscale', 'color'].includes(this.triggerType)
213+
},
214+
128215
/**
129216
* Check if trigger is date-based (date, datetime, time)
130217
*/
@@ -175,6 +262,19 @@ export default {
175262
return this.isSingleSelect ? selected[0] || null : selected
176263
},
177264
265+
/**
266+
* Value condition type options (for linear scale)
267+
*/
268+
valueConditionTypes() {
269+
return [
270+
{ value: 'value_equals', label: t('forms', 'Equals') },
271+
{ value: 'value_not_equals', label: t('forms', 'Not Equals') },
272+
{ value: 'value_range', label: t('forms', 'In range') },
273+
{ value: 'value_min', label: t('forms', 'Higher than') },
274+
{ value: 'value_max', label: t('forms', 'Lower than') },
275+
]
276+
},
277+
178278
/**
179279
* Current condition type from branch
180280
*/
@@ -192,10 +292,24 @@ export default {
192292
* Default condition type based on trigger
193293
*/
194294
defaultConditionType() {
295+
if (this.triggerType === 'linearscale') return 'value_equals'
195296
if (this.isDateBasedTrigger) return 'date_range'
196297
return null
197298
},
198299
300+
/**
301+
* Current condition value
302+
*/
303+
conditionValue: {
304+
get() {
305+
return this.branch.conditions?.[0]?.value || ''
306+
},
307+
308+
set(value) {
309+
this.updateCondition({ value })
310+
},
311+
},
312+
199313
/**
200314
* Storage format of date and time conditions, the same as for answers
201315
*/
@@ -364,11 +478,31 @@ export default {
364478
flex-wrap: wrap;
365479
}
366480
481+
.condition-type-select {
482+
min-width: 150px;
483+
}
484+
485+
.condition-value-input {
486+
flex: 1;
487+
min-width: 200px;
488+
}
489+
490+
.condition-range-input {
491+
width: 100px;
492+
}
493+
367494
.condition-range-separator {
368495
color: var(--color-text-maxcontrast);
369496
}
370497
371498
.condition-date-input {
372499
flex: 1;
373500
}
501+
502+
.color-preview {
503+
width: 24px;
504+
height: 24px;
505+
border-radius: var(--border-radius);
506+
border: 1px solid var(--color-border-dark);
507+
}
374508
</style>

‎src/components/Questions/QuestionColor.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</div>
4242
</div>
4343
<template #insert>
44+
<slot name="branches" />
4445
<slot name="insert" />
4546
</template>
4647
</Question>

‎src/components/Questions/QuestionLinearScale.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
</div>
130130
</div>
131131
<template #insert>
132+
<slot name="branches" />
132133
<slot name="insert" />
133134
</template>
134135
</Question>

‎src/utils/Branches.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const BRANCH_MATCHERS: Record<string, BranchMatcher> = {
2222
datetime: matchDateRange,
2323
time: matchDateRange,
2424
file: matchFileUploaded,
25+
linearscale: matchValue,
26+
color: matchColor,
2527
}
2628

2729
/**
@@ -93,6 +95,58 @@ function matchFileUploaded(
9395
)
9496
}
9597

98+
/**
99+
* Linear scales match if the value fits any condition
100+
*
101+
* @param conditions the conditions of the branch
102+
* @param values the answer of the question
103+
*/
104+
function matchValue(
105+
conditions: Record<string, unknown>[],
106+
values: string[],
107+
): boolean {
108+
const value = parseFloat(values[0]) || 0
109+
const min = (condition: Record<string, unknown>) =>
110+
condition.min ?? Number.MIN_SAFE_INTEGER
111+
const max = (condition: Record<string, unknown>) =>
112+
condition.max ?? Number.MAX_SAFE_INTEGER
113+
return conditions.some((condition) => {
114+
switch (condition.type ?? 'value_equals') {
115+
case 'value_equals':
116+
return value === parseFloat(String(condition.value))
117+
case 'value_not_equals':
118+
return value !== parseFloat(String(condition.value))
119+
case 'value_min':
120+
return value >= Number(min(condition))
121+
case 'value_max':
122+
return value <= Number(max(condition))
123+
case 'value_range':
124+
return (
125+
value >= Number(min(condition))
126+
&& value <= Number(max(condition))
127+
)
128+
default:
129+
return false
130+
}
131+
})
132+
}
133+
134+
/**
135+
* Colors match if they equal the value of any condition, ignoring case
136+
*
137+
* @param conditions the conditions of the branch
138+
* @param values the answer of the question
139+
*/
140+
function matchColor(
141+
conditions: Record<string, unknown>[],
142+
values: string[],
143+
): boolean {
144+
const color = values[0].toLowerCase()
145+
return conditions.some(
146+
(condition) => color === String(condition.value ?? '').toLowerCase(),
147+
)
148+
}
149+
96150
/**
97151
* @param type the question type
98152
*/

0 commit comments

Comments
 (0)