aboutsummaryrefslogtreecommitdiff
path: root/ui/static/js/unread_counter_handler.js
blob: 87a1aaafb7d1b3babc0d6cf0d6f23585b4836f40 (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
class UnreadCounterHandler {
    static decrement(n) {
        this.updateValue((current) => {
            return current - n;
        });
    }

    static increment(n) {
        this.updateValue((current) => {
            return current + n;
        });
    }

    static updateValue(callback) {
        let counterElements = document.querySelectorAll("span.unread-counter");
        counterElements.forEach((element) => {
            let oldValue = parseInt(element.textContent, 10);
            element.innerHTML = callback(oldValue);
        });

        if (window.location.href.endsWith('/unread')) {
            let oldValue = parseInt(document.title.split('(')[1], 10);
            let newValue = callback(oldValue);

            document.title = document.title.replace(
                /(.*?)\(\d+\)(.*?)/,
                function (match, prefix, suffix, offset, string) {
                    return prefix + '(' + newValue + ')' + suffix;
                }
            );
        }
    }
}