2017-10-09 05:04:13 -04:00
|
|
|
export default class AjaxLoadingSpinner {
|
2016-10-09 10:17:14 -04:00
|
|
|
static init() {
|
|
|
|
const $elements = $('.js-ajax-loading-spinner');
|
|
|
|
|
|
|
|
$elements.on('ajax:beforeSend', AjaxLoadingSpinner.ajaxBeforeSend);
|
|
|
|
$elements.on('ajax:complete', AjaxLoadingSpinner.ajaxComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ajaxBeforeSend(e) {
|
|
|
|
e.target.setAttribute('disabled', '');
|
|
|
|
const iconElement = e.target.querySelector('i');
|
|
|
|
// get first fa- icon
|
2017-08-04 04:07:05 -04:00
|
|
|
const originalIcon = iconElement.className.match(/(fa-)([^\s]+)/g)[0];
|
2016-10-09 10:17:14 -04:00
|
|
|
iconElement.dataset.icon = originalIcon;
|
|
|
|
AjaxLoadingSpinner.toggleLoadingIcon(iconElement);
|
|
|
|
$(e.target).off('ajax:beforeSend', AjaxLoadingSpinner.ajaxBeforeSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ajaxComplete(e) {
|
|
|
|
e.target.removeAttribute('disabled');
|
|
|
|
const iconElement = e.target.querySelector('i');
|
|
|
|
AjaxLoadingSpinner.toggleLoadingIcon(iconElement);
|
|
|
|
$(e.target).off('ajax:complete', AjaxLoadingSpinner.ajaxComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
static toggleLoadingIcon(iconElement) {
|
|
|
|
const classList = iconElement.classList;
|
|
|
|
classList.toggle(iconElement.dataset.icon);
|
|
|
|
classList.toggle('fa-spinner');
|
|
|
|
classList.toggle('fa-spin');
|
|
|
|
}
|
|
|
|
}
|