cloneparts
This commit is contained in:
@@ -2,93 +2,111 @@
|
||||
* modals_gridData.js — Photos, Parts, Tested Component handlers for gridData pages
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
// ── Photos — use photos.js loadPopupContent (exported to window) ──
|
||||
$(document).on('click', '.photos-btn', function () {
|
||||
const iddatadb = $(this).data('iddatadb') || null;
|
||||
const idquotations = $(this).data('idquotations') || null;
|
||||
const modal = document.getElementById('photosModal');
|
||||
$(document).on("click", ".photos-btn", function () {
|
||||
const iddatadb = $(this).data("iddatadb") || null;
|
||||
const idquotations = $(this).data("idquotations") || null;
|
||||
const modal = document.getElementById("photosModal");
|
||||
if (!modal) return;
|
||||
|
||||
modal.style.display = 'block';
|
||||
modal.style.display = "block";
|
||||
|
||||
if (typeof window.loadPopupContent === 'function') {
|
||||
if (typeof window.loadPopupContent === "function") {
|
||||
window.loadPopupContent(iddatadb, idquotations);
|
||||
}
|
||||
});
|
||||
|
||||
// Close photos modal
|
||||
$(document).on('click', '.close-btn', function () {
|
||||
const modal = document.getElementById('photosModal');
|
||||
if (modal) modal.style.display = 'none';
|
||||
$(document).on("click", ".close-btn", function () {
|
||||
const modal = document.getElementById("photosModal");
|
||||
if (modal) modal.style.display = "none";
|
||||
});
|
||||
|
||||
// Close on backdrop click
|
||||
$(document).on('click', '#photosModal', function (e) {
|
||||
$(document).on("click", "#photosModal", function (e) {
|
||||
if (e.target === this) {
|
||||
this.style.display = 'none';
|
||||
this.style.display = "none";
|
||||
}
|
||||
});
|
||||
|
||||
// ── Parts (matching import_edit2.php behavior) ─────────────────────
|
||||
$(document).on('click', '.parts-btn', function () {
|
||||
const iddatadb = $(this).data('iddatadb') || null;
|
||||
const idquotations = $(this).data('idquotations') || null;
|
||||
$(document).on("click", ".parts-btn", function () {
|
||||
const iddatadb = $(this).data("iddatadb") || null;
|
||||
const idquotations = $(this).data("idquotations") || null;
|
||||
|
||||
$.ajax({
|
||||
url: 'modal_partsTable.php',
|
||||
method: 'GET',
|
||||
url: "modal_partsTable.php",
|
||||
method: "GET",
|
||||
data: { iddatadb: iddatadb },
|
||||
success: function (response) {
|
||||
$('#partsModalContainer').html(response);
|
||||
const modalElement = document.getElementById('partsModal');
|
||||
$("#partsModalContainer").html(response);
|
||||
const modalElement = document.getElementById("partsModal");
|
||||
if (!modalElement) return;
|
||||
|
||||
$("#trfHeader").text(iddatadb || idquotations || '');
|
||||
$("#partsModal").data("iddatadb", iddatadb).data("idquotations", idquotations);
|
||||
$("#trfHeader").text(iddatadb || idquotations || "");
|
||||
|
||||
const visibleIddatadbList = Array.isArray(window.gridData)
|
||||
? window.gridData
|
||||
.map((r) => parseInt(r.iddatadb, 10))
|
||||
.filter((v) => !isNaN(v) && v > 0)
|
||||
: [];
|
||||
|
||||
$("#partsModal")
|
||||
.data("iddatadb", iddatadb)
|
||||
.data("idquotations", idquotations)
|
||||
.data("visible-iddatadb-list", visibleIddatadbList);
|
||||
|
||||
let modal = bootstrap.Modal.getInstance(modalElement);
|
||||
if (!modal) modal = new bootstrap.Modal(modalElement, { backdrop: true, keyboard: true, focus: true });
|
||||
if (!modal)
|
||||
modal = new bootstrap.Modal(modalElement, {
|
||||
backdrop: true,
|
||||
keyboard: true,
|
||||
focus: true,
|
||||
});
|
||||
modal.show();
|
||||
|
||||
if (typeof window.loadParts === 'function') {
|
||||
if (typeof window.loadParts === "function") {
|
||||
window.loadParts(iddatadb, idquotations);
|
||||
}
|
||||
},
|
||||
error: function (xhr, status, error) {
|
||||
console.error('Error loading parts:', error);
|
||||
}
|
||||
console.error("Error loading parts:", error);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('hidden.bs.modal', '#partsModal', function () {
|
||||
const modalElement = document.getElementById('partsModal');
|
||||
$(document).on("hidden.bs.modal", "#partsModal", function () {
|
||||
const modalElement = document.getElementById("partsModal");
|
||||
if (modalElement) {
|
||||
const modal = bootstrap.Modal.getInstance(modalElement);
|
||||
if (modal) modal.dispose();
|
||||
}
|
||||
$('#partsModalContainer').empty();
|
||||
$('.modal-backdrop').remove();
|
||||
$('body').removeClass('modal-open').css('padding-right', '');
|
||||
$("#partsModalContainer").empty();
|
||||
$(".modal-backdrop").remove();
|
||||
$("body").removeClass("modal-open").css("padding-right", "");
|
||||
});
|
||||
|
||||
// ── Tested Component quick add ───────────────────────────────────────
|
||||
$(document).on('click', '.add-part-btn', async function () {
|
||||
const iddatadb = $(this).data('iddatadb') || null;
|
||||
const rowIndex = parseInt($(this).data('row'));
|
||||
$(document).on("click", ".add-part-btn", async function () {
|
||||
const iddatadb = $(this).data("iddatadb") || null;
|
||||
const rowIndex = parseInt($(this).data("row"));
|
||||
const row = window.gridData?.[rowIndex];
|
||||
const id = iddatadb || (row ? row.iddatadb : null);
|
||||
if (!id) return;
|
||||
|
||||
const $cell = $(this).closest('.grid-cell, div');
|
||||
const $input = $cell.find('input');
|
||||
const raw = ($input.val() || '').trim();
|
||||
const parts = raw.split('|').map(s => s.trim()).filter(s => s.length > 0);
|
||||
const $cell = $(this).closest(".grid-cell, div");
|
||||
const $input = $cell.find("input");
|
||||
const raw = ($input.val() || "").trim();
|
||||
const parts = raw
|
||||
.split("|")
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s.length > 0);
|
||||
const uniqueParts = [...new Set(parts)];
|
||||
|
||||
if (!uniqueParts.length) {
|
||||
alert('Insert a description first.');
|
||||
alert("Insert a description first.");
|
||||
$input.focus();
|
||||
return;
|
||||
}
|
||||
@@ -96,14 +114,17 @@
|
||||
try {
|
||||
for (const p of uniqueParts) {
|
||||
const formData = new FormData();
|
||||
formData.append('iddatadb', id);
|
||||
formData.append('part_description', p);
|
||||
await fetch('add_part_quick.php', { method: 'POST', body: formData });
|
||||
formData.append("iddatadb", id);
|
||||
formData.append("part_description", p);
|
||||
await fetch("add_part_quick.php", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
}
|
||||
alert(`Added ${uniqueParts.length} part(s).`);
|
||||
$input.val('');
|
||||
$input.val("");
|
||||
} catch (e) {
|
||||
alert('Error: ' + e.message);
|
||||
alert("Error: " + e.message);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -111,44 +132,47 @@
|
||||
let deleteIddatadb = null;
|
||||
let deleteRowIndex = null;
|
||||
|
||||
$(document).on('click', '.delete-btn', function () {
|
||||
deleteIddatadb = $(this).data('iddatadb');
|
||||
deleteRowIndex = parseInt($(this).data('row'));
|
||||
const modalEl = document.getElementById('deleteConfirmModal');
|
||||
$(document).on("click", ".delete-btn", function () {
|
||||
deleteIddatadb = $(this).data("iddatadb");
|
||||
deleteRowIndex = parseInt($(this).data("row"));
|
||||
const modalEl = document.getElementById("deleteConfirmModal");
|
||||
if (modalEl) {
|
||||
document.getElementById('deleteIddatadbText').textContent = deleteIddatadb;
|
||||
document.getElementById("deleteIddatadbText").textContent =
|
||||
deleteIddatadb;
|
||||
new bootstrap.Modal(modalEl).show();
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('click', '#deleteConfirmBtn', async function () {
|
||||
const modalEl = document.getElementById('deleteConfirmModal');
|
||||
$(document).on("click", "#deleteConfirmBtn", async function () {
|
||||
const modalEl = document.getElementById("deleteConfirmModal");
|
||||
const modal = bootstrap.Modal.getInstance(modalEl);
|
||||
if (modal) modal.hide();
|
||||
|
||||
if (!deleteIddatadb) return;
|
||||
|
||||
try {
|
||||
const resp = await fetch('delete_record.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ id: deleteIddatadb })
|
||||
const resp = await fetch("delete_record.php", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ id: deleteIddatadb }),
|
||||
});
|
||||
const result = await resp.json();
|
||||
|
||||
if (result.success) {
|
||||
// Remove from gridData
|
||||
const idx = window.gridData.findIndex(r => r.iddatadb === deleteIddatadb);
|
||||
const idx = window.gridData.findIndex(
|
||||
(r) => r.iddatadb === deleteIddatadb,
|
||||
);
|
||||
if (idx >= 0) window.gridData.splice(idx, 1);
|
||||
|
||||
// Re-render
|
||||
const gr = window.gridRenderer;
|
||||
if (gr) gr.renderVisibleRows();
|
||||
} else {
|
||||
alert('Error: ' + result.message);
|
||||
alert("Error: " + result.message);
|
||||
}
|
||||
} catch (e) {
|
||||
alert('Error: ' + e.message);
|
||||
alert("Error: " + e.message);
|
||||
}
|
||||
|
||||
deleteIddatadb = null;
|
||||
@@ -156,20 +180,26 @@
|
||||
});
|
||||
|
||||
// ── Add new row ──────────────────────────────────────────────────────
|
||||
$(document).on('click', '#addRowBtn', async function () {
|
||||
$(document).on("click", "#addRowBtn", async function () {
|
||||
const btn = this;
|
||||
btn.disabled = true;
|
||||
|
||||
try {
|
||||
const templateId = window.gridMeta?.templateId;
|
||||
if (!templateId) { alert('Template ID missing'); return; }
|
||||
if (!templateId) {
|
||||
alert("Template ID missing");
|
||||
return;
|
||||
}
|
||||
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const importref = urlParams.get('importref') || '';
|
||||
const resp = await fetch('add_record.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ template_id: templateId, importreferencecode: importref })
|
||||
const importref = urlParams.get("importref") || "";
|
||||
const resp = await fetch("add_record.php", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
template_id: templateId,
|
||||
importreferencecode: importref,
|
||||
}),
|
||||
});
|
||||
const result = await resp.json();
|
||||
|
||||
@@ -177,17 +207,20 @@
|
||||
// Build new row object
|
||||
const newRow = {
|
||||
iddatadb: result.iddatadb,
|
||||
status: 'i',
|
||||
idclient: window.gridMeta?.defaultIdclient || '',
|
||||
status: "i",
|
||||
idclient: window.gridMeta?.defaultIdclient || "",
|
||||
cliente_fornitore_id: null,
|
||||
commessaweb: null,
|
||||
user_name: result.user_name || '',
|
||||
importreferencecode: result.importreferencecode || '',
|
||||
filename_import: '',
|
||||
importdate: new Date().toISOString().slice(0, 19).replace('T', ' '),
|
||||
user_name: result.user_name || "",
|
||||
importreferencecode: result.importreferencecode || "",
|
||||
filename_import: "",
|
||||
importdate: new Date()
|
||||
.toISOString()
|
||||
.slice(0, 19)
|
||||
.replace("T", " "),
|
||||
fixedFields: {},
|
||||
details: {},
|
||||
mainFieldValue: '',
|
||||
mainFieldValue: "",
|
||||
_dirty: false,
|
||||
};
|
||||
|
||||
@@ -199,20 +232,27 @@
|
||||
if (gr) gr.renderVisibleRows();
|
||||
|
||||
// Highlight new row briefly
|
||||
const newGridRow = document.querySelector(`.grid-row[data-id="${result.iddatadb}"]`);
|
||||
const newGridRow = document.querySelector(
|
||||
`.grid-row[data-id="${result.iddatadb}"]`,
|
||||
);
|
||||
if (newGridRow) {
|
||||
newGridRow.classList.add('row-just-created');
|
||||
newGridRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||
setTimeout(() => newGridRow.classList.remove('row-just-created'), 4000);
|
||||
newGridRow.classList.add("row-just-created");
|
||||
newGridRow.scrollIntoView({
|
||||
behavior: "smooth",
|
||||
block: "center",
|
||||
});
|
||||
setTimeout(
|
||||
() => newGridRow.classList.remove("row-just-created"),
|
||||
4000,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
alert('Error: ' + (result.message || 'Unknown error'));
|
||||
alert("Error: " + (result.message || "Unknown error"));
|
||||
}
|
||||
} catch (e) {
|
||||
alert('Error: ' + e.message);
|
||||
alert("Error: " + e.message);
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user