aboutsummaryrefslogtreecommitdiffstats
path: root/qt-gerrit-ui-plugin/qt-gerrit-ui-plugin.html
blob: f077159b406b19923aa24be12ee0a0cc9e88e678 (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
//
// 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.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> \
                                    #qt-header ul { \
                                        width: 100%; \
                                        margin: 0; \
                                        text-align: left; \
                                        padding: 0.5em 0 .1em 0; \
                                    } \
                                    #qt-header li { \
                                        display: inline; \
                                        padding: 0.3em; \
                                    } \
                                    #qt-header img { \
                                        vertical-align: top; \
                                    } \
                                  </style>';
                const html_str = '<div id="qt-header"> \
                                    <ul> \
                                      <li><img src="/static/logo_qt.png"/></li> \
                                      <li><img src="/static/logo_open_gov.png"/></li> \
                                    </ul> \
                                  </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 = 'Deferred';
                link_elem.href = '/q/status:deferred';
                ul_elem.insertBefore(li_elem, ul_elem.children[3]);
            });

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

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

                // 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) {
                e.preventDefault();
                this.plugin.restApi().post(this.get('api_url'), {})
                    .then((ok_resp) => {
                        this.plugin.custom_popup_promise.close();
                        this.plugin.custom_popup_promise = null;
                        window.location.reload(true);
                    }).catch((failed_resp) => {
                        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>