@qti-components/test 2.0.0-next.18 → 2.0.0-next.19
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/mixins/test-state.mixin.js +2 -24
- package/dist/mixins/test-state.mixin.js.map +1 -1
- package/dist/state-from-contexts.d.ts +46 -0
- package/dist/state-from-contexts.js +56 -0
- package/dist/state-from-contexts.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import './register.js';
|
|
|
2
2
|
export * from './components/index.js';
|
|
3
3
|
export * from './plugins/index.js';
|
|
4
4
|
export * from './plugin-state.js';
|
|
5
|
+
export * from './state-from-contexts.js';
|
|
5
6
|
export * from './types/iqti-test.js';
|
|
6
7
|
export * from './types/qti-test-state.js';
|
|
7
8
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAC;AAEvB,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC","sourcesContent":["import './register.js';\n\nexport * from './components/index.js';\nexport * from './plugins/index.js';\nexport * from './plugin-state.js';\nexport * from './types/iqti-test.js';\nexport * from './types/qti-test-state.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAC;AAEvB,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC","sourcesContent":["import './register.js';\n\nexport * from './components/index.js';\nexport * from './plugins/index.js';\nexport * from './plugin-state.js';\nexport * from './state-from-contexts.js';\nexport * from './types/iqti-test.js';\nexport * from './types/qti-test-state.js';\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { QTI_TEST_STATE_VERSION } from '../types/qti-test-state';
|
|
2
2
|
import { dispatchPublic, listenPublic } from '../public-events';
|
|
3
|
+
import { stateFromContexts } from '../state-from-contexts';
|
|
3
4
|
const copy = (value) => Array.isArray(value) ? [...value] : (value ?? null);
|
|
4
5
|
const isQtiTestState = (value) => {
|
|
5
6
|
const state = value;
|
|
@@ -9,29 +10,6 @@ const isQtiTestState = (value) => {
|
|
|
9
10
|
typeof state.session === 'object' &&
|
|
10
11
|
state.session !== null);
|
|
11
12
|
};
|
|
12
|
-
const toState = (test, session) => ({
|
|
13
|
-
version: QTI_TEST_STATE_VERSION,
|
|
14
|
-
test: {
|
|
15
|
-
items: (test?.items ?? [])
|
|
16
|
-
.filter(item => item.identifier)
|
|
17
|
-
.map(item => ({
|
|
18
|
-
identifier: item.identifier,
|
|
19
|
-
variables: (item.variables ?? []).map(v => ({
|
|
20
|
-
identifier: v.identifier,
|
|
21
|
-
...(v.type ? { type: v.type } : {}),
|
|
22
|
-
value: copy(v.value)
|
|
23
|
-
})),
|
|
24
|
-
...(item.state ? { state: { ...item.state } } : {})
|
|
25
|
-
})),
|
|
26
|
-
outcomes: (test?.testOutcomeVariables ?? []).map(v => ({ identifier: v.identifier, value: copy(v.value) }))
|
|
27
|
-
},
|
|
28
|
-
session: {
|
|
29
|
-
...(session?.plugins && Object.keys(session.plugins).length > 0 ? { plugins: { ...session.plugins } } : {}),
|
|
30
|
-
...(session?.navPartId !== undefined ? { navPartId: session.navPartId } : {}),
|
|
31
|
-
...(session?.navSectionId !== undefined ? { navSectionId: session.navSectionId } : {}),
|
|
32
|
-
...(session?.navItemRefId !== undefined ? { navItemRefId: session.navItemRefId } : {})
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
13
|
/**
|
|
36
14
|
* Persist and resume a test session through one value, `state`, and one event,
|
|
37
15
|
* `qc-test-state-changed`.
|
|
@@ -84,7 +62,7 @@ export const TestStateMixin = (superClass) => {
|
|
|
84
62
|
this.sessionContext = { ...this.sessionContext, plugins: next };
|
|
85
63
|
}
|
|
86
64
|
get state() {
|
|
87
|
-
return
|
|
65
|
+
return stateFromContexts(this.testContext, this.sessionContext);
|
|
88
66
|
}
|
|
89
67
|
set state(value) {
|
|
90
68
|
if (!isQtiTestState(value)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-state.mixin.js","sourceRoot":"","sources":["../../src/mixins/test-state.mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AA0BhE,MAAM,IAAI,GAAG,CAAC,KAAkC,EAAS,EAAE,CACzD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAE,KAAmC,IAAI,IAAI,CAAC,CAAC;AAErF,MAAM,cAAc,GAAG,CAAC,KAAc,EAAyB,EAAE;IAC/D,MAAM,KAAK,GAAG,KAA4B,CAAC;IAC3C,OAAO,CACL,KAAK,EAAE,OAAO,KAAK,sBAAsB;QACzC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC;QAChC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC;QACnC,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;QACjC,KAAK,CAAC,OAAO,KAAK,IAAI,CACvB,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CAAC,IAA2B,EAAE,OAAiC,EAAgB,E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CAAC,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,GAAG,QAAQ,gBAAgB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;YAC5F,OAAO;gBACL,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACvF,GAAG,CAAC,MAAM,CAAC,wBAAwB,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACzG,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,OAAO,CAAC,YAAY,CAAC;oBACzE,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE;oBACxC,CAAC,CAAC,EAAE,CAAC;aACR,CAAC;QACJ,CAAC;QAED,kBAAkB,CAAC,IAAwB,EAAE,EAAU;YACrD,cAAc,CAAC,IAAI,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACzD,CAAC;KACF;IAED,OAAO,cAAkD,CAAC;AAC5D,CAAC,CAAC","sourcesContent":["import { QTI_TEST_STATE_VERSION } from '../types/qti-test-state';\nimport { dispatchPublic, listenPublic } from '../public-events';\n\nimport type { PropertyValues } from 'lit';\nimport type { SessionContext, TestContext } from '@qti-components/base';\nimport type { QtiTestState, QtiTestStateSession } from '../types/qti-test-state';\nimport type { TestBaseInterface } from './test-base';\n\ntype Constructor<T = {}> = abstract new (...args: any[]) => T;\ntype Value = string | string[] | null;\n\nexport interface ITestStateMixin {\n /**\n * The candidate's persistable state: what they answered and where they are.\n *\n * Assign a stored state to resume. It may be assigned at any time: before the test document\n * has loaded it is held and applied as soon as the test connects — before the first item\n * loads, so items and the initial navigation start from it — and after that it is applied\n * immediately and the test navigates to the restored position.\n *\n * Items are matched by item-ref identifier; items the state does not mention start fresh, and\n * stored items, test outcomes or a position the test no longer has are ignored. Restoring is a\n * resume, so linear-mode navigation restrictions do not apply.\n */\n state: QtiTestState;\n}\n\nconst copy = (value: Readonly<Value> | undefined): Value =>\n Array.isArray(value) ? [...value] : ((value as string | null | undefined) ?? null);\n\nconst isQtiTestState = (value: unknown): value is QtiTestState => {\n const state = value as QtiTestState | null;\n return (\n state?.version === QTI_TEST_STATE_VERSION &&\n Array.isArray(state.test?.items) &&\n Array.isArray(state.test?.outcomes) &&\n typeof state.session === 'object' &&\n state.session !== null\n );\n};\n\nconst toState = (test: Readonly<TestContext>, session: Readonly<SessionContext>): QtiTestState => ({\n version: QTI_TEST_STATE_VERSION,\n test: {\n items: (test?.items ?? [])\n .filter(item => item.identifier)\n .map(item => ({\n identifier: item.identifier,\n variables: (item.variables ?? []).map(v => ({\n identifier: v.identifier,\n ...(v.type ? { type: v.type } : {}),\n value: copy(v.value)\n })),\n ...(item.state ? { state: { ...item.state } } : {})\n })),\n outcomes: (test?.testOutcomeVariables ?? []).map(v => ({ identifier: v.identifier, value: copy(v.value) }))\n },\n session: {\n ...(session?.plugins && Object.keys(session.plugins).length > 0 ? { plugins: { ...session.plugins } } : {}),\n ...(session?.navPartId !== undefined ? { navPartId: session.navPartId } : {}),\n ...(session?.navSectionId !== undefined ? { navSectionId: session.navSectionId } : {}),\n ...(session?.navItemRefId !== undefined ? { navItemRefId: session.navItemRefId } : {})\n }\n});\n\n/**\n * Persist and resume a test session through one value, `state`, and one event,\n * `qc-test-state-changed`.\n *\n * Sits directly on top of `TestBaseMixin`, so its `qc-test-loaded` listener runs\n * after the base has built `testContext.items` from the item-refs, and before the navigation\n * mixin's listener picks the first item to load.\n */\nexport const TestStateMixin = <T extends Constructor<TestBaseInterface>>(superClass: T) => {\n abstract class TestStateClass extends superClass implements ITestStateMixin {\n /** A state assigned before the test connected, applied when it does. */\n #pending: QtiTestState | null = null;\n\n /**\n * False while a test document is loading. Loading resets both contexts and rebuilds them, so\n * the intermediate states are empty — emitting one would invite a host to save it over the\n * real one.\n */\n #ready = false;\n\n #lastEmitted: string | null = null;\n\n constructor(...args: any[]) {\n super(...args);\n this.addEventListener('qti-testdoc-loaded', () => {\n this.#ready = false;\n this.#lastEmitted = null;\n });\n listenPublic(this, 'qc-test-update-plugin-state', ({ detail }) => this.#updatePluginState(detail));\n listenPublic(this, 'qc-test-loaded', () => {\n if (this.#pending) {\n this.#apply(this.#pending);\n this.#pending = null;\n }\n this.#ready = true;\n });\n }\n\n /** One plugin's entry, as JSON: the test stores it without reading it. `null` removes it. */\n #updatePluginState({ plugin, value }: { plugin: string; value: unknown }): void {\n if (typeof plugin !== 'string' || plugin === '') return;\n const { [plugin]: _removed, ...others } = this.sessionContext.plugins ?? {};\n let next: Record<string, unknown> = others;\n if (value !== null && value !== undefined) {\n try {\n next = { ...others, [plugin]: JSON.parse(JSON.stringify(value)) };\n } catch {\n console.warn(`[qti-test] Ignoring plugin state for \"${plugin}\": it is not JSON.`);\n return;\n }\n }\n this.sessionContext = { ...this.sessionContext, plugins: next };\n }\n\n get state(): QtiTestState {\n return toState(this.testContext, this.sessionContext);\n }\n\n set state(value: QtiTestState) {\n if (!isQtiTestState(value)) {\n console.warn(`[qti-test] Ignoring a state that is not a version ${QTI_TEST_STATE_VERSION} QtiTestState.`);\n return;\n }\n if (!this.#ready) {\n this.#pending = value;\n return;\n }\n\n const { navItemRefId, navSectionId } = this.sessionContext;\n this.#apply(value);\n\n const restored = this.sessionContext;\n if (restored.navItemRefId && restored.navItemRefId !== navItemRefId) {\n this.#requestNavigation('item', restored.navItemRefId);\n } else if (!restored.navItemRefId && restored.navSectionId && restored.navSectionId !== navSectionId) {\n this.#requestNavigation('section', restored.navSectionId);\n }\n }\n\n protected override updated(changed: PropertyValues): void {\n super.updated(changed);\n if (!this.#ready || !(changed.has('testContext') || changed.has('sessionContext'))) return;\n\n const state = this.state;\n const serialized = JSON.stringify(state);\n if (serialized === this.#lastEmitted) return;\n this.#lastEmitted = serialized;\n dispatchPublic(this, 'qc-test-state-changed', state);\n }\n\n #apply(state: QtiTestState): void {\n // Before the items load this only fills the test context; items already on screen are\n // updated too.\n for (const saved of state.test.items) this.updateItemVariables(saved.identifier, saved.variables, saved.state);\n\n const savedOutcomes = new Map(state.test.outcomes.map(outcome => [outcome.identifier, outcome.value]));\n this.testContext = {\n ...this.testContext,\n testOutcomeVariables: this.testContext.testOutcomeVariables?.map(v =>\n savedOutcomes.has(v.identifier) ? { ...v, value: copy(savedOutcomes.get(v.identifier)) } : v\n )\n };\n\n // Written before any navigation request, so the linear-mode restriction — which compares\n // against the current position — sees the restored item as current and lets it load.\n // Plugins are replaced, not merged: a state without them clears what an earlier one left.\n this.sessionContext = {\n ...this.sessionContext,\n ...this.#validSession(state.session),\n plugins: state.session.plugins && typeof state.session.plugins === 'object' ? { ...state.session.plugins } : {}\n };\n }\n\n /** A stored position may point at an item or section a revised test no longer has. */\n #validSession(session: QtiTestStateSession): QtiTestStateSession {\n const exists = (selector: string, id: string | null | undefined) =>\n !!id && !!this._testElement?.querySelector(`${selector}[identifier=\"${CSS.escape(id)}\"]`);\n return {\n ...(exists('qti-test-part', session.navPartId) ? { navPartId: session.navPartId } : {}),\n ...(exists('qti-assessment-section', session.navSectionId) ? { navSectionId: session.navSectionId } : {}),\n ...(this.testContext.items.some(i => i.identifier === session.navItemRefId)\n ? { navItemRefId: session.navItemRefId }\n : {})\n };\n }\n\n #requestNavigation(type: 'item' | 'section', id: string): void {\n dispatchPublic(this, 'qc-test-navigate', { type, id });\n }\n }\n\n return TestStateClass as Constructor<ITestStateMixin> & T;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"test-state.mixin.js","sourceRoot":"","sources":["../../src/mixins/test-state.mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB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K,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE;aAChH,CAAC;QACJ,CAAC;QAED,sFAAsF;QACtF,aAAa,CAAC,OAA4B;YACxC,MAAM,MAAM,GAAG,CAAC,QAAgB,EAAE,EAA6B,EAAE,EAAE,CACjE,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,GAAG,QAAQ,gBAAgB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;YAC5F,OAAO;gBACL,GAAG,CAAC,MAAM,CAAC,eAAe,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACvF,GAAG,CAAC,MAAM,CAAC,wBAAwB,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACzG,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,OAAO,CAAC,YAAY,CAAC;oBACzE,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE;oBACxC,CAAC,CAAC,EAAE,CAAC;aACR,CAAC;QACJ,CAAC;QAED,kBAAkB,CAAC,IAAwB,EAAE,EAAU;YACrD,cAAc,CAAC,IAAI,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;QACzD,CAAC;KACF;IAED,OAAO,cAAkD,CAAC;AAC5D,CAAC,CAAC","sourcesContent":["import { QTI_TEST_STATE_VERSION } from '../types/qti-test-state';\nimport { dispatchPublic, listenPublic } from '../public-events';\nimport { stateFromContexts } from '../state-from-contexts';\n\nimport type { PropertyValues } from 'lit';\nimport type { QtiTestState, QtiTestStateSession } from '../types/qti-test-state';\nimport type { TestBaseInterface } from './test-base';\n\ntype Constructor<T = {}> = abstract new (...args: any[]) => T;\ntype Value = string | string[] | null;\n\nexport interface ITestStateMixin {\n /**\n * The candidate's persistable state: what they answered and where they are.\n *\n * Assign a stored state to resume. It may be assigned at any time: before the test document\n * has loaded it is held and applied as soon as the test connects — before the first item\n * loads, so items and the initial navigation start from it — and after that it is applied\n * immediately and the test navigates to the restored position.\n *\n * Items are matched by item-ref identifier; items the state does not mention start fresh, and\n * stored items, test outcomes or a position the test no longer has are ignored. Restoring is a\n * resume, so linear-mode navigation restrictions do not apply.\n */\n state: QtiTestState;\n}\n\nconst copy = (value: Readonly<Value> | undefined): Value =>\n Array.isArray(value) ? [...value] : ((value as string | null | undefined) ?? null);\n\nconst isQtiTestState = (value: unknown): value is QtiTestState => {\n const state = value as QtiTestState | null;\n return (\n state?.version === QTI_TEST_STATE_VERSION &&\n Array.isArray(state.test?.items) &&\n Array.isArray(state.test?.outcomes) &&\n typeof state.session === 'object' &&\n state.session !== null\n );\n};\n\n/**\n * Persist and resume a test session through one value, `state`, and one event,\n * `qc-test-state-changed`.\n *\n * Sits directly on top of `TestBaseMixin`, so its `qc-test-loaded` listener runs\n * after the base has built `testContext.items` from the item-refs, and before the navigation\n * mixin's listener picks the first item to load.\n */\nexport const TestStateMixin = <T extends Constructor<TestBaseInterface>>(superClass: T) => {\n abstract class TestStateClass extends superClass implements ITestStateMixin {\n /** A state assigned before the test connected, applied when it does. */\n #pending: QtiTestState | null = null;\n\n /**\n * False while a test document is loading. Loading resets both contexts and rebuilds them, so\n * the intermediate states are empty — emitting one would invite a host to save it over the\n * real one.\n */\n #ready = false;\n\n #lastEmitted: string | null = null;\n\n constructor(...args: any[]) {\n super(...args);\n this.addEventListener('qti-testdoc-loaded', () => {\n this.#ready = false;\n this.#lastEmitted = null;\n });\n listenPublic(this, 'qc-test-update-plugin-state', ({ detail }) => this.#updatePluginState(detail));\n listenPublic(this, 'qc-test-loaded', () => {\n if (this.#pending) {\n this.#apply(this.#pending);\n this.#pending = null;\n }\n this.#ready = true;\n });\n }\n\n /** One plugin's entry, as JSON: the test stores it without reading it. `null` removes it. */\n #updatePluginState({ plugin, value }: { plugin: string; value: unknown }): void {\n if (typeof plugin !== 'string' || plugin === '') return;\n const { [plugin]: _removed, ...others } = this.sessionContext.plugins ?? {};\n let next: Record<string, unknown> = others;\n if (value !== null && value !== undefined) {\n try {\n next = { ...others, [plugin]: JSON.parse(JSON.stringify(value)) };\n } catch {\n console.warn(`[qti-test] Ignoring plugin state for \"${plugin}\": it is not JSON.`);\n return;\n }\n }\n this.sessionContext = { ...this.sessionContext, plugins: next };\n }\n\n get state(): QtiTestState {\n return stateFromContexts(this.testContext, this.sessionContext);\n }\n\n set state(value: QtiTestState) {\n if (!isQtiTestState(value)) {\n console.warn(`[qti-test] Ignoring a state that is not a version ${QTI_TEST_STATE_VERSION} QtiTestState.`);\n return;\n }\n if (!this.#ready) {\n this.#pending = value;\n return;\n }\n\n const { navItemRefId, navSectionId } = this.sessionContext;\n this.#apply(value);\n\n const restored = this.sessionContext;\n if (restored.navItemRefId && restored.navItemRefId !== navItemRefId) {\n this.#requestNavigation('item', restored.navItemRefId);\n } else if (!restored.navItemRefId && restored.navSectionId && restored.navSectionId !== navSectionId) {\n this.#requestNavigation('section', restored.navSectionId);\n }\n }\n\n protected override updated(changed: PropertyValues): void {\n super.updated(changed);\n if (!this.#ready || !(changed.has('testContext') || changed.has('sessionContext'))) return;\n\n const state = this.state;\n const serialized = JSON.stringify(state);\n if (serialized === this.#lastEmitted) return;\n this.#lastEmitted = serialized;\n dispatchPublic(this, 'qc-test-state-changed', state);\n }\n\n #apply(state: QtiTestState): void {\n // Before the items load this only fills the test context; items already on screen are\n // updated too.\n for (const saved of state.test.items) this.updateItemVariables(saved.identifier, saved.variables, saved.state);\n\n const savedOutcomes = new Map(state.test.outcomes.map(outcome => [outcome.identifier, outcome.value]));\n this.testContext = {\n ...this.testContext,\n testOutcomeVariables: this.testContext.testOutcomeVariables?.map(v =>\n savedOutcomes.has(v.identifier) ? { ...v, value: copy(savedOutcomes.get(v.identifier)) } : v\n )\n };\n\n // Written before any navigation request, so the linear-mode restriction — which compares\n // against the current position — sees the restored item as current and lets it load.\n // Plugins are replaced, not merged: a state without them clears what an earlier one left.\n this.sessionContext = {\n ...this.sessionContext,\n ...this.#validSession(state.session),\n plugins: state.session.plugins && typeof state.session.plugins === 'object' ? { ...state.session.plugins } : {}\n };\n }\n\n /** A stored position may point at an item or section a revised test no longer has. */\n #validSession(session: QtiTestStateSession): QtiTestStateSession {\n const exists = (selector: string, id: string | null | undefined) =>\n !!id && !!this._testElement?.querySelector(`${selector}[identifier=\"${CSS.escape(id)}\"]`);\n return {\n ...(exists('qti-test-part', session.navPartId) ? { navPartId: session.navPartId } : {}),\n ...(exists('qti-assessment-section', session.navSectionId) ? { navSectionId: session.navSectionId } : {}),\n ...(this.testContext.items.some(i => i.identifier === session.navItemRefId)\n ? { navItemRefId: session.navItemRefId }\n : {})\n };\n }\n\n #requestNavigation(type: 'item' | 'section', id: string): void {\n dispatchPublic(this, 'qc-test-navigate', { type, id });\n }\n }\n\n return TestStateClass as Constructor<ITestStateMixin> & T;\n};\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { QtiTestState } from './types/qti-test-state.js';
|
|
2
|
+
/** A value as a 9.x host may have stored it: QTI values are strings, but JSON kept numbers as numbers. */
|
|
3
|
+
type StoredValue = string | number | boolean | null | undefined | readonly (string | number | boolean)[];
|
|
4
|
+
/** A variable as stored: only `identifier`, `value` and `type` are read; declaration metadata is ignored. */
|
|
5
|
+
export interface StoredVariable {
|
|
6
|
+
identifier: string;
|
|
7
|
+
value?: StoredValue;
|
|
8
|
+
type?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A `testContext` as a 9.x host stored it (`qtiTest.testContext` or the `qti-test-context-updated`
|
|
12
|
+
* payload), plain JSON. Fields this does not name, such as an item's `href` or a host's own
|
|
13
|
+
* additions, are ignored.
|
|
14
|
+
*/
|
|
15
|
+
export interface StoredTestContext {
|
|
16
|
+
items?: readonly {
|
|
17
|
+
identifier?: string;
|
|
18
|
+
variables?: readonly StoredVariable[];
|
|
19
|
+
state?: Readonly<Record<string, string | null>>;
|
|
20
|
+
}[];
|
|
21
|
+
testOutcomeVariables?: readonly StoredVariable[];
|
|
22
|
+
}
|
|
23
|
+
/** A `sessionContext` as a 9.x host stored it: the position and the plugin entries. */
|
|
24
|
+
export interface StoredSessionContext {
|
|
25
|
+
navPartId?: string | null;
|
|
26
|
+
navSectionId?: string | null;
|
|
27
|
+
navItemRefId?: string | null;
|
|
28
|
+
plugins?: Readonly<Record<string, unknown>>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Turns a stored `testContext` and `sessionContext` into a `QtiTestState`, so a host that stored the
|
|
32
|
+
* contexts in 9.x can resume with `qtiTest.state = stateFromContexts(testContext, sessionContext)`.
|
|
33
|
+
* `qtiTest.state` reads the same way from the live contexts.
|
|
34
|
+
*
|
|
35
|
+
* It keeps the values only: each item's variables (identifier, value and `type` when it is one QTI
|
|
36
|
+
* knows) and per-interaction state, the test outcomes, the position and the plugin entries. Values
|
|
37
|
+
* are written as QTI's strings, so a number a host stored as a JSON number (`SCORE: 0`) becomes
|
|
38
|
+
* `'0'`. Declaration metadata (`baseType`, `cardinality`, `correctResponse`, …) is left out, as in
|
|
39
|
+
* every `QtiTestState`. Items without an identifier are skipped.
|
|
40
|
+
*
|
|
41
|
+
* @param testContext The stored test context, plain JSON.
|
|
42
|
+
* @param sessionContext The stored session context, if the host kept one. A host that stored only the
|
|
43
|
+
* item it was on passes `{ navItemRefId }`.
|
|
44
|
+
*/
|
|
45
|
+
export declare const stateFromContexts: (testContext: Readonly<StoredTestContext> | null | undefined, sessionContext?: Readonly<StoredSessionContext> | null) => QtiTestState;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { QTI_TEST_STATE_VERSION } from './types/qti-test-state.js';
|
|
2
|
+
const VARIABLE_TYPES = ['response', 'outcome', 'template', 'context'];
|
|
3
|
+
const valueOf = (value) => {
|
|
4
|
+
if (value === null || value === undefined)
|
|
5
|
+
return null;
|
|
6
|
+
if (Array.isArray(value))
|
|
7
|
+
return value.map(String);
|
|
8
|
+
return String(value);
|
|
9
|
+
};
|
|
10
|
+
const variable = ({ identifier, type, value }) => ({
|
|
11
|
+
identifier,
|
|
12
|
+
...(VARIABLE_TYPES.includes(type)
|
|
13
|
+
? { type: type }
|
|
14
|
+
: {}),
|
|
15
|
+
value: valueOf(value)
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Turns a stored `testContext` and `sessionContext` into a `QtiTestState`, so a host that stored the
|
|
19
|
+
* contexts in 9.x can resume with `qtiTest.state = stateFromContexts(testContext, sessionContext)`.
|
|
20
|
+
* `qtiTest.state` reads the same way from the live contexts.
|
|
21
|
+
*
|
|
22
|
+
* It keeps the values only: each item's variables (identifier, value and `type` when it is one QTI
|
|
23
|
+
* knows) and per-interaction state, the test outcomes, the position and the plugin entries. Values
|
|
24
|
+
* are written as QTI's strings, so a number a host stored as a JSON number (`SCORE: 0`) becomes
|
|
25
|
+
* `'0'`. Declaration metadata (`baseType`, `cardinality`, `correctResponse`, …) is left out, as in
|
|
26
|
+
* every `QtiTestState`. Items without an identifier are skipped.
|
|
27
|
+
*
|
|
28
|
+
* @param testContext The stored test context, plain JSON.
|
|
29
|
+
* @param sessionContext The stored session context, if the host kept one. A host that stored only the
|
|
30
|
+
* item it was on passes `{ navItemRefId }`.
|
|
31
|
+
*/
|
|
32
|
+
export const stateFromContexts = (testContext, sessionContext) => ({
|
|
33
|
+
version: QTI_TEST_STATE_VERSION,
|
|
34
|
+
test: {
|
|
35
|
+
items: (testContext?.items ?? [])
|
|
36
|
+
.filter(item => item?.identifier)
|
|
37
|
+
.map(item => ({
|
|
38
|
+
identifier: item.identifier,
|
|
39
|
+
variables: (item.variables ?? []).map(variable),
|
|
40
|
+
...(item.state ? { state: { ...item.state } } : {})
|
|
41
|
+
})),
|
|
42
|
+
outcomes: (testContext?.testOutcomeVariables ?? []).map(v => ({
|
|
43
|
+
identifier: v.identifier,
|
|
44
|
+
value: valueOf(v.value)
|
|
45
|
+
}))
|
|
46
|
+
},
|
|
47
|
+
session: {
|
|
48
|
+
...(sessionContext?.plugins && Object.keys(sessionContext.plugins).length > 0
|
|
49
|
+
? { plugins: { ...sessionContext.plugins } }
|
|
50
|
+
: {}),
|
|
51
|
+
...(sessionContext?.navPartId !== undefined ? { navPartId: sessionContext.navPartId } : {}),
|
|
52
|
+
...(sessionContext?.navSectionId !== undefined ? { navSectionId: sessionContext.navSectionId } : {}),
|
|
53
|
+
...(sessionContext?.navItemRefId !== undefined ? { navItemRefId: sessionContext.navItemRefId } : {})
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=state-from-contexts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-from-contexts.js","sourceRoot":"","sources":["../src/state-from-contexts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAoCnE,MAAM,cAAc,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,CAAU,CAAC;AAE/E,MAAM,OAAO,GAAG,CAAC,KAAkB,EAA8B,EAAE;IACjE,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACnD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC;AAEF,MAAM,QAAQ,GAA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C,OAAO,CAAC,CAAC,MAAM,GAAG,CAAC;YAC3E,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,GAAG,cAAc,CAAC,OAAO,EAAE,EAAE;YAC5C,CAAC,CAAC,EAAE,CAAC;QACP,GAAG,CAAC,cAAc,EAAE,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,cAAc,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3F,GAAG,CAAC,cAAc,EAAE,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACpG,GAAG,CAAC,cAAc,EAAE,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACrG;CACF,CAAC,CAAC","sourcesContent":["import { QTI_TEST_STATE_VERSION } from './types/qti-test-state.js';\n\nimport type { QtiTestState, QtiTestStateItem, QtiTestStateValue } from './types/qti-test-state.js';\n\n/** A value as a 9.x host may have stored it: QTI values are strings, but JSON kept numbers as numbers. */\ntype StoredValue = string | number | boolean | null | undefined | readonly (string | number | boolean)[];\n\n/** A variable as stored: only `identifier`, `value` and `type` are read; declaration metadata is ignored. */\nexport interface StoredVariable {\n identifier: string;\n value?: StoredValue;\n type?: string;\n}\n\n/**\n * A `testContext` as a 9.x host stored it (`qtiTest.testContext` or the `qti-test-context-updated`\n * payload), plain JSON. Fields this does not name, such as an item's `href` or a host's own\n * additions, are ignored.\n */\nexport interface StoredTestContext {\n items?: readonly {\n identifier?: string;\n variables?: readonly StoredVariable[];\n state?: Readonly<Record<string, string | null>>;\n }[];\n testOutcomeVariables?: readonly StoredVariable[];\n}\n\n/** A `sessionContext` as a 9.x host stored it: the position and the plugin entries. */\nexport interface StoredSessionContext {\n navPartId?: string | null;\n navSectionId?: string | null;\n navItemRefId?: string | null;\n plugins?: Readonly<Record<string, unknown>>;\n}\n\nconst VARIABLE_TYPES = ['response', 'outcome', 'template', 'context'] as const;\n\nconst valueOf = (value: StoredValue): QtiTestStateValue['value'] => {\n if (value === null || value === undefined) return null;\n if (Array.isArray(value)) return value.map(String);\n return String(value);\n};\n\nconst variable = ({ identifier, type, value }: StoredVariable): QtiTestStateItem['variables'][number] => ({\n identifier,\n ...(VARIABLE_TYPES.includes(type as (typeof VARIABLE_TYPES)[number])\n ? { type: type as (typeof VARIABLE_TYPES)[number] }\n : {}),\n value: valueOf(value)\n});\n\n/**\n * Turns a stored `testContext` and `sessionContext` into a `QtiTestState`, so a host that stored the\n * contexts in 9.x can resume with `qtiTest.state = stateFromContexts(testContext, sessionContext)`.\n * `qtiTest.state` reads the same way from the live contexts.\n *\n * It keeps the values only: each item's variables (identifier, value and `type` when it is one QTI\n * knows) and per-interaction state, the test outcomes, the position and the plugin entries. Values\n * are written as QTI's strings, so a number a host stored as a JSON number (`SCORE: 0`) becomes\n * `'0'`. Declaration metadata (`baseType`, `cardinality`, `correctResponse`, …) is left out, as in\n * every `QtiTestState`. Items without an identifier are skipped.\n *\n * @param testContext The stored test context, plain JSON.\n * @param sessionContext The stored session context, if the host kept one. A host that stored only the\n * item it was on passes `{ navItemRefId }`.\n */\nexport const stateFromContexts = (\n testContext: Readonly<StoredTestContext> | null | undefined,\n sessionContext?: Readonly<StoredSessionContext> | null\n): QtiTestState => ({\n version: QTI_TEST_STATE_VERSION,\n test: {\n items: (testContext?.items ?? [])\n .filter(item => item?.identifier)\n .map(item => ({\n identifier: item.identifier as string,\n variables: (item.variables ?? []).map(variable),\n ...(item.state ? { state: { ...item.state } } : {})\n })),\n outcomes: (testContext?.testOutcomeVariables ?? []).map(v => ({\n identifier: v.identifier,\n value: valueOf(v.value)\n }))\n },\n session: {\n ...(sessionContext?.plugins && Object.keys(sessionContext.plugins).length > 0\n ? { plugins: { ...sessionContext.plugins } }\n : {}),\n ...(sessionContext?.navPartId !== undefined ? { navPartId: sessionContext.navPartId } : {}),\n ...(sessionContext?.navSectionId !== undefined ? { navSectionId: sessionContext.navSectionId } : {}),\n ...(sessionContext?.navItemRefId !== undefined ? { navItemRefId: sessionContext.navItemRefId } : {})\n }\n});\n"]}
|