-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathbutton.js
64 lines (56 loc) · 2.09 KB
/
button.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
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Function/prototype/bind'
var KEY_SPACE = 32
var DEBOUNCE_TIMEOUT_IN_SECONDS = 1
function Button ($module) {
this.$module = $module
this.debounceFormSubmitTimer = null
}
/**
* JavaScript 'shim' to trigger the click event of element(s) when the space key is pressed.
*
* Created since some Assistive Technologies (for example some Screenreaders)
* will tell a user to press space on a 'button', so this functionality needs to be shimmed
* See https://github.com/alphagov/govuk_elements/pull/272#issuecomment-233028270
*
* @param {object} event event
*/
Button.prototype.handleKeyDown = function (event) {
// get the target element
var target = event.target
// if the element has a role='button' and the pressed key is a space, we'll simulate a click
if (target.getAttribute('role') === 'button' && event.keyCode === KEY_SPACE) {
event.preventDefault()
// trigger the target's click event
target.click()
}
}
/**
* If the click quickly succeeds a previous click then nothing will happen.
* This stops people accidentally causing multiple form submissions by
* double clicking buttons.
*/
Button.prototype.debounce = function (event) {
var target = event.target
// Check the button that is clicked on has the preventDoubleClick feature enabled
if (target.getAttribute('data-prevent-double-click') !== 'true') {
return
}
// If the timer is still running then we want to prevent the click from submitting the form
if (this.debounceFormSubmitTimer) {
event.preventDefault()
return false
}
this.debounceFormSubmitTimer = setTimeout(function () {
this.debounceFormSubmitTimer = null
}.bind(this), DEBOUNCE_TIMEOUT_IN_SECONDS * 1000)
}
/**
* Initialise an event listener for keydown at document level
* this will help listening for later inserted elements with a role="button"
*/
Button.prototype.init = function () {
this.$module.addEventListener('keydown', this.handleKeyDown)
this.$module.addEventListener('click', this.debounce)
}
export default Button