Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
0590195880
593 files changed
+27942
No files matched your search
@@ -0,0 +1,111 @@
|
||||
(function () {
|
||||
function init() {
|
||||
if (!window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
||||
var saveForm = document.querySelector('[data-cab-save]');
|
||||
var passwordForm = document.querySelector('[data-cab-password]');
|
||||
|
||||
function key(code) {
|
||||
return code.replace(/_([a-z])/g, function (match, letter) { return letter.toUpperCase(); });
|
||||
}
|
||||
|
||||
function message(form, code) {
|
||||
var name = 'msg' + key(code).charAt(0).toUpperCase() + key(code).slice(1);
|
||||
return form.dataset[name] || form.dataset.msgError;
|
||||
}
|
||||
|
||||
function status(form, text, ok) {
|
||||
var line = form.querySelector('[data-cab-status]');
|
||||
line.textContent = text;
|
||||
line.classList.toggle('vino-cab__status--ok', !!ok);
|
||||
}
|
||||
|
||||
function mark(field, bad) {
|
||||
field.classList.toggle('vino-inp--invalid', bad);
|
||||
field.setAttribute('aria-invalid', bad ? 'true' : 'false');
|
||||
}
|
||||
|
||||
function code(response) {
|
||||
return response && response.errors && response.errors[0] ? response.errors[0].code : '';
|
||||
}
|
||||
|
||||
function phoneValue(input) {
|
||||
var digits = input.value.replace(/\D/g, '');
|
||||
return digits.length > 1 ? '+' + digits : '';
|
||||
}
|
||||
|
||||
[saveForm, passwordForm].forEach(function (form) {
|
||||
if (!form) return;
|
||||
form.addEventListener('input', function (event) {
|
||||
if (event.target.classList.contains('vino-inp--invalid')) mark(event.target, false);
|
||||
status(form, '', false);
|
||||
});
|
||||
});
|
||||
|
||||
if (saveForm) {
|
||||
saveForm.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
var name = saveForm.elements.name;
|
||||
var phone = saveForm.elements.phone;
|
||||
var submit = saveForm.querySelector('[type="submit"]');
|
||||
var badName = name.value.trim() === '' || /[<>\x00-\x1f]/.test(name.value);
|
||||
var badPhone = !phone.checkValidity();
|
||||
mark(name, badName);
|
||||
mark(phone, badPhone);
|
||||
if (badName || badPhone) {
|
||||
status(saveForm, badName ? (name.value.trim() === '' ? saveForm.dataset.msgName : saveForm.dataset.msgNameChars) : saveForm.dataset.msgPhone, false);
|
||||
(badName ? name : phone).focus();
|
||||
return;
|
||||
}
|
||||
status(saveForm, '', false);
|
||||
if (submit.getAttribute('aria-disabled') === 'true') return;
|
||||
submit.setAttribute('aria-disabled', 'true');
|
||||
BX.ajax.runAction('jinove:vinoteca.profile.save', { data: { name: name.value.trim(), phone: phoneValue(phone) } }).then(function () {
|
||||
submit.removeAttribute('aria-disabled');
|
||||
status(saveForm, saveForm.dataset.msgDone, true);
|
||||
}, function (response) {
|
||||
submit.removeAttribute('aria-disabled');
|
||||
status(saveForm, message(saveForm, code(response)), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (passwordForm) {
|
||||
passwordForm.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
var current = passwordForm.elements.current;
|
||||
var next = passwordForm.elements.password;
|
||||
var submit = passwordForm.querySelector('[type="submit"]');
|
||||
var badCurrent = current.value === '';
|
||||
var badNext = next.value.length < 8;
|
||||
mark(current, badCurrent);
|
||||
mark(next, badNext);
|
||||
if (badCurrent || badNext) {
|
||||
status(passwordForm, badCurrent ? passwordForm.dataset.msgCurrent : passwordForm.dataset.msgPassword, false);
|
||||
(badCurrent ? current : next).focus();
|
||||
return;
|
||||
}
|
||||
status(passwordForm, '', false);
|
||||
if (submit.getAttribute('aria-disabled') === 'true') return;
|
||||
submit.setAttribute('aria-disabled', 'true');
|
||||
BX.ajax.runAction('jinove:vinoteca.profile.password', { data: { current: current.value, password: next.value } }).then(function () {
|
||||
submit.removeAttribute('aria-disabled');
|
||||
current.value = '';
|
||||
next.value = '';
|
||||
status(passwordForm, passwordForm.dataset.msgDone, true);
|
||||
}, function (response) {
|
||||
submit.removeAttribute('aria-disabled');
|
||||
var errorCode = code(response);
|
||||
if (errorCode === 'current') mark(current, true);
|
||||
if (errorCode === 'password' || errorCode === 'same') mark(next, true);
|
||||
status(passwordForm, message(passwordForm, errorCode), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user