From: Kohei Yoshino Date: Wed, 3 Jul 2019 22:38:20 +0000 (-0400) Subject: Bug 1380437 - Implement REST API utility methods in global.js to replace bugzilla_aja... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=beecadc209df2b2daf39f95271e8fd19aac04083;p=thirdparty%2Fbugzilla.git Bug 1380437 - Implement REST API utility methods in global.js to replace bugzilla_ajax(), eliminate JSON-RPC usage --- diff --git a/extensions/Bitly/web/js/bitly.js b/extensions/Bitly/web/js/bitly.js index 1bc39d411..1de0d0421 100644 --- a/extensions/Bitly/web/js/bitly.js +++ b/extensions/Bitly/web/js/bitly.js @@ -9,7 +9,7 @@ $(function() { 'use strict'; var popup, urls = []; - function execute() { + async function execute() { var type = $('#bitly-type').val(); if (urls[type]) { @@ -18,17 +18,15 @@ $(function() { } $('#bitly-url').val(''); - var request = `${BUGZILLA.config.basepath}rest/bitly/${type}?` + - `url=${encodeURIComponent($('#bitly-shorten').data('url'))}&` + - `Bugzilla_api_token=${encodeURIComponent(BUGZILLA.api_token)}`; - $.ajax(request) - .done(function(data) { - urls[type] = data.url; - $('#bitly-url').val(urls[type]).select().focus(); - }) - .fail(function(data) { - $('#bitly-url').val(data.responseJSON.message); - }); + + try { + const { url } = await Bugzilla.API.get(`bitly/${type}`, { url: $('#bitly-shorten').data('url') }); + + urls[type] = url; + $('#bitly-url').val(urls[type]).select().focus(); + } catch ({ message }) { + $('#bitly-url').val(message); + } } $('#bitly-shorten') diff --git a/extensions/BugModal/template/en/default/bug_modal/edit.html.tmpl b/extensions/BugModal/template/en/default/bug_modal/edit.html.tmpl index 058599c68..609351dc8 100644 --- a/extensions/BugModal/template/en/default/bug_modal/edit.html.tmpl +++ b/extensions/BugModal/template/en/default/bug_modal/edit.html.tmpl @@ -162,7 +162,7 @@ [%# === header === %] - + diff --git a/extensions/BugModal/web/bug_modal.css b/extensions/BugModal/web/bug_modal.css index 3fd0c4b98..f4a9710bc 100644 --- a/extensions/BugModal/web/bug_modal.css +++ b/extensions/BugModal/web/bug_modal.css @@ -970,7 +970,7 @@ h3.change-name a { vertical-align: top; } -#xhr-error { +#io-error { margin: 5px 0; border-radius: 2px; border: 1px solid var(--error-message-foreground-color); diff --git a/extensions/BugModal/web/bug_modal.js b/extensions/BugModal/web/bug_modal.js index c8ca6fe59..c819d7aa2 100644 --- a/extensions/BugModal/web/bug_modal.js +++ b/extensions/BugModal/web/bug_modal.js @@ -266,50 +266,50 @@ $(function() { ); } - function ccListUpdate() { - bugzilla_ajax( - { - url: `${BUGZILLA.config.basepath}rest/bug_modal/cc/${BUGZILLA.bug_id}` - }, - function(data) { - $('#cc-list').html(data.html); - $('#cc-latch').data('fetched', true); - $('#cc-list .cc-user').hover( - function() { - $('#ccr-' + $(this).data('n')).css('visibility', 'visible'); - }, - function() { - $('#ccr-' + $(this).data('n')).css('visibility', 'hidden'); + async function ccListUpdate() { + try { + const { html } = await Bugzilla.API.get(`bug_modal/cc/${BUGZILLA.bug_id}`); + + $('#io-error').empty().hide(); + $('#cc-list').html(html); + $('#cc-latch').data('fetched', true); + $('#cc-list .cc-user').hover( + function() { + $('#ccr-' + $(this).data('n')).css('visibility', 'visible'); + }, + function() { + $('#ccr-' + $(this).data('n')).css('visibility', 'hidden'); + } + ); + $('#cc-list .show_usermenu').click(function() { + const $this = $(this); + return show_usermenu($this.data('user-id'), $this.data('user-email'), $this.data('show-edit'), + $this.data('hide-profile')); + }); + $('#cc-list .cc-remove') + .click(function(event) { + event.preventDefault(); + $('#top-save-btn').show(); + var n = $(this).data('n'); + var ccu = $('#ccu-' + n); + if (ccu.hasClass('cc-removed')) { + ccu.removeClass('cc-removed'); + $('#cc-' + n).remove(); + } + else { + $('#removecc').val('on'); + ccu.addClass('cc-removed'); + $('').attr({ + type: 'hidden', + id: 'cc-' + n, + value: $('#ccr-' + n).data('login'), + name: 'cc' + }).appendTo('#changeform'); } - ); - $('#cc-list .show_usermenu').click(function() { - const $this = $(this); - return show_usermenu($this.data('user-id'), $this.data('user-email'), $this.data('show-edit'), - $this.data('hide-profile')); }); - $('#cc-list .cc-remove') - .click(function(event) { - event.preventDefault(); - $('#top-save-btn').show(); - var n = $(this).data('n'); - var ccu = $('#ccu-' + n); - if (ccu.hasClass('cc-removed')) { - ccu.removeClass('cc-removed'); - $('#cc-' + n).remove(); - } - else { - $('#removecc').val('on'); - ccu.addClass('cc-removed'); - $('').attr({ - type: 'hidden', - id: 'cc-' + n, - value: $('#ccr-' + n).data('login'), - name: 'cc' - }).appendTo('#changeform'); - } - }); - } - ); + } catch ({ message }) { + $('#io-error').html(message).show('fast'); + } } if (BUGZILLA.user.id) { @@ -510,7 +510,7 @@ $(function() { // edit/save mode button $('#mode-btn') - .click(function(event) { + .click(async event => { event.preventDefault(); // hide buttons, old error messages @@ -535,119 +535,119 @@ $(function() { $('#mode-btn').prop('disabled', true); // load the missing select data - bugzilla_ajax( - { - url: `${BUGZILLA.config.basepath}rest/bug_modal/edit/${BUGZILLA.bug_id}` - }, - function(data) { - $('#mode-btn').hide(); - - // populate select menus - Object.entries(data.options).forEach(([key, value]) => { - const $select = document.querySelector(`#${key}`); - if (!$select) return; - // It can be radio-button-like UI - const use_buttons = $select.matches('.buttons.toggle'); - const is_required = $select.matches('[aria-required="true"]'); - const selected = use_buttons ? $select.querySelector('input').value : $select.value; - $select.innerHTML = ''; - value.forEach(({ name }) => { - if (is_required && name === '--') { - return; - } - if (use_buttons) { - $select.insertAdjacentHTML('beforeend', ` -
- - -
- `); - } else { - $select.insertAdjacentHTML('beforeend', ` - - `); - } - }); - if ($select.matches('[multiple]') && value.length < 5) { - $select.size = value.length; - } - }); + try { + const data = await Bugzilla.API.get(`bug_modal/edit/${BUGZILLA.bug_id}`); - // build our product description hash - $.each(data.options.product, function() { - products[this.name] = this.description; - }); + $('#io-error').empty().hide(); + $('#mode-btn').hide(); - // keywords is a multi-value autocomplete - keywords = data.keywords; - $('#keywords') - .devbridgeAutocomplete({ - appendTo: $('#main-inner'), - forceFixPosition: true, - lookup: function(query, done) { - query = query.toLowerCase(); - var matchStart = - $.grep(keywords, function(keyword) { - return keyword.toLowerCase().substr(0, query.length) === query; - }); - var matchSub = - $.grep(keywords, function(keyword) { - return keyword.toLowerCase().indexOf(query) !== -1 && - $.inArray(keyword, matchStart) === -1; - }); - var suggestions = - $.map($.merge(matchStart, matchSub), function(suggestion) { - return { value: suggestion }; - }); - done({ suggestions: suggestions }); - }, - tabDisabled: true, - delimiter: /,\s*/, - minChars: 0, - autoSelectFirst: false, - triggerSelectOnValidInput: false, - formatResult: function(suggestion, currentValue) { - // disable wrapping of matched substring - return suggestion.value.htmlEncode(); - }, - onSearchStart: function(params) { - var that = $(this); - // adding spaces shouldn't initiate a new search - var parts = that.val().split(/,\s*/); - var query = parts[parts.length - 1]; - return query === $.trim(query); - }, - onSelect: function() { - this.value = this.value + ', '; - this.focus(); - } - }) - .addClass('bz_autocomplete'); - - $('#cancel-btn').prop('disabled', false); - $('#top-save-btn').show(); - $('#cancel-btn').show(); - $('#commit-btn').show(); - }, - function() { - $('#mode-btn-readonly').show(); - $('#mode-btn-loading').hide(); - $('#mode-btn').prop('disabled', false); - $('#mode-btn').show(); - $('#cancel-btn').hide(); - $('#commit-btn').hide(); - - $('.edit-show').hide(); - $('.edit-hide').show(); + // populate select menus + for (const [key, value] of Object.entries(data.options)) { + const $select = document.querySelector(`#${key}`); + if (!$select) { + continue; + } + // It can be radio-button-like UI + const use_buttons = $select.matches('.buttons.toggle'); + const is_required = $select.matches('[aria-required="true"]'); + const selected = use_buttons ? $select.querySelector('input').value : $select.value; + $select.innerHTML = ''; + for (const { name } of value) { + if (is_required && name === '--') { + continue; + } + if (use_buttons) { + $select.insertAdjacentHTML('beforeend', ` +
+ + +
+ `); + } else { + $select.insertAdjacentHTML('beforeend', ` + + `); + } + } + if ($select.matches('[multiple]') && value.length < 5) { + $select.size = value.length; + } } - ); + + // build our product description hash + for (const { name, description } of data.options.product) { + products[name] = description; + } + + // keywords is a multi-value autocomplete + keywords = data.keywords; + $('#keywords') + .devbridgeAutocomplete({ + appendTo: $('#main-inner'), + forceFixPosition: true, + lookup: function(query, done) { + query = query.toLowerCase(); + var matchStart = + $.grep(keywords, function(keyword) { + return keyword.toLowerCase().substr(0, query.length) === query; + }); + var matchSub = + $.grep(keywords, function(keyword) { + return keyword.toLowerCase().indexOf(query) !== -1 && + $.inArray(keyword, matchStart) === -1; + }); + var suggestions = + $.map($.merge(matchStart, matchSub), function(suggestion) { + return { value: suggestion }; + }); + done({ suggestions }); + }, + tabDisabled: true, + delimiter: /,\s*/, + minChars: 0, + autoSelectFirst: false, + triggerSelectOnValidInput: false, + formatResult: function(suggestion, currentValue) { + // disable wrapping of matched substring + return suggestion.value.htmlEncode(); + }, + onSearchStart: function(params) { + var that = $(this); + // adding spaces shouldn't initiate a new search + var parts = that.val().split(/,\s*/); + var query = parts[parts.length - 1]; + return query === $.trim(query); + }, + onSelect: function() { + this.value = this.value + ', '; + this.focus(); + } + }) + .addClass('bz_autocomplete'); + + $('#cancel-btn').prop('disabled', false); + $('#top-save-btn').show(); + $('#cancel-btn').show(); + $('#commit-btn').show(); + } catch ({ message }) { + $('#io-error').html(message).show('fast'); + $('#mode-btn-readonly').show(); + $('#mode-btn-loading').hide(); + $('#mode-btn').prop('disabled', false); + $('#mode-btn').show(); + $('#cancel-btn').hide(); + $('#commit-btn').hide(); + + $('.edit-show').hide(); + $('.edit-hide').show(); + } }); $('#mode-btn').prop('disabled', false); @@ -675,7 +675,7 @@ $(function() { // cc toggle (follow/stop following) $('#cc-btn') - .click(function(event) { + .click(async event => { event.preventDefault(); var is_cced = $(event.target).data('is-cced') == '1'; @@ -731,36 +731,34 @@ $(function() { $('#add-self-cc').attr('disabled', false); } - bugzilla_ajax( - { - url: `${BUGZILLA.config.basepath}rest/bug/${BUGZILLA.bug_id}`, - type: 'PUT', - data: JSON.stringify({ cc: cc_change }) - }, - function(data) { - $('#cc-btn').prop('disabled', false); - if (!(data.bugs[0].changes && data.bugs[0].changes.cc)) - return; - if (data.bugs[0].changes.cc.added == BUGZILLA.user.login) { - $('#cc-btn') - .text('Stop Following') - .data('is-cced', '1'); - } - else if (data.bugs[0].changes.cc.removed == BUGZILLA.user.login) { - $('#cc-btn') - .text('Follow') - .data('is-cced', '0'); - } - if ($('#cc-latch').data('expanded')) - ccListUpdate(); - }, - function(message) { - $('#cc-btn').prop('disabled', false); - if ($('#cc-latch').data('expanded')) - ccListUpdate(); + try { + const { bugs } = await Bugzilla.API.put(`bug/${BUGZILLA.bug_id}`, { cc: cc_change }); + const { changes } = bugs[0]; + + $('#io-error').empty().hide(); + $('#cc-btn').prop('disabled', false); + + if (!(changes && changes.cc)) { + return; } - ); + if (changes.cc.added == BUGZILLA.user.login) { + $('#cc-btn').text('Stop Following').data('is-cced', '1'); + } else if (changes.cc.removed == BUGZILLA.user.login) { + $('#cc-btn').text('Follow').data('is-cced', '0'); + } + + if ($('#cc-latch').data('expanded')) { + ccListUpdate(); + } + } catch ({ message }) { + $('#io-error').html(message).show('fast'); + $('#cc-btn').prop('disabled', false); + + if ($('#cc-latch').data('expanded')) { + ccListUpdate(); + } + } }); // cancel button, reset the ui back to read-only state @@ -903,18 +901,16 @@ $(function() { var prefix = "(In reply to " + comment_author + " from comment #" + comment_no + ")\n"; var reply_text = ""; - var quoteMarkdown = function($comment) { + var quoteMarkdown = async $comment => { const uid = $comment.data('comment-id'); - bugzilla_ajax( - { - url: `rest/bug/comment/${uid}`, - }, - (data) => { - const quoted = data['comments'][uid]['text'].replace(/\n/g, "\n> "); - reply_text = `${prefix}> ${quoted}\n\n`; - populateNewComment(); - } - ); + + try { + const { comments } = await Bugzilla.API.get(`bug/comment/${uid}`, { include_fields: 'text' }); + const quoted = comments[uid]['text'].replace(/\n/g, '\n> '); + + reply_text = `${prefix}> ${quoted}\n\n`; + populateNewComment(); + } catch (ex) {} } var populateNewComment = function() { @@ -1141,7 +1137,7 @@ $(function() { $(this).data('default', $(this).val()); }); $('#product') - .change(function(event) { + .change(async event => { $('#product-throbber').show(); $('#component, #version, #target_milestone').attr('disabled', true); @@ -1154,74 +1150,72 @@ $(function() { } }); - bugzilla_ajax( - { - url: `${BUGZILLA.config.basepath}rest/bug_modal/new_product/${BUGZILLA.bug_id}?` + - `product=${encodeURIComponent($('#product').val())}` - }, - function(data) { - $('#product-throbber').hide(); - $('#component, #version, #target_milestone').attr('disabled', false); - var is_default = $('#product').val() == $('#product').data('default'); - - // populate selects - $.each(data, function(key, value) { - if (key == 'groups') return; - var el = $('#' + key); - if (!el) return; - el.empty(); - var selected = el.data('preselect'); - $(value).each(function(i, v) { - el.append($('