Deduplicate configuration revisions and update revisions UI

This commit is contained in:
2026-02-19 14:09:00 +03:00
parent 71f73e2f1d
commit cbaeafa9c8
10 changed files with 839 additions and 188 deletions

View File

@@ -42,6 +42,35 @@ function escapeHtml(text) {
return div.innerHTML;
}
function formatMoney(value) {
const num = Number(value);
if (!Number.isFinite(num)) return '—';
return '$\u00A0' + num.toLocaleString('ru-RU', {minimumFractionDigits: 0, maximumFractionDigits: 2});
}
function parseVersionSnapshot(version) {
try {
const raw = typeof version.data === 'string' ? version.data : '';
if (!raw) return { article: '—', price: null, serverCount: 1 };
const parsed = JSON.parse(raw);
return {
article: parsed.article || '—',
price: typeof parsed.total_price === 'number' ? parsed.total_price : null,
serverCount: Number.isFinite(Number(parsed.server_count)) && Number(parsed.server_count) > 0
? Number(parsed.server_count)
: 1
};
} catch (_) {
return { article: '—', price: null, serverCount: 1 };
}
}
function truncateBreadcrumbSpecName(name) {
const maxLength = 16;
if (!name || name.length <= maxLength) return name;
return name.slice(0, maxLength - 1) + '…';
}
async function loadConfigInfo() {
try {
const resp = await fetch('/api/configs/' + configUUID);
@@ -52,7 +81,10 @@ async function loadConfigInfo() {
}
configData = await resp.json();
document.getElementById('breadcrumb-config').textContent = configData.name || 'Конфигурация';
const fullConfigName = configData.name || 'Конфигурация';
const configBreadcrumbEl = document.getElementById('breadcrumb-config');
configBreadcrumbEl.textContent = truncateBreadcrumbSpecName(fullConfigName);
configBreadcrumbEl.title = fullConfigName;
document.getElementById('breadcrumb-config-link').href = '/configurator?uuid=' + configUUID;
if (configData.project_uuid) {
@@ -114,14 +146,16 @@ function renderVersions(versions) {
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Версия</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Дата</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Автор</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Примечание</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Артикул</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Цена</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Серверов</th>';
html += '<th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase">Действия</th>';
html += '</tr></thead><tbody class="divide-y">';
versions.forEach((v, idx) => {
const date = new Date(v.created_at).toLocaleString('ru-RU');
const author = v.created_by || '—';
const note = v.change_note || '—';
const snapshot = parseVersionSnapshot(v);
const isCurrent = idx === 0;
html += '<tr class="hover:bg-gray-50' + (isCurrent ? ' bg-blue-50' : '') + '">';
@@ -131,7 +165,9 @@ function renderVersions(versions) {
html += '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(date) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(author) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(note) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(snapshot.article) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + formatMoney(snapshot.price) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(String(snapshot.serverCount)) + '</td>';
html += '<td class="px-4 py-3 text-sm text-right space-x-2">';
// Open in configurator (readonly view)

View File

@@ -401,18 +401,28 @@ function updateConfigBreadcrumbs() {
}
codeEl.textContent = code;
variantEl.textContent = variant;
configEl.textContent = configName || 'Конфигурация';
const fullConfigName = configName || 'Конфигурация';
configEl.textContent = truncateBreadcrumbSpecName(fullConfigName);
configEl.title = fullConfigName;
versionEl.textContent = 'v' + (currentVersionNo || 1);
const configNameLinkEl = document.getElementById('breadcrumb-config-name-link');
if (configNameLinkEl && configUUID) {
configNameLinkEl.href = '/configs/' + configUUID + '/revisions';
}
}
function truncateBreadcrumbSpecName(name) {
const maxLength = 16;
if (!name || name.length <= maxLength) return name;
return name.slice(0, maxLength - 1) + '…';
}
let currentTab = 'base';
let allComponents = [];
let cart = [];
let categoryOrderMap = {}; // Category code -> display_order mapping
let autoSaveTimeout = null; // Timeout for debounced autosave
let hasUnsavedChanges = false;
let exitSaveStarted = false;
let serverCount = 1; // Server count for the configuration
let serverModelForQuote = '';
let supportCode = '';
@@ -1890,13 +1900,128 @@ function getCurrentArticle() {
return currentArticle || '';
}
function getAutosaveStorageKey() {
return `qf_config_autosave_${configUUID || 'default'}`;
}
function buildSavePayload() {
const customPriceInput = document.getElementById('custom-price-input');
const customPriceValue = parseFloat(customPriceInput.value);
const customPrice = customPriceValue > 0 ? customPriceValue : null;
return {
name: configName,
items: cart,
custom_price: customPrice,
notes: '',
server_count: serverCount,
server_model: serverModelForQuote,
support_code: supportCode,
article: getCurrentArticle(),
pricelist_id: selectedPricelistIds.estimate,
only_in_stock: onlyInStock
};
}
function persistAutosaveDraft() {
if (!configUUID) return;
try {
sessionStorage.setItem(getAutosaveStorageKey(), JSON.stringify({
payload: buildSavePayload(),
saved_at: Date.now()
}));
} catch (_) {
// ignore storage failures
}
}
function clearAutosaveDraft() {
try {
sessionStorage.removeItem(getAutosaveStorageKey());
} catch (_) {
// ignore storage failures
}
}
function restoreAutosaveDraftIfAny() {
if (!configUUID) return;
let raw = null;
try {
raw = sessionStorage.getItem(getAutosaveStorageKey());
} catch (_) {
raw = null;
}
if (!raw) return;
try {
const parsed = JSON.parse(raw);
const payload = parsed && parsed.payload ? parsed.payload : null;
if (!payload) return;
if (Array.isArray(payload.items)) {
cart = payload.items.map(item => ({
lot_name: item.lot_name,
quantity: item.quantity,
unit_price: item.unit_price,
estimate_price: item.unit_price,
warehouse_price: null,
competitor_price: null,
description: item.description || '',
category: item.category || getCategoryFromLotName(item.lot_name)
}));
}
if (typeof payload.server_count === 'number' && payload.server_count > 0) {
serverCount = payload.server_count;
const serverCountInput = document.getElementById('server-count');
if (serverCountInput) serverCountInput.value = serverCount;
const totalServerCount = document.getElementById('total-server-count');
if (totalServerCount) totalServerCount.textContent = serverCount;
}
serverModelForQuote = payload.server_model || serverModelForQuote;
supportCode = payload.support_code || supportCode;
currentArticle = payload.article || currentArticle;
selectedPricelistIds.estimate = payload.pricelist_id || selectedPricelistIds.estimate;
onlyInStock = Boolean(payload.only_in_stock);
const customPriceInput = document.getElementById('custom-price-input');
if (customPriceInput) {
if (typeof payload.custom_price === 'number' && payload.custom_price > 0) {
customPriceInput.value = payload.custom_price.toFixed(2);
} else {
customPriceInput.value = '';
}
}
hasUnsavedChanges = true;
} catch (_) {
// ignore invalid draft
}
}
function saveConfigOnExit() {
if (!configUUID || !hasUnsavedChanges || exitSaveStarted) return;
exitSaveStarted = true;
try {
fetch('/api/configs/' + configUUID, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(buildSavePayload()),
keepalive: true
});
} catch (_) {
// best effort save on page exit
}
}
function triggerAutoSave() {
// Debounce autosave - wait 1 second after last change
// Autosave keeps local draft only; server revision is created on Save/Exit.
hasUnsavedChanges = true;
if (autoSaveTimeout) {
clearTimeout(autoSaveTimeout);
}
autoSaveTimeout = setTimeout(() => {
saveConfig(false); // false = don't show notification
persistAutosaveDraft();
}, 1000);
}
@@ -1910,32 +2035,13 @@ async function saveConfig(showNotification = true) {
await refreshPriceLevels({ force: true, noCache: true });
// Get custom price if set
const customPriceInput = document.getElementById('custom-price-input');
const customPriceValue = parseFloat(customPriceInput.value);
const customPrice = customPriceValue > 0 ? customPriceValue : null;
// Get server count
const serverCountValue = serverCount;
try {
const resp = await fetch('/api/configs/' + configUUID, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: configName,
items: cart,
custom_price: customPrice,
notes: '',
server_count: serverCountValue,
server_model: serverModelForQuote,
support_code: supportCode,
article: getCurrentArticle(),
pricelist_id: selectedPricelistIds.estimate,
only_in_stock: onlyInStock
})
body: JSON.stringify(buildSavePayload())
});
if (!resp.ok) {
@@ -1951,6 +2057,9 @@ async function saveConfig(showNotification = true) {
const versionEl = document.getElementById('breadcrumb-config-version');
if (versionEl) versionEl.textContent = 'v' + currentVersionNo;
}
hasUnsavedChanges = false;
clearAutosaveDraft();
exitSaveStarted = false;
if (showNotification) {
showToast('Сохранено', 'success');

View File

@@ -114,38 +114,42 @@
</div>
</div>
<div id="rename-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div id="config-action-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4 p-6">
<h2 class="text-xl font-semibold mb-4">Переименовать квоту</h2>
<h2 class="text-xl font-semibold mb-4">Действия с квотой</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Новое название</label>
<input type="text" id="rename-input"
<label class="block text-sm font-medium text-gray-700 mb-1">Название</label>
<input type="text" id="config-action-name"
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<input type="hidden" id="rename-uuid">
</div>
</div>
<div class="flex justify-end space-x-3 mt-6">
<button onclick="closeRenameModal()" class="px-4 py-2 text-gray-600 hover:text-gray-800">Отмена</button>
<button onclick="renameConfig()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Сохранить</button>
</div>
</div>
</div>
<div id="clone-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4 p-6">
<h2 class="text-xl font-semibold mb-4">Копировать квоту</h2>
<div class="space-y-4">
<label class="flex items-center gap-2 text-sm text-gray-700">
<input type="checkbox" id="config-action-copy" class="rounded border-gray-300">
Создать копию
</label>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Название копии</label>
<input type="text" id="clone-input"
<label class="block text-sm font-medium text-gray-700 mb-1">Проект</label>
<input id="config-action-project-input"
list="config-action-project-options"
placeholder="Начните вводить проект"
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<input type="hidden" id="clone-uuid">
<datalist id="config-action-project-options"></datalist>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Вариант</label>
<input id="config-action-variant-input"
list="config-action-variant-options"
placeholder="Выберите вариант"
class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<datalist id="config-action-variant-options"></datalist>
</div>
<input type="hidden" id="config-action-uuid">
<input type="hidden" id="config-action-current-name">
<input type="hidden" id="config-action-current-project">
</div>
<div class="flex justify-end space-x-3 mt-6">
<button onclick="closeCloneModal()" class="px-4 py-2 text-gray-600 hover:text-gray-800">Отмена</button>
<button onclick="cloneConfig()" class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700">Копировать</button>
<button onclick="closeConfigActionModal()" class="px-4 py-2 text-gray-600 hover:text-gray-800">Отмена</button>
<button onclick="saveConfigAction()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Сохранить</button>
</div>
</div>
</div>
@@ -202,30 +206,13 @@
</div>
</div>
<div id="transfer-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4 p-6">
<h2 class="text-xl font-semibold mb-4">Перенести квоту в другой вариант</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Целевой вариант</label>
<select id="transfer-variant-select" class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
</select>
<input type="hidden" id="transfer-config-uuid">
</div>
</div>
<div class="flex justify-end space-x-3 mt-6">
<button onclick="closeTransferModal()" class="px-4 py-2 text-gray-600 hover:text-gray-800">Отмена</button>
<button onclick="transferConfig()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Перенести</button>
</div>
</div>
</div>
<script>
const projectUUID = '{{.ProjectUUID}}';
let configStatusMode = 'active';
let project = null;
let allConfigs = [];
let projectVariants = [];
let projectsCatalog = [];
let variantMenuInitialized = false;
function escapeHtml(text) {
@@ -258,12 +245,16 @@ function normalizeVariantLabel(variant) {
}
async function loadVariantsForCode(code) {
if (!code) return;
try {
const resp = await fetch('/api/projects/all');
if (!resp.ok) return;
const data = await resp.json();
const allProjects = Array.isArray(data) ? data : (data.projects || []);
projectsCatalog = allProjects.filter(p => p && p.uuid && p.is_active !== false);
if (!code) {
projectVariants = [];
return;
}
projectVariants = allProjects
.filter(p => (p.code || '').trim() === code && p.is_active !== false)
.map(p => ({uuid: p.uuid, variant: (p.variant || '').trim()}));
@@ -401,12 +392,8 @@ function renderConfigs(configs) {
} else {
html += '<a href="/configs/' + c.uuid + '/revisions" class="text-purple-600 hover:text-purple-800 inline-block" title="Ревизии">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg></a>';
html += '<button onclick="openTransferModal(\'' + c.uuid + '\')" class="text-indigo-600 hover:text-indigo-800" title="Перенести в другой вариант">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path></svg></button>';
html += '<button onclick="openCloneModal(\'' + c.uuid + '\', \'' + escapeHtml(c.name).replace(/'/g, "\\'") + '\')" class="text-green-600 hover:text-green-800" title="Копировать">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path></svg></button>';
html += '<button onclick="openRenameModal(\'' + c.uuid + '\', \'' + escapeHtml(c.name).replace(/'/g, "\\'") + '\')" class="text-blue-600 hover:text-blue-800" title="Переименовать">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg></button>';
html += '<button onclick="openConfigActionModal(\'' + c.uuid + '\', \'' + escapeHtml(c.name).replace(/'/g, "\\'") + '\', \'' + (c.project_uuid || projectUUID) + '\')" class="text-indigo-600 hover:text-indigo-800" title="Переименовать / копировать / переместить">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7h16M4 12h16M4 17h16"></path></svg></button>';
html += '<button onclick="deleteConfig(\'' + c.uuid + '\')" class="text-red-600 hover:text-red-800" title="В архив">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg></button>';
}
@@ -576,68 +563,227 @@ async function reactivateConfig(uuid) {
loadConfigs();
}
function openRenameModal(uuid, currentName) {
document.getElementById('rename-uuid').value = uuid;
document.getElementById('rename-input').value = currentName;
document.getElementById('rename-modal').classList.remove('hidden');
document.getElementById('rename-modal').classList.add('flex');
}
function closeRenameModal() {
document.getElementById('rename-modal').classList.add('hidden');
document.getElementById('rename-modal').classList.remove('flex');
}
async function renameConfig() {
const uuid = document.getElementById('rename-uuid').value;
const name = document.getElementById('rename-input').value.trim();
if (!name) {
alert('Введите название');
return;
}
const resp = await fetch('/api/configs/' + uuid + '/rename', {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: name})
function projectCodeEntries() {
const byCode = new Map();
projectsCatalog.forEach(p => {
const code = (p.code || '').trim();
if (!code || byCode.has(code)) return;
byCode.set(code, {
code: code,
name: (p.name || '').trim()
});
});
if (!resp.ok) {
alert('Не удалось переименовать');
return;
return Array.from(byCode.values()).sort((a, b) => a.code.localeCompare(b.code, 'ru'));
}
function formatProjectAutocompleteValue(entry) {
if (!entry) return '';
return entry.name ? (entry.code + ' - ' + entry.name) : entry.code;
}
function resolveProjectCodeFromInput(rawInput) {
const input = (rawInput || '').trim();
if (!input) return '';
const entries = projectCodeEntries();
const exactCode = entries.find(e => e.code.toLowerCase() === input.toLowerCase());
if (exactCode) return exactCode.code;
const exactDisplayMatches = entries.filter(e => formatProjectAutocompleteValue(e).toLowerCase() === input.toLowerCase());
if (exactDisplayMatches.length === 1) return exactDisplayMatches[0].code;
const byUniqueName = entries.filter(e => (e.name || '').toLowerCase() === input.toLowerCase());
if (byUniqueName.length === 1) return byUniqueName[0].code;
if (input.includes(' - ')) {
const codeCandidate = input.split(' - ')[0].trim();
const byCandidate = entries.find(e => e.code.toLowerCase() === codeCandidate.toLowerCase());
if (byCandidate) return byCandidate.code;
}
closeRenameModal();
loadConfigs();
return '';
}
function openCloneModal(uuid, currentName) {
document.getElementById('clone-uuid').value = uuid;
document.getElementById('clone-input').value = currentName + ' (копия)';
document.getElementById('clone-modal').classList.remove('hidden');
document.getElementById('clone-modal').classList.add('flex');
}
function closeCloneModal() {
document.getElementById('clone-modal').classList.add('hidden');
document.getElementById('clone-modal').classList.remove('flex');
}
async function cloneConfig() {
const uuid = document.getElementById('clone-uuid').value;
const name = document.getElementById('clone-input').value.trim();
if (!name) {
alert('Введите название');
return;
}
const resp = await fetch('/api/projects/' + projectUUID + '/configs/' + uuid + '/clone', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: name})
function populateProjectAutocomplete() {
const options = document.getElementById('config-action-project-options');
options.innerHTML = '';
projectCodeEntries().forEach(entry => {
const opt = document.createElement('option');
opt.value = formatProjectAutocompleteValue(entry);
opt.label = entry.code;
options.appendChild(opt);
});
if (!resp.ok) {
alert('Не удалось скопировать');
}
function variantsForProjectCode(projectCode) {
const code = (projectCode || '').trim();
if (!code) return [];
return projectsCatalog
.filter(p => (p.code || '').trim() === code)
.map(p => ({uuid: p.uuid, variant: normalizeVariantLabel(p.variant || '')}))
.sort((a, b) => a.variant.localeCompare(b.variant, 'ru'));
}
function populateVariantAutocomplete(projectCode, selectedVariantLabel) {
const options = document.getElementById('config-action-variant-options');
const input = document.getElementById('config-action-variant-input');
const variants = variantsForProjectCode(projectCode);
options.innerHTML = '';
variants.forEach(v => {
const opt = document.createElement('option');
opt.value = v.variant;
options.appendChild(opt);
});
if (selectedVariantLabel) {
input.value = selectedVariantLabel;
} else if (variants.length === 1) {
input.value = variants[0].variant;
} else {
input.value = '';
}
}
function resolveTargetProjectUUIDFromInputs() {
const projectCode = resolveProjectCodeFromInput(document.getElementById('config-action-project-input').value);
if (!projectCode) {
return {error: 'Выберите проект из подсказок'};
}
const variantLabel = normalizeVariantLabel(document.getElementById('config-action-variant-input').value || 'main');
const target = projectsCatalog.find(p =>
(p.code || '').trim() === projectCode &&
normalizeVariantLabel(p.variant || '') === variantLabel
);
if (!target) {
return {error: 'Выберите вариант из подсказок'};
}
return {uuid: target.uuid};
}
function syncActionModalMode() {
const copyCheckbox = document.getElementById('config-action-copy');
if (copyCheckbox.checked) {
// no-op: copy always uses latest revision
} else {
// no-op: copy always uses latest revision
}
}
async function openConfigActionModal(uuid, currentName, currentProjectUUID) {
document.getElementById('config-action-uuid').value = uuid;
document.getElementById('config-action-current-name').value = currentName;
document.getElementById('config-action-current-project').value = currentProjectUUID || projectUUID;
document.getElementById('config-action-name').value = currentName;
document.getElementById('config-action-copy').checked = false;
populateProjectAutocomplete();
const currentProject = projectsCatalog.find(p => p.uuid === (currentProjectUUID || projectUUID));
if (currentProject) {
const entry = {
code: (currentProject.code || '').trim(),
name: (currentProject.name || '').trim()
};
document.getElementById('config-action-project-input').value = formatProjectAutocompleteValue(entry);
populateVariantAutocomplete(entry.code, normalizeVariantLabel(currentProject.variant || ''));
} else {
document.getElementById('config-action-project-input').value = '';
populateVariantAutocomplete('', '');
}
syncActionModalMode();
document.getElementById('config-action-modal').classList.remove('hidden');
document.getElementById('config-action-modal').classList.add('flex');
const nameInput = document.getElementById('config-action-name');
nameInput.focus();
nameInput.select();
}
function closeConfigActionModal() {
document.getElementById('config-action-modal').classList.add('hidden');
document.getElementById('config-action-modal').classList.remove('flex');
}
async function saveConfigAction() {
const notify = (message, type) => {
if (typeof showToast === 'function') {
showToast(message, type || 'success');
} else {
alert(message);
}
};
const uuid = document.getElementById('config-action-uuid').value;
const currentName = document.getElementById('config-action-current-name').value;
const currentProjectUUID = document.getElementById('config-action-current-project').value || projectUUID;
const name = document.getElementById('config-action-name').value.trim();
const copy = document.getElementById('config-action-copy').checked;
const targetProject = resolveTargetProjectUUIDFromInputs();
if (targetProject.error) {
notify(targetProject.error, 'error');
return;
}
closeCloneModal();
loadConfigs();
const targetProjectUUID = targetProject.uuid || currentProjectUUID;
if (!name) {
notify('Введите название', 'error');
return;
}
if (copy) {
const cloneResp = await fetch('/api/projects/' + targetProjectUUID + '/configs/' + uuid + '/clone', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: name})
});
if (!cloneResp.ok) {
notify('Не удалось скопировать квоту', 'error');
return;
}
closeConfigActionModal();
await loadConfigs();
notify('Копия создана', 'success');
if (targetProjectUUID && targetProjectUUID !== projectUUID) {
window.location.href = '/projects/' + targetProjectUUID;
return;
}
return;
}
let changed = false;
if (name !== currentName) {
const renameResp = await fetch('/api/configs/' + uuid + '/rename', {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: name})
});
if (!renameResp.ok) {
notify('Не удалось переименовать квоту', 'error');
return;
}
changed = true;
}
if (targetProjectUUID !== currentProjectUUID) {
const moveResp = await fetch('/api/configs/' + uuid + '/project', {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({project_uuid: targetProjectUUID})
});
if (!moveResp.ok) {
notify('Не удалось перенести квоту', 'error');
return;
}
changed = true;
}
if (!changed) {
closeConfigActionModal();
return;
}
closeConfigActionModal();
await loadConfigs();
notify('Изменения сохранены', 'success');
if (targetProjectUUID && targetProjectUUID !== projectUUID) {
window.location.href = '/projects/' + targetProjectUUID;
}
}
function openImportModal() {
@@ -822,62 +968,29 @@ function updateDeleteVariantButton() {
}
}
function openTransferModal(configUUID) {
const select = document.getElementById('transfer-variant-select');
select.innerHTML = '';
const otherVariants = projectVariants.filter(v => v.uuid !== projectUUID);
if (otherVariants.length === 0) {
alert('Нет других вариантов для переноса');
return;
}
otherVariants.forEach(v => {
const opt = document.createElement('option');
opt.value = v.uuid;
opt.textContent = normalizeVariantLabel(v.variant);
select.appendChild(opt);
});
document.getElementById('transfer-config-uuid').value = configUUID;
document.getElementById('transfer-modal').classList.remove('hidden');
document.getElementById('transfer-modal').classList.add('flex');
}
function closeTransferModal() {
document.getElementById('transfer-modal').classList.add('hidden');
document.getElementById('transfer-modal').classList.remove('flex');
}
async function transferConfig() {
const configUUID = document.getElementById('transfer-config-uuid').value;
const targetProjectUUID = document.getElementById('transfer-variant-select').value;
if (!configUUID || !targetProjectUUID) return;
const resp = await fetch('/api/configs/' + configUUID + '/project', {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({project_uuid: targetProjectUUID})
});
if (!resp.ok) {
alert('Не удалось перенести квоту');
return;
}
closeTransferModal();
loadConfigs();
}
document.getElementById('create-modal').addEventListener('click', function(e) { if (e.target === this) closeCreateModal(); });
document.getElementById('rename-modal').addEventListener('click', function(e) { if (e.target === this) closeRenameModal(); });
document.getElementById('new-variant-modal').addEventListener('click', function(e) { if (e.target === this) closeNewVariantModal(); });
document.getElementById('clone-modal').addEventListener('click', function(e) { if (e.target === this) closeCloneModal(); });
document.getElementById('config-action-modal').addEventListener('click', function(e) { if (e.target === this) closeConfigActionModal(); });
document.getElementById('import-modal').addEventListener('click', function(e) { if (e.target === this) closeImportModal(); });
document.getElementById('project-settings-modal').addEventListener('click', function(e) { if (e.target === this) closeProjectSettingsModal(); });
document.getElementById('transfer-modal').addEventListener('click', function(e) { if (e.target === this) closeTransferModal(); });
document.getElementById('config-action-project-input').addEventListener('input', function(e) {
const code = resolveProjectCodeFromInput(e.target.value);
populateVariantAutocomplete(code, '');
});
document.getElementById('config-action-copy').addEventListener('change', function(e) {
const currentName = document.getElementById('config-action-current-name').value;
const nameInput = document.getElementById('config-action-name');
if (e.target.checked && nameInput.value.trim() === currentName.trim()) {
nameInput.value = currentName + ' (копия)';
}
syncActionModalMode();
});
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
closeCreateModal();
closeRenameModal();
closeCloneModal();
closeConfigActionModal();
closeImportModal();
closeProjectSettingsModal();
closeTransferModal();
}
});