-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable-buttons-on-click.js
116 lines (101 loc) · 3.93 KB
/
disable-buttons-on-click.js
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
// Source: https://github.com/atensoftware/disable-buttons-on-click
var DisableButtonsOnClick = DisableButtonsOnClick || {};
DisableButtonsOnClick.utils = {
// Define the wait image URL
DISABLE_BUTTONS_WAIT_IMAGE_URL: "https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/clock.svg",
// Attach event listeners to each button on the page that matches the selector
InitializeDisableButtons: function (selectors) {
if (selectors == null) {
selectors = 'button.DisableOnClick';
}
// Get all buttons
var buttonNodeList = document.querySelectorAll(selectors);
// Loop through each button
buttonNodeList.forEach(buttonNode => {
// Do not attach a click handler to reset buttons.
// These can be disabled, but do not disable anything
if (buttonNode.tagName === 'BUTTON' && buttonNode.type === 'reset')
{
return;
}
// Add onclick event listener
buttonNode.addEventListener('click', function (event) {
DisableButtonsOnClick.utils.DisableButtonsOnPage(selectors, event);
});
});
// Preload the background image to avoid rendering delay
if (buttonNodeList.length > 0) {
// Create a new Image object
var img = new Image();
// Set the src attribute to the image URL
img.src = DisableButtonsOnClick.utils.DISABLE_BUTTONS_WAIT_IMAGE_URL;
}
},
// Handle the button click by marking all the buttons matching the selector as disabled,
// and showing a wait icon on the clicked button.
DisableButtonsOnPage: function (selectors, event) {
// Get all buttons
var buttonNodeList = document.querySelectorAll(selectors, event);
// Get element to which the event handler is attached (always a button).
// NOTE: This is not necessarily the same as the 'clicked' element,
// which could be a child element of the button.
let clickedButton = event.currentTarget;
// If a submit button was clicked, and the form is not valid, do nothing
// except report the validity
if (clickedButton.tagName === 'BUTTON' && clickedButton.type === 'submit')
{
let formToSubmit = clickedButton.form;
// Return if the form is invalid
if (formToSubmit.checkValidity() == false)
{
// Report validity if supported
if(typeof formToSubmit.reportValidity === 'function')
{
formToSubmit.reportValidity();
}
return;
}
}
// Loop through each button
buttonNodeList.forEach(buttonNode => {
// Disable the button
buttonNode.disabled = true;
// Set disabled class
buttonNode.classList.add("btn-disabled");
// Only the clicked button gets the wait icon
if (clickedButton == buttonNode)
{
// Wrap button contents in a span
buttonNode.innerHTML = '<span style="opacity: 0.4;">' + buttonNode.innerHTML + '</span>';
// Display the background spinner image
buttonNode.style.backgroundImage = "url('" + DisableButtonsOnClick.utils.DISABLE_BUTTONS_WAIT_IMAGE_URL + "')";
buttonNode.style.backgroundSize = "auto 90%";
buttonNode.style.backgroundRepeat = "no-repeat";
buttonNode.style.backgroundPosition = "center";
}
});
// Since the submit buttons are disabled, they will not trigger a form submit
// So manually submit the form instead if it is the event target
if (clickedButton.tagName === 'BUTTON' && clickedButton.type === 'submit')
{
let formToSubmit = clickedButton.form;
// Submit the form if it is valid, support 'required' attributes
if (formToSubmit.checkValidity() == true)
{
// If the disabled button has a name/value, copy it into
// a hidden field so it gets submitted with the form
// This is not needed for any other buttons of any type
// with a name assigned.
if(clickedButton.getAttribute("name"))
{
const hiddenInput = document.createElement("input");
hiddenInput.type = "hidden";
hiddenInput.name = clickedButton.getAttribute("name");
hiddenInput.value = clickedButton.getAttribute("value");
formToSubmit.appendChild(hiddenInput);
}
formToSubmit.submit();
}
}
}
};