diff options
Diffstat (limited to 'app/assets/javascripts/branches')
-rw-r--r-- | app/assets/javascripts/branches/ajax_loading_spinner.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/assets/javascripts/branches/ajax_loading_spinner.js b/app/assets/javascripts/branches/ajax_loading_spinner.js new file mode 100644 index 00000000000..79f4f919f3d --- /dev/null +++ b/app/assets/javascripts/branches/ajax_loading_spinner.js @@ -0,0 +1,31 @@ +import $ from 'jquery'; + +export default class AjaxLoadingSpinner { + static init() { + const $elements = $('.js-ajax-loading-spinner'); + $elements.on('ajax:beforeSend', AjaxLoadingSpinner.ajaxBeforeSend); + } + + static ajaxBeforeSend(e) { + const button = e.target; + const newButton = document.createElement('button'); + newButton.classList.add('btn', 'btn-default', 'disabled', 'gl-button'); + newButton.setAttribute('disabled', 'disabled'); + + const spinner = document.createElement('span'); + spinner.classList.add('align-text-bottom', 'gl-spinner', 'gl-spinner-sm', 'gl-spinner-orange'); + newButton.appendChild(spinner); + + button.classList.add('hidden'); + button.parentNode.insertBefore(newButton, button.nextSibling); + + $(button).one('ajax:error', () => { + newButton.remove(); + button.classList.remove('hidden'); + }); + + $(button).one('ajax:success', () => { + $(button).off('ajax:beforeSend', AjaxLoadingSpinner.ajaxBeforeSend); + }); + } +} |