|
1 | 1 | <script> |
2 | 2 |
|
3 | 3 | import get from 'lodash/get'; |
| 4 | + import isArray from 'lodash/isArray'; |
4 | 5 | import shuffled from 'kolibri-common/utils/shuffled'; |
5 | | - import { computed, h, inject, provide } from 'vue'; |
| 6 | + import { computed, getCurrentInstance, h, inject, nextTick, provide, ref, shallowRef, watch } from 'vue'; |
6 | 7 | import { BooleanProp, NonNegativeIntProp, QTIIdentifierProp } from '../../utils/props'; |
7 | 8 | import useTypedProps from '../../composables/useTypedProps'; |
8 | 9 |
|
9 | 10 | function getComponentTag(vnode) { |
10 | 11 | return get(vnode, ['componentOptions', 'Ctor', 'extendOptions', 'tag']); |
11 | 12 | } |
12 | 13 |
|
| 14 | + /** |
| 15 | + * Safely normalizes a response value to an array. |
| 16 | + * Handles null, undefined, scalars, and arrays uniformly. |
| 17 | + */ |
| 18 | + function getSelectionsArray(value) { |
| 19 | + if (value === null || value === undefined) { |
| 20 | + return []; |
| 21 | + } |
| 22 | + if (isArray(value)) { |
| 23 | + return value; |
| 24 | + } |
| 25 | + return [value]; |
| 26 | + } |
| 27 | +
|
13 | 28 | export default { |
14 | 29 | name: 'QtiChoiceInteraction', |
15 | 30 | tag: 'qti-choice-interaction', |
16 | 31 |
|
17 | 32 | setup(props, { slots, attrs }) { |
| 33 | + const { proxy } = getCurrentInstance(); |
18 | 34 | const responses = inject('responses'); |
19 | 35 |
|
20 | 36 | const QTI_CONTEXT = inject('QTI_CONTEXT'); |
|
27 | 43 | return typedProps.maxChoices.value !== 1; |
28 | 44 | }); |
29 | 45 |
|
30 | | - const isSelected = identifier => { |
| 46 | + // shallowRef wrapper so computeds re-evaluate when the underlying |
| 47 | + // QTIVariable is replaced (responses object is not reactive). |
| 48 | + const trackedVariable = shallowRef(null); |
| 49 | + const selectionVersion = ref(0); |
| 50 | +
|
| 51 | + function syncTrackedVariable() { |
31 | 52 | const variable = responses[typedProps.responseIdentifier.value]; |
32 | | - if (!variable.value) { |
33 | | - return false; |
| 53 | + if (trackedVariable.value !== variable) { |
| 54 | + trackedVariable.value = variable || null; |
34 | 55 | } |
35 | | - if (multiSelectable.value) { |
36 | | - return variable.value.includes(identifier); |
| 56 | + } |
| 57 | +
|
| 58 | + const isSelected = identifier => { |
| 59 | + const variable = trackedVariable.value; |
| 60 | + // eslint-disable-next-line no-unused-expressions |
| 61 | + selectionVersion.value; |
| 62 | + if (!variable || variable.value === null || variable.value === undefined) { |
| 63 | + return false; |
37 | 64 | } |
38 | | - return variable.value === identifier; |
| 65 | + return getSelectionsArray(variable.value).includes(identifier); |
39 | 66 | }; |
40 | 67 |
|
41 | 68 | const toggleSelection = identifier => { |
42 | 69 | if (!interactive.value) { |
43 | 70 | return; |
44 | 71 | } |
| 72 | + syncTrackedVariable(); |
45 | 73 | const currentlySelected = isSelected(identifier); |
46 | | - const variable = responses[typedProps.responseIdentifier.value]; |
| 74 | + const variable = trackedVariable.value; |
| 75 | + if (!variable) { |
| 76 | + return false; |
| 77 | + } |
47 | 78 |
|
48 | 79 | if (currentlySelected) { |
49 | | - variable.value = multiSelectable.value |
50 | | - ? variable.value.filter(v => v !== identifier) |
51 | | - : null; |
| 80 | + if (multiSelectable.value) { |
| 81 | + variable.value = getSelectionsArray(variable.value).filter(v => v !== identifier); |
| 82 | + } else { |
| 83 | + variable.value = null; |
| 84 | + } |
52 | 85 | } else { |
53 | | - variable.value = multiSelectable.value |
54 | | - ? [...(variable.value || []), identifier] |
55 | | - : identifier; |
| 86 | + if (multiSelectable.value) { |
| 87 | + const maxChoices = typedProps.maxChoices.value; |
| 88 | + const currentSelections = getSelectionsArray(variable.value); |
| 89 | + if (maxChoices > 0 && currentSelections.length >= maxChoices) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + variable.value = [...currentSelections, identifier]; |
| 93 | + } else { |
| 94 | + variable.value = identifier; |
| 95 | + } |
56 | 96 | } |
57 | 97 |
|
| 98 | + selectionVersion.value++; |
58 | 99 | return true; |
59 | 100 | }; |
60 | 101 |
|
61 | | - // Provide functions to child components |
| 102 | + // When maxChoices changes (e.g. sandbox XML editing), trim excess |
| 103 | + // selections so the constraint is immediately enforced. |
| 104 | + watch( |
| 105 | + () => typedProps.maxChoices.value, |
| 106 | + newMax => { |
| 107 | + syncTrackedVariable(); |
| 108 | + const variable = trackedVariable.value; |
| 109 | + if (!variable) { |
| 110 | + return; |
| 111 | + } |
| 112 | + const selections = getSelectionsArray(variable.value); |
| 113 | + if (newMax > 0 && selections.length > newMax) { |
| 114 | + variable.value = multiSelectable.value |
| 115 | + ? selections.slice(0, newMax) |
| 116 | + : selections[0] || null; |
| 117 | + selectionVersion.value++; |
| 118 | + } |
| 119 | + }, |
| 120 | + ); |
| 121 | +
|
| 122 | + // Roving tabindex: only one option has tabindex="0" at a time; |
| 123 | + // the rest get tabindex="-1". Arrow keys move focus between options. |
| 124 | + const focusedIndex = ref(0); |
| 125 | + // Ordered list of identifiers, updated each render via nextTick. |
| 126 | + // Used by isFocusTarget and setFocusedIndex provided to children. |
| 127 | + const orderedIdentifiers = ref([]); |
| 128 | +
|
| 129 | + function handleListKeydown(event) { |
| 130 | + const count = orderedIdentifiers.value.length; |
| 131 | + if (count === 0) { |
| 132 | + return; |
| 133 | + } |
| 134 | + const { key } = event; |
| 135 | + let newIndex = focusedIndex.value; |
| 136 | + switch (key) { |
| 137 | + case 'ArrowDown': |
| 138 | + newIndex = (newIndex + 1) % count; |
| 139 | + break; |
| 140 | + case 'ArrowUp': |
| 141 | + newIndex = (newIndex - 1 + count) % count; |
| 142 | + break; |
| 143 | + case 'Home': |
| 144 | + newIndex = 0; |
| 145 | + break; |
| 146 | + case 'End': |
| 147 | + newIndex = count - 1; |
| 148 | + break; |
| 149 | + default: |
| 150 | + // Don't prevent default for keys we don't handle |
| 151 | + return; |
| 152 | + } |
| 153 | + event.preventDefault(); |
| 154 | + focusedIndex.value = newIndex; |
| 155 | + const listEl = event.currentTarget; |
| 156 | + const options = listEl.querySelectorAll('[role="option"]'); |
| 157 | + if (options[newIndex]) { |
| 158 | + options[newIndex].focus(); |
| 159 | + } |
| 160 | + } |
| 161 | +
|
| 162 | + // Provide functions to child components (SimpleChoice). |
| 163 | + // NOTE: Only plain functions work via provide/inject here, NOT refs. |
| 164 | + // SafeHTML (functional component) creates SimpleChoice vnodes in its |
| 165 | + // own render scope, so refs provided here are invisible to SimpleChoice. |
| 166 | + // Functions work because Vue 2 resolves inject values up through the |
| 167 | + // _provided chain, which includes ChoiceInteraction regardless of how |
| 168 | + // the vnode was created. |
62 | 169 | provide('isSelected', isSelected); |
63 | 170 | provide('toggleSelection', toggleSelection); |
| 171 | + provide('isFocusTarget', identifier => { |
| 172 | + const idx = orderedIdentifiers.value.indexOf(identifier); |
| 173 | + return idx >= 0 && idx === focusedIndex.value; |
| 174 | + }); |
| 175 | + provide('setFocusedIndex', identifier => { |
| 176 | + const idx = orderedIdentifiers.value.indexOf(identifier); |
| 177 | + if (idx >= 0) { |
| 178 | + focusedIndex.value = idx; |
| 179 | + } |
| 180 | + }); |
64 | 181 |
|
65 | 182 | const getShuffledOrder = choices => { |
66 | | - if (!typedProps.shuffle) { |
| 183 | + if (!typedProps.shuffle.value) { |
67 | 184 | return choices; |
68 | 185 | } |
69 | 186 |
|
|
85 | 202 | return result; |
86 | 203 | }; |
87 | 204 |
|
88 | | - // Return render function |
89 | 205 | return () => { |
| 206 | + syncTrackedVariable(); |
90 | 207 | const allContent = slots.default(); |
91 | 208 | const nonChoiceContent = allContent.filter( |
92 | 209 | vnode => getComponentTag(vnode) !== 'qti-simple-choice', |
|
109 | 226 | // Get shuffled order (or original if shuffle=false) |
110 | 227 | const orderedChoices = getShuffledOrder(choices); |
111 | 228 |
|
| 229 | + // Keep orderedIdentifiers in sync so that provided functions |
| 230 | + // (isFocusTarget, setFocusedIndex) can map identifier -> index. |
| 231 | + // Use nextTick to avoid mutating reactive state during render, |
| 232 | + // which would trigger an infinite re-render loop in Vue 2. |
| 233 | + const ids = orderedChoices.map(c => c.identifier); |
| 234 | + const idsChanged = |
| 235 | + ids.length !== orderedIdentifiers.value.length || |
| 236 | + ids.some((id, i) => id !== orderedIdentifiers.value[i]); |
| 237 | + if (idsChanged || focusedIndex.value >= ids.length) { |
| 238 | + nextTick(() => { |
| 239 | + orderedIdentifiers.value = ids; |
| 240 | + if (focusedIndex.value >= ids.length) { |
| 241 | + focusedIndex.value = Math.max(0, ids.length - 1); |
| 242 | + } |
| 243 | + }); |
| 244 | + } |
| 245 | +
|
112 | 246 | const choicesList = h( |
113 | 247 | 'ul', |
114 | 248 | { |
115 | 249 | attrs: { |
| 250 | + role: 'listbox', |
| 251 | + 'aria-label': proxy.$tr('choiceListLabel'), |
116 | 252 | 'aria-multiselectable': multiSelectable.value, |
117 | | - class: (attrs.class || '') + ' qti-choice-interaction', |
| 253 | + }, |
| 254 | + class: [(attrs.class || ''), 'qti-choice-interaction'], |
| 255 | + on: { |
| 256 | + keydown: handleListKeydown, |
118 | 257 | }, |
119 | 258 | }, |
120 | 259 | orderedChoices.map(choice => choice.vnode), |
121 | 260 | ); |
122 | 261 |
|
123 | | - // Create container with non-choice content first, then choices list |
124 | 262 | return h('div', [...nonChoiceContent, choicesList]); |
125 | 263 | }; |
126 | 264 | }, |
|
136 | 274 | }, |
137 | 275 | /* eslint-enable */ |
138 | 276 | }, |
| 277 | + $trs: { |
| 278 | + choiceListLabel: { |
| 279 | + message: 'Answer choices', |
| 280 | + context: 'Accessible label for the list of answer choices in an assessment question', |
| 281 | + }, |
| 282 | + }, |
139 | 283 | }; |
140 | 284 |
|
141 | 285 | </script> |
|
0 commit comments