1class FormatDate { 2 static getValueByFormat(value, format) { 3 const date = new Date(value) 4 const char = format.includes('/') ? '/' : (format.includes('.') ? '.' : false) 5 const parsing = format.includes('/') ? format.split('/') : (format.includes('.') ? format.split('.') : false) 6 const conformities = { 7 dd: 'getDate', 8 mm: 'getMonth', 9 yyyy: 'getFullYear' 10 } 11 return parsing.map((item, index) => { 12 const v = date[conformities[item]]().toString() 13 return v.length < 2 ? `0${v}` : v 14 }).join(char) 15 } 16} 17 18class UnisenderForm { 19 constructor(form) { 20 if (!form) return false 21 if (!unisenderValidationErrorTexts) return false 22 if (!UNISENDER_AJAX) throw new Error(`"UNISENDER_AJAX" not found`) 23 this.form = form 24 this.contactList = this.form.getAttribute('data-contact-list') 25 this.fields = this.form.querySelectorAll('[data-unisender-field]') 26 this.submitButton = this.form.querySelector('.b-unisender-form__submit-button') 27 this.fieldWraps = this.form.querySelectorAll('[data-unisender-field-name]') 28 this.errorPlaces = this.form.querySelectorAll('[data-unisender-field-error]') 29 this.errorTexts = unisenderValidationErrorTexts 30 this.errors = [] 31 this.ajaxUrl = UNISENDER_AJAX.url 32 33 this.test = { 34 required: v => { 35 return !v 36 }, 37 minLength: (v, r) => { 38 return v ? !(v.length >= parseFloat(r)) : false 39 }, 40 maxLength: (v, r) => { 41 return v ? !(v.length <= parseFloat(r)) : false 42 }, 43 minNumber: (v, r) => { 44 return v ? !(parseFloat(v) >= parseFloat(r)) : false 45 }, 46 maxNumber: (v, r) => { 47 return v ? !(parseFloat(v) <= parseFloat(r)) : false 48 }, 49 regExp: (v, r) => { 50 return v ? !v : false 51 }, 52 email: (v, r) => { 53 const regExp = /\S+@\S+\.\S+/ 54 return !regExp.test(v) 55 } 56 } 57 58 this.init() 59 } 60 init() { 61 this.listeners() 62 } 63 64 listeners() { 65 this.submitButton.addEventListener('click', e => { 66 this.submitButtonHandleClick() 67 }) 68 } 69 70 submitButtonHandleClick() { 71 this.errors = [] 72 this.form.classList.remove('_show-message') 73 this.form.classList.remove('_success-text') 74 this.form.classList.remove('_validation-error-text') 75 this.form.classList.remove('_server-error-text') 76 77 for (let i = 0; i < this.fields.length; i++) { 78 const value = this.fields[i].getAttribute('type') === 'checkbox' ? this.fields[i].checked : this.fields[i].value 79 const rulesJson = this.fields[i].getAttribute('data-validation-rules') 80 const rules = rulesJson ? JSON.parse(rulesJson) : null 81 const activeRules = [] 82 if (this.fields[i].type === 'email') { 83 activeRules.push({ 84 name: 'email', 85 value: true 86 }) 87 } 88 Object.keys(rules).forEach(key => { 89 if (rules[key]) { 90 activeRules.push({ 91 name: key, 92 value: rules[key] 93 }) 94 } 95 }) 96 97 if (activeRules.length > 0) { 98 this.validationField(this.fields[i], activeRules, value) 99 } 100 } 101 102 if (this.errors.length === 0) { 103 this.request() 104 } 105 } 106 107 validationField(field, rules, value) { 108 const index = Array.prototype.indexOf.call(this.fields, field) 109 this.fieldWraps[index].classList.remove('_has-error') 110 this.errorPlaces[index].innerHTML = '' 111 112 const errors = []
113 rules.forEach(rule => { 114 if (this.test[rule.name](value, rule.value)) { 115 errors.push(rule.name) 116 } 117 }) 118 119 if (errors.length > 0) { 120 this.fieldWraps[index].classList.add('_has-error') 121 this.form.classList.add('_show-message') 122 this.form.classList.add('_validation-error-text') 123 124 errors.forEach(error => { 125 const errorKey = `${error}ErrorText` 126 console.log(errorKey) 127 this.errorPlaces[index].innerHTML = this.errorTexts[errorKey] 128 }) 129 130 this.errors.push(errors) 131 } 132 } 133 134 request() { 135 this.submitButton.parentElement.classList.add('request') 136 const formData = new FormData() 137 const data = {} 138 139 formData.set('action', 'unisender_subscribe') 140 formData.set('contact_list', this.contactList) 141 142 for (let i = 0; i < this.fields.length; i++) { 143 const field = this.fields[i] 144 const name = field.getAttribute('name') 145 const type = field.type 146 const value = type === 'date' ? FormatDate.getValueByFormat(field.value, field.getAttribute('data-date-type')) : field.value 147 if (name) { 148 data[name] = value 149 } 150 } 151 152 formData.set('data', JSON.stringify(data)) 153 fetch( 154 this.ajaxUrl, 155 { 156 method: 'POST', 157 body: formData 158 } 159 ) 160 .then(response => response.json()) 161 .then(response => { 162 this.submitButton.parentElement.classList.remove('request') 163 if (response.status === 'success') { 164 this.form.classList.add('_show-message') 165 this.form.classList.add('_success-text') 166 this.dropFieldsValue() 167 } else { 168 this.form.classList.add('_show-message') 169 this.form.classList.add('_server-error-text') 170 } 171 }) 172 } 173 dropFieldsValue() { 174 for (let i = 0; i < this.fields.length; i++) { 175 const field = this.fields[i] 176 if (field.type !== 'checkbox') { 177 field.value = null 178 } else { 179 field.checked = false 180 } 181 } 182 } 183} 184 185document.addEventListener('DOMContentLoaded', () => { 186 const unisenderForms = document.querySelectorAll('[data-contact-list]') 187 for (let i = 0; i < unisenderForms.length; i++) { 188 new UnisenderForm(unisenderForms[i]) 189 } 190})
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.