aboutsummaryrefslogtreecommitdiffstats
path: root/qt-gerrit-ui-plugin/qt-gerrit-ui-plugin.html
blob: 9bedbb9952349fb06cc7e2a0ee433f739936d168 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Copyright (C) 2019 The Qt Company
//
// This plugin provides UI customization for codereview.qt-project.org
//

<dom-module id="qt-gerrit-ui-plugin">
    <script>
        'use strict';

        var BUTTONS = [
            'gerrit-plugin-qt-workflow~abandon',
            'gerrit-plugin-qt-workflow~defer',
            'gerrit-plugin-qt-workflow~reopen',
            'gerrit-plugin-qt-workflow~stage',
            'gerrit-plugin-qt-workflow~unstage'
        ];

        Gerrit.install(plugin => {

            plugin.buttons = null;

            function htmlToElement(html) {
                var template = document.createElement('template');
                html = html.trim(); // No white space
                template.innerHTML = html;
                return template.content.firstChild;
            }

            // Customize header
            plugin.hook('header-title',  {replace: true} ).onAttached(element => {
                const css_str = '<style> \
                                  .titleText::before {\
                                  background-image: url("/static/logo_qt.png");\
                                  background-size: 40px 30px;\
                                  background-repeat: no-repeat;\
                                  content: "";\
                                  display: inline-block;\
                                  height: 36px;\
                                  vertical-align: text-top;\
                                  width: 46px;\
                                }\
                                </style>';
                const html_str = '<div id="qt-header"> \
                                    <div class="titleText">Code Review</div> \
                                  </div>';
                var elem = htmlToElement(css_str);
                element.appendChild(elem);
                elem = htmlToElement(html_str);
                element.appendChild(elem);
            });

            // Customize header changes dropdown menu
            plugin.hook('header-dropdown-Changes').onAttached(element => {
                // this is ugly, but there is no API for this
                var ul_elem = element.content.children[1].children[0].children[0].children[0];
                var li_elem;
                var link_elem;

                li_elem = htmlToElement(ul_elem.children[1].outerHTML);
                link_elem = li_elem.children[0].children[1];
                link_elem.text = 'Staged';
                link_elem.href = '/q/status:staged';
                ul_elem.insertBefore(li_elem, ul_elem.children[2]);

                li_elem = htmlToElement(ul_elem.children[1].outerHTML);
                link_elem = li_elem.children[0].children[1];
                link_elem.text = 'Integrating';
                link_elem.href = '/q/status:integrating';
                ul_elem.insertBefore(li_elem, ul_elem.children[3]);

                li_elem = htmlToElement(ul_elem.children[1].outerHTML);
                link_elem = li_elem.children[0].children[1];
                link_elem.text = 'Deferred';
                link_elem.href = '/q/status:deferred';
                ul_elem.insertBefore(li_elem, ul_elem.children[5]);
            });

            // Hide Sanity Bot review score row by default in reply dialog
            plugin.hook('review-label-scores-Sanity-Review').onAttached(element => {
                const html = '<div onclick="\
                                            document.getElementById(\'sanitybotreviewmorediv\').style.display=\'none\'; \
                                            document.getElementById(\'sanitybotreviewscorediv\').style.display=\'block\'; \
                                            \"> \
                                  <div id="sanitybotreviewmorediv" style="display:block;">more...</div> \
                                  <div id="sanitybotreviewscorediv" style="display:none;"></div> \
                              </div>';
                var wrapper_elem = document.createElement('div');
                wrapper_elem.innerHTML = html;
                var child_elem = element.content.children[0];
                element.content.replaceChild(wrapper_elem, child_elem);
                document.getElementById('sanitybotreviewscorediv').appendChild(child_elem);
            });

            // Customize change view
            plugin.on('labelchange', function(changeInfo) {
                plugin.ca = plugin.changeActions();

                // always hide the rebase button
                plugin.ca.setActionHidden('revision', 'rebase', true);

                // Hide 'Sanity-Review+1' button in header
                var secondaryActionsElem = document.getElementById("secondaryActions");
                if (secondaryActionsElem &&
                    secondaryActionsElem.innerHTML &&
                    secondaryActionsElem.innerHTML.indexOf('Sanity-Review+1') != -1) {
                    plugin.ca.hideQuickApproveAction();
                }

                // Remove any existing buttons
                if (plugin.buttons) {
                    BUTTONS.forEach((key) => {
                        if(typeof plugin.buttons[key] !== 'undefined' && plugin.buttons[key] !== null) {
                            plugin.ca.removeTapListener(plugin.buttons[key], (param) => {} );
                            plugin.ca.remove(plugin.buttons[key]);
                            plugin.buttons[key] = null;
                        }
                    });
                } else plugin.buttons = [];

                // Add buttons based on server response
                BUTTONS.forEach((key) => {
                    var action = plugin.ca.getActionDetails(key);
                    if (action) {
                        // hide dropdown action
                        plugin.ca.setActionHidden(action.__type, action.__key, true);

                        // add button
                        plugin.buttons[key] = plugin.ca.add(action.__type, action.label);
                        plugin.ca.setTitle(plugin.buttons[key], action.title);
                        plugin.ca.addTapListener(plugin.buttons[key], buttonEventCallback);
                    }
                });

                function buttonEventCallback(event) {
                    var button_key = event.type.substring(0, event.type.indexOf('-tap'));
                    var button_action = null;
                    var button_index;
                    for (var k in plugin.buttons) {
                        if (plugin.buttons[k] === button_key) {
                            button_action = plugin.ca.getActionDetails(k);
                            button_index = k;
                            break;
                        }
                    }
                    if (button_action) {
                        const buttonEl = this.$$(`[data-action-key="${button_key}"]`);
                        buttonEl.setAttribute('loading', true);
                        buttonEl.disabled = true;
                        plugin.restApi().post(button_action.__url, {})
                            .then((ok_resp) => {
                                buttonEl.removeAttribute('loading');
                                buttonEl.disabled = false;
                                window.location.reload(true);
                            }).catch((failed_resp) => {
                                buttonEl.removeAttribute('loading');
                                buttonEl.disabled = false;
                                this.fire('show-alert', {message: 'FAILED: ' + failed_resp});
                            });
                    } else console.log('unexpected error: no action');
                }
            });
        });
    </script>
</dom-module>