Skip to content

@astrolabe/player

The <astl-player> element, element base classes, the attempt controller, httpChecker and accessibility helpers. Contains no answer logic.

  • LitElement
new AstlElement(): AstlElement;

AstlElement

LitElement.constructor
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.
get context(): ElementContext;

The player's context, or the default English one.

ElementContext

get abstract slotIds(): readonly string[];

Slot ids this element answers, in reading order. Stable after the element connects.

readonly string[]

Slot ids this element answers, in reading order. Stable after the element connects.

PlayerElementApi.slotIds

attach(context): void;

Called by the player once, before the first render. Elements must work with a default context too.

Parameter Type
context ElementContext

void

PlayerElementApi.attach

abstract getAnswers(): Answers;

Answered slots only.

Answers

PlayerElementApi.getAnswers

abstract setAnswers(answers): void;

Unknown ids are ignored; '' clears a slot. Does not fire a change event.

Parameter Type
answers Answers

void

PlayerElementApi.setAnswers

abstract units(): Units;

Progress units, e.g. 3 of 5 cards placed.

Units

PlayerElementApi.units


  • LitElement
new AstlPlayer(): AstlPlayer;

AstlPlayer

LitElement.constructor
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.
get page(): number;

0-based index of the current page.

number

get state(): AttemptState;

loading, ready, active, checking, submitting, submitted, review or error.

AttemptState

get viewHash(): string | null;

Lowercase hex SHA-256 of the mounted view; null before mounting or without WebCrypto.

string | null

check(): Promise<CheckResponse | null>;

Practice: grades the answers and shows verdicts. Failures are shown and emitted, never thrown.

Promise<CheckResponse | null>

getAnswers(): Answers;

Answers

getSnapshot(): Snapshot | null;

Snapshot | null

goTo(index): boolean;
Parameter Type
index number

boolean

load(input): Promise<void>;

Plays a view. Resolves when it is mounted (astl-ready); rejects with the error code when it cannot.

Parameter Type
input LoadInput

Promise<void>

next(): boolean;

boolean

previous(): boolean;

boolean

reset(): boolean;

Clears answers and results and goes back to the first page (not after submitting).

boolean

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.

Parameter Type
answers Answers

void

submit(): Promise<
| SubmitResponse
| null>;

Hands the answers in (final). Failures are shown and emitted, never thrown.

Promise< | SubmitResponse | null>


new AttemptController(options): AttemptController;
Parameter Type
options AttemptControllerOptions

AttemptController

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 -
get answers(): Answers;

Answers

get checks(): number;

number

get checksLeft(): number | null;

null = unlimited.

number | null

get page(): number;

0-based index of the current page.

number

get pageCount(): number;

number

get readOnly(): boolean;

Answers cannot change: review, submitting or submitted.

boolean

get response():
| CheckResponse
| SubmitResponse
| null;

The last CheckResponse or SubmitResponse.

| CheckResponse | SubmitResponse | null

get result(): GradeResult | null;

The last result (practice keeps it while the student goes on).

GradeResult | null

get solution(): Readonly<Record<string, string>> | null;

Readonly<Record<string, string>> | null

get state(): AttemptState;

AttemptState

canCheck(): boolean;

boolean

canGoTo(index): boolean;
Parameter Type
index number

boolean

canSubmit(): boolean;

boolean

check(page?): Promise<CheckResponse>;

Practice only. Grades everything, or one view page. A second call while checking joins the first.

Parameter Type
page? string

Promise<CheckResponse>

emptySlots(page?): string[];

Slot ids without an answer, on one page or on all of them.

Parameter Type
page? number

string[]

goTo(index): boolean;
Parameter Type
index number

boolean

next(): boolean;

boolean

previous(): boolean;

boolean

reset(): boolean;

Clears answers and the last result and goes back to the first page. Not after submitting.

boolean

static restorable(snapshot, viewHash): Snapshot | null;

The snapshot when it is valid and belongs to the view (viewHash null or equal), else null.

Parameter Type
snapshot unknown
viewHash string | null

Snapshot | null

setAnswers(partial): Answers;

Applies a partial update ('' deletes) and returns the slots that actually changed. Unknown ids are ignored.

Parameter Type
partial Answers

Answers

snapshot(ui?): Snapshot;
Parameter Type
ui? Readonly<Record<string, JsonValue>>

Snapshot

start(): void;

ready -> active. Called implicitly by the first answer, check or submit.

void

submit(): Promise<SubmitResponse>;

Final. Waits for a running check first; a second call while submitting joins the first.

Promise<SubmitResponse>

subscribe(listener): () => void;
Parameter Type
listener AttemptListener

() => void

timeLeft(): number | null;

Milliseconds to the policy deadline (0 when past it); null without a deadline.

number | null


An element with exactly one slot whose id is the element id (a gap, a single choice, a slider).

new SingleSlotElement(): SingleSlotElement;

SingleSlotElement

AstlElement.constructor

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.
get context(): ElementContext;

The player's context, or the default English one.

ElementContext

AstlElement.context

get slotIds(): readonly string[];

Slot ids this element answers, in reading order. Stable after the element connects.

readonly string[]

Slot ids this element answers, in reading order. Stable after the element connects.

AstlElement.slotIds

attach(context): void;

Called by the player once, before the first render. Elements must work with a default context too.

Parameter Type
context ElementContext

void

AstlElement.attach

getAnswers(): Answers;

Answered slots only.

Answers

AstlElement.getAnswers

setAnswers(answers): void;

Unknown ids are ignored; '' clears a slot. Does not fire a change event.

Parameter Type
answers Answers

void

AstlElement.setAnswers

units(): Units;

Progress units, e.g. 3 of 5 cards placed.

Units

AstlElement.units

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.

Property Modifier Type Description
id readonly string View page id.
slotIds readonly readonly string[] Slots answered on this page, in reading order.

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).

One component element type for the player: its manifest, element class and message catalogs.

Property Modifier Type
element readonly CustomElementConstructor
manifest readonly ComponentManifest
messages readonly readonly MessageCatalog[]

Property Modifier Type Description
credentials? readonly RequestCredentials Fetch credentials mode (cookies). Default: the browser default (same-origin).
endpoint readonly string URL of one attempt, e.g. /api/attempts/123; /check and /submit are appended.
fetch? readonly (input, init) => Promise<Response> Fetch implementation, for tests or instrumented clients. Default: the global fetch.
headers? readonly Readonly<Record<string, string>> Extra request headers, e.g. Authorization.

Imported the first time a view mounted by contains tag; resolves to that tag's registration.

Property Modifier Type
load readonly () => Promise<ElementRegistration>
tag readonly string

What load() accepts; omitted fields keep the player's current properties.

Property Modifier Type
checker? readonly Checker | null
policy? readonly AttemptPolicy
snapshot? readonly Snapshot | null
view readonly string

Property Modifier Type Description
dir? readonly "ltr" | "rtl" -
hiddenWord? readonly boolean Hide the word visually (it stays readable, e.g. through aria-describedby).
id? readonly string id of the word element, for aria-describedby.
lang? readonly string UI language and direction of the word. Pass the context locale: the word is UI text, and inside content of another language it would otherwise be read with the content's voice and bidi rules.
part? readonly string part name of the wrapper.
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";
};
{
previous: AttemptState;
state: AttemptState;
type: "state";
}

{
answers: Answers;
type: "answers";
}

Only the slots that changed; '' = cleared.


{
index: number;
previous: number;
type: "page";
}

{
response: | CheckResponse
| SubmitResponse;
type: "result";
}

{
type: "reset";
}

type AttemptListener = (change) => void;
Parameter Type
change AttemptChange

void


type ElementEntry =
| ElementRegistration
| LazyElement;

A component element for the player: registered right away, or lazily on first use.


type Verdict = GroupVerdict;

Slot verdicts (correct, incorrect) are a subset of group verdicts.

const PLAYER_NAMESPACE: "astl.player" = 'astl.player';

i18n namespace of the player chrome and of strings shared by all elements (verdict words).


const playerMessages: readonly MessageCatalog[];

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.


const visuallyHidden: CSSResult;

.astl-vh: hidden on screen, still read by screen readers.

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.

Parameter Type
region HTMLElement
message string

void


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).

Parameter Type
options DefinePlayerOptions

void


function httpChecker(options): Checker;
Parameter Type
options HttpCheckerOptions

Checker


function renderVerdict(
verdict,
t,
options?
): TemplateResult;

Icon + word (verdict.* messages, e.g. "Dobrze", "Jeszcze nie") inside a [data-verdict] wrapper.

Parameter Type
verdict GroupVerdict
t Translate
options? VerdictOptions

TemplateResult


function seededShuffle<T>(items, seed): T[];

Deterministic Fisher-Yates shuffle: the same seed always gives the same order.

Type Parameter
T
Parameter Type
items readonly T[]
seed string

T[]


function verdictIcon(verdict): TemplateResult;

A decorative icon per verdict (hidden from assistive technology: the word carries the meaning).

Parameter Type
verdict GroupVerdict

TemplateResult