aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/js/feed.js
blob: 50ebfcd38549d5881c4005fd71c5d12750c21295 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// <reference lib="es2022" />

import { $, $$, on, dispatch, halts, attr, next, prev, asHtml, hotkey, behavior, makelogger } from "./19.js"

const ilog = makelogger("feed");
const sFeed = "[role=feed]";
const sScopedArticle = ":scope > article, :scope > [role=article]";
const sArticle = "article, [role=article]";
const sFocusable = ":is(a, area)[href], :is(button, details, embed, iframe, label, select, textarea):not([disabled]), :is(audio, video)[controls], :is(img, object)[usemap], [tabindex]:not([tabindex='-1'])"

/**
 * @param {HTMLElement} feed
 * @returns {HTMLElement[]}
 */
const articles = feed => $$(feed, sScopedArticle);


/**
 * @param {HTMLElement} feed
 * @param {HTMLElement} activeElement
 * @returns {null}
 */
const focusPreviousArticle = (feed, activeElement) => asHtml(prev(feed, sArticle, activeElement, {wrap: false}))?.focus();


/**
 * @param {HTMLElement} feed
 * @param {HTMLElement} activeElement
 * @returns {null}
 */
const focusNextArticle = (feed, activeElement) => asHtml(next(feed, sArticle, activeElement.closest(sArticle), {wrap: false}))?.focus();


/**
 * @param {HTMLElement} article
 * @returns {null}
 */
const focusOutsideFeed = article => asHtml(article.parentElement.closest(sArticle))?.focus();


/**
 * @param {HTMLElement} activeElement
 * @returns {null}
 */
const focusInsideFeed = activeElement => asHtml($(activeElement.closest(sArticle), sArticle))?.focus();


/**
 * @param {HTMLElement} feed
 * @returns {null}
 */
const focusBeforeFeed = feed => asHtml(prev(document.body, sFocusable, feed, {}))?.focus();


/**
 * @param {HTMLElement} feed
 * @returns {null}
 */
const focusAfterFeed = feed => asHtml(next(document.body, sFocusable, feed, {}))?.focus();


export const feed = behavior(sFeed, (feed, { root }) => {

    if (!(feed instanceof HTMLElement)) return;

    feed.setAttribute("aria-busy", "false");
    articles(feed).forEach((article, idx, articles) => {
        article.setAttribute("tabindex", "0");
        article.setAttribute("aria-posinset", idx + 1);
        article.setAttribute("aria-setsize", articles.length);
        if (!(article.hasAttribute("aria-labelledby") || article.hasAttribute("aria-label")))
            console.error("Article", article, "has no accessible name (aria-label or aria-labelledby)");
    });

    on(feed, "keydown", hotkey({
        "PageUp": halts("default propagation", _ => focusPreviousArticle(feed, root.activeElement)),
        "PageDown": halts("default propagation", _ => focusNextArticle(feed, root.activeElement)),
        "Alt+PageUp": halts("default", _ => focusOutsideFeed(root.activeElement)),
        "Alt+PageDown": halts("default", _ => focusInsideFeed(root.activeElement)),
        "Ctrl+Home": halts("default propagation", _ => focusBeforeFeed(feed)),
        "Ctrl+End": halts("default propagation", _ => focusAfterFeed(feed)),
    }));
});

feed(document);