Skip to content

Commit 6e7c64d

Browse files
feat(conditional): add linear scale and color as trigger
A linear scale trigger activates a branch when its value equals, does not equal, lies within a range, or is above a minimum or below a maximum. A color trigger activates 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 fe30885 commit 6e7c64d

7 files changed

Lines changed: 291 additions & 1 deletion

File tree

‎docs/DataStructure.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,11 @@ Supported trigger types for conditional questions:
308308
| `multiple_unique` | `option_selected` | Radio buttons - the selected option |
309309
| `dropdown` | `option_selected` | Dropdown - the selected option |
310310
| `multiple` | `options_combination` | Checkboxes - all options of a combination |
311+
| `linearscale` | `value_equals`, `value_not_equals`, `value_range`, `value_min`, `value_max` | Linear scale - numeric value matching |
311312
| `date` | `date_range` | Date - range including its bounds (`YYYY-MM-DD`) |
312313
| `datetime` | `date_range` | Deprecated date and time (`YYYY-MM-DD HH:mm`) |
313314
| `time` | `date_range` | Time - range including its bounds (`HH:mm`) |
315+
| `color` | `value_equals` | Color - exact value, ignoring case |
314316
| `file` | `file_uploaded` | File - whether a file was uploaded |
315317

316318
A branch is shown when any of its conditions matches. Several branches can be shown at once.
@@ -361,6 +363,24 @@ Conditions determine when a branch is activated. The structure depends on the tr
361363

362364
All options in `optionIds` must be selected for the condition to match.
363365

366+
#### value_equals, value_not_equals (for linearscale), value_equals (for color)
367+
368+
```json
369+
{ "type": "value_equals", "value": 3 }
370+
```
371+
372+
```json
373+
{ "type": "value_equals", "value": "#ff0000" }
374+
```
375+
376+
#### value_range, value_min, value_max (for linearscale)
377+
378+
```json
379+
{ "type": "value_range", "min": 3, "max": 5 }
380+
```
381+
382+
`value_min` only uses `min`, `value_max` only uses `max`.
383+
364384
#### date_range (for date, datetime, time)
365385

366386
```json

‎lib/Constants.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ class Constants {
261261
*/
262262
public const CONDITION_TYPE_OPTION_SELECTED = 'option_selected';
263263
public const CONDITION_TYPE_OPTIONS_COMBINATION = 'options_combination';
264+
public const CONDITION_TYPE_VALUE_EQUALS = 'value_equals';
265+
public const CONDITION_TYPE_VALUE_NOT_EQUALS = 'value_not_equals';
266+
public const CONDITION_TYPE_VALUE_RANGE = 'value_range';
267+
public const CONDITION_TYPE_VALUE_MIN = 'value_min';
268+
public const CONDITION_TYPE_VALUE_MAX = 'value_max';
264269
public const CONDITION_TYPE_DATE_RANGE = 'date_range';
265270
public const CONDITION_TYPE_FILE_UPLOADED = 'file_uploaded';
266271

@@ -272,9 +277,11 @@ class Constants {
272277
self::ANSWER_TYPE_MULTIPLEUNIQUE => [self::CONDITION_TYPE_OPTION_SELECTED],
273278
self::ANSWER_TYPE_DROPDOWN => [self::CONDITION_TYPE_OPTION_SELECTED],
274279
self::ANSWER_TYPE_MULTIPLE => [self::CONDITION_TYPE_OPTIONS_COMBINATION],
280+
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],
275281
self::ANSWER_TYPE_DATE => [self::CONDITION_TYPE_DATE_RANGE],
276282
self::ANSWER_TYPE_DATETIME => [self::CONDITION_TYPE_DATE_RANGE],
277283
self::ANSWER_TYPE_TIME => [self::CONDITION_TYPE_DATE_RANGE],
284+
self::ANSWER_TYPE_COLOR => [self::CONDITION_TYPE_VALUE_EQUALS],
278285
self::ANSWER_TYPE_FILE => [self::CONDITION_TYPE_FILE_UPLOADED],
279286
];
280287

‎lib/Service/SubmissionService.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,45 @@ private function evaluateBranchConditions(string $triggerType, array $triggerAns
11571157
}
11581158
}
11591159
return false;
1160+
case Constants::ANSWER_TYPE_LINEARSCALE:
1161+
$numValue = (float)($triggerAnswer[0] ?? 0);
1162+
foreach ($conditions as $condition) {
1163+
$type = $condition['type'] ?? Constants::CONDITION_TYPE_VALUE_EQUALS;
1164+
if ($type === Constants::CONDITION_TYPE_VALUE_EQUALS) {
1165+
if ($numValue == (float)($condition['value'] ?? 0)) {
1166+
return true;
1167+
}
1168+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_NOT_EQUALS) {
1169+
if ($numValue != (float)($condition['value'] ?? 0)) {
1170+
return true;
1171+
}
1172+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_RANGE) {
1173+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1174+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1175+
if ($numValue >= $min && $numValue <= $max) {
1176+
return true;
1177+
}
1178+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MIN) {
1179+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1180+
if ($numValue >= $min) {
1181+
return true;
1182+
}
1183+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MAX) {
1184+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1185+
if ($numValue <= $max) {
1186+
return true;
1187+
}
1188+
}
1189+
}
1190+
return false;
1191+
case Constants::ANSWER_TYPE_COLOR:
1192+
$colorValue = is_scalar($triggerAnswer[0] ?? null) ? (string)$triggerAnswer[0] : '';
1193+
foreach ($conditions as $condition) {
1194+
if (strcasecmp($colorValue, $condition['value'] ?? '') === 0) {
1195+
return true;
1196+
}
1197+
}
1198+
return false;
11601199
case Constants::ANSWER_TYPE_FILE:
11611200
$hasFile = !empty($triggerAnswer);
11621201
foreach ($conditions as $condition) {

‎src/components/Questions/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">
@@ -66,17 +140,23 @@
66140

67141
<script>
68142
import moment from '@nextcloud/moment'
143+
import NcButton from '@nextcloud/vue/components/NcButton'
69144
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
145+
import NcColorPicker from '@nextcloud/vue/components/NcColorPicker'
70146
import NcDateTimePicker from '@nextcloud/vue/components/NcDateTimePicker'
71147
import NcSelect from '@nextcloud/vue/components/NcSelect'
148+
import NcTextField from '@nextcloud/vue/components/NcTextField'
72149
73150
export default {
74151
name: 'BranchConditionEditor',
75152
76153
components: {
154+
NcButton,
77155
NcCheckboxRadioSwitch,
156+
NcColorPicker,
78157
NcDateTimePicker,
79158
NcSelect,
159+
NcTextField,
80160
},
81161
82162
props: {
@@ -124,6 +204,13 @@ export default {
124204
return ['multiple_unique', 'dropdown'].includes(this.triggerType)
125205
},
126206
207+
/**
208+
* Check if trigger is value-based (linearscale, color)
209+
*/
210+
isValueBasedTrigger() {
211+
return ['linearscale', 'color'].includes(this.triggerType)
212+
},
213+
127214
/**
128215
* Check if trigger is date-based (date, datetime, time)
129216
*/
@@ -174,6 +261,19 @@ export default {
174261
return this.isSingleSelect ? selected[0] || null : selected
175262
},
176263
264+
/**
265+
* Value condition type options (for linear scale)
266+
*/
267+
valueConditionTypes() {
268+
return [
269+
{ value: 'value_equals', label: t('forms', 'Equals') },
270+
{ value: 'value_not_equals', label: t('forms', 'Not Equals') },
271+
{ value: 'value_range', label: t('forms', 'In range') },
272+
{ value: 'value_min', label: t('forms', 'Higher than') },
273+
{ value: 'value_max', label: t('forms', 'Lower than') },
274+
]
275+
},
276+
177277
/**
178278
* Current condition type from branch
179279
*/
@@ -191,10 +291,24 @@ export default {
191291
* Default condition type based on trigger
192292
*/
193293
defaultConditionType() {
294+
if (this.triggerType === 'linearscale') return 'value_equals'
194295
if (this.isDateBasedTrigger) return 'date_range'
195296
return null
196297
},
197298
299+
/**
300+
* Current condition value
301+
*/
302+
conditionValue: {
303+
get() {
304+
return this.branch.conditions?.[0]?.value || ''
305+
},
306+
307+
set(value) {
308+
this.updateCondition({ value })
309+
},
310+
},
311+
198312
/**
199313
* Storage format of date and time conditions, the same as for answers
200314
*/
@@ -363,11 +477,31 @@ export default {
363477
flex-wrap: wrap;
364478
}
365479
480+
.condition-type-select {
481+
min-width: 150px;
482+
}
483+
484+
.condition-value-input {
485+
flex: 1;
486+
min-width: 200px;
487+
}
488+
489+
.condition-range-input {
490+
width: 100px;
491+
}
492+
366493
.condition-range-separator {
367494
color: var(--color-text-maxcontrast);
368495
}
369496
370497
.condition-date-input {
371498
flex: 1;
372499
}
500+
501+
.color-preview {
502+
width: 24px;
503+
height: 24px;
504+
border-radius: var(--border-radius);
505+
border: 1px solid var(--color-border-dark);
506+
}
373507
</style>

‎src/components/Questions/QuestionConditional.vue‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export default defineComponent({
442442
},
443443
{
444444
label: t('forms', 'Other'),
445-
types: ['file'],
445+
types: ['linearscale', 'color', 'file'],
446446
},
447447
]
448448
@@ -1144,6 +1144,35 @@ export default defineComponent({
11441144
return false
11451145
}
11461146
1147+
if (this.triggerType === 'linearscale') {
1148+
const numValue = parseFloat(this.triggerValues[0]) || 0
1149+
return branch.conditions.some((c) => {
1150+
if (c.type === 'value_equals') {
1151+
return numValue === parseFloat(c.value)
1152+
} else if (c.type === 'value_not_equals') {
1153+
return numValue !== parseFloat(c.value)
1154+
} else if (c.type === 'value_min') {
1155+
const min = c.min ?? Number.MIN_SAFE_INTEGER
1156+
return numValue >= min
1157+
} else if (c.type === 'value_max') {
1158+
const max = c.max ?? Number.MAX_SAFE_INTEGER
1159+
return numValue <= max
1160+
} else if (c.type === 'value_range') {
1161+
const min = c.min ?? Number.MIN_SAFE_INTEGER
1162+
const max = c.max ?? Number.MAX_SAFE_INTEGER
1163+
return numValue >= min && numValue <= max
1164+
}
1165+
return false
1166+
})
1167+
}
1168+
1169+
if (this.triggerType === 'color') {
1170+
const colorValue = (this.triggerValues[0] || '').toLowerCase()
1171+
return branch.conditions.some(
1172+
(c) => colorValue === (c.value || '').toLowerCase(),
1173+
)
1174+
}
1175+
11471176
if (this.triggerType === 'file') {
11481177
const hasFile =
11491178
this.triggerValues.length > 0 && this.triggerValues[0] !== ''

‎tests/Unit/Service/FormsServiceTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,16 @@ public static function dataAreExtraSettingsValid() {
19361936
'questionType' => Constants::ANSWER_TYPE_CONDITIONAL,
19371937
'expected' => true
19381938
],
1939+
'valid-conditional-linearscale-trigger' => [
1940+
'extraSettings' => [
1941+
'triggerType' => Constants::ANSWER_TYPE_LINEARSCALE,
1942+
'branches' => [
1943+
['id' => 'branch-1', 'conditions' => [['type' => 'value_range', 'min' => 1, 'max' => 3]]]
1944+
]
1945+
],
1946+
'questionType' => Constants::ANSWER_TYPE_CONDITIONAL,
1947+
'expected' => true
1948+
],
19391949
'valid-conditional-date-trigger' => [
19401950
'extraSettings' => [
19411951
'triggerType' => Constants::ANSWER_TYPE_DATE,

0 commit comments

Comments
 (0)