update preview foto dan alamat surveyor

This commit is contained in:
majid
2024-12-05 01:34:16 +07:00
parent 405dc82ac6
commit d797b0d2c0
8 changed files with 442 additions and 293 deletions

View File

@@ -202,94 +202,132 @@
function setupInputHandlers(containerId, buttonId, labelText, inputDataClass, buttonDeleteClass) {
const inputContainer = document.getElementById(containerId);
const addButton = document.getElementById(buttonId);
const addButton = document.getElementById(buttonId);
const inputContainer = document.getElementById(containerId);
function updateLabels() {
const labels = inputContainer.querySelectorAll('.form-label span');
labels.forEach((label, index) => {
label.textContent = `${labelText} ${index + 1}`;
});
if (!addButton || !inputContainer) {
console.error(`Element with ID ${containerId} or ${buttonId} not found.`);
return;
}
function updateLabels() {
const labels = inputContainer.querySelectorAll('.form-label span');
labels.forEach((label, index) => {
label.textContent = `${labelText} ${index + 1}`;
});
}
function handleDeleteButtons() {
const deleteBtns = inputContainer.querySelectorAll(`.${buttonDeleteClass}`);
deleteBtns.forEach(btn => {
btn.style.display = inputContainer.children.length > 1 ? 'block' : 'none';
});
}
function createNewInput() {
const newDiv = inputContainer.children[0].cloneNode(true);
// Reset semua input dalam elemen baru
const inputFile = newDiv.querySelector(`.${inputDataClass}`);
if (inputFile) {
inputFile.id = `inputLingkungan-${inputContainer.children.length}`;
inputFile.value = ''; // Reset input file
// Tambahkan event listener untuk preview
inputFile.addEventListener('change', function () {
const file = this.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = document.getElementById(
`foto_lingkungan_preview_${inputFile.id}`
);
img.src = e.target.result;
img.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
}
function handleDeleteButtons() {
const deleteBtns = inputContainer.querySelectorAll(`.${buttonDeleteClass}`);
deleteBtns.forEach(btn => {
btn.style.display = inputContainer.children.length > 1 ? 'block' : 'none';
});
}
function createNewInput() {
const newDiv = inputContainer.children[0].cloneNode(true);
const inputFile = newDiv.querySelector(`.${inputDataClass}`);
inputFile.id = `inputRute-${inputContainer.children.length}`;
// Reset input file value
if (inputFile) {
inputFile.value = '';
}
// Update the camera button to set currentInputField
const cameraButton = newDiv.querySelector('#btnCamera');
if (cameraButton) {
cameraButton.addEventListener('click', function(e) {
e.preventDefault();
currentInputField = inputFile; // Set current input field to the new input
modal.classList.remove('hidden');
modal.classList.add('modal-open');
editor.startCamera();
});
}
// Add delete button functionality
const deleteBtn = newDiv.querySelector(`.${buttonDeleteClass}`);
if (deleteBtn) {
deleteBtn.addEventListener('click', function() {
inputContainer.removeChild(newDiv);
handleDeleteButtons();
updateLabels();
});
}
// Update preview container
const previewContainer = document.createElement('div');
previewContainer.className = 'preview-container';
const img = document.createElement('img');
img.id = `foto_rute-preview-${inputFile.id}`;
img.src = '';
img.className = 'mt-2 h-auto';
img.style.display = 'none';
img.style.width = '30rem';
previewContainer.appendChild(img);
// Append preview container to the new input group
const inputGroup = newDiv.querySelector('.input-group');
inputGroup.appendChild(previewContainer);
newDiv.style.marginTop = '10px';
inputContainer.appendChild(newDiv);
updateLabels();
handleDeleteButtons();
}
if (addButton) {
addButton.addEventListener('click', createNewInput);
}
const firstDeleteBtn = inputContainer.children[0].querySelector(`.${buttonDeleteClass}`);
if (firstDeleteBtn) {
firstDeleteBtn.addEventListener('click', function() {
if (inputContainer.children.length > 1) {
inputContainer.removeChild(this.closest('.flex'));
handleDeleteButtons();
updateLabels();
}
});
}
// Tambahkan logika reset untuk elemen preview
const imgPreview = newDiv.querySelector('img');
if (imgPreview) {
imgPreview.src = '';
imgPreview.style.display = 'none';
}
const deleteBtn = newDiv.querySelector(`.${buttonDeleteClass}`);
if (deleteBtn) {
deleteBtn.addEventListener('click', function () {
inputContainer.removeChild(newDiv);
handleDeleteButtons();
updateLabels();
});
}
// Tambahkan container preview jika tidak ada
let previewContainer = newDiv.querySelector('.preview-container');
if (!previewContainer) {
previewContainer = document.createElement('div');
previewContainer.className = 'preview-container';
const img = document.createElement('img');
img.id = `foto_lingkungan_preview_${inputFile.id}`;
img.src = '';
img.className = 'mt-2 h-auto';
img.style.display = 'none';
img.style.width = '30rem';
previewContainer.appendChild(img);
const inputGroup = newDiv.querySelector('.input-group');
inputGroup.appendChild(previewContainer);
}
newDiv.style.marginTop = '10px';
inputContainer.appendChild(newDiv);
updateLabels();
handleDeleteButtons();
}
addButton.addEventListener('click', createNewInput);
// Terapkan event listener pada elemen yang sudah ada
const existingInputs = inputContainer.querySelectorAll(`.${inputDataClass}`);
existingInputs.forEach((input, index) => {
const deleteBtn = input.closest('.flex').querySelector(`.${buttonDeleteClass}`);
if (deleteBtn) {
deleteBtn.addEventListener('click', function () {
if (inputContainer.children.length > 1) {
inputContainer.removeChild(this.closest('.flex'));
handleDeleteButtons();
updateLabels();
}
});
}
input.addEventListener('change', function () {
const file = this.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const img = document.getElementById(
`foto_lingkungan_preview_${index}`
);
img.src = e.target.result;
img.style.display = 'block';
};
reader.readAsDataURL(file);
}
});
});
updateLabels();
handleDeleteButtons();
}
setupInputHandlers('inputContainerRute', 'btnRute', 'Foto Rute Menuju Lokasi', 'file-input',
'delete-btn');