@qti-components/base 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ import type { ResponseVariable } from '../lib/variables';
10
10
  */
11
11
  export declare abstract class Interaction extends LitElement implements ValidatableInteraction {
12
12
  #private;
13
- private _context;
13
+ private get _context();
14
14
  /**
15
15
  * Delivery configuration, from the nearest provider — `qti-test` or `qti-item`.
16
16
  *
@@ -4,7 +4,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { consume, provide } from '@lit/context';
7
+ import { ContextConsumer, consume, provide } from '@lit/context';
8
8
  import { LitElement } from 'lit';
9
9
  import { property } from 'lit/decorators.js';
10
10
  import { configContext } from '../context/config.context';
@@ -15,6 +15,86 @@ import { itemContext } from '../context/item.context';
15
15
  * `@qti-components/corrections` package.
16
16
  */
17
17
  export class Interaction extends LitElement {
18
+ /**
19
+ * The item's variables. The interaction takes its response from here as well as publishing it:
20
+ * when the item's value for this response changes to something other than what the interaction
21
+ * shows, it adopts it. That is how `item.variables = …` reaches it, with no list of interactions
22
+ * kept by the item. See {@link Interaction.#adoptResponse}.
23
+ */
24
+ #itemContext;
25
+ get _context() {
26
+ return this.#itemContext.value;
27
+ }
28
+ #seenReadonly;
29
+ #seenDisabled;
30
+ /**
31
+ * Follows the item: its `readonly` and `disabled`, and the value it holds for this response.
32
+ * `readonly` and `disabled` are only taken when the item says something, so an interaction
33
+ * authored with either stays so inside an item that does not mention it.
34
+ */
35
+ #followItem(context) {
36
+ if (context?.readonly !== undefined && context.readonly !== this.#seenReadonly) {
37
+ this.#seenReadonly = context.readonly;
38
+ this.readonly = context.readonly;
39
+ }
40
+ if (context?.disabled !== undefined && context.disabled !== this.#seenDisabled) {
41
+ this.#seenDisabled = context.disabled;
42
+ this.disabled = context.disabled;
43
+ }
44
+ this.#adoptResponse(context);
45
+ }
46
+ /** The item's last value for this response, serialised, to tell a change from an unrelated update. */
47
+ #seenResponse;
48
+ /**
49
+ * What this interaction last published or adopted, serialised. It is kept from the interaction's
50
+ * own `qti-interaction-response` events, which every emitter dispatches on the interaction itself,
51
+ * and not read back from `response`: that getter has no common shape (the drag-drop interactions
52
+ * return one comma-joined string where they publish an array), so it cannot be compared.
53
+ */
54
+ #lastResponse;
55
+ #adoptScheduled;
56
+ /**
57
+ * Adopts the item's value for this response when it changed and the interaction does not already
58
+ * show it.
59
+ *
60
+ * Two things keep this from fighting the candidate. It reacts to the item's value for *this*
61
+ * response changing, not to any context update, so a score or a timer ticking never rewrites what
62
+ * is being typed. And when the item's value is what the interaction itself last published (which
63
+ * is what happens right after it publishes an answer) it does nothing, so there is no echo.
64
+ *
65
+ * The first value is only adopted when it says something: an unanswered item must not wipe a
66
+ * `response` attribute the author put on the interaction.
67
+ *
68
+ * Setting `response` on an interaction that has not rendered yet is what the item used to avoid
69
+ * by waiting, so until the first update this waits too, then looks at the latest value.
70
+ */
71
+ #adoptResponse(context) {
72
+ const id = this.responseIdentifier ?? this.getAttribute('response-identifier');
73
+ const variable = context?.variables?.find(v => v.type === 'response' && v.identifier === id);
74
+ if (!variable)
75
+ return;
76
+ if (!this.hasUpdated) {
77
+ if (!this.#adoptScheduled) {
78
+ this.#adoptScheduled = true;
79
+ void this.updateComplete.then(() => {
80
+ this.#adoptScheduled = false;
81
+ this.#adoptResponse(this._context);
82
+ });
83
+ }
84
+ return;
85
+ }
86
+ const first = this.#seenResponse === undefined;
87
+ const serialised = JSON.stringify(variable.value ?? null);
88
+ if (serialised === this.#seenResponse)
89
+ return;
90
+ this.#seenResponse = serialised;
91
+ if (first && (variable.value === null || variable.value === undefined))
92
+ return;
93
+ if (serialised === this.#lastResponse)
94
+ return;
95
+ this.#lastResponse = serialised;
96
+ this.response = variable.value;
97
+ }
18
98
  #didLogDisableAfterIfMaxChoicesReachedDeprecation;
19
99
  static { this.formAssociated = true; }
20
100
  get internals() {
@@ -84,7 +164,7 @@ export class Interaction extends LitElement {
84
164
  if (config.disableAfterIfMaxChoicesReached !== undefined) {
85
165
  if (!this.#didLogDisableAfterIfMaxChoicesReachedDeprecation) {
86
166
  this.#didLogDisableAfterIfMaxChoicesReachedDeprecation = true;
87
- console.log('[QTI Config] `disableAfterIfMaxChoicesReached` is deprecated. Use `disableAfterMaxReached` instead.');
167
+ console.warn('[QTI Config] `disableAfterIfMaxChoicesReached` is deprecated. Use `disableAfterMaxReached` instead.');
88
168
  }
89
169
  return config.disableAfterIfMaxChoicesReached;
90
170
  }
@@ -157,6 +237,18 @@ export class Interaction extends LitElement {
157
237
  }
158
238
  constructor() {
159
239
  super();
240
+ /**
241
+ * The item's variables. The interaction takes its response from here as well as publishing it:
242
+ * when the item's value for this response changes to something other than what the interaction
243
+ * shows, it adopts it. That is how `item.variables = …` reaches it, with no list of interactions
244
+ * kept by the item. See {@link Interaction.#adoptResponse}.
245
+ */
246
+ this.#itemContext = new ContextConsumer(this, {
247
+ context: itemContext,
248
+ subscribe: true,
249
+ callback: context => this.#followItem(context)
250
+ });
251
+ this.#adoptScheduled = false;
160
252
  this.#didLogDisableAfterIfMaxChoicesReachedDeprecation = false;
161
253
  /*
162
254
  * Published to the choice elements inside this interaction — their role, and how to tell
@@ -170,6 +262,9 @@ export class Interaction extends LitElement {
170
262
  this.disabled = false;
171
263
  this.readonly = false;
172
264
  this._internals = this.attachInternals();
265
+ this.addEventListener('qti-interaction-response', (e) => {
266
+ this.#lastResponse = JSON.stringify(e.detail?.response ?? null);
267
+ });
173
268
  }
174
269
  /**
175
270
  * Optional lifecycle bridge for interaction subclasses that do not need changed-property data.
@@ -185,9 +280,6 @@ export class Interaction extends LitElement {
185
280
  */
186
281
  firstUpdated(_changedProperties) { }
187
282
  }
188
- __decorate([
189
- consume({ context: itemContext, subscribe: true })
190
- ], Interaction.prototype, "_context", void 0);
191
283
  __decorate([
192
284
  consume({ context: configContext, subscribe: true })
193
285
  ], Interaction.prototype, "configContext", void 0);
@@ -1 +1 @@
1
- {"version":3,"file":"interaction.js","sourceRoot":"","sources":["../../src/abstract/interaction.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAStD;;;GAGG;AACH,MAAM,OAAgB,WAAY,SAAQ,UAAU;IA+BlD,iDAAiD,CAAS;aAanD,mBAAc,GAAG,IAAI,AAAP,CAAQ;IAG7B,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAUD,qFAAqF;IACrF,IAAc,iBAAiB;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,gBAAgB;QAClB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC;YAC9B,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,iBAAiB,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAuB,CAAC;QAC3G,MAAM,kBAAkB,GAAG,IAAI,CAAC,YAAY,CAAC,qBAAqB,CAAC,CAAC;QACpE,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC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wDACoE;AAS/C;IAA7D,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,qBAAqB,EAAE,CAAC;uDAA4B;AAE7C;IAA3C,QAAQ,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;6CAAkB;AAEjB;IAA3C,QAAQ,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;6CAAkB;AAEjC;IAA3B,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;yCAAM","sourcesContent":["import { consume, provide } from '@lit/context';\nimport { LitElement } from 'lit';\nimport { property } from 'lit/decorators.js';\n\nimport { configContext } from '../context/config.context';\nimport { interactionContext } from '../context/interaction.context';\nimport { itemContext } from '../context/item.context';\n\nimport type { PropertyValues } from 'lit';\nimport type { InteractionContext } from '../context/interaction.context';\nimport type { ConfigContext, ValidationDisplayMode } from '../context/config.context';\nimport type { ItemContext } from '../context/types/item.types';\nimport type { ValidatableInteraction } from '../lib/interaction.interface';\nimport type { ResponseVariable } from '../lib/variables';\n\n/**\n * Shared QTI interaction base. Formative correction behavior is supplied by the\n * `@qti-components/corrections` package.\n */\nexport abstract class Interaction extends LitElement implements ValidatableInteraction {\n @consume({ context: itemContext, subscribe: true })\n private _context: ItemContext;\n\n /**\n * Delivery configuration, from the nearest provider — `qti-test` or `qti-item`.\n *\n * **Public so it can be assigned directly, which is the development-time route.** An interaction\n * has to work standalone, and in a story or a spec there is usually no provider above it — so\n * setting the field is all that is needed:\n *\n * orderInteraction.configContext = { allowReorder: false, validationDisplayMode: 'none' };\n *\n * With no provider, nothing ever overwrites it. That is the whole contract, and its limit: where a\n * provider DOES exist it re-emits on its next update and wins, because `@consume` writes this same\n * field. There is no merging — set the value on the provider in that case.\n *\n * In a lit-html template, bind it as a property rather than assigning after the fact:\n *\n * html`<qti-order-interaction .configContext=${{ allowReorder: false }}>…`\n *\n * The binding is committed on the detached template clone, before the fragment is inserted, so it\n * is in place by `connectedCallback` — which matters for anything that reads config on connect.\n *\n * It was `protected`, which fooled nobody: every caller reached it anyway through an\n * `as any` / `as { configContext?: … }` cast. Public says what was already true and removes the\n * casts, rather than pretending at an encapsulation the type system was the only thing enforcing.\n */\n @consume({ context: configContext, subscribe: true })\n public configContext: ConfigContext;\n\n #didLogDisableAfterIfMaxChoicesReachedDeprecation = false;\n\n /*\n * Published to the choice elements inside this interaction — their role, and how to tell\n * whether they are draggable. Provided here on the base rather than in a mixin so that\n * ChoicesMixin and the drag-drop mixins can each contribute without two providers of the same\n * context competing on one element.\n *\n * Reassign, never mutate: an in-place change does not notify consumers.\n */\n @provide({ context: interactionContext })\n protected _interactionContext: Readonly<InteractionContext> = { choiceRole: null, draggablesSelector: null };\n\n static formAssociated = true;\n protected _internals: ElementInternals;\n\n get internals(): ElementInternals {\n return this._internals;\n }\n\n @property({ type: String, attribute: 'response-identifier' }) responseIdentifier: string;\n\n @property({ reflect: true, type: Boolean }) disabled = false;\n\n @property({ reflect: true, type: Boolean }) readonly = false;\n\n @property({ type: String }) name;\n\n /** Extension point used by wrappers that should not register as candidate inputs. */\n protected get registersWithItem(): boolean {\n return true;\n }\n\n get isInline(): boolean {\n return false;\n }\n\n get responseVariable(): ResponseVariable | undefined {\n if (!this._context?.variables) {\n return undefined;\n }\n const responseVariables = this._context.variables.filter(v => v.type === 'response') as ResponseVariable[];\n const responseIdentifier = this.getAttribute('response-identifier');\n return responseVariables.find(v => v.identifier === responseIdentifier);\n }\n\n abstract validate(): boolean;\n\n get value(): string | null {\n return JSON.stringify(this.response);\n }\n\n set value(val: string | null) {\n this.response = val ? JSON.parse(val) : null;\n }\n\n abstract get response(): string | string[] | null;\n abstract set response(val: string | string[] | null);\n\n public reportValidity(): boolean {\n const isValid = this._internals.validity.valid;\n const mode = this.validationDisplayMode;\n\n if (mode === 'native' || mode === 'both') {\n this._internals.reportValidity();\n }\n\n if (mode === 'inline' || mode === 'both') {\n this.updateInlineValidationMessage();\n } else {\n this.clearInlineValidationMessage();\n }\n\n if (mode === 'none') {\n this.clearInlineValidationMessage();\n }\n\n return isValid;\n }\n\n protected get validationDisplayMode(): ValidationDisplayMode {\n return this.configContext?.validationDisplayMode ?? 'inline';\n }\n\n protected resolveAllowReorder(options?: { defaultWhenUnset?: boolean }): boolean {\n const defaultWhenUnset = options?.defaultWhenUnset ?? true;\n const config = this.configContext;\n if (!config) {\n return defaultWhenUnset;\n }\n if (config.allowReorder !== undefined) {\n return config.allowReorder;\n }\n return defaultWhenUnset;\n }\n\n protected resolveDisableAfterMaxReached(options?: { defaultWhenUnset?: boolean }): boolean {\n const defaultWhenUnset = options?.defaultWhenUnset ?? false;\n const config = this.configContext;\n\n if (!config) {\n return defaultWhenUnset;\n }\n\n if (config.disableAfterMaxReached !== undefined) {\n return config.disableAfterMaxReached;\n }\n\n if (config.disableAfterIfMaxChoicesReached !== undefined) {\n if (!this.#didLogDisableAfterIfMaxChoicesReachedDeprecation) {\n this.#didLogDisableAfterIfMaxChoicesReachedDeprecation = true;\n console.log(\n '[QTI Config] `disableAfterIfMaxChoicesReached` is deprecated. Use `disableAfterMaxReached` instead.'\n );\n }\n return config.disableAfterIfMaxChoicesReached;\n }\n\n return defaultWhenUnset;\n }\n\n protected setInteractionValidity(\n isValid: boolean,\n validityMessage = '',\n anchor?: HTMLElement | null,\n options?: { suppressInline?: boolean }\n ): void {\n const validityAnchor = anchor ?? this;\n this._internals.setValidity(isValid ? {} : { customError: true }, validityMessage, validityAnchor);\n\n if (\n options?.suppressInline !== true &&\n (this.validationDisplayMode === 'inline' || this.validationDisplayMode === 'both')\n ) {\n this.updateInlineValidationMessage();\n }\n }\n\n protected updateInlineValidationMessage(): void {\n const validationMessageElement = this.getValidationMessageElement();\n if (!validationMessageElement) {\n return;\n }\n\n if (!this._internals.validity.valid) {\n validationMessageElement.textContent = this._internals.validationMessage;\n validationMessageElement.style.setProperty('display', 'block', 'important');\n return;\n }\n\n validationMessageElement.textContent = '';\n validationMessageElement.style.display = 'none';\n }\n\n protected clearInlineValidationMessage(): void {\n const validationMessageElement = this.getValidationMessageElement();\n if (!validationMessageElement) {\n return;\n }\n\n validationMessageElement.textContent = '';\n validationMessageElement.style.display = 'none';\n }\n\n protected getValidationMessageElement(): HTMLElement | null {\n return this.shadowRoot?.querySelector('#validation-message') as HTMLElement | null;\n }\n\n public reset(): void {\n this.response = null;\n }\n\n public formResetCallback(): void {\n this.reset();\n }\n\n public override connectedCallback(): void {\n super.connectedCallback();\n\n if (!this.registersWithItem) {\n return;\n }\n\n this.dispatchEvent(\n new CustomEvent('qti-register-interaction', {\n bubbles: true,\n composed: true,\n cancelable: false,\n detail: {\n interactionElement: this,\n responseIdentifier: this.responseIdentifier\n }\n })\n );\n }\n\n public saveResponse(value: string | string[], state?: string | null): void {\n this.dispatchEvent(\n new CustomEvent('qti-interaction-response', {\n bubbles: true,\n composed: true,\n cancelable: false,\n detail: {\n responseIdentifier: this.responseIdentifier,\n response: Array.isArray(value) ? [...value] : value,\n ...(state !== undefined ? { state } : {})\n }\n })\n );\n }\n\n constructor() {\n super();\n this._internals = this.attachInternals();\n }\n\n /**\n * Optional lifecycle bridge for interaction subclasses that do not need changed-property data.\n *\n * The parameter is `PropertyValues`, matching LitElement, and NOT `PropertyValues<this>`.\n * The polymorphic `this` narrowed the parameter per subclass, which made every interaction\n * class structurally incompatible with `LitElement` — so `Constructor<LitElement>`, the\n * standard constraint for a Lit mixin, rejected all of them and consumers could not wrap an\n * interaction in a mixin without casting. `this` buys nothing in a parameter position and\n * costs contravariant compatibility with the base class.\n *\n * Still optional, so subclasses that ignore the argument can override with `firstUpdated()`.\n */\n protected override firstUpdated(_changedProperties?: PropertyValues): void {}\n}\n"]}
1
+ {"version":3,"file":"interaction.js","sourceRoot":"","sources":["../../src/abstract/interaction.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;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AAE,IAAI,EAAE,MAAM,EAAE,CAAC;yCAAM","sourcesContent":["import { ContextConsumer, consume, provide } from '@lit/context';\nimport { LitElement } from 'lit';\nimport { property } from 'lit/decorators.js';\n\nimport { configContext } from '../context/config.context';\nimport { interactionContext } from '../context/interaction.context';\nimport { itemContext } from '../context/item.context';\n\nimport type { PropertyValues } from 'lit';\nimport type { InteractionContext } from '../context/interaction.context';\nimport type { ConfigContext, ValidationDisplayMode } from '../context/config.context';\nimport type { ItemContext } from '../context/types/item.types';\nimport type { ValidatableInteraction } from '../lib/interaction.interface';\nimport type { ResponseVariable } from '../lib/variables';\n\n/**\n * Shared QTI interaction base. Formative correction behavior is supplied by the\n * `@qti-components/corrections` package.\n */\nexport abstract class Interaction extends LitElement implements ValidatableInteraction {\n /**\n * The item's variables. The interaction takes its response from here as well as publishing it:\n * when the item's value for this response changes to something other than what the interaction\n * shows, it adopts it. That is how `item.variables = …` reaches it, with no list of interactions\n * kept by the item. See {@link Interaction.#adoptResponse}.\n */\n #itemContext = new ContextConsumer(this, {\n context: itemContext,\n subscribe: true,\n callback: context => this.#followItem(context)\n });\n\n private get _context(): ItemContext | undefined {\n return this.#itemContext.value;\n }\n\n #seenReadonly?: boolean;\n #seenDisabled?: boolean;\n\n /**\n * Follows the item: its `readonly` and `disabled`, and the value it holds for this response.\n * `readonly` and `disabled` are only taken when the item says something, so an interaction\n * authored with either stays so inside an item that does not mention it.\n */\n #followItem(context: ItemContext | undefined) {\n if (context?.readonly !== undefined && context.readonly !== this.#seenReadonly) {\n this.#seenReadonly = context.readonly;\n this.readonly = context.readonly;\n }\n if (context?.disabled !== undefined && context.disabled !== this.#seenDisabled) {\n this.#seenDisabled = context.disabled;\n this.disabled = context.disabled;\n }\n this.#adoptResponse(context);\n }\n\n /** The item's last value for this response, serialised, to tell a change from an unrelated update. */\n #seenResponse?: string;\n /**\n * What this interaction last published or adopted, serialised. It is kept from the interaction's\n * own `qti-interaction-response` events, which every emitter dispatches on the interaction itself,\n * and not read back from `response`: that getter has no common shape (the drag-drop interactions\n * return one comma-joined string where they publish an array), so it cannot be compared.\n */\n #lastResponse?: string;\n #adoptScheduled = false;\n\n /**\n * Adopts the item's value for this response when it changed and the interaction does not already\n * show it.\n *\n * Two things keep this from fighting the candidate. It reacts to the item's value for *this*\n * response changing, not to any context update, so a score or a timer ticking never rewrites what\n * is being typed. And when the item's value is what the interaction itself last published (which\n * is what happens right after it publishes an answer) it does nothing, so there is no echo.\n *\n * The first value is only adopted when it says something: an unanswered item must not wipe a\n * `response` attribute the author put on the interaction.\n *\n * Setting `response` on an interaction that has not rendered yet is what the item used to avoid\n * by waiting, so until the first update this waits too, then looks at the latest value.\n */\n #adoptResponse(context: ItemContext | undefined) {\n const id = this.responseIdentifier ?? this.getAttribute('response-identifier');\n const variable = context?.variables?.find(v => v.type === 'response' && v.identifier === id);\n if (!variable) return;\n\n if (!this.hasUpdated) {\n if (!this.#adoptScheduled) {\n this.#adoptScheduled = true;\n void this.updateComplete.then(() => {\n this.#adoptScheduled = false;\n this.#adoptResponse(this._context);\n });\n }\n return;\n }\n\n const first = this.#seenResponse === undefined;\n const serialised = JSON.stringify(variable.value ?? null);\n if (serialised === this.#seenResponse) return;\n this.#seenResponse = serialised;\n if (first && (variable.value === null || variable.value === undefined)) return;\n if (serialised === this.#lastResponse) return;\n this.#lastResponse = serialised;\n this.response = variable.value as string | string[] | null;\n }\n\n /**\n * Delivery configuration, from the nearest provider — `qti-test` or `qti-item`.\n *\n * **Public so it can be assigned directly, which is the development-time route.** An interaction\n * has to work standalone, and in a story or a spec there is usually no provider above it — so\n * setting the field is all that is needed:\n *\n * orderInteraction.configContext = { allowReorder: false, validationDisplayMode: 'none' };\n *\n * With no provider, nothing ever overwrites it. That is the whole contract, and its limit: where a\n * provider DOES exist it re-emits on its next update and wins, because `@consume` writes this same\n * field. There is no merging — set the value on the provider in that case.\n *\n * In a lit-html template, bind it as a property rather than assigning after the fact:\n *\n * html`<qti-order-interaction .configContext=${{ allowReorder: false }}>…`\n *\n * The binding is committed on the detached template clone, before the fragment is inserted, so it\n * is in place by `connectedCallback` — which matters for anything that reads config on connect.\n *\n * It was `protected`, which fooled nobody: every caller reached it anyway through an\n * `as any` / `as { configContext?: … }` cast. Public says what was already true and removes the\n * casts, rather than pretending at an encapsulation the type system was the only thing enforcing.\n */\n @consume({ context: configContext, subscribe: true })\n public configContext: ConfigContext;\n\n #didLogDisableAfterIfMaxChoicesReachedDeprecation = false;\n\n /*\n * Published to the choice elements inside this interaction — their role, and how to tell\n * whether they are draggable. Provided here on the base rather than in a mixin so that\n * ChoicesMixin and the drag-drop mixins can each contribute without two providers of the same\n * context competing on one element.\n *\n * Reassign, never mutate: an in-place change does not notify consumers.\n */\n @provide({ context: interactionContext })\n protected _interactionContext: Readonly<InteractionContext> = { choiceRole: null, draggablesSelector: null };\n\n static formAssociated = true;\n protected _internals: ElementInternals;\n\n get internals(): ElementInternals {\n return this._internals;\n }\n\n @property({ type: String, attribute: 'response-identifier' }) responseIdentifier: string;\n\n @property({ reflect: true, type: Boolean }) disabled = false;\n\n @property({ reflect: true, type: Boolean }) readonly = false;\n\n @property({ type: String }) name;\n\n /** Extension point used by wrappers that should not register as candidate inputs. */\n protected get registersWithItem(): boolean {\n return true;\n }\n\n get isInline(): boolean {\n return false;\n }\n\n get responseVariable(): ResponseVariable | undefined {\n if (!this._context?.variables) {\n return undefined;\n }\n const responseVariables = this._context.variables.filter(v => v.type === 'response') as ResponseVariable[];\n const responseIdentifier = this.getAttribute('response-identifier');\n return responseVariables.find(v => v.identifier === responseIdentifier);\n }\n\n abstract validate(): boolean;\n\n get value(): string | null {\n return JSON.stringify(this.response);\n }\n\n set value(val: string | null) {\n this.response = val ? JSON.parse(val) : null;\n }\n\n abstract get response(): string | string[] | null;\n abstract set response(val: string | string[] | null);\n\n public reportValidity(): boolean {\n const isValid = this._internals.validity.valid;\n const mode = this.validationDisplayMode;\n\n if (mode === 'native' || mode === 'both') {\n this._internals.reportValidity();\n }\n\n if (mode === 'inline' || mode === 'both') {\n this.updateInlineValidationMessage();\n } else {\n this.clearInlineValidationMessage();\n }\n\n if (mode === 'none') {\n this.clearInlineValidationMessage();\n }\n\n return isValid;\n }\n\n protected get validationDisplayMode(): ValidationDisplayMode {\n return this.configContext?.validationDisplayMode ?? 'inline';\n }\n\n protected resolveAllowReorder(options?: { defaultWhenUnset?: boolean }): boolean {\n const defaultWhenUnset = options?.defaultWhenUnset ?? true;\n const config = this.configContext;\n if (!config) {\n return defaultWhenUnset;\n }\n if (config.allowReorder !== undefined) {\n return config.allowReorder;\n }\n return defaultWhenUnset;\n }\n\n protected resolveDisableAfterMaxReached(options?: { defaultWhenUnset?: boolean }): boolean {\n const defaultWhenUnset = options?.defaultWhenUnset ?? false;\n const config = this.configContext;\n\n if (!config) {\n return defaultWhenUnset;\n }\n\n if (config.disableAfterMaxReached !== undefined) {\n return config.disableAfterMaxReached;\n }\n\n if (config.disableAfterIfMaxChoicesReached !== undefined) {\n if (!this.#didLogDisableAfterIfMaxChoicesReachedDeprecation) {\n this.#didLogDisableAfterIfMaxChoicesReachedDeprecation = true;\n console.warn(\n '[QTI Config] `disableAfterIfMaxChoicesReached` is deprecated. Use `disableAfterMaxReached` instead.'\n );\n }\n return config.disableAfterIfMaxChoicesReached;\n }\n\n return defaultWhenUnset;\n }\n\n protected setInteractionValidity(\n isValid: boolean,\n validityMessage = '',\n anchor?: HTMLElement | null,\n options?: { suppressInline?: boolean }\n ): void {\n const validityAnchor = anchor ?? this;\n this._internals.setValidity(isValid ? {} : { customError: true }, validityMessage, validityAnchor);\n\n if (\n options?.suppressInline !== true &&\n (this.validationDisplayMode === 'inline' || this.validationDisplayMode === 'both')\n ) {\n this.updateInlineValidationMessage();\n }\n }\n\n protected updateInlineValidationMessage(): void {\n const validationMessageElement = this.getValidationMessageElement();\n if (!validationMessageElement) {\n return;\n }\n\n if (!this._internals.validity.valid) {\n validationMessageElement.textContent = this._internals.validationMessage;\n validationMessageElement.style.setProperty('display', 'block', 'important');\n return;\n }\n\n validationMessageElement.textContent = '';\n validationMessageElement.style.display = 'none';\n }\n\n protected clearInlineValidationMessage(): void {\n const validationMessageElement = this.getValidationMessageElement();\n if (!validationMessageElement) {\n return;\n }\n\n validationMessageElement.textContent = '';\n validationMessageElement.style.display = 'none';\n }\n\n protected getValidationMessageElement(): HTMLElement | null {\n return this.shadowRoot?.querySelector('#validation-message') as HTMLElement | null;\n }\n\n public reset(): void {\n this.response = null;\n }\n\n public formResetCallback(): void {\n this.reset();\n }\n\n public override connectedCallback(): void {\n super.connectedCallback();\n\n if (!this.registersWithItem) {\n return;\n }\n\n this.dispatchEvent(\n new CustomEvent('qti-register-interaction', {\n bubbles: true,\n composed: true,\n cancelable: false,\n detail: {\n interactionElement: this,\n responseIdentifier: this.responseIdentifier\n }\n })\n );\n }\n\n public saveResponse(value: string | string[], state?: string | null): void {\n this.dispatchEvent(\n new CustomEvent('qti-interaction-response', {\n bubbles: true,\n composed: true,\n cancelable: false,\n detail: {\n responseIdentifier: this.responseIdentifier,\n response: Array.isArray(value) ? [...value] : value,\n ...(state !== undefined ? { state } : {})\n }\n })\n );\n }\n\n constructor() {\n super();\n this._internals = this.attachInternals();\n this.addEventListener('qti-interaction-response', (e: Event) => {\n this.#lastResponse = JSON.stringify((e as CustomEvent).detail?.response ?? null);\n });\n }\n\n /**\n * Optional lifecycle bridge for interaction subclasses that do not need changed-property data.\n *\n * The parameter is `PropertyValues`, matching LitElement, and NOT `PropertyValues<this>`.\n * The polymorphic `this` narrowed the parameter per subclass, which made every interaction\n * class structurally incompatible with `LitElement` — so `Constructor<LitElement>`, the\n * standard constraint for a Lit mixin, rejected all of them and consumers could not wrap an\n * interaction in a mixin without casting. `this` buys nothing in a parameter position and\n * costs contravariant compatibility with the base class.\n *\n * Still optional, so subclasses that ignore the argument can override with `firstUpdated()`.\n */\n protected override firstUpdated(_changedProperties?: PropertyValues): void {}\n}\n"]}
@@ -1,17 +1,41 @@
1
1
  import { LitElement } from 'lit';
2
2
  import type { ItemContext } from '../context/types/item.types';
3
3
  import type { QtiContext, QtiContextType } from '../context/qti.context';
4
+ import type { BaseType } from '../lib/expression-result';
4
5
  import type { ResponseVariable, VariableDeclaration } from '../lib/variables';
5
6
  import type { TestContext } from '../context/test.context';
7
+ type ExpressionVariable = ResponseVariable | VariableDeclaration<QtiContextType>;
6
8
  export interface QtiExpressionBase<T> {
7
9
  calculate(): Readonly<T>;
8
10
  }
9
11
  export declare abstract class QtiExpression<T> extends LitElement implements QtiExpressionBase<T> {
12
+ #private;
10
13
  protected result: any;
14
+ /**
15
+ * What the last `calculate()` produced, or undefined if it has not run.
16
+ *
17
+ * This is the supported way to inspect a scoring run: process the responses,
18
+ * then walk the rule tree reading this off each expression. A tool that wants
19
+ * to visualise how a score came about gets the whole tree annotated with its
20
+ * values, without the components having to re-render to show them.
21
+ */
22
+ get lastResult(): Readonly<T> | undefined;
23
+ get lastVariables(): readonly ExpressionVariable[];
11
24
  static styles: import("lit").CSSResult;
12
25
  render(): import("lit").TemplateResult<1>;
26
+ protected shouldUpdate(): boolean;
13
27
  calculate(): Readonly<T>;
14
28
  protected getResult(): Readonly<T>;
29
+ /**
30
+ * The base type this operator always produces, when the QTI vocabulary fixes
31
+ * one regardless of its operands — `qti-divide` is a float even dividing two
32
+ * integers exactly, and `qti-round` is an integer whatever it rounds.
33
+ *
34
+ * Left undefined by operators whose result type follows their operands (a
35
+ * `qti-sum` of integers is an integer) and by those that return no number at
36
+ * all; the type is then read off the value.
37
+ */
38
+ get resultBaseType(): BaseType | undefined;
15
39
  protected context?: ItemContext;
16
40
  protected qtiContext?: QtiContext;
17
41
  protected _testContext?: TestContext;
@@ -24,5 +48,6 @@ export declare abstract class QtiExpression<T> extends LitElement implements Qti
24
48
  * throwing, so callers degrade gracefully.
25
49
  */
26
50
  protected resolveVariable(identifier: string): VariableDeclaration<string | string[] | null> | null;
27
- getVariables: () => (ResponseVariable | VariableDeclaration<QtiContextType>)[];
51
+ getVariables: () => ExpressionVariable[];
28
52
  }
53
+ export {};