| 1 |
// TODO STEPPED INPUT |
| 2 |
const { age } = window.ag_stepped; |
| 3 |
|
| 4 |
class AgeGateSteps { |
| 5 |
constructor() { |
| 6 |
const date = new Date(); |
| 7 |
date.setFullYear(date.getFullYear() - age); |
| 8 |
const dateTime = date.toISOString().split('T').slice(0, 1)[0].split('-'); |
| 9 |
|
| 10 |
// elements |
| 11 |
this.form = document.querySelector('.age-gate-form, .age-gate__form'); |
| 12 |
|
| 13 |
if (('requestSubmit' in this.form) === false) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
this.passed = false; |
| 18 |
|
| 19 |
this.inputDay = this.form.querySelector('[name="age_gate[d]"]'); |
| 20 |
this.inputMonth = this.form.querySelector('[name="age_gate[m]"]'); |
| 21 |
this.inputYear = this.form.querySelector('[name="age_gate[y]"]'); |
| 22 |
this.inputSubmit = this.form.querySelector('.age-gate-submit, .age-gate__submit'); |
| 23 |
this.elements = this.form.querySelector('.age-gate-form-elements, .age-gate__form-elements'); |
| 24 |
this.wrapper = this.form.parentNode; |
| 25 |
|
| 26 |
this.inputDay.tabIndex = '-1'; |
| 27 |
this.inputMonth.tabIndex = '-1'; |
| 28 |
|
| 29 |
// values |
| 30 |
// values |
| 31 |
this.current = 'y'; |
| 32 |
this.userYear = 0; |
| 33 |
this.userMonth = 0; |
| 34 |
this.userDay = 0; |
| 35 |
this.minYear = parseInt(dateTime[0]); |
| 36 |
this.minMonth = parseInt(dateTime[1]); |
| 37 |
this.minDay = parseInt(dateTime[2]); |
| 38 |
|
| 39 |
this.init(); |
| 40 |
} |
| 41 |
|
| 42 |
init = () => { |
| 43 |
console.log('bindings') |
| 44 |
this.form.setAttribute('novalidate', true); |
| 45 |
|
| 46 |
// hide elements |
| 47 |
this.inputSubmit.style.display = 'none'; |
| 48 |
|
| 49 |
this.wrapper.classList.add('age-gate--stepped'); |
| 50 |
|
| 51 |
// events |
| 52 |
this.inputYear.addEventListener('keyup', this.handleYear); |
| 53 |
this.inputMonth.addEventListener('keyup', this.handleMonth); |
| 54 |
} |
| 55 |
|
| 56 |
handleYear = (e) => { |
| 57 |
if (this.inputYear.value.length === this.inputYear.maxLength) { |
| 58 |
if (parseInt(this.inputYear.value) !== this.minYear) { |
| 59 |
if (!this.passed) { |
| 60 |
this.passed = true; |
| 61 |
this.inputDay.value = '01'; |
| 62 |
this.inputMonth.value = '01'; |
| 63 |
this.form.requestSubmit(); |
| 64 |
} |
| 65 |
} else { |
| 66 |
this.inputMonth.value = ''; |
| 67 |
this.inputDay.value = ''; |
| 68 |
this.inputMonth.focus(); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
handleMonth = (e) => { |
| 74 |
if (this.inputMonth.value.length === this.inputMonth.maxLength) { |
| 75 |
if (this.inputMonth.value < this.minMonth) { |
| 76 |
console.log('wibble'); |
| 77 |
this.inputDay.value = '01'; |
| 78 |
|
| 79 |
if (!this.passed) { |
| 80 |
this.form.requestSubmit(); |
| 81 |
} |
| 82 |
} else { |
| 83 |
// this.inputDay.parentNode.style.display = 'block'; |
| 84 |
// this.inputYear.parentNode.style.display = 'block'; |
| 85 |
this.inputSubmit.style.display = ''; |
| 86 |
this.wrapper.classList.remove('age-gate--stepped'); |
| 87 |
|
| 88 |
|
| 89 |
this.inputYear.removeEventListener('keyup', this.handleYear); |
| 90 |
this.inputMonth.removeEventListener('keyup', this.handleMonth); |
| 91 |
|
| 92 |
this.inputDay.value = ''; |
| 93 |
this.inputDay.focus(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
window.addEventListener('age_gate_shown', () => { |
| 102 |
new AgeGateSteps |
| 103 |
}); |
| 104 |
|
| 105 |
// console.log(age); |
| 106 |
|