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 if (!unisenderDoubleOptin) return false 24 this.doubleOptin = unisenderDoubleOptin 25 this.form = form 26 this.contactList = this.form.getAttribute('data-contact-list') 27 this.fields = this.form.querySelectorAll('[data-unisender-field]') 28 this.submitButton = this.form.querySelector('.b-unisender-form__submit-button') 29 this.fieldWraps = this.form.querySelectorAll('[data-unisender-field-name]') 30 this.errorPlaces = this.form.querySelectorAll('[data-unisender-field-error]') 31 this.errorTexts = unisenderValidationErrorTexts 32 this.errors = [] 33 this.ajaxUrl = UNISENDER_AJAX.url 34 35 this.test = { 36 required: v => { 37 return !v 38 }, 39 minLength: (v, r) => { 40 return v ? !(v.length >= parseFloat(r)) : false 41 }, 42 maxLength: (v, r) => { 43 return v ? !(v.length <= parseFloat(r)) : false 44 }, 45 minNumber: (v, r) => { 46 return v ? !(parseFloat(v) >= parseFloat(r)) : false 47 }, 48 maxNumber: (v, r) => { 49 return v ? !(parseFloat(v) <= parseFloat(r)) : false 50 }, 51 regExp: (v, r) => { 52 return v ? !v : false 53 }, 54 email: (v, r) => { 55 const regExp = /\S+@\S+\.\S+/ 56 return !regExp.test(v) 57 } 58 } 59 60 this.init() 61 } 62 init() { 63 this.listeners() 64 } 65 66 listeners() { 67 this.submitButton.addEventListener('click', e => { 68 this.submitButtonHandleClick() 69 }) 70 } 71 72 submitButtonHandleClick() { 73 this.errors = [] 74 this.form.classList.remove('_show-message') 75 this.form.classList.remove('_success-text') 76 this.form.classList.remove('_validation-error-text') 77 this.form.classList.remove('_server-error-text') 78 79 const policyInput = this.form.querySelector('.b-unisender-form__policy-input') 80 const policyError = this.form.querySelector('.policy-error') 81 82 policyError.style.display = 'none'; 83 84 for (let i = 0; i < this.fields.length; i++) { 85 const value = this.fields[i].getAttribute('type') === 'checkbox' ? this.fields[i].checked : this.fields[i].value 86 const rulesJson = this.fields[i].getAttribute('data-validation-rules') 87 const rules = rulesJson ? JSON.parse(rulesJson) : null 88 const activeRules = [] 89 if (this.fields[i].type === 'email') { 90 activeRules.push({ 91 name: 'email', 92 value: true 93 }) 94 } 95 Object.keys(rules).forEach(key => { 96 if (rules[key]) { 97 activeRules.push({ 98 name: key, 99 value: rules[key] 100 }) 101 } 102 }) 103 104 if (activeRules.length > 0) { 105 this.validationField(this.fields[i], activeRules, value) 106 } 107 } 108 109 if (!policyInput || !policyInput.checked) { 110 this.form.classList.add('_show-message') 111 this.form.classList.add('_validation-error-text') 112 113 policyError.style.display = 'block'; 114 policyError.style.color = '#df1b1b'; 115 // вÑвеÑÑи оÑÐ¸Ð±ÐºÑ 116 117 } else if (this.errors.length === 0) { 118 this.request() 119 } 120 } 121 122 validationField(field, rules, value) { 123 const index = Array.prototype.indexOf.call(this.fields, field) 124 this.fieldWraps[index].classList.remove('_has-error') 125 this.errorPlaces[index].innerHTML = '' 126 127 const errors = []
128 rules.forEach(rule => { 129 if (this.test[rule.name](value, rule.value)) { 130 errors.push(rule.name) 131 } 132 }) 133 134 if (errors.length > 0) { 135 this.fieldWraps[index].classList.add('_has-error') 136 this.form.classList.add('_show-message') 137 this.form.classList.add('_validation-error-text') 138 139 errors.forEach(error => { 140 const errorKey = `${error}ErrorText` 141 console.log(errorKey) 142 this.errorPlaces[index].innerHTML = this.errorTexts[errorKey] 143 }) 144 145 this.errors.push(errors) 146 } 147 } 148 149 request() { 150 this.submitButton.parentElement.classList.add('request') 151 const formData = new FormData() 152 const data = {} 153 154 formData.set('action', 'unisender_subscribe') 155 formData.set('contact_list', this.contactList) 156 formData.set('double_optin', this.doubleOptin) 157 158 for (let i = 0; i < this.fields.length; i++) { 159 const field = this.fields[i] 160 const name = field.getAttribute('name') 161 const type = field.type 162 const value = type === 'date' ? FormatDate.getValueByFormat(field.value, field.getAttribute('data-date-type')) : field.value 163 if (name) { 164 data[name] = value 165 } 166 } 167 168 formData.set('data', JSON.stringify(data)) 169 fetch( 170 this.ajaxUrl, 171 { 172 method: 'POST', 173 body: formData 174 } 175 ) 176 .then(response => response.json()) 177 .then(response => { 178 this.submitButton.parentElement.classList.remove('request') 179 if (response.status === 'success') { 180 this.form.classList.add('_show-message') 181 this.form.classList.add('_success-text') 182 this.dropFieldsValue() 183 } else { 184 this.form.classList.add('_show-message') 185 this.form.classList.add('_server-error-text') 186 } 187 }) 188 } 189 dropFieldsValue() { 190 for (let i = 0; i < this.fields.length; i++) { 191 const field = this.fields[i] 192 if (field.type !== 'checkbox') { 193 field.value = null 194 } else { 195 field.checked = false 196 } 197 } 198 } 199} 200 201document.addEventListener('DOMContentLoaded', () => { 202 const unisenderForms = document.querySelectorAll('[data-contact-list]') 203 for (let i = 0; i < unisenderForms.length; i++) { 204 new UnisenderForm(unisenderForms[i]) 205 } 206})
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.