moth

Monarch Of The Hill game server
git clone https://git.woozle.org/neale/moth.git

moth / example-puzzles / example / 5
Neale Pickett  ·  2020-09-11

helpers.js

  1// jshint asi:true
  2
  3async function helperUpdateAnswer(event) {
  4  let e = event.currentTarget
  5  let value = e.value
  6  let inputs = e.querySelectorAll("input")
  7  
  8  if (inputs.length > 0) {
  9    // If there are child input nodes, join their values with commas
 10    let values = []
 11    for (let c of inputs) {
 12      if (c.type == "checkbox") {
 13        if (c.checked) {
 14          values.push(c.value)
 15        }
 16      } else {
 17        values.push(c.value)
 18      }
 19    }
 20    if (e.classList.contains("sort")) {
 21      values.sort()
 22    }
 23    let join = e.dataset.join
 24    if (join === undefined) {
 25      join = ","
 26    }
 27    if (values.length == 0) {
 28      value = "None"
 29    } else {
 30      value = values.join(join)
 31    }
 32  }
 33
 34  // First make any adjustments to the value
 35  if (e.classList.contains("lower")) {
 36    value = value.toLowerCase()
 37  }
 38  if (e.classList.contains("upper")) {
 39    value = value.toUpperCase()
 40  }
 41
 42  // "substrings" answers try all substrings. If any are the answer, they're filled in.
 43  if (e.classList.contains("substring")) {
 44    let validated = null
 45    let anchorEnd = e.classList.contains("anchor-end")
 46    let anchorBeg = e.classList.contains("anchor-beg")
 47
 48    for (let end = 0; end <= value.length; end += 1) {
 49      for (let beg = 0; beg < value.length; beg += 1) {
 50        if (anchorEnd && (end != value.length)) {
 51          continue
 52        }
 53        if (anchorBeg && (beg != 0)) {
 54          continue
 55        }
 56        let sub = value.substring(beg, end)
 57        if (await checkAnswer(sub)) {
 58          validated = sub
 59        }
 60      }
 61    }
 62
 63    value = validated
 64  }
 65
 66  // If anything zeroed out value, don't update the answer field
 67  if (!value) {
 68    return
 69  }
 70
 71  let answer = document.querySelector("#answer")
 72  answer.value = value
 73  answer.dispatchEvent(new InputEvent("input"))
 74}
 75
 76function helperRemoveInput(e) {
 77  let item = e.target.parentElement
 78  let container = item.parentElement
 79  item.remove()
 80  
 81  var event = new Event("input")
 82  container.dispatchEvent(event)
 83}
 84
 85function helperExpandInputs(e) {
 86  let item = e.target.parentElement
 87  let container = item.parentElement
 88  let template = container.firstElementChild
 89  let newElement = template.cloneNode(true)
 90
 91  // Add remove button
 92  let remove = document.createElement("button")
 93  remove.innerText = "➖"
 94  remove.title = "Remove this input"
 95  remove.addEventListener("click", helperRemoveInput)
 96  newElement.appendChild(remove)
 97
 98  // Zero it out, otherwise whatever's in first element is copied too
 99  newElement.querySelector("input").value = ""
100
101  container.insertBefore(newElement, item)
102  
103  var event = new Event("input")
104  container.dispatchEvent(event)
105}
106
107function helperActivate(e) {
108  e.addEventListener("input", helperUpdateAnswer)
109  for (let exp of e.querySelectorAll(".expand")) {
110    exp.addEventListener("click", helperExpandInputs)
111  }
112}
113
114{
115  let init = function(event) {
116    for (let e of document.querySelectorAll(".answer")) {
117      helperActivate(e)
118    }
119  }
120
121  if (document.readyState === "loading") {
122    document.addEventListener("DOMContentLoaded", init)
123  } else {
124    init()
125  }
126}