// DOM side of p5's accessibility features (src/accessibility/describe.js
// and outputs.js): descriptions and outputs live next to the canvas, as
// its fallback content or as visible labels.
///|
/// Writes a canvas description (`_describeHTML`). `kind` is "fallback" or
/// "label".
pub extern "js" fn describe_html(
c : Canvas,
kind : String,
text : String,
) -> Unit =
#|(c, type, text) => {
#| const id = c.id, dom = c.parentNode; if (!dom) return;
#| const d = c.__p5mbtDesc || (c.__p5mbtDesc = { fallbackElements: {}, labelElements: {} });
#| const q = s => dom.querySelector(s);
#| if (type === 'fallback') {
#| if (d.fallback) { if (d.fallback.innerHTML !== text) d.fallback.innerHTML = text; return; }
#| if (!q(`#${id}_Description`)) {
#| const html = ``;
#| if (!q(`#${id}accessibleOutput`)) q(`#${id}`).innerHTML = html;
#| else q(`#${id}accessibleOutput`).insertAdjacentHTML('beforebegin', html);
#| } else {
#| q(`#${id}_fallbackTable`).insertAdjacentHTML('beforebegin', ``);
#| }
#| d.fallback = q(`#${id}_fallbackDesc`);
#| d.fallback.innerHTML = text;
#| } else {
#| if (d.label) { if (d.label.innerHTML !== text) d.label.innerHTML = text; return; }
#| if (!q(`#${id}_Label`)) {
#| const html = ``;
#| if (!q(`#${id}accessibleOutputLabel`)) q(`#${id}`).insertAdjacentHTML('afterend', html);
#| else q(`#${id}accessibleOutputLabel`).insertAdjacentHTML('beforebegin', html);
#| } else if (q(`#${id}_labelTable`)) {
#| q(`#${id}_labelTable`).insertAdjacentHTML('beforebegin', ``);
#| }
#| d.label = q(`#${id}_labelDesc`);
#| d.label.innerHTML = text;
#| }
#|}
///|
/// Writes one row of the canvas element descriptions
/// (`_describeElementHTML`); `name` is already stripped for use in ids.
pub extern "js" fn describe_element_html(
c : Canvas,
kind : String,
name : String,
inner : String,
) -> Unit =
#|(c, type, name, inner) => {
#| const id = c.id, dom = c.parentNode; if (!dom) return;
#| const d = c.__p5mbtDesc || (c.__p5mbtDesc = { fallbackElements: {}, labelElements: {} });
#| const q = s => dom.querySelector(s);
#| const els = type === 'fallback' ? d.fallbackElements : d.labelElements;
#| if (els[name]) { if (els[name].innerHTML !== inner) els[name].innerHTML = inner; return; }
#| if (type === 'fallback') {
#| if (!q(`#${id}_Description`)) {
#| const html = `Canvas elements and their descriptions
`;
#| if (!q(`#${id}accessibleOutput`)) q(`#${id}`).innerHTML = html;
#| else q(`#${id}accessibleOutput`).insertAdjacentHTML('beforebegin', html);
#| } else if (!q(`#${id}_fallbackTable`)) {
#| q(`#${id}_fallbackDesc`).insertAdjacentHTML('afterend', `Canvas elements and their descriptions
`);
#| }
#| const row = document.createElement('tr'); row.id = `${id}_fte_${name}`;
#| q(`#${id}_fallbackTable`).appendChild(row);
#| } else {
#| if (!q(`#${id}_Label`)) {
#| const html = ``;
#| if (!q(`#${id}accessibleOutputLabel`)) q(`#${id}`).insertAdjacentHTML('afterend', html);
#| else q(`#${id}accessibleOutputLabel`).insertAdjacentHTML('beforebegin', html);
#| } else if (!q(`#${id}_labelTable`)) {
#| q(`#${id}_labelDesc`).insertAdjacentHTML('afterend', ``);
#| }
#| const row = document.createElement('tr'); row.id = `${id}_lte_${name}`;
#| q(`#${id}_labelTable`).appendChild(row);
#| }
#| els[name] = q(`#${id}_${type === 'fallback' ? 'fte' : 'lte'}_${name}`);
#| els[name].innerHTML = inner;
#|}
///|
/// Sets (or, with "", removes) the `lang` attribute of the canvas and of
/// its descriptions.
pub extern "js" fn set_description_lang(c : Canvas, lang : String) -> Unit =
#|(c, lang) => {
#| const d = c.__p5mbtDesc || {};
#| for (const el of [c, d.fallback, d.label]) {
#| if (!el) continue;
#| if (lang) el.setAttribute('lang', lang); else el.removeAttribute('lang');
#| }
#|}
///|
/// Creates the containers of a text or grid output (`_createOutput`).
/// `kind` is "textOutput" or "gridOutput", `display` "Fallback" or "Label".
pub extern "js" fn create_output(
c : Canvas,
kind : String,
display : String,
) -> Unit =
#|(c, type, display) => {
#| const cnvId = c.id, dom = c.parentNode; if (!dom) return;
#| const q = s => dom.querySelector(s);
#| let cIdT, container, query = '';
#| if (display === 'Fallback') {
#| cIdT = cnvId + type; container = cnvId + 'accessibleOutput';
#| if (!q(`#${container}`)) {
#| if (!q(`#${cnvId}_Description`)) q(`#${cnvId}`).innerHTML = ``;
#| else q(`#${cnvId}_Description`).insertAdjacentHTML('afterend', ``);
#| }
#| } else {
#| query = display; cIdT = cnvId + type + display; container = cnvId + 'accessibleOutput' + display;
#| if (!q(`#${container}`)) {
#| if (!q(`#${cnvId}_Label`)) q(`#${cnvId}`).insertAdjacentHTML('afterend', ``);
#| else q(`#${cnvId}_Label`).insertAdjacentHTML('afterend', ``);
#| }
#| }
#| if (q(`#${cIdT}`)) return;
#| let inner;
#| if (type === 'textOutput') {
#| query = `#${cnvId}gridOutput${query}`;
#| inner = ``;
#| if (q(query)) q(query).insertAdjacentHTML('beforebegin', inner); else q(`#${container}`).innerHTML = inner;
#| } else {
#| query = `#${cnvId}textOutput${query}`;
#| inner = ``;
#| if (q(query)) q(query).insertAdjacentHTML('afterend', inner); else q(`#${container}`).innerHTML = inner;
#| }
#|}
///|
/// Sets the HTML of one part of an output (`summary`, `list`, `map` or
/// `shapeDetails`) when it changed. `id` is the output's id without the
/// canvas id prefix, e.g. "textOutput" or "gridOutputLabel".
pub extern "js" fn set_output_part(
c : Canvas,
id : String,
part : String,
html : String,
) -> Unit =
#|(c, id, part, html) => {
#| const dom = c.parentNode; if (!dom) return;
#| const el = dom.querySelector(`#${c.id}${id}_${part}`);
#| if (el && el.innerHTML !== html) el.innerHTML = html;
#|}
///|
/// The id of a canvas element.
pub extern "js" fn canvas_id(c : Canvas) -> String = "(c) => c.id"