aboutsummaryrefslogtreecommitdiffstats
path: root/qt-gerrit-ui-plugin/qt-gerrit-ui-plugin.html
blob: 3bd29f68aa5eb45454c440b5310e8540730343b7 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// 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' : {
                header: 'Abandon the change?',
                action_name: 'abandon'
            },
            'gerrit-plugin-qt-workflow~defer' : {
                header: 'Defer the change?',
                action_name: 'defer'
            },
            'gerrit-plugin-qt-workflow~reopen' : {
                header: 'Reopen the change?',
                action_name: 'reopen'
            },
            'gerrit-plugin-qt-workflow~stage' : {
                header: 'Submit to staging?',
                action_name: 'stage'
            },
            'gerrit-plugin-qt-workflow~unstage' : {
                header: 'Unstage the change?',
                action_name: 'unstage'
            }
        };

        Gerrit.install(plugin => {

            plugin.custom_popup = null;
            plugin.custom_popup_promise = null;
            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) {
                    for (var key in BUTTONS) {
                        if (BUTTONS.hasOwnProperty(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
                for (var key in BUTTONS) {
                    if (BUTTONS.hasOwnProperty(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) {
                        plugin.popup('qt-gerrit-ui-confirm-dialog').then( (param) => {
                            plugin.custom_popup_promise = param;
                            plugin.custom_popup.set('header', BUTTONS[button_index].header);
                            plugin.custom_popup.set('subject', changeInfo.subject);
                            plugin.custom_popup.set('action_name', BUTTONS[button_index].action_name);
                            plugin.custom_popup.set('api_url', button_action.__url);
                        }).catch( (param) => { console.log('unexpected error: promise failed');});
                    } else console.log('unexpected error: no action');
                }
            });
        });
    </script>
</dom-module>

<dom-module id="qt-gerrit-ui-confirm-dialog">
    <template>
        <style include="shared-styles">
            #dialog {
                min-width: 40em;
            }
            p {
                margin-bottom: 1em;
            }
            @media screen and (max-width: 50em) {
                #dialog {
                      min-width: inherit;
                      width: 100%;
                }
            }
        </style>
        <gr-dialog
            id="dialog"
            confirm-label="Continue"
            confirm-on-enter
            on-cancel="_handleCancelTap"
            on-confirm="_handleConfirmTap">
            <div class="header" slot="header">[[header]]</div>
            <div class="main" slot="main">
                <p>Ready to [[action_name]] &ldquo;<strong>[[subject]]</strong>&rdquo;?</p>
                <p class="main" style="color: red;font-weight: bold;">[[errorMessage]]</p>
            </div>
        </gr-dialog>
    </template>
    <script>
        'use strict';

        var qtgerrituiconfirmdialog = Polymer({
            is: 'qt-gerrit-ui-confirm-dialog',

            properties: {
                header: {
                    type: String,
                    value: '...wait...'
                },
                action_name: {
                    type: String,
                    value: ''
                },
                api_url: {
                    type: String,
                    value: ''
                },
                subject: {
                    type: String,
                    value: ''
                },
                errorMessage: {
                    type: String,
                    value: ''
                }
            },

            attached: function() {
                this.plugin.custom_popup = this;
            },

            resetFocus(e) {
                this.$.dialog.resetFocus();
            },

            _handleConfirmTap(e) {
                this.$.dialog.disabled = true;
                e.preventDefault();
                this.plugin.restApi().post(this.get('api_url'), {})
                    .then((ok_resp) => {
                        this.$.dialog.disabled = false;
                        this.plugin.custom_popup_promise.close();
                        this.plugin.custom_popup_promise = null;
                        window.location.reload(true);
                    }).catch((failed_resp) => {
                        this.$.dialog.disabled = false;
                        this.set('errorMessage', 'FAILED: ' + failed_resp);
                    });
            },

            _handleCancelTap(e) {
                e.preventDefault();
                this.plugin.custom_popup_promise.close();
                this.plugin.custom_popup_promise = null;
            },
        });
    </script>
</dom-module>