-
Notifications
You must be signed in to change notification settings - Fork 334
/
character-count.js
172 lines (145 loc) · 5.47 KB
/
character-count.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import '../../vendor/polyfills/Element/prototype/classList'
function CharacterCount ($module) {
this.$module = $module
this.$textarea = $module.querySelector('.govuk-js-character-count')
if (this.$textarea) {
this.$countMessage = $module.querySelector('[id=' + this.$textarea.id + '-info]')
}
}
CharacterCount.prototype.defaults = {
characterCountAttribute: 'data-maxlength',
wordCountAttribute: 'data-maxwords'
}
// Initialize component
CharacterCount.prototype.init = function () {
// Check for module
var $module = this.$module
var $textarea = this.$textarea
var $countMessage = this.$countMessage
if (!$textarea || !$countMessage) {
return
}
// We move count message right after the field
// Kept for backwards compatibility
$textarea.insertAdjacentElement('afterend', $countMessage)
// Read options set using dataset ('data-' values)
this.options = this.getDataset($module)
// Determine the limit attribute (characters or words)
var countAttribute = this.defaults.characterCountAttribute
if (this.options.maxwords) {
countAttribute = this.defaults.wordCountAttribute
}
// Save the element limit
this.maxLength = $module.getAttribute(countAttribute)
// Check for limit
if (!this.maxLength) {
return
}
// Remove hard limit if set
$module.removeAttribute('maxlength')
// Bind event changes to the textarea
var boundChangeEvents = this.bindChangeEvents.bind(this)
boundChangeEvents()
// Update count message
var boundUpdateCountMessage = this.updateCountMessage.bind(this)
boundUpdateCountMessage()
}
// Read data attributes
CharacterCount.prototype.getDataset = function (element) {
var dataset = {}
var attributes = element.attributes
if (attributes) {
for (var i = 0; i < attributes.length; i++) {
var attribute = attributes[i]
var match = attribute.name.match(/^data-(.+)/)
if (match) {
dataset[match[1]] = attribute.value
}
}
}
return dataset
}
// Counts characters or words in text
CharacterCount.prototype.count = function (text) {
var length
if (this.options.maxwords) {
var tokens = text.match(/\S+/g) || [] // Matches consecutive non-whitespace chars
length = tokens.length
} else {
length = text.length
}
return length
}
// Bind input propertychange to the elements and update based on the change
CharacterCount.prototype.bindChangeEvents = function () {
var $textarea = this.$textarea
$textarea.addEventListener('keyup', this.checkIfValueChanged.bind(this))
// Bind focus/blur events to start/stop polling
$textarea.addEventListener('focus', this.handleFocus.bind(this))
$textarea.addEventListener('blur', this.handleBlur.bind(this))
}
// Speech recognition software such as Dragon NaturallySpeaking will modify the
// fields by directly changing its `value`. These changes don't trigger events
// in JavaScript, so we need to poll to handle when and if they occur.
CharacterCount.prototype.checkIfValueChanged = function () {
if (!this.$textarea.oldValue) this.$textarea.oldValue = ''
if (this.$textarea.value !== this.$textarea.oldValue) {
this.$textarea.oldValue = this.$textarea.value
var boundUpdateCountMessage = this.updateCountMessage.bind(this)
boundUpdateCountMessage()
}
}
// Update message box
CharacterCount.prototype.updateCountMessage = function () {
var countElement = this.$textarea
var options = this.options
var countMessage = this.$countMessage
// Determine the remaining number of characters/words
var currentLength = this.count(countElement.value)
var maxLength = this.maxLength
var remainingNumber = maxLength - currentLength
// Set threshold if presented in options
var thresholdPercent = options.threshold ? options.threshold : 0
var thresholdValue = maxLength * thresholdPercent / 100
if (thresholdValue > currentLength) {
countMessage.classList.add('govuk-character-count__message--disabled')
// Ensure threshold is hidden for users of assistive technologies
countMessage.setAttribute('aria-hidden', true)
} else {
countMessage.classList.remove('govuk-character-count__message--disabled')
// Ensure threshold is visible for users of assistive technologies
countMessage.removeAttribute('aria-hidden')
}
// Update styles
if (remainingNumber < 0) {
countElement.classList.add('govuk-textarea--error')
countMessage.classList.remove('govuk-hint')
countMessage.classList.add('govuk-error-message')
} else {
countElement.classList.remove('govuk-textarea--error')
countMessage.classList.remove('govuk-error-message')
countMessage.classList.add('govuk-hint')
}
// Update message
var charVerb = 'remaining'
var charNoun = 'character'
var displayNumber = remainingNumber
if (options.maxwords) {
charNoun = 'word'
}
charNoun = charNoun + ((remainingNumber === -1 || remainingNumber === 1) ? '' : 's')
charVerb = (remainingNumber < 0) ? 'too many' : 'remaining'
displayNumber = Math.abs(remainingNumber)
countMessage.innerHTML = 'You have ' + displayNumber + ' ' + charNoun + ' ' + charVerb
}
CharacterCount.prototype.handleFocus = function () {
// Check if value changed on focus
this.valueChecker = setInterval(this.checkIfValueChanged.bind(this), 1000)
}
CharacterCount.prototype.handleBlur = function () {
// Cancel value checking on blur
clearInterval(this.valueChecker)
}
export default CharacterCount