2019-02-26 16:52:23 -07:00
|
|
|
|
// jshint asi:true
|
|
|
|
|
|
2020-09-11 17:33:43 -06:00
|
|
|
|
async function helperUpdateAnswer(event) {
|
2019-02-24 15:51:40 -07:00
|
|
|
|
let e = event.currentTarget
|
|
|
|
|
let value = e.value
|
|
|
|
|
let inputs = e.querySelectorAll("input")
|
|
|
|
|
|
|
|
|
|
if (inputs.length > 0) {
|
|
|
|
|
// If there are child input nodes, join their values with commas
|
|
|
|
|
let values = []
|
|
|
|
|
for (let c of inputs) {
|
|
|
|
|
if (c.type == "checkbox") {
|
|
|
|
|
if (c.checked) {
|
|
|
|
|
values.push(c.value)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
values.push(c.value)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-10 13:56:17 -06:00
|
|
|
|
if (e.classList.contains("sort")) {
|
|
|
|
|
values.sort()
|
|
|
|
|
}
|
2019-04-18 21:58:34 -06:00
|
|
|
|
let join = e.dataset.join
|
|
|
|
|
if (join === undefined) {
|
|
|
|
|
join = ","
|
|
|
|
|
}
|
2020-09-11 17:33:43 -06:00
|
|
|
|
if (values.length == 0) {
|
|
|
|
|
value = "None"
|
|
|
|
|
} else {
|
|
|
|
|
value = values.join(join)
|
|
|
|
|
}
|
2019-02-24 15:51:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First make any adjustments to the value
|
|
|
|
|
if (e.classList.contains("lower")) {
|
|
|
|
|
value = value.toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
if (e.classList.contains("upper")) {
|
|
|
|
|
value = value.toUpperCase()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 17:33:43 -06:00
|
|
|
|
// "substrings" answers try all substrings. If any are the answer, they're filled in.
|
|
|
|
|
if (e.classList.contains("substring")) {
|
|
|
|
|
let validated = null
|
|
|
|
|
let anchorEnd = e.classList.contains("anchor-end")
|
|
|
|
|
let anchorBeg = e.classList.contains("anchor-beg")
|
|
|
|
|
|
|
|
|
|
for (let end = 0; end <= value.length; end += 1) {
|
|
|
|
|
for (let beg = 0; beg < value.length; beg += 1) {
|
|
|
|
|
if (anchorEnd && (end != value.length)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if (anchorBeg && (beg != 0)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
let sub = value.substring(beg, end)
|
|
|
|
|
if (await checkAnswer(sub)) {
|
|
|
|
|
validated = sub
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = validated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If anything zeroed out value, don't update the answer field
|
|
|
|
|
if (!value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 16:52:23 -07:00
|
|
|
|
let answer = document.querySelector("#answer")
|
|
|
|
|
answer.value = value
|
|
|
|
|
answer.dispatchEvent(new InputEvent("input"))
|
2019-02-24 15:51:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 13:56:17 -06:00
|
|
|
|
function helperRemoveInput(e) {
|
|
|
|
|
let item = e.target.parentElement
|
2019-04-10 14:08:33 -06:00
|
|
|
|
let container = item.parentElement
|
2019-04-10 13:56:17 -06:00
|
|
|
|
item.remove()
|
2019-04-10 14:08:33 -06:00
|
|
|
|
|
|
|
|
|
var event = new Event("input")
|
|
|
|
|
container.dispatchEvent(event)
|
2019-04-10 13:56:17 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function helperExpandInputs(e) {
|
|
|
|
|
let item = e.target.parentElement
|
|
|
|
|
let container = item.parentElement
|
|
|
|
|
let template = container.firstElementChild
|
|
|
|
|
let newElement = template.cloneNode(true)
|
|
|
|
|
|
|
|
|
|
// Add remove button
|
|
|
|
|
let remove = document.createElement("button")
|
|
|
|
|
remove.innerText = "➖"
|
2019-04-10 14:04:04 -06:00
|
|
|
|
remove.title = "Remove this input"
|
2019-04-10 13:56:17 -06:00
|
|
|
|
remove.addEventListener("click", helperRemoveInput)
|
|
|
|
|
newElement.appendChild(remove)
|
|
|
|
|
|
|
|
|
|
// Zero it out, otherwise whatever's in first element is copied too
|
|
|
|
|
newElement.querySelector("input").value = ""
|
|
|
|
|
|
|
|
|
|
container.insertBefore(newElement, item)
|
2019-04-10 14:08:33 -06:00
|
|
|
|
|
|
|
|
|
var event = new Event("input")
|
|
|
|
|
container.dispatchEvent(event)
|
2019-04-10 13:56:17 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-24 15:51:40 -07:00
|
|
|
|
function helperActivate(e) {
|
|
|
|
|
e.addEventListener("input", helperUpdateAnswer)
|
2019-04-10 14:03:12 -06:00
|
|
|
|
for (let exp of e.querySelectorAll(".expand")) {
|
|
|
|
|
exp.addEventListener("click", helperExpandInputs)
|
2019-04-10 13:56:17 -06:00
|
|
|
|
}
|
2019-02-24 15:51:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 17:33:43 -06:00
|
|
|
|
{
|
|
|
|
|
let init = function(event) {
|
|
|
|
|
for (let e of document.querySelectorAll(".answer")) {
|
|
|
|
|
helperActivate(e)
|
|
|
|
|
}
|
2019-02-24 15:51:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 17:33:43 -06:00
|
|
|
|
if (document.readyState === "loading") {
|
|
|
|
|
document.addEventListener("DOMContentLoaded", init)
|
|
|
|
|
} else {
|
|
|
|
|
init()
|
|
|
|
|
}
|
2019-02-24 15:51:40 -07:00
|
|
|
|
}
|