99 lines
4.1 KiB
JavaScript
99 lines
4.1 KiB
JavaScript
(function() {
|
||
|
||
const checkbox = document.querySelector('form #generator-generatemigration');
|
||
|
||
function toggleFields() {
|
||
const isChecked = checkbox.checked;
|
||
const migrationFields = document.querySelectorAll('form .field-generator-migrationns, form .field-generator-migrationname');
|
||
migrationFields.forEach(function(item) {
|
||
item.style.display = isChecked ? '' : 'none';
|
||
});
|
||
}
|
||
checkbox.addEventListener('change', toggleFields);
|
||
|
||
toggleFields();
|
||
|
||
let modelNsInput = document.querySelector('#generator-modelns');
|
||
let modelInput = document.querySelector('#generator-modelname');
|
||
let controllerInput = document.querySelector('#generator-controllerid');
|
||
let attributesContainer = document.querySelector('#generator-attributes');
|
||
|
||
let autoUpdateController = true;
|
||
let debounceTimer = null;
|
||
|
||
controllerInput && controllerInput.addEventListener('input', () => {
|
||
autoUpdateController = false;
|
||
});
|
||
|
||
modelInput && modelInput.addEventListener('input', () => {
|
||
if (debounceTimer) clearTimeout(debounceTimer);
|
||
debounceTimer = setTimeout(() => {
|
||
const modelNs = modelNsInput.value.trim();
|
||
if (modelNs.length === 0) return;
|
||
|
||
const modelClassName = modelInput.value.trim();
|
||
if (modelClassName.length === 0) return;
|
||
|
||
const data = new FormData();
|
||
data.append('Generator[modelNs]', modelNs)
|
||
data.append('Generator[modelName]', modelClassName)
|
||
data.append(yii.getCsrfParam(), yii.getCsrfToken());
|
||
|
||
// AJAX-запрос на получение атрибутов
|
||
fetch('default/action?id=dictionary&name=GetAttributes', {
|
||
method: 'POST',
|
||
body: data,
|
||
})
|
||
.then(res => res.json())
|
||
.then(data => {
|
||
if(data.result) {
|
||
if (attributesContainer) {
|
||
attributesContainer.innerHTML = '';
|
||
data.attributes.forEach(attr => {
|
||
|
||
let label = document.createElement('label');
|
||
label.className = 'checkbox-label';
|
||
|
||
let checkbox = document.createElement('input');
|
||
checkbox.type = 'checkbox';
|
||
checkbox.name = 'Generator[attributes][]';
|
||
checkbox.value = attr;
|
||
|
||
label.appendChild(checkbox);
|
||
label.appendChild(document.createTextNode(' ' + attr));
|
||
attributesContainer.appendChild(label);
|
||
attributesContainer.appendChild(document.createElement('br'));
|
||
});
|
||
}
|
||
|
||
controllerInput.value = data.controller_id;
|
||
generateMigrationName(data.controller_id);
|
||
}
|
||
|
||
});
|
||
}, 500);
|
||
});
|
||
|
||
function generateMigrationName(controllerId) {
|
||
let migrationNameInput = document.querySelector('#generator-migrationname');
|
||
// Получаем текущие дата и время
|
||
const now = new Date();
|
||
|
||
// Форматируем дату и время в виде "yymmdd_hhmmss"
|
||
const year = now.getFullYear().toString().slice(-2);
|
||
const month = ('0' + (now.getMonth() + 1)).slice(-2);
|
||
const day = ('0' + now.getDate()).slice(-2);
|
||
const hours = ('0' + now.getHours()).slice(-2);
|
||
const minutes = ('0' + now.getMinutes()).slice(-2);
|
||
const seconds = ('0' + now.getSeconds()).slice(-2);
|
||
|
||
const timestamp = `${year}${month}${day}_${hours}${minutes}${seconds}`;
|
||
|
||
// Модифицируем имя таблицы: заменяем дефисы на подчеркивания, убираем расширения
|
||
// Предположим, что tableName — это строка с дефисами, например: "furniture-packs"
|
||
const formattedName = controllerId.replace(/-/g, '_');
|
||
|
||
migrationNameInput.value = `m${timestamp}_insert_${formattedName}_permissions`;
|
||
}
|
||
|
||
})(); |