Добавлена поддержка автосохранения товаров в черновиках заказов
- Исправлен ID контейнера формсета (order-items-container) - Добавлена поддержка событий Select2 для полей выбора товара - Добавлен MutationObserver для отслеживания новых форм товаров - Удалены отладочные console.log 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -25,25 +25,20 @@
|
||||
* Инициализация модуля автосохранения
|
||||
*/
|
||||
function init() {
|
||||
console.log('[Autosave] Attempting to initialize...');
|
||||
|
||||
// Проверяем, что мы на странице редактирования
|
||||
const isEditPage = window.location.pathname.includes('/edit/');
|
||||
if (!isEditPage) {
|
||||
console.log('[Autosave] Not on edit page, exiting');
|
||||
return;
|
||||
}
|
||||
|
||||
const orderForm = document.getElementById('order-form');
|
||||
if (!orderForm) {
|
||||
console.log('[Autosave] Order form not found, exiting');
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем ID заказа из URL
|
||||
const urlMatch = window.location.pathname.match(/\/orders\/(\d+)\/edit\//);
|
||||
if (!urlMatch) {
|
||||
console.log('[Autosave] Could not extract order ID from URL, exiting');
|
||||
return;
|
||||
}
|
||||
orderId = urlMatch[1];
|
||||
@@ -51,12 +46,9 @@
|
||||
// Проверяем, является ли заказ черновиком
|
||||
isDraft = checkIfDraft();
|
||||
if (!isDraft) {
|
||||
console.log('[Autosave] Not a draft order, exiting');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Autosave] Initialized for draft order #' + orderId);
|
||||
|
||||
// Инициализируем UI индикатора
|
||||
initStatusIndicator();
|
||||
|
||||
@@ -71,7 +63,6 @@
|
||||
// Проверяем через data-атрибут на форме
|
||||
const form = document.getElementById('order-form');
|
||||
if (form && form.dataset.isDraft === 'true') {
|
||||
console.log('[Autosave] Form has data-is-draft="true"');
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -165,12 +156,9 @@
|
||||
function attachEventListeners() {
|
||||
const form = document.getElementById('order-form');
|
||||
if (!form) {
|
||||
console.log('[Autosave] Form not found in attachEventListeners');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Autosave] Attaching event listeners to form fields');
|
||||
|
||||
// Слушаем изменения в основных полях заказа
|
||||
const fieldsToWatch = [
|
||||
'select[name="customer"]',
|
||||
@@ -202,15 +190,13 @@
|
||||
|
||||
// Слушаем изменения в формах товаров (formset)
|
||||
observeFormsetChanges();
|
||||
|
||||
console.log('[Autosave] Event listeners attached');
|
||||
}
|
||||
|
||||
/**
|
||||
* Наблюдает за изменениями в формсете товаров
|
||||
*/
|
||||
function observeFormsetChanges() {
|
||||
const formsetContainer = document.getElementById('order-items-formset');
|
||||
const formsetContainer = document.getElementById('order-items-container');
|
||||
if (!formsetContainer) {
|
||||
return;
|
||||
}
|
||||
@@ -242,9 +228,15 @@
|
||||
}
|
||||
|
||||
const fields = form.querySelectorAll('select, input[type="number"], input[type="checkbox"]');
|
||||
|
||||
fields.forEach(field => {
|
||||
if (field.tagName === 'SELECT' || field.type === 'checkbox') {
|
||||
field.addEventListener('change', scheduleAutosave);
|
||||
|
||||
// Для Select2 добавляем дополнительный обработчик
|
||||
if (window.jQuery && jQuery(field).data('select2')) {
|
||||
jQuery(field).on('select2:select', scheduleAutosave);
|
||||
}
|
||||
} else {
|
||||
field.addEventListener('input', scheduleAutosave);
|
||||
}
|
||||
@@ -267,8 +259,6 @@
|
||||
autosaveTimer = setTimeout(() => {
|
||||
performAutosave();
|
||||
}, CONFIG.AUTOSAVE_DELAY);
|
||||
|
||||
console.log('[Autosave] Scheduled in ' + CONFIG.AUTOSAVE_DELAY + 'ms');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,7 +266,6 @@
|
||||
*/
|
||||
async function performAutosave() {
|
||||
if (isAutosaving) {
|
||||
console.log('[Autosave] Already in progress, skipping');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -303,15 +292,12 @@
|
||||
if (response.ok && data.success) {
|
||||
const lastSaved = formatDateTime(data.last_saved);
|
||||
showStatus('success', 'Сохранено ' + lastSaved);
|
||||
console.log('[Autosave] Success:', data);
|
||||
} else {
|
||||
showStatus('error', 'Ошибка: ' + (data.error || 'Неизвестная ошибка'));
|
||||
console.error('[Autosave] Error:', data);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
showStatus('error', 'Ошибка соединения с сервером');
|
||||
console.error('[Autosave] Exception:', error);
|
||||
} finally {
|
||||
isAutosaving = false;
|
||||
}
|
||||
@@ -408,7 +394,7 @@
|
||||
const items = [];
|
||||
const itemForms = document.querySelectorAll('.order-item-form');
|
||||
|
||||
itemForms.forEach((form, index) => {
|
||||
itemForms.forEach(form => {
|
||||
// Пропускаем удаленные формы
|
||||
const deleteCheckbox = form.querySelector('input[name$="-DELETE"]');
|
||||
if (deleteCheckbox && deleteCheckbox.checked) {
|
||||
|
||||
Reference in New Issue
Block a user