Упрощена логика автосоздания черновика
Черновик теперь создаётся ТОЛЬКО при выборе клиента, а не при добавлении товаров или изменении других полей. Это логично, так как: - Без клиента черновик не может быть создан - После переадресации на страницу редактирования работает полноценное автосохранение 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -80,112 +80,9 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Слушаем изменения в других основных полях
|
||||
const fieldsToWatch = [
|
||||
'input[name="delivery_date"]',
|
||||
'input[name="delivery_time_start"]',
|
||||
'input[name="delivery_time_end"]',
|
||||
'select[name="payment_method"]',
|
||||
'textarea[name="special_instructions"]',
|
||||
'input[type="checkbox"]',
|
||||
'select[name="delivery_address"]',
|
||||
'select[name="pickup_shop"]',
|
||||
];
|
||||
|
||||
fieldsToWatch.forEach(selector => {
|
||||
const fields = form.querySelectorAll(selector);
|
||||
fields.forEach(field => {
|
||||
const eventType = (field.tagName === 'SELECT' || field.type === 'checkbox') ? 'change' : 'input';
|
||||
|
||||
// Обычное событие
|
||||
field.addEventListener(eventType, function() {
|
||||
console.log('[DraftCreator] Field changed:', field.name);
|
||||
// Создаём черновик только если выбран клиент
|
||||
const customer = form.querySelector('select[name="customer"]');
|
||||
if (customer && customer.value && !draftCreated) {
|
||||
scheduleCreateDraft();
|
||||
}
|
||||
});
|
||||
|
||||
// Для select полей также слушаем Select2
|
||||
if (field.tagName === 'SELECT' && window.jQuery && jQuery(field).data('select2')) {
|
||||
jQuery(field).on('select2:select', function() {
|
||||
console.log('[DraftCreator] Field changed (select2):', field.name);
|
||||
const customer = form.querySelector('select[name="customer"]');
|
||||
if (customer && customer.value && !draftCreated) {
|
||||
scheduleCreateDraft();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Слушаем изменения в формах товаров
|
||||
observeFormsetChanges();
|
||||
}
|
||||
|
||||
/**
|
||||
* Наблюдает за изменениями в формсете товаров
|
||||
*/
|
||||
function observeFormsetChanges() {
|
||||
const formsetContainer = document.getElementById('order-items-container');
|
||||
if (!formsetContainer) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Наблюдаем за добавлением новых форм
|
||||
const observer = new MutationObserver(() => {
|
||||
attachFormsetEventListeners();
|
||||
});
|
||||
|
||||
observer.observe(formsetContainer, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
|
||||
// Прикрепляем обработчики к существующим формам
|
||||
attachFormsetEventListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Прикрепляет обработчики к полям в формах товаров
|
||||
*/
|
||||
function attachFormsetEventListeners() {
|
||||
const itemForms = document.querySelectorAll('.order-item-form');
|
||||
|
||||
itemForms.forEach(form => {
|
||||
// Если уже прикреплены обработчики, пропускаем
|
||||
if (form.dataset.draftCreatorAttached === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
const fields = form.querySelectorAll('select, input[type="number"]');
|
||||
fields.forEach(field => {
|
||||
const eventType = field.tagName === 'SELECT' ? 'change' : 'input';
|
||||
|
||||
// Обычное событие
|
||||
field.addEventListener(eventType, function() {
|
||||
console.log('[DraftCreator] Item field changed:', field.name);
|
||||
const customerField = document.querySelector('select[name="customer"]');
|
||||
if (customerField && customerField.value && !draftCreated) {
|
||||
scheduleCreateDraft();
|
||||
}
|
||||
});
|
||||
|
||||
// Для select полей товаров также слушаем Select2
|
||||
if (field.tagName === 'SELECT' && window.jQuery && jQuery(field).data('select2')) {
|
||||
jQuery(field).on('select2:select', function() {
|
||||
console.log('[DraftCreator] Item field changed (select2):', field.name);
|
||||
const customerField = document.querySelector('select[name="customer"]');
|
||||
if (customerField && customerField.value && !draftCreated) {
|
||||
scheduleCreateDraft();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
form.dataset.draftCreatorAttached = 'true';
|
||||
});
|
||||
// Черновик создаётся ТОЛЬКО при выборе клиента.
|
||||
// После создания и переадресации на страницу редактирования
|
||||
// уже работает полноценное автосохранение для всех полей и товаров.
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user