|
| 1 | +const { promptCreateInput } = require("./prompt"); |
| 2 | + |
| 3 | +module.exports = { promptCreateCounter , validateCounterInput } |
| 4 | + |
| 5 | +let options; |
| 6 | + |
| 7 | + |
| 8 | +function promptCreateCounter(promptOptions, parentElement) { |
| 9 | + options = promptOptions; |
| 10 | + if (options.counterOptions?.multiFire) { |
| 11 | + document.onmouseup = () => { |
| 12 | + if (nextTimeoutID) { |
| 13 | + clearTimeout(nextTimeoutID) |
| 14 | + nextTimeoutID = null; |
| 15 | + } |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + options.value = validateCounterInput(options.value); |
| 20 | + |
| 21 | + const dataElement = promptCreateInput(); |
| 22 | + dataElement.onkeypress = function isNumberKey(e) { |
| 23 | + if (Number.isNaN(parseInt(e.key)) && e.key !== "Backspace" && e.key !== "Delete") |
| 24 | + return false; |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + dataElement.style.width = "unset"; |
| 29 | + dataElement.style["text-align"] = "center"; |
| 30 | + |
| 31 | + parentElement.append(createMinusButton(dataElement)); |
| 32 | + parentElement.append(dataElement); |
| 33 | + parentElement.append(createPlusButton(dataElement)); |
| 34 | + |
| 35 | + return dataElement; |
| 36 | +} |
| 37 | + |
| 38 | +let nextTimeoutID = null; |
| 39 | + |
| 40 | +/** Function execute callback in 3 accelerated intervals based on timer. |
| 41 | + * Terminated from document.onmouseup() that is registered from promptCreateCounter() |
| 42 | + * @param {function} callback function to execute |
| 43 | + * @param {object} timer { |
| 44 | + * * time: First delay in miliseconds |
| 45 | + * * scaleSpeed: Speed change per tick on first acceleration |
| 46 | + * * limit: First Speed Limit, gets divided by 2 after $20 calls. $number change exponentially |
| 47 | + * } |
| 48 | + * @param {int} stepArgs argument for callback representing Initial steps per click, default to 1 |
| 49 | + * steps starts to increase when speed is too fast to notice |
| 50 | + * @param {int} counter used internally to decrease timer.limit |
| 51 | + */ |
| 52 | +function multiFire(callback, timer = { time: 300, scaleSpeed: 100, limit: 100 }, stepsArg = 1, counter = 0) { |
| 53 | + callback(stepsArg); |
| 54 | + |
| 55 | + const nextTimeout = timer.time; |
| 56 | + |
| 57 | + if (counter > 20) { |
| 58 | + counter = 0 - stepsArg; |
| 59 | + if (timer.limit > 1) { |
| 60 | + timer.limit /= 2; |
| 61 | + } else { |
| 62 | + stepsArg *= 2; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (timer.time !== timer.limit) { |
| 67 | + timer.time = Math.max(timer.time - timer.scaleSpeed, timer.limit) |
| 68 | + } |
| 69 | + |
| 70 | + nextTimeoutID = setTimeout( |
| 71 | + multiFire, //callback |
| 72 | + nextTimeout, //timer |
| 73 | + //multiFire args: |
| 74 | + callback, |
| 75 | + timer, |
| 76 | + stepsArg, |
| 77 | + counter + 1 |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +function createMinusButton(dataElement) { |
| 82 | + function doMinus(steps) { |
| 83 | + dataElement.value = validateCounterInput(parseInt(dataElement.value) - steps); |
| 84 | + } |
| 85 | + |
| 86 | + const minusBtn = document.createElement("span"); |
| 87 | + minusBtn.textContent = "-"; |
| 88 | + minusBtn.classList.add("minus"); |
| 89 | + |
| 90 | + if (options.counterOptions?.multiFire) { |
| 91 | + minusBtn.onmousedown = () => { |
| 92 | + multiFire(doMinus); |
| 93 | + }; |
| 94 | + } else { |
| 95 | + minusBtn.onmousedown = () => { |
| 96 | + doMinus(); |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + return minusBtn; |
| 101 | +} |
| 102 | + |
| 103 | +function createPlusButton(dataElement) { |
| 104 | + function doPlus(steps) { |
| 105 | + dataElement.value = validateCounterInput(parseInt(dataElement.value) + steps); |
| 106 | + } |
| 107 | + |
| 108 | + const plusBtn = document.createElement("span"); |
| 109 | + plusBtn.textContent = "+"; |
| 110 | + plusBtn.classList.add("plus"); |
| 111 | + |
| 112 | + if (options.counterOptions?.multiFire) { |
| 113 | + plusBtn.onmousedown = () => { |
| 114 | + multiFire(doPlus); |
| 115 | + }; |
| 116 | + } else { |
| 117 | + plusBtn.onmousedown = () => { |
| 118 | + doPlus(); |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + return plusBtn; |
| 123 | +} |
| 124 | + |
| 125 | +//validate counter |
| 126 | +function validateCounterInput(input) { |
| 127 | + |
| 128 | + const min = options.counterOptions?.minimum; |
| 129 | + const max = options.counterOptions?.maximum; |
| 130 | + //note that !min/max would proc if min/max are 0 |
| 131 | + if (min !== null && min !== undefined && input < min) { |
| 132 | + return min; |
| 133 | + } |
| 134 | + |
| 135 | + if (max !== null && max !== undefined && input > max) { |
| 136 | + return max; |
| 137 | + } |
| 138 | + |
| 139 | + return input; |
| 140 | +} |
0 commit comments