summaryrefslogtreecommitdiff
path: root/cgit.js
blob: 05141866cac650b14c5231a3315d07c270f78c30 (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
86
87
88
/* This does client-side modifications which should probably be implemented on
 * HTML generation, inside cgit. But they aren't (shouldn't be) very heavy or
 * slow, so it's probably ok.
 */

// Proper device scaling
const viewport = document.createElement("meta");
viewport.id = "viewport";
viewport.name = "viewport";
viewport.content = "width=device-width initial-scale=1";
document.head.appendChild(viewport);

document.addEventListener("DOMContentLoaded", function(){
    // Edit file tree
    const list = document.getElementsByClassName('list')[0]

    if (list !== undefined && list.summary === "tree listing") {
        const table = list.children[0]
        const rows = table.children

        // Reorder directories (and submodules) before files
        var lastDir = 1
        for (let i = 1; i < rows.length; ++i) {
            if (!rows[i].children[0].innerText.startsWith('-')) {
                if (i > lastDir) {
                    table.insertBefore(rows[i], rows[lastDir])
                }
                ++lastDir
            }
        }

        // Human-readable size format
        for (const row of rows) {
            if (row.children[2].classList.contains('ls-size')) {
                let size = row.children[2].innerText
                if (size < 1024) {
                    size += ' B'
                }
                else if (size < 1048576) {
                    size = (size / 1024).toFixed(1) + ' KiB'
                }
                else if (size < 1073741824) {
                    size = (size / 1048576).toFixed(1) + ' MiB'
                }
                else {
                    size = (size / 1073741824).toFixed(1) + ' GiB'
                }
                row.children[2].innerText = size
            }
        }
    }

    // Edit the tabs
    const tabs = document.getElementsByClassName('tabs')[0].children[0].children[0].children[0]

    if (tabs !== undefined && tabs.children.length === 7) {
        const about   = tabs.children[0]
        const summary = tabs.children[1]
        const refs    = tabs.children[2]
        const log     = tabs.children[3]
        const tree    = tabs.children[4]
        const commit  = tabs.children[5]
        const diff    = tabs.children[6]

        tabs.insertBefore(tree, refs)
        tabs.insertBefore(log, refs)

        about.innerText = 'README'
        summary.innerText = 'Summary'
        tree.innerText = 'Files'
        log.innerText = 'Commits'
    }
    else if (tabs !== undefined && tabs.children.length === 6) {
        const summary = tabs.children[0]
        const refs    = tabs.children[1]
        const log     = tabs.children[2]
        const tree    = tabs.children[3]
        const commit  = tabs.children[4]
        const diff    = tabs.children[5]

        tabs.insertBefore(tree, refs)
        tabs.insertBefore(log, refs)

        summary.innerText = 'Summary'
        tree.innerText = 'Files'
        log.innerText = 'Commits'
    }
});