@astrolabe/player
The <astl-player> element, element base classes, the attempt controller, httpChecker and accessibility helpers. Contains no answer logic.
Classes
Section titled “Classes”abstract AstlElement
Section titled “abstract AstlElement”Extends
Section titled “Extends”LitElement
Extended by
Section titled “Extended by”Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AstlElement(): AstlElement;Returns
Section titled “Returns”Overrides
Section titled “Overrides”LitElement.constructorProperties
Section titled “Properties”| Property | Modifier | Type | Default value | Description |
|---|---|---|---|---|
disabled |
public |
boolean |
undefined |
- |
feedback |
public |
| ElementFeedback | null |
undefined |
Verdicts to show; null clears them. Editing a slot clears its own verdict and the group verdict. |
manifest |
static |
| ComponentManifest | null |
null |
The element type's manifest: drives the typed attribute getters and the default message namespace. |
messages |
static |
readonly MessageCatalog[] |
[] |
Catalogs of the manifest's namespace, used until a player attaches its own context. |
solution |
public |
Readonly<Record<string, string>> | null |
undefined |
Correct answers to show next to the student's; null hides them. |
Accessors
Section titled “Accessors”context
Section titled “context”Get Signature
Section titled “Get Signature”get context(): ElementContext;The player's context, or the default English one.
Returns
Section titled “Returns”slotIds
Section titled “slotIds”Get Signature
Section titled “Get Signature”get abstract slotIds(): readonly string[];Slot ids this element answers, in reading order. Stable after the element connects.
Returns
Section titled “Returns”readonly string[]
Slot ids this element answers, in reading order. Stable after the element connects.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”attach()
Section titled “attach()”attach(context): void;Called by the player once, before the first render. Elements must work with a default context too.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
context |
ElementContext |
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”getAnswers()
Section titled “getAnswers()”abstract getAnswers(): Answers;Answered slots only.
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”setAnswers()
Section titled “setAnswers()”abstract setAnswers(answers): void;Unknown ids are ignored; '' clears a slot. Does not fire a change event.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
answers |
Answers |
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”units()
Section titled “units()”abstract units(): Units;Progress units, e.g. 3 of 5 cards placed.
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”AstlPlayer
Section titled “AstlPlayer”Extends
Section titled “Extends”LitElement
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AstlPlayer(): AstlPlayer;Returns
Section titled “Returns”Overrides
Section titled “Overrides”LitElement.constructorProperties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
checker |
Checker | null |
Grades checks and submits. Default: an HTTP checker for endpoint, if set. |
endpoint |
string | null |
Grading endpoint (protocol 1) of this attempt. |
integrity |
string | null |
Lowercase hex SHA-256 the fetched src must match. |
locale |
string | null |
UI locale; default: the lang attribute, else the browser languages. |
persist |
boolean |
Keep snapshots in localStorage (per view hash) and restore them on load. |
policy |
AttemptPolicy |
- |
snapshot |
Snapshot | null |
Restored on load when its viewHash matches the view (or is null). |
src |
string | null |
URL of a view to fetch when no view is given. |
styleTemplate |
string | null |
The host's default style template (a registered template with the document scope), used when the view's <astl-doc> names none that this player knows. |
theme |
ThemeSetting | null |
- |
view |
string |
View HTML to play (compiled, no answers). Setting it loads it. |
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get page(): number;0-based index of the current page.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get state(): AttemptState;loading, ready, active, checking, submitting, submitted, review or error.
Returns
Section titled “Returns”viewHash
Section titled “viewHash”Get Signature
Section titled “Get Signature”get viewHash(): string | null;Lowercase hex SHA-256 of the mounted view; null before mounting or without WebCrypto.
Returns
Section titled “Returns”string | null
Methods
Section titled “Methods”check()
Section titled “check()”check(): Promise<CheckResponse | null>;Practice: grades the answers and shows verdicts. Failures are shown and emitted, never thrown.
Returns
Section titled “Returns”Promise<CheckResponse | null>
getAnswers()
Section titled “getAnswers()”getAnswers(): Answers;Returns
Section titled “Returns”getSnapshot()
Section titled “getSnapshot()”getSnapshot(): Snapshot | null;Returns
Section titled “Returns”Snapshot | null
goTo()
Section titled “goTo()”goTo(index): boolean;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index |
number |
Returns
Section titled “Returns”boolean
load()
Section titled “load()”load(input): Promise<void>;Plays a view. Resolves when it is mounted (astl-ready); rejects with the error code when it cannot.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
input |
LoadInput |
Returns
Section titled “Returns”Promise<void>
next()
Section titled “next()”next(): boolean;Returns
Section titled “Returns”boolean
previous()
Section titled “previous()”previous(): boolean;Returns
Section titled “Returns”boolean
reset()
Section titled “reset()”reset(): boolean;Clears answers and results and goes back to the first page (not after submitting).
Returns
Section titled “Returns”boolean
setAnswers()
Section titled “setAnswers()”setAnswers(answers): void;Sets answers programmatically ('' clears); no change events from elements. The attempt keeps what the
elements accept (an unknown option id or a flag other than "true" is dropped), so what is checked and handed
in is always what the student sees.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
answers |
Answers |
Returns
Section titled “Returns”void
submit()
Section titled “submit()”submit(): Promise< | SubmitResponse| null>;Hands the answers in (final). Failures are shown and emitted, never thrown.
Returns
Section titled “Returns”Promise<
| SubmitResponse
| null>
AttemptController
Section titled “AttemptController”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AttemptController(options): AttemptController;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
AttemptControllerOptions |
Returns
Section titled “Returns”Properties
Section titled “Properties”| Property | Modifier | Type | Description |
|---|---|---|---|
checker |
public |
Checker |
The checker can be swapped at any time (e.g. a host sets it after the view). |
pages |
readonly |
readonly AttemptPage[] |
- |
policy |
readonly |
AttemptPolicy |
- |
restored |
readonly |
boolean |
Whether the snapshot passed in was applied. |
viewHash |
readonly |
string | null |
- |
Accessors
Section titled “Accessors”answers
Section titled “answers”Get Signature
Section titled “Get Signature”get answers(): Answers;Returns
Section titled “Returns”checks
Section titled “checks”Get Signature
Section titled “Get Signature”get checks(): number;Returns
Section titled “Returns”number
checksLeft
Section titled “checksLeft”Get Signature
Section titled “Get Signature”get checksLeft(): number | null;null = unlimited.
Returns
Section titled “Returns”number | null
Get Signature
Section titled “Get Signature”get page(): number;0-based index of the current page.
Returns
Section titled “Returns”number
pageCount
Section titled “pageCount”Get Signature
Section titled “Get Signature”get pageCount(): number;Returns
Section titled “Returns”number
readOnly
Section titled “readOnly”Get Signature
Section titled “Get Signature”get readOnly(): boolean;Answers cannot change: review, submitting or submitted.
Returns
Section titled “Returns”boolean
response
Section titled “response”Get Signature
Section titled “Get Signature”get response(): | CheckResponse | SubmitResponse | null;The last CheckResponse or SubmitResponse.
Returns
Section titled “Returns”| CheckResponse
| SubmitResponse
| null
result
Section titled “result”Get Signature
Section titled “Get Signature”get result(): GradeResult | null;The last result (practice keeps it while the student goes on).
Returns
Section titled “Returns”GradeResult | null
solution
Section titled “solution”Get Signature
Section titled “Get Signature”get solution(): Readonly<Record<string, string>> | null;Returns
Section titled “Returns”Readonly<Record<string, string>> | null
Get Signature
Section titled “Get Signature”get state(): AttemptState;Returns
Section titled “Returns”Methods
Section titled “Methods”canCheck()
Section titled “canCheck()”canCheck(): boolean;Returns
Section titled “Returns”boolean
canGoTo()
Section titled “canGoTo()”canGoTo(index): boolean;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index |
number |
Returns
Section titled “Returns”boolean
canSubmit()
Section titled “canSubmit()”canSubmit(): boolean;Returns
Section titled “Returns”boolean
check()
Section titled “check()”check(page?): Promise<CheckResponse>;Practice only. Grades everything, or one view page. A second call while checking joins the first.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
page? |
string |
Returns
Section titled “Returns”Promise<CheckResponse>
emptySlots()
Section titled “emptySlots()”emptySlots(page?): string[];Slot ids without an answer, on one page or on all of them.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
page? |
number |
Returns
Section titled “Returns”string[]
goTo()
Section titled “goTo()”goTo(index): boolean;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index |
number |
Returns
Section titled “Returns”boolean
next()
Section titled “next()”next(): boolean;Returns
Section titled “Returns”boolean
previous()
Section titled “previous()”previous(): boolean;Returns
Section titled “Returns”boolean
reset()
Section titled “reset()”reset(): boolean;Clears answers and the last result and goes back to the first page. Not after submitting.
Returns
Section titled “Returns”boolean
restorable()
Section titled “restorable()”static restorable(snapshot, viewHash): Snapshot | null;The snapshot when it is valid and belongs to the view (viewHash null or equal), else null.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
snapshot |
unknown |
viewHash |
string | null |
Returns
Section titled “Returns”Snapshot | null
setAnswers()
Section titled “setAnswers()”setAnswers(partial): Answers;Applies a partial update ('' deletes) and returns the slots that actually changed. Unknown ids are ignored.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
partial |
Answers |
Returns
Section titled “Returns”snapshot()
Section titled “snapshot()”snapshot(ui?): Snapshot;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
ui? |
Readonly<Record<string, JsonValue>> |
Returns
Section titled “Returns”start()
Section titled “start()”start(): void;ready -> active. Called implicitly by the first answer, check or submit.
Returns
Section titled “Returns”void
submit()
Section titled “submit()”submit(): Promise<SubmitResponse>;Final. Waits for a running check first; a second call while submitting joins the first.
Returns
Section titled “Returns”Promise<SubmitResponse>
subscribe()
Section titled “subscribe()”subscribe(listener): () => void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
listener |
AttemptListener |
Returns
Section titled “Returns”() => void
timeLeft()
Section titled “timeLeft()”timeLeft(): number | null;Milliseconds to the policy deadline (0 when past it); null without a deadline.
Returns
Section titled “Returns”number | null
abstract SingleSlotElement
Section titled “abstract SingleSlotElement”An element with exactly one slot whose id is the element id (a gap, a single choice, a slider).
Extends
Section titled “Extends”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SingleSlotElement(): SingleSlotElement;Returns
Section titled “Returns”Overrides
Section titled “Overrides”Properties
Section titled “Properties”| Property | Modifier | Type | Default value | Description |
|---|---|---|---|---|
disabled |
public |
boolean |
undefined |
- |
feedback |
public |
| ElementFeedback | null |
undefined |
Verdicts to show; null clears them. Editing a slot clears its own verdict and the group verdict. |
manifest |
static |
| ComponentManifest | null |
null |
The element type's manifest: drives the typed attribute getters and the default message namespace. |
messages |
static |
readonly MessageCatalog[] |
[] |
Catalogs of the manifest's namespace, used until a player attaches its own context. |
solution |
public |
Readonly<Record<string, string>> | null |
undefined |
Correct answers to show next to the student's; null hides them. |
Accessors
Section titled “Accessors”context
Section titled “context”Get Signature
Section titled “Get Signature”get context(): ElementContext;The player's context, or the default English one.
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”slotIds
Section titled “slotIds”Get Signature
Section titled “Get Signature”get slotIds(): readonly string[];Slot ids this element answers, in reading order. Stable after the element connects.
Returns
Section titled “Returns”readonly string[]
Slot ids this element answers, in reading order. Stable after the element connects.
Overrides
Section titled “Overrides”Methods
Section titled “Methods”attach()
Section titled “attach()”attach(context): void;Called by the player once, before the first render. Elements must work with a default context too.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
context |
ElementContext |
Returns
Section titled “Returns”void
Inherited from
Section titled “Inherited from”getAnswers()
Section titled “getAnswers()”getAnswers(): Answers;Answered slots only.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”setAnswers()
Section titled “setAnswers()”setAnswers(answers): void;Unknown ids are ignored; '' clears a slot. Does not fire a change event.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
answers |
Answers |
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”units()
Section titled “units()”units(): Units;Progress units, e.g. 3 of 5 cards placed.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Interfaces
Section titled “Interfaces”AttemptControllerOptions
Section titled “AttemptControllerOptions”Properties
Section titled “Properties”| Property | Modifier | Type | Description |
|---|---|---|---|
checker |
readonly |
Checker |
- |
now? |
readonly |
() => number |
Clock in milliseconds, for deadlines and savedAt. Default Date.now. |
pages |
readonly |
readonly AttemptPage[] |
- |
policy |
readonly |
AttemptPolicy |
- |
snapshot? |
readonly |
Snapshot | null |
Restored when it is valid and its viewHash is null or equals viewHash. |
viewHash? |
readonly |
string | null |
Hash of the view the attempt runs on; written into snapshots. |
AttemptPage
Section titled “AttemptPage”Properties
Section titled “Properties”| Property | Modifier | Type | Description |
|---|---|---|---|
id |
readonly |
string |
View page id. |
slotIds |
readonly |
readonly string[] |
Slots answered on this page, in reading order. |
DefinePlayerOptions
Section titled “DefinePlayerOptions”Properties
Section titled “Properties”| Property | Modifier | Type | Description |
|---|---|---|---|
catalogs? |
readonly |
readonly MessageCatalog[] |
More or overriding catalogs (any namespace), e.g. another UI locale. |
compileSource? |
readonly |
SourceCompiler |
Enables light-DOM sources (HTML with answers). |
elements |
readonly |
readonly ElementEntry[] |
Component elements: a registration is defined right away, a LazyElement the first time a view contains its tag. |
registry? |
readonly |
CustomElementRegistry |
Default: the global customElements. |
styleTemplates? |
readonly |
readonly StyleTemplate[] |
Looks that exercises can pick with style-template on <astl-doc> or on a component, and hosts with <astl-player style-template> (trusted, like the host's own CSS). |
urlPolicy? |
readonly |
UrlPolicy |
Which URLs of a view stay when the player mounts it (links, pictures, url attributes): the same policy as the engine that compiled the views (createEngine({ urlPolicy })), or the player removes what the engine kept. Default isSafeUrl. One policy for every player of the page: the last call that passes one sets it. An address stays only when the policy answers true (one that throws blocks it). |
ElementRegistration
Section titled “ElementRegistration”One component element type for the player: its manifest, element class and message catalogs.
Properties
Section titled “Properties”| Property | Modifier | Type |
|---|---|---|
element |
readonly |
CustomElementConstructor |
manifest |
readonly |
ComponentManifest |
messages |
readonly |
readonly MessageCatalog[] |
HttpCheckerOptions
Section titled “HttpCheckerOptions”Properties
Section titled “Properties”LazyElement
Section titled “LazyElement”Imported the first time a view mounted by tag; resolves to that tag's registration.
Properties
Section titled “Properties”| Property | Modifier | Type |
|---|---|---|
load |
readonly |
() => Promise<ElementRegistration> |
tag |
readonly |
string |
LoadInput
Section titled “LoadInput”What load() accepts; omitted fields keep the player's current properties.
Properties
Section titled “Properties”| Property | Modifier | Type |
|---|---|---|
checker? |
readonly |
Checker | null |
policy? |
readonly |
AttemptPolicy |
snapshot? |
readonly |
Snapshot | null |
view |
readonly |
string |
VerdictOptions
Section titled “VerdictOptions”Properties
Section titled “Properties”Type Aliases
Section titled “Type Aliases”AttemptChange
Section titled “AttemptChange”type AttemptChange = | { previous: AttemptState; state: AttemptState; type: "state";} | { answers: Answers; type: "answers";} | { index: number; previous: number; type: "page";} | { response: | CheckResponse | SubmitResponse; type: "result";} | { type: "reset";};Union Members
Section titled “Union Members”Type Literal
Section titled “Type Literal”{ previous: AttemptState; state: AttemptState; type: "state";}Type Literal
Section titled “Type Literal”{ answers: Answers; type: "answers";}Only the slots that changed; '' = cleared.
Type Literal
Section titled “Type Literal”{ index: number; previous: number; type: "page";}Type Literal
Section titled “Type Literal”{ response: | CheckResponse | SubmitResponse; type: "result";}Type Literal
Section titled “Type Literal”{ type: "reset";}AttemptListener
Section titled “AttemptListener”type AttemptListener = (change) => void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
change |
AttemptChange |
Returns
Section titled “Returns”void
ElementEntry
Section titled “ElementEntry”type ElementEntry = | ElementRegistration | LazyElement;A component element for the player: registered right away, or lazily on first use.
Verdict
Section titled “Verdict”type Verdict = GroupVerdict;Slot verdicts (correct, incorrect) are a subset of group verdicts.
Variables
Section titled “Variables”PLAYER_NAMESPACE
Section titled “PLAYER_NAMESPACE”const PLAYER_NAMESPACE: "astl.player" = 'astl.player';i18n namespace of the player chrome and of strings shared by all elements (verdict words).
playerMessages
Section titled “playerMessages”const playerMessages: readonly MessageCatalog[];verdictStyles
Section titled “verdictStyles”const verdictStyles: CSSResult;Verdict styling. [data-verdict] sets --astl-verdict-color, --astl-verdict-soft and --astl-verdict-border
(solid = correct, double = partial, dashed = incorrect, dotted = empty) for borders of the element that carries it.
visuallyHidden
Section titled “visuallyHidden”const visuallyHidden: CSSResult;.astl-vh: hidden on screen, still read by screen readers.
Functions
Section titled “Functions”announce()
Section titled “announce()”function announce(region, message): void;Puts a message into a polite live region. The region is emptied first and filled in a later task, so screen readers also announce a message that repeats the previous one.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
region |
HTMLElement |
message |
string |
Returns
Section titled “Returns”void
definePlayer()
Section titled “definePlayer()”function definePlayer(options): void;Defines <astl-player> and the given elements, and registers style templates. Safe to call again: a tag already
defined by the same class, a lazy element with the same load, or a template already registered as the same
object is skipped. A tag taken by another class, by another loader or by the other kind of entry (eager or lazy),
or a template name taken by another template, throws (two copies of a bundle would otherwise mix silently).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
DefinePlayerOptions |
Returns
Section titled “Returns”void
httpChecker()
Section titled “httpChecker()”function httpChecker(options): Checker;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
HttpCheckerOptions |
Returns
Section titled “Returns”renderVerdict()
Section titled “renderVerdict()”function renderVerdict( verdict, t, options?): TemplateResult;Icon + word (verdict.* messages, e.g. "Dobrze", "Jeszcze nie") inside a [data-verdict] wrapper.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
verdict |
GroupVerdict |
t |
Translate |
options? |
VerdictOptions |
Returns
Section titled “Returns”TemplateResult
seededShuffle()
Section titled “seededShuffle()”function seededShuffle<T>(items, seed): T[];Deterministic Fisher-Yates shuffle: the same seed always gives the same order.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
items |
readonly T[] |
seed |
string |
Returns
Section titled “Returns”T[]
verdictIcon()
Section titled “verdictIcon()”function verdictIcon(verdict): TemplateResult;A decorative icon per verdict (hidden from assistive technology: the word carries the meaning).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
verdict |
GroupVerdict |
Returns
Section titled “Returns”TemplateResult