-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
223 lines (194 loc) · 7.72 KB
/
script.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* global fieldProperties, setAnswer, getPluginParameter, setMetaData, goToNextField */
var input = document.querySelector('#field')
var formGroup = document.querySelector('.form-group')
var controlMessage = document.querySelector('.control-message')
var formattedSpan = document.querySelector('#formatted')
var buttonContainer = document.querySelector('#buttons')
var warningContainer = document.querySelector('#warning')
var yesButton = document.querySelector('#yes')
var noButton = document.querySelector('#no')
var invalidBox = document.querySelector('.error-box')
var autoAdvance = getPluginParameter('autoadvance')
if (autoAdvance === 1) {
autoAdvance = true
} else {
autoAdvance = false
}
var fieldType = fieldProperties.FIELDTYPE
var appearance = fieldProperties.APPEARANCE
var altValues = []
var buttonsDisp = ''
var specialConstraint
invalidBox.style.display = 'none'
if (fieldType === 'integer') {
input.inputmode = 'numeric'
input.type = 'number'
specialConstraint = new RegExp('^-?[0-9]+$')
invalidBox.innerHTML = 'Invalid: Answer must be a valid integer.'
} else if (fieldType === 'decimal') {
input.inputmode = 'decimal'
input.type = 'number'
specialConstraint = new RegExp('^-?([0-9]+.?[0-9]*)|([0-9]*.?[0-9]+)$')
invalidBox.innerHTML = 'Invalid: Answer must be a valid decimal number.'
} else { // All that should be left is "text"
if (appearance.indexOf('numbers_phone') !== -1) {
input.inputmode = 'tel'
input.type = 'tel'
specialConstraint = new RegExp('^[0-9-+.#* ]+$')
invalidBox.innerHTML = 'Invalid: Answer can only contain numbers, hyphens (-), plus signs (+), dots (.), hash signs (#), asterisks (*), and/or spaces.'
} else if (appearance.indexOf('numbers_decimal') !== -1) {
input.inputmode = 'decimal'
input.type = 'number'
specialConstraint = new RegExp('^-?([0-9]+.?[0-9]*)|([0-9]*.?[0-9]+)$')
invalidBox.innerHTML = 'Invalid: Answer must be a valid decimal number.'
} else if (appearance.indexOf('numbers') !== -1) {
input.inputmode = 'numeric'
input.type = 'number'
specialConstraint = new RegExp('^[0-9-+. ]+$')
invalidBox.innerHTML = 'Invalid: Answer can only contain numbers, hyphens (-), plus signs (+), dots (.), and/or spaces.'
} else {
specialConstraint = new RegExp('.+')
}
}
for (var buttonNumber = 1; buttonNumber <= 100; buttonNumber++) {
var buttonLabel = getPluginParameter('button' + String(buttonNumber))
var buttonValue = getPluginParameter('value' + String(buttonNumber))
if ((buttonLabel != null) && (buttonValue != null)) {
var buttonHtml = '<button id="' + buttonLabel + '" class="altbutton button' + String(buttonNumber % 2) + '" value="' + buttonValue + '" dir="auto">' + buttonLabel + '</button>'
buttonsDisp += buttonHtml
altValues.push(buttonValue) // Currently not used
} else {
break // Stop looking for buttons when number in parameter name is not found
}
}
buttonContainer.innerHTML = buttonsDisp
var allButtons = document.querySelectorAll('#buttons button')
var numButtons = allButtons.length
for (var b = 0; b < numButtons; b++) {
var button = allButtons[b]
buttonFontAdjuster(button)
if (!fieldProperties.READONLY) {
button.addEventListener('click', function (e) { // Adds event listener to buttons
var clickedLabel = e.target.innerHTML
var clickedValue = e.target.value
var currentInput = input.value
if ((currentInput === '') || (currentInput == null) || (String(clickedValue) === String(currentInput))) {
setMetaData(clickedLabel)
setAnswer(clickedValue)
input.value = clickedValue
if (autoAdvance) {
goToNextField()
}
} else {
dispWarning(clickedLabel, clickedValue)
}
})
}
}
var yesButtonText = getPluginParameter('yes')
if (yesButtonText != null) {
yesButton.innerHTML = yesButtonText
}
var noButtonText = getPluginParameter('no')
if (noButtonText != null) {
noButton.innerHTML = noButtonText
}
var warningMessage = getPluginParameter('warning')
if (warningMessage == null) {
warningMessage = 'Warning: This field already has a value. Are you sure you would like to replace it?'
} else {
warningContainer.querySelector('#warning-message').innerHTML = warningMessage
}
warningContainer.style.display = 'none'
input.oninput = function () {
formGroup.classList.remove('has-error')
controlMessage.innerHTML = ''
var currentAnswer = input.value
if (appearance.indexOf('show_formatted') !== -1) {
var ansString = currentAnswer.toString()
var pointLoc = currentAnswer.indexOf('.')
if (pointLoc === -1) {
formattedSpan.innerHTML = ansString.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
} else {
var beforePoint = ansString.substring(0, pointLoc).replace(/\B(?=(\d{3})+(?!\d))/g, ',') // efore the decimal point
// The part below adds commas to the numbers after the decimal point. Unfortunately, a lookbehind assersion breaks the JS in iOS right now, so this has been commented out for now.
/* var midPoint = answer.substring(pointLoc + 1, pointLoc + 3) // he first two digits after the decimal point this is because the first two digits after the decimal point are the "tenths" and "hundredths", while after that is "thousandths"
var afterPoint = answer.substring(pointLoc + 3, answer.length).replace(/\B(?<=(^(\d{3})+))/g, ",") // fter the first two digits after the decimal point
var total = beforePoint
if (midPoint != '') { // dds the decimal point only if it is needed
total += '.' + midPoint
if (afterPoint != '') { // dds the comma after "midPoint" and the rest only if they are needed
total += ',' + afterPoint
}
} */
var afterPoint = ansString.substring(pointLoc, ansString.length)
var total = beforePoint + afterPoint
formattedSpan.innerHTML = total
}
}
if ((currentAnswer === '') || specialConstraint.test(currentAnswer)) {
invalidBox.style.display = 'none'
setMetaData('')
setAnswer(currentAnswer)
} else {
invalidBox.style.display = ''
}
}
function buttonFontAdjuster (button) { // djusts size of the text of the buttons in case the text is too long
var fontSize = parseInt(window.getComputedStyle(button, null).getPropertyValue('font-size'))
var stopper = 50
while (button.scrollHeight > button.clientHeight) {
fontSize--
button.style.fontSize = fontSize + 'px'
stopper--
if (stopper <= 0) {
return
}
}
}
function clearAnswer () {
input.value = ''
setMetaData('')
setAnswer('')
}
function setFocus () {
input.focus()
if (!fieldProperties.READONLY) {
if (window.showSoftKeyboard) {
window.showSoftKeyboard()
}
}
}
function cursorToEnd (el) { // oves cursor to end of text in text box (incondistent in non-text fields)
if (typeof el.selectionStart === 'number') {
el.selectionStart = el.selectionEnd = el.value.length
} else if (typeof el.createTextRange !== 'undefined') {
el.focus()
var range = el.createTextRange()
range.collapse(false)
range.select()
}
}
function handleConstraintMessage (message) {
formGroup.classList.add('has-error')
controlMessage.innerHTML = message
setFocus()
}
function handleRequiredMessage (message) {
handleConstraintMessage(message)
}
function dispWarning (clickedLabel, clickedValue) { // Displays the warning when tapping a button when there is already content in the text box
warningContainer.style.display = ''
document.querySelector('#yes').addEventListener('click', function () {
setMetaData(clickedLabel)
setAnswer(clickedValue)
input.value = clickedValue
warningContainer.style.display = 'none'
if (autoAdvance) {
goToNextField()
}
})
document.querySelector('#no').addEventListener('click', function () {
warningContainer.style.display = 'none'
})
}