|
36 | 36 | xhr.onload = callback; |
37 | 37 |
|
38 | 38 | xhr.onerror = function() { |
39 | | - console.error('Error while communicating with GitLab'); |
| 39 | + alert('Error while communicating with GitLab.'); |
40 | 40 | }; |
41 | 41 |
|
42 | 42 | xhr.open(method, this.createEndpointUrl(endpoint, queryStringParameters)); |
|
62 | 62 |
|
63 | 63 | class ContentScript { |
64 | 64 | /** |
65 | | - * Initialize the content script of the extension which is executed in the context of the page. |
| 65 | + * The content script of the extension which is executed in the context of the page. |
66 | 66 | */ |
67 | 67 | constructor() { |
68 | 68 | this.currentProjectId = this.getCurrentProjectId(); |
|
87 | 87 | this.baseApiUrl = this.baseUrl + '/api/v4/'; |
88 | 88 | this.apiClient = new GitLabApiClient(this.baseApiUrl); |
89 | 89 |
|
90 | | - console.debug('Current project ID:', this.currentProjectId); |
91 | | - console.debug('Base project URL:', this.baseProjectUrl); |
92 | | - console.debug('GitLab base URL:', this.baseUrl); |
93 | | - console.debug('GitLab API base URL:', this.baseApiUrl); |
94 | | - |
95 | 90 | let currentMergeRequestIds = this.getCurrentMergeRequestIdsAndSetUuidDataAttributes(); |
| 91 | + let preferencesManager = new globals.Gmrle.PreferencesManager(); |
96 | 92 |
|
97 | | - console.debug('Current merge requests IDs:', currentMergeRequestIds); |
| 93 | + let self = this; |
98 | 94 |
|
99 | | - this.fetchMergeRequestsDetailsThenUpdateUI(currentMergeRequestIds); |
| 95 | + preferencesManager.getAll(function(preferences) { |
| 96 | + self.preferences = preferences; |
| 97 | + self.fetchMergeRequestsDetailsThenUpdateUI(currentMergeRequestIds); |
| 98 | + }); |
100 | 99 | } |
101 | 100 |
|
102 | 101 | /** |
|
149 | 148 | if (this.status == 200) { |
150 | 149 | self.removeExistingTargetBranchNodes(); |
151 | 150 | self.updateMergeRequestsNodes(this.response); |
| 151 | + self.attachClickEventToCopyBranchNameButtons(); |
152 | 152 | } else { |
| 153 | + alert('Got error from GitLab, check console for more information.'); |
| 154 | + |
153 | 155 | console.error('Got error from GitLab:', this.status, this.response); |
154 | 156 | } |
155 | 157 | }, |
|
191 | 193 | let infoDiv = document |
192 | 194 | .querySelector('.mr-list .merge-request[data-iid="' + mergeRequest.iid + '"] .issuable-main-info'); |
193 | 195 |
|
194 | | - let html = '<div class="issuable-info"><span class="project-ref-path has-tooltip" title="Source branch">' + |
195 | | - '<a class="ref-name" href="' + self.baseProjectUrl + '/-/commits/' + mergeRequest.source_branch + '">' + mergeRequest.source_branch + '</a>' + |
196 | | - '</span>' + |
197 | | - ' <i class="fa fa-long-arrow-right" aria-hidden="true"></i> ' + |
198 | | - '<span class="project-ref-path has-tooltip" title="Target branch">' + |
199 | | - '<a class="ref-name" href="' + self.baseProjectUrl + '/-/commits/' + mergeRequest.target_branch + '">' + mergeRequest.target_branch + '</a>' + |
200 | | - '</span></div>'; |
| 196 | + let html = '<div class="issuable-info">' + |
| 197 | + '<span class="project-ref-path has-tooltip" title="Source branch">' + |
| 198 | + '<a class="ref-name" href="' + self.baseProjectUrl + '/-/commits/' + mergeRequest.source_branch + '">' + mergeRequest.source_branch + '</a>' + |
| 199 | + '</span>'; |
| 200 | + |
| 201 | + if (self.preferences.enable_buttons_to_copy_source_and_target_branches_name) { |
| 202 | + html += ' <button class="btn btn-secondary btn-md btn-default btn-transparent btn-clipboard has-tooltip gmrle-copy-branch-name" title="Copy branch name" data-branch-name="' + mergeRequest.source_branch + '">' + |
| 203 | + '<i class="fa fa-clipboard" aria-hidden="true"></i>' + |
| 204 | + '</button>' |
| 205 | + } |
| 206 | + |
| 207 | + html += ' <i class="fa fa-long-arrow-right" aria-hidden="true"></i> ' + |
| 208 | + '<span class="project-ref-path has-tooltip" title="Target branch">' + |
| 209 | + '<a class="ref-name" href="' + self.baseProjectUrl + '/-/commits/' + mergeRequest.target_branch + '">' + mergeRequest.target_branch + '</a>' + |
| 210 | + '</span>'; |
| 211 | + |
| 212 | + if (self.preferences.enable_buttons_to_copy_source_and_target_branches_name) { |
| 213 | + html += ' <button class="btn btn-secondary btn-md btn-default btn-transparent btn-clipboard has-tooltip gmrle-copy-branch-name" title="Copy branch name" data-branch-name="' + mergeRequest.target_branch + '">' + |
| 214 | + '<i class="fa fa-clipboard" aria-hidden="true"></i>' + |
| 215 | + '</button>'; |
| 216 | + } |
| 217 | + |
| 218 | + html += '</div>'; |
201 | 219 |
|
202 | 220 | self.parseHtmlAndAppendChild( |
203 | 221 | infoDiv, |
204 | 222 | html |
205 | 223 | ); |
206 | 224 | }); |
207 | 225 | } |
| 226 | + |
| 227 | + /** |
| 228 | + * Attach a click event to all buttons inserted by the extension allowing to copy the source and target |
| 229 | + * branches name (if feature is enabled by the user). |
| 230 | + */ |
| 231 | + attachClickEventToCopyBranchNameButtons() { |
| 232 | + if (!this.preferences.enable_buttons_to_copy_source_and_target_branches_name) { |
| 233 | + return |
| 234 | + } |
| 235 | + |
| 236 | + document.querySelectorAll('button.gmrle-copy-branch-name').forEach(function(el) { |
| 237 | + el.addEventListener('click', function(e) { |
| 238 | + e.preventDefault(); |
| 239 | + |
| 240 | + navigator.clipboard.writeText(el.dataset.branchName).then(function() { |
| 241 | + // Do nothing if copy was successful. |
| 242 | + }, function() { |
| 243 | + alert('Unable to copy branch name.'); |
| 244 | + }); |
| 245 | + }); |
| 246 | + }); |
| 247 | + } |
208 | 248 | } |
209 | 249 |
|
210 | 250 | let cs = new ContentScript(); |
|
0 commit comments