@qti-components/portable-custom-interaction 1.2.1-next.1 → 1.3.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.
- package/dist/interface.d.ts +3 -2
- package/dist/interface.js.map +1 -1
- package/dist/qti-portable-custom-interaction.d.ts +10 -14
- package/dist/qti-portable-custom-interaction.js +71 -129
- package/dist/qti-portable-custom-interaction.js.map +1 -1
- package/dist/record-response.d.ts +36 -0
- package/dist/record-response.js +195 -0
- package/dist/record-response.js.map +1 -0
- package/dist/vendor/require-js.d.ts +3 -0
- package/dist/vendor/require-js.js +4 -0
- package/dist/vendor/require-js.js.map +1 -0
- package/package.json +7 -4
package/dist/interface.d.ts
CHANGED
|
@@ -9,10 +9,11 @@ export type PciInteractionStatus = 'interacting' | 'suspended' | 'closed' | 'sol
|
|
|
9
9
|
* Correct response of a response declaration, mirroring `qti-correct-response`.
|
|
10
10
|
*
|
|
11
11
|
* `value` holds the authored `qti-value` content: a single value for `single`
|
|
12
|
-
* cardinality,
|
|
12
|
+
* cardinality, a field map (`{ time: '4.0', point: '460 245' }`) for `record`
|
|
13
|
+
* cardinality, and an array of values for every other cardinality.
|
|
13
14
|
*/
|
|
14
15
|
export interface PciCorrectResponse {
|
|
15
|
-
value: string | string[]
|
|
16
|
+
value: string | string[] | Record<string, string | string[] | null>;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Response declaration passed into the `getInstance` configuration so a PCI can
|
package/dist/interface.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { BaseType, Cardinality } from '@qti-components/base';\n\n/**\n * Shared status values for PCI lifecycle callbacks.\n *\n * Matches the status vocabulary used in IMS QTI 3 PCI documentation.\n */\nexport type PciInteractionStatus = 'interacting' | 'suspended' | 'closed' | 'solution' | 'review';\n\n/**\n * Correct response of a response declaration, mirroring `qti-correct-response`.\n *\n * `value` holds the authored `qti-value` content: a single value for `single`\n * cardinality, an array of values for every other cardinality.\n */\nexport interface PciCorrectResponse {\n value: string | string[]
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"","sourcesContent":["import type { BaseType, Cardinality } from '@qti-components/base';\n\n/**\n * Shared status values for PCI lifecycle callbacks.\n *\n * Matches the status vocabulary used in IMS QTI 3 PCI documentation.\n */\nexport type PciInteractionStatus = 'interacting' | 'suspended' | 'closed' | 'solution' | 'review';\n\n/**\n * Correct response of a response declaration, mirroring `qti-correct-response`.\n *\n * `value` holds the authored `qti-value` content: a single value for `single`\n * cardinality, a field map (`{ time: '4.0', point: '460 245' }`) for `record`\n * cardinality, and an array of values for every other cardinality.\n */\nexport interface PciCorrectResponse {\n value: string | string[] | Record<string, string | string[] | null>;\n}\n\n/**\n * Response declaration passed into the `getInstance` configuration so a PCI can\n * render a correct response itself when the item is in `solution` status.\n *\n * Mirrors `qti-response-declaration` with camelCased names. Every field is\n * optional, a delivery engine passes as much or as little as it has, and\n * `correctResponse` is absent when the response variable has none.\n *\n * `correctResponse` is also withheld while the candidate is still working: it is\n * only sent when `status` is `solution` or `review`, so a PCI never holds the\n * answer key during `interacting`.\n *\n * See https://github.com/1EdTech/qti-project-management/issues/210\n */\nexport interface PciResponseDeclaration {\n baseType?: BaseType;\n cardinality?: Cardinality;\n correctResponse?: PciCorrectResponse;\n}\n\n/**\n * Payload passed to `onready` once a PCI reports it is ready.\n */\nexport interface PciReadyPayload {\n responseIdentifier: string; // The response identifier of the PCI\n status: PciInteractionStatus; // Current status\n}\n\n/**\n * Payload that may be passed to `ondone` when PCI finishes candidate interaction.\n */\nexport interface PciDonePayload {\n responseIdentifier: string; // The response identifier of the PCI\n status: PciInteractionStatus; // The final status after completion\n outcome?: unknown; // Optional outcome data (e.g., score, correctness)\n}\n\n/**\n * Configuration object shape exposed to a PCI instance.\n *\n * Notes for authors:\n * - `properties` contains `data-*` attributes from the host element using dataset rules.\n * - `templateVariables`, `contextVariables`, and `boundTo` follow QTI Appendix-C style JSON values.\n * - `onready` must be called by the PCI once it is interactive.\n * - `ondone` is optional and may be used for explicit completion workflows.\n */\nexport interface ConfigProperties<T> {\n properties: T; // Follows dataset conversion rules (camelCased keys)\n templateVariables: Record<string, unknown>; // Follows structure in Appendix C\n contextVariables: Record<string, unknown>; // Follows structure in Appendix C\n boundTo: unknown; // Follows structure in Appendix C\n responseIdentifier: string; // Unique within interaction scope\n responseDeclaration?: PciResponseDeclaration; // Optional; carries correctResponse only when status is solution/review\n\n onready: (payload: PciReadyPayload) => void; // Callback for when PCI is fully constructed and ready\n ondone?: (payload: PciDonePayload) => void; // Optional callback when candidate finishes interaction\n\n status?: PciInteractionStatus; // Optional, defaults to \"interacting\"\n}\n\n/**\n * IMS-style PCI factory and instance contract.\n *\n * A PCI module registers an object with a `typeIdentifier` and a `getInstance(...)` factory method.\n * The returned PCI instance should implement response/state read/write methods.\n */\nexport interface IMSpci<T> {\n /**\n * Stable PCI type key, expected to match `custom-interaction-type-identifier`.\n */\n typeIdentifier: string;\n\n /** @access public\n * @method getInstance Create a new instance of this portable custom interaction\n * Will be called by the qtiCustomInteractionContext\n * @param {DOM Element} dom - the DOM Element this PCI is being added to\n * @param {Object} configuration - the configuration to apply to this PCI\n * @param {String} state - a previous saved state to apply to this instance.\n * This must have been obtained from a prior call to getState on an\n * instance of this type (same typeIdentifier)\n */\n getInstance: (dom: HTMLElement, configuration: Configuration<T>, state: string) => void;\n\n /** @access public\n * @method getResponse\n * @return {Object} - the value to assign to the bound QTI response variable\n */\n getResponse: () => QtiVariableJSON | undefined;\n\n /** @access public\n * @method setResponse\n * @param {Object} value - the value to assign to the bound QTI response variable\n * NOTE: This is not an official QTI method, so no guarantee it is implemented\n * in all PCIs.\n */\n setResponse: (value: QtiVariableJSON) => void;\n\n /** @access public\n * @method getState\n * @return {String} The current state of this PCI. May be passed to\n * getInstance to later restore this PCI instance.\n * FIXME: host runtime can round-trip object states via prefixed JSON strings;\n * this type remains `string` for IMS compatibility.\n */\n getState: () => string;\n\n /** @access public\n * @method setState\n * @param {String} value - a state previously obtained from getState\n * to restore this PCI instance to a prior state.\n * FIXME: runtime may deserialize prefixed JSON state before applying it.\n */\n setState(value: string): unknown;\n\n /**\n * Optional teardown callback. If present, host may call before unload/cleanup.\n */\n oncompleted?: () => void;\n\n /**\n * Non-standard optional destroy hook; included for compatibility with legacy engines.\n */\n destroy?: () => void; // Not used in IMS and not in TAO implementation, so not used here (optional)\n}\n\n/**\n * Bridge callback signature used by this codebase for PCI registration.\n *\n * IMPORTANT:\n * - This type is intentionally minimal for existing call sites.\n * - Runtime initialization payload in `qti-portable-custom-interaction.ts` contains\n * additional fields (`boundTo`, `templateVariables`, `contextVariables`, etc.).\n * FIXME: align this type with runtime payload to remove ambiguity in generated docs.\n */\nexport declare type Configuration<T> = {\n onready: (pci: IMSpci<ConfigProperties<T>>, state?: string) => void;\n properties: T;\n // PK: following this: https://www.imsglobal.org/spec/qti/v3p0/impl#h.1mc9puik2ft6\n // All below are not used in the IMS Reference implementation, keeping for ref\n /* \n ondone: (pci: IMSPci<T>,response:any,state:string, status?: \"interacting\" | \"closed\" | \"solution\" | \"review\" ) => void;\n status: string;\n templateVariables: any; \n boundTo: any;\n contextVariables\n responseIdentifier\n */\n};\n\n/**\n * QTI base-type `file` representation.\n */\nexport type QtiFileData = {\n data: string;\n mime: string;\n name?: string;\n};\n\nexport type QtiBaseBoolean = boolean;\nexport type QtiBaseInteger = number;\nexport type QtiBaseFloat = number;\nexport type QtiBaseString = string;\nexport type QtiBasePoint = [number, number];\nexport type QtiBasePair = [string, string];\nexport type QtiBaseDirectedPair = [string, string];\nexport type QtiBaseDuration = string; // ISO 8601 duration format\nexport type QtiBaseFile = QtiFileData;\nexport type QtiBaseUri = string;\nexport type QtiBaseIntOrIdentifier = number | string;\nexport type QtiBaseIdentifier = string;\n\n/**\n * Convenience union for primitive response values used by this implementation.\n */\nexport type ResponseType =\n | boolean\n | number\n | string\n | QtiBasePair\n | QtiBasePoint\n | QtiFileData\n | Array<boolean | number | string | QtiBasePair | QtiBasePoint | QtiFileData>;\n\n/**\n * A QTI record entry for `record` cardinality values.\n */\nexport type QtiRecordItem = {\n name: string;\n base?: QtiBaseTypeJSON | null;\n list?: QtiListTypeJSON | null;\n};\nexport type ResponseVariableType = QtiVariableJSON;\n\n/**\n * QTI Appendix-C style JSON representation for base cardinality.\n */\nexport type QtiBaseTypeJSON = {\n boolean?: QtiBaseBoolean;\n integer?: QtiBaseInteger;\n float?: QtiBaseFloat;\n string?: QtiBaseString;\n point?: QtiBasePoint;\n pair?: QtiBasePair;\n directedPair?: QtiBaseDirectedPair;\n duration?: QtiBaseDuration;\n file?: QtiBaseFile;\n uri?: QtiBaseUri;\n intOrIdentifier?: QtiBaseIntOrIdentifier;\n identifier?: QtiBaseIdentifier;\n} | null;\n\n/**\n * QTI Appendix-C style JSON representation for list cardinality.\n */\nexport type QtiListTypeJSON = {\n boolean?: QtiBaseBoolean[];\n integer?: QtiBaseInteger[];\n float?: QtiBaseFloat[];\n string?: QtiBaseString[];\n point?: QtiBasePoint[];\n pair?: QtiBasePair[];\n directedPair?: QtiBaseDirectedPair[];\n duration?: QtiBaseDuration[];\n file?: QtiBaseFile[];\n uri?: QtiBaseUri[];\n intOrIdentifier?: QtiBaseIntOrIdentifier[];\n identifier?: QtiBaseIdentifier[];\n} | null;\n\n/**\n * Complete QTI variable JSON envelope used by PCI bridge messaging.\n */\nexport declare type QtiVariableJSON = {\n base?: QtiBaseTypeJSON;\n list?: QtiListTypeJSON;\n record?: QtiRecordItem[] | null;\n};\n\n/**\n * RequireJS module resolution config accepted by helper utilities and stories.\n */\nexport interface ModuleResolutionConfig {\n waitSeconds?: number;\n context?: string;\n catchError?: boolean;\n paths: {\n [key: string]: string | string[];\n };\n shim?: {\n [key: string]: {\n deps?: string[]; // Array of dependencies\n exports?: string; // The global variable to use as the module's value\n };\n };\n}\n"]}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { Interaction } from '@qti-components/base';
|
|
2
2
|
import type { CSSResultGroup } from 'lit';
|
|
3
|
-
import type { BaseType, Cardinality, ItemContext, QtiContext } from '@qti-components/base';
|
|
3
|
+
import type { BaseType, Cardinality, ItemContext, QtiContext, RecordValue } from '@qti-components/base';
|
|
4
4
|
import type { PciInteractionStatus, PciResponseDeclaration, QtiVariableJSON, ResponseVariableType } from './interface';
|
|
5
|
+
/** Text for an inline `<script>`: a `</script` inside it would end the element early. */
|
|
6
|
+
export declare const inlineScriptText: (text: string) => string;
|
|
7
|
+
/** Text for an inline `<style>`: a `</style` inside it would end the element early. */
|
|
8
|
+
export declare const inlineStyleText: (text: string) => string;
|
|
9
|
+
/** A response value as this interaction holds it: a record response is its field map. */
|
|
10
|
+
type PciResponseValue = string | string[] | RecordValue;
|
|
5
11
|
export declare class QtiPortableCustomInteraction extends Interaction {
|
|
6
12
|
#private;
|
|
7
13
|
protected _iframeLoaded: boolean;
|
|
@@ -17,6 +23,7 @@ export declare class QtiPortableCustomInteraction extends Interaction {
|
|
|
17
23
|
customInteractionTypeIdentifier: string;
|
|
18
24
|
requirePathsJson: string;
|
|
19
25
|
requireShimJson: string;
|
|
26
|
+
/** A RequireJS to load in the iframe instead of the inlined copy (src/vendor/require-js.ts). */
|
|
20
27
|
requireJsUrl: string;
|
|
21
28
|
baseUrl: string;
|
|
22
29
|
useDefaultPaths: boolean;
|
|
@@ -44,10 +51,6 @@ export declare class QtiPortableCustomInteraction extends Interaction {
|
|
|
44
51
|
* Get direct child elements by tag name
|
|
45
52
|
*/
|
|
46
53
|
private getDirectChildrenByTag;
|
|
47
|
-
/**
|
|
48
|
-
* Coerce a scalar value to the expected base-type when possible
|
|
49
|
-
*/
|
|
50
|
-
private coerceBaseValue;
|
|
51
54
|
/**
|
|
52
55
|
* Normalize an input into a list form
|
|
53
56
|
*/
|
|
@@ -56,14 +59,6 @@ export declare class QtiPortableCustomInteraction extends Interaction {
|
|
|
56
59
|
* Convert a variable value to QtiVariableJSON
|
|
57
60
|
*/
|
|
58
61
|
private variableToQtiVariableJSON;
|
|
59
|
-
/**
|
|
60
|
-
* Convert a record object into QtiVariableJSON
|
|
61
|
-
*/
|
|
62
|
-
private recordToQtiVariableJSON;
|
|
63
|
-
/**
|
|
64
|
-
* Build a record entry from an arbitrary value
|
|
65
|
-
*/
|
|
66
|
-
private recordEntryFromValue;
|
|
67
62
|
/**
|
|
68
63
|
* Extract default QTI_CONTEXT values from qti-context-declaration elements
|
|
69
64
|
*/
|
|
@@ -83,7 +78,7 @@ export declare class QtiPortableCustomInteraction extends Interaction {
|
|
|
83
78
|
/**
|
|
84
79
|
* Converts response variables to QtiVariableJSON
|
|
85
80
|
*/
|
|
86
|
-
protected responseVariablesToQtiVariableJSON(input:
|
|
81
|
+
protected responseVariablesToQtiVariableJSON(input: PciResponseValue, cardinality: Cardinality, baseType: BaseType, fieldBaseTypes?: Record<string, BaseType>): QtiVariableJSON;
|
|
87
82
|
validate(): boolean;
|
|
88
83
|
set value(v: string | null);
|
|
89
84
|
get value(): string | null;
|
|
@@ -123,3 +118,4 @@ declare global {
|
|
|
123
118
|
'qti-portable-custom-interaction': QtiPortableCustomInteraction;
|
|
124
119
|
}
|
|
125
120
|
}
|
|
121
|
+
export {};
|
|
@@ -7,20 +7,31 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
import { css, html } from 'lit';
|
|
8
8
|
import { property, state } from 'lit/decorators.js';
|
|
9
9
|
import { consume } from '@lit/context';
|
|
10
|
-
import { Interaction, itemContext, qtiContext, removeDoubleSlashes, responseAttributeConverter } from '@qti-components/base';
|
|
10
|
+
import { Interaction, isRecordValue, itemContext, qtiContext, readRecordFieldValues, removeDoubleSlashes, responseAttributeConverter } from '@qti-components/base';
|
|
11
|
+
import nativeCss from '@qti-components/theme/native-css';
|
|
11
12
|
import styles from './qti-portable-custom-interaction.styles';
|
|
13
|
+
import { coerceBaseValue, recordFromQtiVariableJSON, recordResponseToQtiVariableJSON, recordToQtiVariableJSON } from './record-response';
|
|
14
|
+
import requireJsSource from './vendor/require-js';
|
|
15
|
+
/** Text for an inline `<script>`: a `</script` inside it would end the element early. */
|
|
16
|
+
export const inlineScriptText = (text) => text.replace(/<\/(script)/gi, '<\\/$1');
|
|
17
|
+
/** Text for an inline `<style>`: a `</style` inside it would end the element early. */
|
|
18
|
+
export const inlineStyleText = (text) => text.replace(/<\/(style)/gi, '<\\/$1');
|
|
12
19
|
/** Statuses in which the item is showing the correct response, so a PCI may legitimately see it. */
|
|
13
20
|
const STATUSES_SHOWING_CORRECT_RESPONSE = ['solution', 'review'];
|
|
14
21
|
/**
|
|
15
22
|
* Whether a response variable actually declares a correct response.
|
|
16
23
|
*
|
|
17
24
|
* Non-single cardinality yields `[]` for an empty `<qti-correct-response>`, which is "no correct
|
|
18
|
-
* response" and must not be reported as one.
|
|
25
|
+
* response" and must not be reported as one. A record's correct response is a field map.
|
|
19
26
|
*/
|
|
20
27
|
function hasCorrectResponseValue(value) {
|
|
21
28
|
if (value === null || value === undefined)
|
|
22
29
|
return false;
|
|
23
|
-
|
|
30
|
+
if (Array.isArray(value))
|
|
31
|
+
return value.length > 0;
|
|
32
|
+
if (isRecordValue(value))
|
|
33
|
+
return Object.keys(value).length > 0;
|
|
34
|
+
return value !== '';
|
|
24
35
|
}
|
|
25
36
|
export class QtiPortableCustomInteraction extends Interaction {
|
|
26
37
|
constructor() {
|
|
@@ -31,7 +42,8 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
31
42
|
this._iframeMessageOrigin = null;
|
|
32
43
|
this.requirePathsJson = '';
|
|
33
44
|
this.requireShimJson = '';
|
|
34
|
-
|
|
45
|
+
/** A RequireJS to load in the iframe instead of the inlined copy (src/vendor/require-js.ts). */
|
|
46
|
+
this.requireJsUrl = '';
|
|
35
47
|
this.baseUrl = '';
|
|
36
48
|
this.useDefaultPaths = false;
|
|
37
49
|
/**
|
|
@@ -132,15 +144,19 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
132
144
|
// Treat null/undefined or unconvertible responses as "cleared".
|
|
133
145
|
// The iframe side is responsible for not emitting an initial clear on load.
|
|
134
146
|
if (converted === null) {
|
|
135
|
-
const
|
|
147
|
+
const cardinality = this.responseVariable?.cardinality;
|
|
148
|
+
// A record has no empty literal: a cleared record is NULL.
|
|
149
|
+
const emptyResponse = cardinality === 'record' ? null : cardinality === 'single' ? '' : [];
|
|
136
150
|
this.response = emptyResponse;
|
|
137
151
|
this.validate();
|
|
138
152
|
this.saveResponse(emptyResponse, state);
|
|
139
153
|
break;
|
|
140
154
|
}
|
|
141
|
-
|
|
155
|
+
// A record response travels as its field map; the shared response types only say string | string[].
|
|
156
|
+
const response = converted;
|
|
157
|
+
this.response = response;
|
|
142
158
|
this.validate();
|
|
143
|
-
this.saveResponse(
|
|
159
|
+
this.saveResponse(response, state);
|
|
144
160
|
break;
|
|
145
161
|
}
|
|
146
162
|
case 'error':
|
|
@@ -271,9 +287,13 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
271
287
|
return this.#getRequireShim();
|
|
272
288
|
}
|
|
273
289
|
/**
|
|
274
|
-
* Converts QtiVariableJSON to a string
|
|
290
|
+
* Converts QtiVariableJSON to a string, a string array, or for a record its field map
|
|
275
291
|
*/
|
|
276
292
|
#convertQtiVariableJSON(input) {
|
|
293
|
+
if (input && Array.isArray(input.record)) {
|
|
294
|
+
const record = recordFromQtiVariableJSON(input.record);
|
|
295
|
+
return Object.keys(record).length ? record : null;
|
|
296
|
+
}
|
|
277
297
|
for (const topLevelKey in input) {
|
|
278
298
|
// eslint-disable-next-line no-prototype-builtins
|
|
279
299
|
if (input.hasOwnProperty(topLevelKey)) {
|
|
@@ -303,44 +323,6 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
303
323
|
const needle = tagName.toLowerCase();
|
|
304
324
|
return Array.from(this.children).filter(child => child.tagName.toLowerCase() === needle);
|
|
305
325
|
}
|
|
306
|
-
/**
|
|
307
|
-
* Coerce a scalar value to the expected base-type when possible
|
|
308
|
-
*/
|
|
309
|
-
coerceBaseValue(value, baseType) {
|
|
310
|
-
if (value === null || value === undefined)
|
|
311
|
-
return value;
|
|
312
|
-
switch (baseType) {
|
|
313
|
-
case 'integer': {
|
|
314
|
-
if (typeof value === 'number')
|
|
315
|
-
return value;
|
|
316
|
-
const parsed = parseInt(String(value), 10);
|
|
317
|
-
return Number.isNaN(parsed) ? value : parsed;
|
|
318
|
-
}
|
|
319
|
-
case 'float':
|
|
320
|
-
case 'duration': {
|
|
321
|
-
if (typeof value === 'number')
|
|
322
|
-
return value;
|
|
323
|
-
const parsed = parseFloat(String(value));
|
|
324
|
-
return Number.isNaN(parsed) ? value : parsed;
|
|
325
|
-
}
|
|
326
|
-
case 'boolean': {
|
|
327
|
-
if (typeof value === 'boolean')
|
|
328
|
-
return value;
|
|
329
|
-
if (typeof value === 'string') {
|
|
330
|
-
const lowered = value.toLowerCase();
|
|
331
|
-
if (lowered === 'true')
|
|
332
|
-
return true;
|
|
333
|
-
if (lowered === 'false')
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
return value;
|
|
337
|
-
}
|
|
338
|
-
case 'string':
|
|
339
|
-
case 'identifier':
|
|
340
|
-
default:
|
|
341
|
-
return String(value);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
326
|
/**
|
|
345
327
|
* Normalize an input into a list form
|
|
346
328
|
*/
|
|
@@ -354,63 +336,13 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
354
336
|
*/
|
|
355
337
|
variableToQtiVariableJSON(value, cardinality, baseType) {
|
|
356
338
|
if (cardinality === 'record' || baseType === 'record') {
|
|
357
|
-
return
|
|
339
|
+
return recordToQtiVariableJSON(value);
|
|
358
340
|
}
|
|
359
341
|
if (cardinality !== 'single') {
|
|
360
|
-
const listValues = this.normalizeListValue(value).map(v =>
|
|
342
|
+
const listValues = this.normalizeListValue(value).map(v => coerceBaseValue(v, baseType));
|
|
361
343
|
return { list: { [baseType]: listValues } };
|
|
362
344
|
}
|
|
363
|
-
return { base: { [baseType]:
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Convert a record object into QtiVariableJSON
|
|
367
|
-
*/
|
|
368
|
-
recordToQtiVariableJSON(value) {
|
|
369
|
-
if (!value || typeof value !== 'object') {
|
|
370
|
-
return { record: [] };
|
|
371
|
-
}
|
|
372
|
-
const recordEntries = [];
|
|
373
|
-
for (const [name, entryValue] of Object.entries(value)) {
|
|
374
|
-
recordEntries.push(this.recordEntryFromValue(name, entryValue));
|
|
375
|
-
}
|
|
376
|
-
return { record: recordEntries };
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Build a record entry from an arbitrary value
|
|
380
|
-
*/
|
|
381
|
-
recordEntryFromValue(name, entryValue) {
|
|
382
|
-
if (Array.isArray(entryValue)) {
|
|
383
|
-
const filtered = entryValue.filter(v => v !== null && v !== undefined);
|
|
384
|
-
if (filtered.length && filtered.every(v => typeof v === 'boolean')) {
|
|
385
|
-
return { name, list: { boolean: filtered } };
|
|
386
|
-
}
|
|
387
|
-
if (filtered.length && filtered.every(v => typeof v === 'number')) {
|
|
388
|
-
const numbers = filtered;
|
|
389
|
-
const isIntegers = numbers.every(n => Number.isInteger(n));
|
|
390
|
-
return { name, list: { [isIntegers ? 'integer' : 'float']: numbers } };
|
|
391
|
-
}
|
|
392
|
-
return { name, list: { string: entryValue.map(v => (v === null || v === undefined ? '' : String(v))) } };
|
|
393
|
-
}
|
|
394
|
-
if (typeof entryValue === 'boolean') {
|
|
395
|
-
return { name, base: { boolean: entryValue } };
|
|
396
|
-
}
|
|
397
|
-
if (typeof entryValue === 'number') {
|
|
398
|
-
if (Number.isInteger(entryValue))
|
|
399
|
-
return { name, base: { integer: entryValue } };
|
|
400
|
-
return { name, base: { float: entryValue } };
|
|
401
|
-
}
|
|
402
|
-
if (entryValue === null || entryValue === undefined) {
|
|
403
|
-
return { name, base: { string: '' } };
|
|
404
|
-
}
|
|
405
|
-
if (typeof entryValue === 'object') {
|
|
406
|
-
try {
|
|
407
|
-
return { name, base: { string: JSON.stringify(entryValue) } };
|
|
408
|
-
}
|
|
409
|
-
catch {
|
|
410
|
-
return { name, base: { string: String(entryValue) } };
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
return { name, base: { string: String(entryValue) } };
|
|
345
|
+
return { base: { [baseType]: coerceBaseValue(value, baseType) } };
|
|
414
346
|
}
|
|
415
347
|
/**
|
|
416
348
|
* Extract default QTI_CONTEXT values from qti-context-declaration elements
|
|
@@ -425,15 +357,10 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
425
357
|
const defaultValueElement = declaration.querySelector('qti-default-value');
|
|
426
358
|
if (!defaultValueElement)
|
|
427
359
|
return;
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
return;
|
|
433
|
-
const baseType = (valueElement.getAttribute('base-type') || 'string');
|
|
434
|
-
const textContent = valueElement.textContent?.trim() || '';
|
|
435
|
-
defaults[fieldIdentifier] = this.coerceBaseValue(textContent, baseType);
|
|
436
|
-
});
|
|
360
|
+
const { value, baseTypes } = readRecordFieldValues(defaultValueElement.querySelectorAll('qti-value'));
|
|
361
|
+
for (const [fieldIdentifier, textContent] of Object.entries(value)) {
|
|
362
|
+
defaults[fieldIdentifier] = coerceBaseValue(textContent, baseTypes[fieldIdentifier] ?? 'string');
|
|
363
|
+
}
|
|
437
364
|
});
|
|
438
365
|
return defaults;
|
|
439
366
|
}
|
|
@@ -452,7 +379,7 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
452
379
|
environmentIdentifier: merged.environmentIdentifier ?? '',
|
|
453
380
|
...merged
|
|
454
381
|
};
|
|
455
|
-
return
|
|
382
|
+
return recordToQtiVariableJSON(normalized);
|
|
456
383
|
}
|
|
457
384
|
/**
|
|
458
385
|
* Collect template variables referenced by qti-template-variable children
|
|
@@ -545,7 +472,11 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
545
472
|
/**
|
|
546
473
|
* Converts response variables to QtiVariableJSON
|
|
547
474
|
*/
|
|
548
|
-
responseVariablesToQtiVariableJSON(input, cardinality, baseType) {
|
|
475
|
+
responseVariablesToQtiVariableJSON(input, cardinality, baseType, fieldBaseTypes) {
|
|
476
|
+
if (cardinality === 'record') {
|
|
477
|
+
// A record declaration has no base-type: the fields carry their own, declared or inferred.
|
|
478
|
+
return recordResponseToQtiVariableJSON(input, fieldBaseTypes);
|
|
479
|
+
}
|
|
549
480
|
if (cardinality !== 'single') {
|
|
550
481
|
const list = { list: {} };
|
|
551
482
|
list.list[baseType] = input;
|
|
@@ -560,6 +491,9 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
560
491
|
#hasNonEmptyResponse() {
|
|
561
492
|
if (this.response === null || this.response === undefined)
|
|
562
493
|
return false;
|
|
494
|
+
if (isRecordValue(this.response)) {
|
|
495
|
+
return Object.values(this.response).some(v => v !== '' && v !== null && v !== undefined);
|
|
496
|
+
}
|
|
563
497
|
if (Array.isArray(this.response)) {
|
|
564
498
|
if (this.response.length === 0)
|
|
565
499
|
return false;
|
|
@@ -590,6 +524,8 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
590
524
|
get value() {
|
|
591
525
|
if (this.#value === null || this.#value === undefined)
|
|
592
526
|
return null;
|
|
527
|
+
if (isRecordValue(this.#value))
|
|
528
|
+
return JSON.stringify(this.#value);
|
|
593
529
|
return Array.isArray(this.#value) ? this.#value.join(',') : String(this.#value);
|
|
594
530
|
}
|
|
595
531
|
set boundTo(newValue) {
|
|
@@ -606,10 +542,11 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
606
542
|
const responseVariable = {};
|
|
607
543
|
const variable = this.context?.variables?.find(v => v.identifier === this.responseIdentifier);
|
|
608
544
|
if (variable) {
|
|
609
|
-
const cardinality =
|
|
610
|
-
const baseType =
|
|
611
|
-
const value =
|
|
612
|
-
const
|
|
545
|
+
const cardinality = variable.cardinality || 'single';
|
|
546
|
+
const baseType = variable.baseType || 'string';
|
|
547
|
+
const value = (variable.value || null);
|
|
548
|
+
const fieldBaseTypes = variable.fieldBaseTypes;
|
|
549
|
+
const responseVal = this.responseVariablesToQtiVariableJSON(value, cardinality, baseType, fieldBaseTypes);
|
|
613
550
|
responseVariable[this.responseIdentifier] = responseVal;
|
|
614
551
|
}
|
|
615
552
|
return responseVariable;
|
|
@@ -825,15 +762,15 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
825
762
|
: `${iframeBaseUrl}/`
|
|
826
763
|
: `${window.location.origin}/`;
|
|
827
764
|
const forwardConsole = this.dataset.forwardConsole === 'true';
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
const
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
765
|
+
// Everything the iframe needs is inline: RequireJS (it loads the PCI module) and the QTI 3 shared vocabulary
|
|
766
|
+
// (qti-layout-row, qti-well, …) that interaction markup may use, both from this version of qti-components. No
|
|
767
|
+
// public CDN is involved. The qti-components bundle and the full item.css are not needed to run a PCI (the markup
|
|
768
|
+
// is plain HTML); a host that wants them in the iframe anyway names its own copy with these attributes.
|
|
769
|
+
const componentsCdnUrl = this.dataset.componentsCdnUrl;
|
|
770
|
+
const componentsCssUrl = this.dataset.componentsCssUrl;
|
|
771
|
+
const requireJsTag = this.requireJsUrl
|
|
772
|
+
? `<script src="${this.requireJsUrl}"></script>`
|
|
773
|
+
: `<script>${inlineScriptText(requireJsSource)}</script>`;
|
|
837
774
|
// Extract just the font-related properties you want to copy
|
|
838
775
|
const fontStyles = `
|
|
839
776
|
font-family: ${parentStyles.getPropertyValue('font-family')};
|
|
@@ -842,15 +779,20 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
842
779
|
font-weight: ${parentStyles.getPropertyValue('font-weight')};
|
|
843
780
|
color: ${parentStyles.getPropertyValue('color')};
|
|
844
781
|
`;
|
|
782
|
+
// The language of the interaction (the item's xml:lang, or the page's lang), so a PCI that reads
|
|
783
|
+
// `lang` in its iframe shows its own texts in the item's language.
|
|
784
|
+
const langHost = this.closest('[lang], [xml\\:lang]');
|
|
785
|
+
const iframeLang = [langHost?.getAttribute('lang'), langHost?.getAttribute('xml:lang'), document.documentElement.lang]
|
|
786
|
+
.map(l => (l ?? '').trim())
|
|
787
|
+
.find(l => /^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$/.test(l)) ?? 'en';
|
|
845
788
|
return /* html */ `<!DOCTYPE html>
|
|
846
|
-
<html lang="
|
|
789
|
+
<html lang="${iframeLang}">
|
|
847
790
|
<head>
|
|
848
791
|
<meta charset="utf-8" />
|
|
849
792
|
<title>QTI PCI Container</title>
|
|
850
793
|
<base href="${iframeBaseHref}" />
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
</script>
|
|
794
|
+
${componentsCdnUrl ? `<script type="module">import '${componentsCdnUrl}';</script>` : ''}
|
|
795
|
+
<style>${inlineStyleText(nativeCss)}</style>
|
|
854
796
|
<style>
|
|
855
797
|
body, html {
|
|
856
798
|
margin: 0;
|
|
@@ -874,8 +816,8 @@ export class QtiPortableCustomInteraction extends Interaction {
|
|
|
874
816
|
min-height: 50px;
|
|
875
817
|
}
|
|
876
818
|
</style>
|
|
877
|
-
|
|
878
|
-
|
|
819
|
+
${componentsCssUrl ? `<link href="${componentsCssUrl}" rel="stylesheet" />` : ''}
|
|
820
|
+
${requireJsTag}
|
|
879
821
|
<script>
|
|
880
822
|
const forwardConsole = ${forwardConsole ? 'true' : 'false'};
|
|
881
823
|
if (forwardConsole) {
|