# gitlab-draft-tab-v2.user.js -rw-r--r-- 3.2 KiB View raw
                                                                                
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
89
90
91
92
93
94
95
96
97
// ==UserScript==
// @name         Gitlab Draft Tab
// @namespace    https://brixit.nl/
// @version      0.1
// @description  Split the "Open Merge Requests" in to open and draft tabs
// @author       Martijn Braam
// @match        https://*/*/-/merge_requests*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gitlab.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function makeUrl(draft) {
        const urlParams = new URLSearchParams(window.location.search);
        urlParams.set('state', 'open');
        urlParams.set('draft', draft);
        return location.pathname + '?' + urlParams;
    }

    function makeTab(name, bubbleCount, url) {
        const tab = document.createElement("LI");
        tab.classList.add("nav-item");

        const a = document.createElement("A");
        a.classList.add("nav-link");
        a.classList.add("gl-tab-nav-item");
        tab.appendChild(a);
        a.href = url;

        const nameSpan = document.createElement("SPAN");
        nameSpan.innerText = name;
        a.appendChild(nameSpan);

        const bubble = document.createElement("SPAN");
        bubble.innerText = bubbleCount;
        bubble.classList.add("gl-badge");
        bubble.classList.add("badge");
        bubble.classList.add("badge-pill");
        bubble.classList.add("badge-muted");
        bubble.classList.add("sm");
        bubble.classList.add("gl-tab-counter-badge");
        bubble.classList.add("gl-display-none");
        bubble.classList.add("gl-sm-display-inline-flex");
        a.appendChild(bubble);

        return tab;
    };

    function makeTabs(state, draft) {
        const tabBar = document.getElementsByClassName("issues-state-filters")[0];
        const oldTab = document.getElementById("state-opened");
        const oldOpenCount = document.querySelector("#state-opened span.badge").innerText.replace(',', '');

        oldTab.style.display = 'none';

        let draftCount = '?';
        let openCount = '?';

        if (state == "open" || state == "opened") {
            if (draft == "no") {
                openCount = oldOpenCount;
            } else {
                draftCount = oldOpenCount;
            }
        }

        const draftTab = makeTab("Draft", draftCount, makeUrl("yes"));
        const openTab = makeTab("Open", openCount, makeUrl("no"));

        if (state == "open" || state == "opened") {
            if (draft == "no") {
                openTab.classList.add("gl-tab-nav-item-active");
            } else {
                draftTab.classList.add("gl-tab-nav-item-active");
            }
        }

        tabBar.prepend(openTab);
        tabBar.prepend(draftTab);
    };

    const body = document.getElementsByTagName('body')[0];
    if (body.dataset.page == 'projects:merge_requests:index') {
        const urlParams = new URLSearchParams(window.location.search);
        const state = urlParams.get('state');
        const draft = urlParams.get('draft');

        if (draft === null && (state === null || state == "open" || state == "opened")) {
            // This is the root MR list, redirect to draft==no
            location.href = makeUrl('no');
        } else {
            makeTabs(state, draft);
        }
    }
})();