Skip to content

Commit bdd436e

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 b287cc5 commit bdd436e

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
@@ -1159,6 +1159,45 @@ private function evaluateBranchConditions(string $triggerType, array $triggerAns
11591159
}
11601160
}
11611161
return false;
1162+
case Constants::ANSWER_TYPE_LINEARSCALE:
1163+
$numValue = (float)($triggerAnswer[0] ?? 0);
1164+
foreach ($conditions as $condition) {
1165+
$type = $condition['type'] ?? Constants::CONDITION_TYPE_VALUE_EQUALS;
1166+
if ($type === Constants::CONDITION_TYPE_VALUE_EQUALS) {
1167+
if ($numValue == (float)($condition['value'] ?? 0)) {
1168+
return true;
1169+
}
1170+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_NOT_EQUALS) {
1171+
if ($numValue != (float)($condition['value'] ?? 0)) {
1172+
return true;
1173+
}
1174+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_RANGE) {
1175+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1176+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1177+
if ($numValue >= $min && $numValue <= $max) {
1178+
return true;
1179+
}
1180+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MIN) {
1181+
$min = $condition['min'] ?? -PHP_FLOAT_MAX;
1182+
if ($numValue >= $min) {
1183+
return true;
1184+
}
1185+
} elseif ($type === Constants::CONDITION_TYPE_VALUE_MAX) {
1186+
$max = $condition['max'] ?? PHP_FLOAT_MAX;
1187+
if ($numValue <= $max) {
1188+
return true;
1189+
}
1190+
}
1191+
}
1192+
return false;
1193+
case Constants::ANSWER_TYPE_COLOR:
1194+
$colorValue = is_scalar($triggerAnswer[0] ?? null) ? (string)$triggerAnswer[0] : '';
1195+
foreach ($conditions as $condition) {
1196+
if (strcasecmp($colorValue, $condition['value'] ?? '') === 0) {
1197+
return true;
1198+
}
1199+
}
1200+
return false;
11621201
case Constants::ANSWER_TYPE_FILE:
11631202
$hasFile = !empty($triggerAnswer);
11641203
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/QuestionConditional.vue‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ export default defineComponent({
417417
},
418418
{
419419
label: t('forms', 'Other'),
420-
types: ['file'],
420+
types: ['linearscale', 'color', 'file'],
421421
},
422422
]
423423
@@ -975,6 +975,35 @@ export default defineComponent({
975975
return false
976976
}
977977
978+
if (this.triggerType === 'linearscale') {
979+
const numValue = parseFloat(this.triggerValues[0]) || 0
980+
return branch.conditions.some((c) => {
981+
if (c.type === 'value_equals') {
982+
return numValue === parseFloat(c.value)
983+
} else if (c.type === 'value_not_equals') {
984+
return numValue !== parseFloat(c.value)
985+
} else if (c.type === 'value_min') {
986+
const min = c.min ?? Number.MIN_SAFE_INTEGER
987+
return numValue >= min
988+
} else if (c.type === 'value_max') {
989+
const max = c.max ?? Number.MAX_SAFE_INTEGER
990+
return numValue <= max
991+
} else if (c.type === 'value_range') {
992+
const min = c.min ?? Number.MIN_SAFE_INTEGER
993+
const max = c.max ?? Number.MAX_SAFE_INTEGER
994+
return numValue >= min && numValue <= max
995+
}
996+
return false
997+
})
998+
}
999+
1000+
if (this.triggerType === 'color') {
1001+
const colorValue = (this.triggerValues[0] || '').toLowerCase()
1002+
return branch.conditions.some(
1003+
(c) => colorValue === (c.value || '').toLowerCase(),
1004+
)
1005+
}
1006+
9781007
if (this.triggerType === 'file') {
9791008
const hasFile =
9801009
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)