send history files to server

This commit is contained in:
Zhora Shalyapin 2025-03-13 09:26:07 +00:00
parent 13ac788635
commit 884fc7088f

57
main.js
View File

@ -16,12 +16,23 @@
'use strict'; 'use strict';
let fileInputs = { let fileInputs = {
"attachment1": "legend_photo", "attachment1": {
"attachment4": "legend_files", "l": "legend_photo",
"h": "history_photo"
},
"attachment4": {
"l": "legend_files",
"h": "history_files"
},
"attachment2": "admin_photo", "attachment2": "admin_photo",
"attachment3": "admin_files" "attachment3": "admin_files"
} }
let fileInpitVariants = {
"l": "для легенды",
"h": "для ист. справки"
}
const localStorageItems = { const localStorageItems = {
NEED_UPDATE_ID: "needUpdateId", NEED_UPDATE_ID: "needUpdateId",
JUST_CREATED: "justCreated", JUST_CREATED: "justCreated",
@ -231,10 +242,11 @@
return downloadLink return downloadLink
} }
async function sendFileDeleted(inputName) { async function sendFileDeleted(inputName, fileInputVariant = null) {
let formData = new FormData() let formData = new FormData()
let normalInputName = fileInputs[inputName] console.log(fileInputs[fileInputVariant])
let normalInputName = fileInputVariant !== null ? fileInputs[inputName][fileInputVariant] : fileInputs[inputName]
formData.set(`cp[id]`, document.querySelector("input[name='cp[id]']").value) formData.set(`cp[id]`, document.querySelector("input[name='cp[id]']").value)
formData.set(`cp[${normalInputName}]`, "-1") formData.set(`cp[${normalInputName}]`, "-1")
let result = await updatePoint(formData) let result = await updatePoint(formData)
@ -246,17 +258,25 @@
} }
} }
function addDisplayedFile(inputName, type, file, preview, removeLink, purpose = null) { function parsePurpose(cell) {
return cell?.querySelector("td:nth-child(2)")?.innerHTML.match(/для[^\|]+/g)?.[0].trim()
}
function parsefileInputVariant(cell) {
return Object.keys(fileInpitVariants).find(key => fileInpitVariants[key] === parsePurpose(cell))
}
function addDisplayedFile(inputName, type, file, preview, removeLink, variant = null) {
if (!inputName || !file || !removeLink) return if (!inputName || !file || !removeLink) return
let fileListContainer let fileListContainer
if (purpose !== null) { if (variant !== null) {
if (inputName == "attachment1" || inputName == "attachment4") { if (inputName == "attachment1" || inputName == "attachment4") {
if (purpose == "для легенды") { if (variant == "l") {
let fileContainerClassName = fileInputs[inputName].replace("_", "-") let fileContainerClassName = fileInputs[inputName][variant].replace("_", "-")
fileListContainer = document.querySelector(`.${fileContainerClassName}-container .file-list-container`) fileListContainer = document.querySelector(`.${fileContainerClassName}-container .file-list-container`)
} }
else if (purpose == "для ист. справки") { else if (variant == "h") {
let filesContainer = document.querySelector(inputName == "attachment1" ? `.history-photo-container` : `.history-files-container`) let filesContainer = document.querySelector(inputName == "attachment1" ? `.history-photo-container` : `.history-files-container`)
fileListContainer = filesContainer.querySelector(`.file-list-container`) fileListContainer = filesContainer.querySelector(`.file-list-container`)
if (fileListContainer == null) { if (fileListContainer == null) {
@ -307,7 +327,7 @@
if (!confirm("Точно?")) return if (!confirm("Точно?")) return
await fetch(removeLink) await fetch(removeLink)
await sendFileDeleted(inputName) await sendFileDeleted(inputName, variant)
removedFilesLinks.push(removeLink) removedFilesLinks.push(removeLink)
fileListContainer.removeChild(fileContainer) fileListContainer.removeChild(fileContainer)
@ -371,7 +391,7 @@
let fileListContainer = this.parentElement.parentElement.querySelector(".file-list-container") let fileListContainer = this.parentElement.parentElement.querySelector(".file-list-container")
let fileContainer = document.createElement("div") let fileContainer = document.createElement("div")
fileContainer.classList.add("file-container") fileContainer.classList.add("file-container", "file-container-new")
const displayedFile = getHtmlElementByFileType(file) const displayedFile = getHtmlElementByFileType(file)
displayedFile.classList.add("preview") displayedFile.classList.add("preview")
@ -522,9 +542,9 @@
return return
} }
let purpose = row.querySelector("td:nth-child(2)")?.innerHTML.match(/для[^\|]+/g)?.[0].trim() let variant = parsefileInputVariant(row)
addDisplayedFile(fileInput, type, fileUrl, preview, deleteUrl, purpose) addDisplayedFile(fileInput, type, fileUrl, preview, deleteUrl, variant)
} }
} }
} }
@ -672,10 +692,10 @@
async function sendForm(onSend) { async function sendForm(onSend) {
let formData = new FormData(document.querySelector("form")) let formData = new FormData(document.querySelector("form"))
let fileContainers = [...document.querySelectorAll(".file-list-container")] let fileListContainers = [...document.querySelectorAll(".file-list-container")]
let fileInputNames = ["legend_photo", "legend_files", "admin_photo", "admin_files"] let fileInputNames = ["legend_photo", "history_photo", "legend_files", "history_files", "admin_photo", "admin_files"]
for (let [i, fileContainer] of fileContainers.entries()) { for (let [i, fileListContainer] of fileListContainers.entries()) {
formData.set(`cp[${fileInputNames[i]}]`, fileContainer.children.length) formData.set(`cp[${fileInputNames[i]}]`, [...fileListContainer.children].filter(el => !el.classList.contains("file-container-new")).length)
} }
if (formData.get("cp[id]") == '') { if (formData.get("cp[id]") == '') {
@ -770,12 +790,13 @@
for (const link of links) { for (const link of links) {
let shortHref = link.href.split("/").at(-1) let shortHref = link.href.split("/").at(-1)
let fileInput = document.querySelector(`tr:has(a[href$="${shortHref}"]) ~ tr:has(input[type="file"]) input[type="file"]`) let fileInput = document.querySelector(`tr:has(a[href$="${shortHref}"]) ~ tr:has(input[type="file"]) input[type="file"]`)
let fileInputVariant = parsefileInputVariant(document.querySelector(`tr:has(a[href$="${shortHref}"])`))
link.addEventListener("click", async e => { link.addEventListener("click", async e => {
e.preventDefault() e.preventDefault()
if (!confirm("Точно?")) return if (!confirm("Точно?")) return
await sendFileDeleted(fileInput.name.split("[")[0]) await sendFileDeleted(fileInput.name.split("[")[0], fileInputVariant)
location.href = link.href location.href = link.href
}) })
} }