diff --git a/main.js b/main.js index 754363f..31adfd8 100644 --- a/main.js +++ b/main.js @@ -94,11 +94,14 @@ } } else if (name == "classes") { - element.classList.add(value.split(" ")) + element.classList.add(...value.split(" ")) } else if (name == "children") { element.append(...value) } + else if (name.startsWith("_")) { + element.setAttribute(name.substring(1), value) + } else element[name] = value } @@ -182,7 +185,7 @@ let prop = new Property() div = prop.toDiv(options.name) } - container.appendChild(div) + container.append(div) } return container @@ -202,19 +205,19 @@ let css = document.createElement("link") css.href = link css.rel = "stylesheet" - document.head.appendChild(css) + document.head.append(css) } function addJs(link) { let script = document.createElement("script") script.src = link - document.head.appendChild(script) + document.head.append(script) } function addStylesToHead(styles) { let styleSheet = document.createElement("style") styleSheet.textContent = styles - document.head.appendChild(styleSheet) + document.head.append(styleSheet) } const sleep = ms => new Promise(res => setTimeout(res, ms)) @@ -304,21 +307,24 @@ } function copyCoordinates() { - let copyButton = document.createElement("button") - copyButton.addEventListener("click", async event => { - event.preventDefault() - let lat = document.querySelector(`input[name="cp[lattitude]"]`).value - let lon = document.querySelector(`input[name="cp[longitude]"]`).value + let copyButton = Tag.button({ + classes: "copy-button", + on: { + click: async event => { + event.preventDefault() + let lat = document.querySelector(`input[name="cp[lattitude]"]`).value + let lon = document.querySelector(`input[name="cp[longitude]"]`).value - const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" }) - const data = new ClipboardItem({ "text/plain": text }) - await navigator.clipboard.write([data]) + const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" }) + const data = new ClipboardItem({ "text/plain": text }) + await navigator.clipboard.write([data]) + } + } }) - copyButton.classList.add("copy-button") let copyImage = document.createElement("img") copyImage.src = "https://upload.wikimedia.org/wikipedia/commons/a/aa/Bw_copy_icon_320x320.svg" - copyButton.appendChild(copyImage) + copyButton.append(copyImage) return copyButton } @@ -352,7 +358,7 @@ let icon = document.createElement("img") icon.src = iconSrc icon.classList.add("map-icon") - ref.appendChild(icon) + ref.append(icon) return ref } @@ -365,11 +371,11 @@ let linksContainer = document.createElement("div") - linksContainer.appendChild(copyCoordinates()) - linksContainer.appendChild(makeRef(yandexMaps, "https://upload.wikimedia.org/wikipedia/commons/7/72/Yandex_Maps_icon.svg", ZOOM)) - linksContainer.appendChild(makeRef(googleMaps, "https://upload.wikimedia.org/wikipedia/commons/a/aa/Google_Maps_icon_%282020%29.svg", METERS)) - linksContainer.appendChild(makeRef(pastvu, "https://pastvu.com/coast-icon.png", ZOOM)) - linksContainer.appendChild(makeRef(twoGis, "https://d-assets.2gis.ru/favicon.png", ZOOM)) + linksContainer.append(copyCoordinates()) + linksContainer.append(makeRef(yandexMaps, "https://upload.wikimedia.org/wikipedia/commons/7/72/Yandex_Maps_icon.svg", ZOOM)) + linksContainer.append(makeRef(googleMaps, "https://upload.wikimedia.org/wikipedia/commons/a/aa/Google_Maps_icon_%282020%29.svg", METERS)) + linksContainer.append(makeRef(pastvu, "https://pastvu.com/coast-icon.png", ZOOM)) + linksContainer.append(makeRef(twoGis, "https://d-assets.2gis.ru/favicon.png", ZOOM)) document.querySelector(`div:has(> div > input[name="cp[longitude]"])`).after(linksContainer) } @@ -425,7 +431,7 @@ if (fileListContainer == null) { fileListContainer = document.createElement("div") fileListContainer.classList.add("file-list-container") - filesContainer.appendChild(fileListContainer) + filesContainer.append(fileListContainer) } } } @@ -460,27 +466,31 @@ let fileButtonsContainer = document.createElement("div") fileButtonsContainer.classList.add("file-buttons-container") - fileButtonsContainer.appendChild(makeDownloadLink(/[^/]*$/.exec(new URL(file).pathname)[0], file)) + fileButtonsContainer.append(makeDownloadLink(/[^/]*$/.exec(new URL(file).pathname)[0], file)) - let deleteButton = document.createElement("button") - deleteButton.type = "button" - deleteButton.classList.add("button-delete") - deleteButton.textContent = "x" - deleteButton.addEventListener("click", async e => { - if (!confirm("Точно?")) return + let deleteButton = Tag.button({ + type: "button", + classes: "button-delete", + textContent: "x", + on: { + click: async e => { + if (!confirm("Точно?")) return - await fetch(removeLink) - await sendFileDeleted(inputName, variant) - removedFilesLinks.push(removeLink) + await fetch(removeLink) + await sendFileDeleted(inputName, variant) + removedFilesLinks.push(removeLink) - fileListContainer.removeChild(fileContainer) + fileListContainer.removeChild(fileContainer) + } + } }) - fileButtonsContainer.appendChild(deleteButton) - fileContainer.appendChild(displayedFile) - fileContainer.appendChild(fileButtonsContainer) + fileButtonsContainer.append(deleteButton) - fileListContainer.appendChild(fileContainer) + fileContainer.append(displayedFile) + fileContainer.append(fileButtonsContainer) + + fileListContainer.append(fileContainer) } function dataURLtoFile(dataurl, filename) { @@ -571,34 +581,38 @@ fileButtonsContainer.classList.add("file-buttons-container") let downloadLink = makeDownloadLink(file.name) - fileButtonsContainer.appendChild(downloadLink) + fileButtonsContainer.append(downloadLink) - let deleteButton = document.createElement("button") - deleteButton.classList.add("button-delete") - deleteButton.textContent = "x" - deleteButton.type = "button" - deleteButton.addEventListener("click", e => { - let index = [...fileContainer.parentElement.children].indexOf(fileContainer) + let deleteButton = Tag.button({ + classes: "button-delete", + textContent: "x", + type: "button", + on: { + click: e => { + let index = [...fileContainer.parentElement.children].indexOf(fileContainer) - if (!confirm("Точно?")) return + if (!confirm("Точно?")) return - storedFiles.splice(storedFiles.length - this.files.length + index, 1) + storedFiles.splice(storedFiles.length - this.files.length + index, 1) - let rdt = new DataTransfer() - for (const file of this.files) { - rdt.items.add(file) + let rdt = new DataTransfer() + for (const file of this.files) { + rdt.items.add(file) + } + rdt.items.remove(index) + this.files = rdt.files + + fileContainer.parentElement.removeChild(fileContainer) + } } - rdt.items.remove(index) - this.files = rdt.files - - fileContainer.parentElement.removeChild(fileContainer) }) - fileButtonsContainer.appendChild(deleteButton) - fileContainer.appendChild(displayedFile) - fileContainer.appendChild(fileButtonsContainer) + fileButtonsContainer.append(deleteButton) - fileListContainer.appendChild(fileContainer) + fileContainer.append(displayedFile) + fileContainer.append(fileButtonsContainer) + + fileListContainer.append(fileContainer) const reader = new FileReader() reader.onload = (e) => { @@ -624,15 +638,17 @@ element.id = `input-file-${index}` element.setAttribute("multiple", "multiple") element.dataset.index = index - let pseudoInput = document.createElement("label") - pseudoInput.classList.add("custom-file-upload") - pseudoInput.setAttribute("for", element.id) - pseudoInput.textContent = "+" + let pseudoInput = Tag.make("label", { + classes: "custom-file-upload", + _for: element.id, + textContent: "+" + }) element.parentElement.insertBefore(pseudoInput, element) - let fileListContainer = document.createElement("div") - fileListContainer.classList.add("file-list-container") - element.parentElement.parentElement.appendChild(fileListContainer) + let fileListContainer = Tag.div({ + classes: "file-list-container" + }) + element.parentElement.parentElement.append(fileListContainer) element.addEventListener("change", makeHandleFilesFunc(), false) }) @@ -721,7 +737,7 @@ let [from, to] = getContainersByVariant(this.value, attachmentIndex) let newFiles = [...from.querySelectorAll(`.file-container-new`)] for (const newFile of newFiles) { - to.querySelector(`.file-list-container`).appendChild(newFile) + to.querySelector(`.file-list-container`).append(newFile) } })) } @@ -734,7 +750,7 @@ function moveInputValues(form, was, became) { let inputValues = saveInputValues(was) form.removeChild(was) - form.appendChild(became) + form.append(became) setInputValues(became, inputValues) createSendButtons() } @@ -757,32 +773,38 @@ } function createAlwaysPrettifyInput(index) { - let alwaysPrettify = document.createElement("div") - alwaysPrettify.classList.add("always-prettify-container") - - let alwaysPrettifyLabel = document.createElement("label") - alwaysPrettifyLabel.setAttribute("for", "always-prettify-" + index) - alwaysPrettifyLabel.textContent = "Всегда" - alwaysPrettify.appendChild(alwaysPrettifyLabel) - - let alwaysPrettifyCheckbox = document.createElement("input") - alwaysPrettifyCheckbox.type = "checkbox" - alwaysPrettifyCheckbox.id = "always-prettify-" + index - alwaysPrettifyCheckbox.name = "always-prettify-" + index - alwaysPrettifyCheckbox.addEventListener("change", function () { - let otherCheckboxes = document.querySelectorAll(`input[name^="always-prettify-"]`) - for (let checkbox of otherCheckboxes) { - if (checkbox.id !== this.id) { - checkbox.checked = this.checked - } - } - if (this.checked) - localStorage.removeItem(localStorageItems.NOT_PRETTIFY_EDIT_CP) - else - localStorage.setItem(localStorageItems.NOT_PRETTIFY_EDIT_CP, "+") + let alwaysPrettify = Tag.div({ + classes: "always-prettify-container" }) - alwaysPrettify.appendChild(alwaysPrettifyCheckbox) + let alwaysPrettifyLabel = Tag.make("label", { + _for: "always-prettify-" + index, + textContent: "Всегда" + }) + + alwaysPrettify.append(alwaysPrettifyLabel) + + let alwaysPrettifyCheckbox = Tag.input({ + type: "checkbox", + id: "always-prettify-" + index, + name: "always-prettify-" + index, + on: { + change: function () { + let otherCheckboxes = document.querySelectorAll(`input[name^="always-prettify-"]`) + for (let checkbox of otherCheckboxes) { + if (checkbox.id !== this.id) { + checkbox.checked = this.checked + } + } + if (this.checked) + localStorage.removeItem(localStorageItems.NOT_PRETTIFY_EDIT_CP) + else + localStorage.setItem(localStorageItems.NOT_PRETTIFY_EDIT_CP, "+") + } + } + }) + + alwaysPrettify.append(alwaysPrettifyCheckbox) return alwaysPrettify } @@ -830,27 +852,27 @@ downloadLink.href = file.dataset.origin downloadLink.text = "Скачать" downloadLink.setAttribute("target", "_blank") - swiperSlide.appendChild(downloadLink) + swiperSlide.append(downloadLink) let swiperFile = file.cloneNode(true) if (swiperFile.dataset.origin) swiperFile.src = swiperFile.dataset.origin - swiperSlide.appendChild(swiperFile) - swiperWrapper.appendChild(swiperSlide) + swiperSlide.append(swiperFile) + swiperWrapper.append(swiperSlide) } - swiperDiv.appendChild(swiperWrapper) + swiperDiv.append(swiperWrapper) let prevButton = document.createElement("div") prevButton.classList.add("swiper-button-prev") - swiperDiv.appendChild(prevButton) + swiperDiv.append(prevButton) let nextButton = document.createElement("div") nextButton.classList.add("swiper-button-next") - swiperDiv.appendChild(nextButton) + swiperDiv.append(nextButton) - dialog.appendChild(swiperDiv) + dialog.append(swiperDiv) new Swiper('.swiper', { initialSlide: files.findIndex(el => el.dataset.origin && el.dataset.origin === src || el.src === src), @@ -924,22 +946,25 @@ } function createSaveAndNewButton() { - let saveAndNewButton = document.createElement("button") - saveAndNewButton.textContent = "+" - saveAndNewButton.type = "button" - saveAndNewButton.classList.add("safe-action") - saveAndNewButton.addEventListener("click", () => { - sendForm(() => { - let cpNumber = document.querySelector(`input[name="cp[number]"]`).value - let lattitude = document.querySelector(`input[name="cp[lattitude]"]`).value - let longitude = document.querySelector(`input[name="cp[longitude]"]`).value + let saveAndNewButton = Tag.button({ + textContent: "+", + type: "button", + classes: "safe-action", + on: { + click: () => { + sendForm(() => { + let cpNumber = document.querySelector(`input[name="cp[number]"]`).value + let lattitude = document.querySelector(`input[name="cp[lattitude]"]`).value + let longitude = document.querySelector(`input[name="cp[longitude]"]`).value - localStorage.setItem(localStorageItems.JUST_CREATED, cpNumber) - localStorage.setItem(localStorageItems.LATTITUDE, lattitude) - localStorage.setItem(localStorageItems.LONGITUDE, longitude) + localStorage.setItem(localStorageItems.JUST_CREATED, cpNumber) + localStorage.setItem(localStorageItems.LATTITUDE, lattitude) + localStorage.setItem(localStorageItems.LONGITUDE, longitude) - document.querySelector(`input[name="save_go"]`).click() - }) + document.querySelector(`input[name="save_go"]`).click() + }) + } + } }) return saveAndNewButton @@ -953,22 +978,25 @@ let saveButtons = [...saveAndStayButtons, ...saveAndExitButtons] for (let saveButton of saveButtons) { - let pseudoSaveButton = document.createElement("button") - pseudoSaveButton.type = "button" - pseudoSaveButton.textContent = saveButton.value - pseudoSaveButton.classList.add("safe-action", "pseudo-save") - pseudoSaveButton.addEventListener("click", async () => await sendForm(() => { - if (document.querySelector(`input[name="cp[id]"]`).value != '') { - saveButton.click() - return - } + let pseudoSaveButton = Tag.button({ + type: "button", + textContent: saveButton.value, + classes: "safe-action pseudo-save", + on: { + click: async () => await sendForm(() => { + if (document.querySelector(`input[name="cp[id]"]`).value != '') { + saveButton.click() + return + } - if (saveButton.name == "save_exit") { - localStorage.setItem(localStorageItems.REDIRECT_EXIT, true) - } + if (saveButton.name == "save_exit") { + localStorage.setItem(localStorageItems.REDIRECT_EXIT, true) + } - saveAndStayButtons[0].click() - })) + saveAndStayButtons[0].click() + }) + } + }) saveButton.style.display = "none" saveButton.parentElement.insertBefore(pseudoSaveButton, saveButton) @@ -1047,8 +1075,8 @@ link.setAttribute("target", "_blank") } - menuItem.appendChild(link) - menu.appendChild(menuItem) + menuItem.append(link) + menu.append(menuItem) } return menu @@ -1068,7 +1096,7 @@ menuContainer.append(createMenuFromLinks(mainLinksFormatted, ["Легенда"])) let goToContainer = Tag.make("li", { - classes: "go-to-container", + classes: "go-to-container", children: [ Tag.input({ type: "text", @@ -1077,12 +1105,12 @@ Tag.span({ textContent: "✏️ КП", on: { - click: async function() { + click: async function () { let number = document.querySelector("#go-to-cp").value let response = await getPointIdByNumber(number) let json = await response.json() let id = json.id - + if (id == null) { alert("Нет КП с таким номером") return @@ -1148,7 +1176,7 @@ function addClearBoth() { let clearEl = document.createElement("div") clearEl.style.clear = "both" - document.body.appendChild(clearEl) + document.body.append(clearEl) } function hasMap() { @@ -1251,13 +1279,16 @@ function bindDeleteButton() { let deleteButton = document.querySelector(`input[name="delete_go"]`) - let pseudoDeleteButton = document.createElement("button") - pseudoDeleteButton.type = "button" - pseudoDeleteButton.textContent = deleteButton.value - pseudoDeleteButton.classList.add("unsafe-action", "pseudo-save") - pseudoDeleteButton.addEventListener("click", async () => { - await deletePoint() - deleteButton.click() + let pseudoDeleteButton = Tag.button({ + type: "button", + textContent: deleteButton.value, + classes: "unsafe-action pseudo-save", + on: { + click: async () => { + await deletePoint() + deleteButton.click() + } + } }) deleteButton.style.display = "none" @@ -1276,33 +1307,39 @@ rowContentWrapper.classList.add("buttons-row__content-wrapper") while ([...el.children].length > 0) { let child = el.firstChild - rowContentWrapper.appendChild(child) + rowContentWrapper.append(child) } - let prettifyButton = document.createElement("button") - prettifyButton.type = "button" - prettifyButton.textContent = "Сделать красиво" - prettifyButton.addEventListener("click", () => { - prettifyEditCpForm(form, oldTable, container, insertedFileRows) + let prettifyButton = Tag.button({ + type: "button", + textContent: "Сделать красиво", + on: { + click: () => { + prettifyEditCpForm(form, oldTable, container, insertedFileRows) + } + } }) - rowContentWrapper.appendChild(prettifyButton) - rowContentWrapper.appendChild(createAlwaysPrettifyInput(index)) + rowContentWrapper.append(prettifyButton) + rowContentWrapper.append(createAlwaysPrettifyInput(index)) - el.appendChild(rowContentWrapper) + el.append(rowContentWrapper) }) ;[topButtonsContainer, bottomButtonsContainer].forEach((el, index) => { - let unglifyButton = document.createElement("button") - unglifyButton.type = "button" - unglifyButton.textContent = "Сделать как было" - unglifyButton.addEventListener("click", () => { - uglifyEditCpForm(form, container, oldTable) + let unglifyButton = Tag.button({ + type: "button", + textContent: "Сделать как было", + on: { + "click": () => { + uglifyEditCpForm(form, container, oldTable) + } + } }) let topRowContentWrapper = el.querySelector("div > div > div") - topRowContentWrapper.appendChild(unglifyButton) - topRowContentWrapper.appendChild(createAlwaysPrettifyInput(index)) + topRowContentWrapper.append(unglifyButton) + topRowContentWrapper.append(createAlwaysPrettifyInput(index)) }) return [topButtonsContainer, bottomButtonsContainer] @@ -1332,8 +1369,9 @@ } function makeLegend(rows) { - let legendContainer = document.createElement("div") - legendContainer.classList.add("legend-container") + let legendContainer = Tag.div({ + classes: "legend-container" + }) /* LEGEND DESC */ @@ -1349,7 +1387,7 @@ let legendLang = document.createElement("div") legendLang.classList.add("legend-desc__lang") legendLang.textContent = LEGEND_RU_LABEL - legendDescHeader.appendChild(legendLang) + legendDescHeader.append(legendLang) let legendEnSwitchContainer = createFrom(rows, "legend-switch-container", [ { index: 48 } @@ -1359,15 +1397,19 @@ show([legendEnDescContainer, legendEnHiddenDescContainer, legendRuSwitchContainer]) legendLang.textContent = LEGEND_EN_LABEL }) - legendDescHeader.appendChild(legendEnSwitchContainer) - let copyDescButton = document.createElement("button") - copyDescButton.type = "button" - copyDescButton.textContent = "Копировать" - copyDescButton.addEventListener("click", () => { - let ruInputs = [...container.querySelectorAll(":is(input, textarea)[name^=\"cp_strings\[ru\]\"]")] - let enInputs = [...container.querySelectorAll(":is(input, textarea)[name^=\"cp_strings\[en\]\"]")] - for (const [i, enInput] of enInputs.entries()) { - enInput.value = ruInputs[i].value + legendDescHeader.append(legendEnSwitchContainer) + + let copyDescButton = Tag.button({ + type: "button", + textContent: "Копировать", + on: { + click: () => { + let ruInputs = [...container.querySelectorAll(":is(input, textarea)[name^=\"cp_strings\[ru\]\"]")] + let enInputs = [...container.querySelectorAll(":is(input, textarea)[name^=\"cp_strings\[en\]\"]")] + for (const [i, enInput] of enInputs.entries()) { + enInput.value = ruInputs[i].value + } + } } }) @@ -1379,9 +1421,9 @@ show([legendRuDescContainer, legendRuHiddenDescContainer, legendEnSwitchContainer]) legendLang.textContent = LEGEND_RU_LABEL }) - legendDescHeader.appendChild(legendRuSwitchContainer) + legendDescHeader.append(legendRuSwitchContainer) - legendDescHeader.appendChild(copyDescButton) + legendDescHeader.append(copyDescButton) let legendRuDescContainer = createFrom(rows, "legend-desc", [ { index: 40, desc: "" }, @@ -1412,24 +1454,28 @@ ]) let hider = document.createElement("div") - let hiderButton = document.createElement("button") - hiderButton.classList.add("collapse-button") - hiderButton.setAttribute("type", "button") - hiderButton.addEventListener("click", event => { - let container = document.querySelector('.legend-container') - container.querySelectorAll(".legend-desc.collapsible").forEach(element => { - element.classList.toggle("collapsed") - }) + let hiderButton = Tag.button({ + classes: "collapse-button", + type: "button", + on: { + click: event => { + let container = document.querySelector('.legend-container') + container.querySelectorAll(".legend-desc.collapsible").forEach(element => { + element.classList.toggle("collapsed") + }) + } + } }) - hider.appendChild(hiderButton) - legendDescContainer.appendChild(legendDescHeader) - legendDescContainer.appendChild(legendRuDescContainer) - legendDescContainer.appendChild(legendRuHiddenDescContainer) - legendDescContainer.appendChild(legendEnDescContainer) - legendDescContainer.appendChild(legendEnHiddenDescContainer) - legendDescContainer.appendChild(hider) - legendContainer.appendChild(legendDescContainer) + hider.append(hiderButton) + + legendDescContainer.append(legendDescHeader) + legendDescContainer.append(legendRuDescContainer) + legendDescContainer.append(legendRuHiddenDescContainer) + legendDescContainer.append(legendEnDescContainer) + legendDescContainer.append(legendEnHiddenDescContainer) + legendDescContainer.append(hider) + legendContainer.append(legendDescContainer) return legendContainer } @@ -1446,22 +1492,22 @@ let imagesForLegendContainer = createFrom(rows, "files-container legend-photo-container", [ { index: 23, name: "Фото в легенде" } ]) - legendFilesContainer.appendChild(imagesForLegendContainer) + legendFilesContainer.append(imagesForLegendContainer) let imagesForHistoryContainer = createFrom(rows, "files-container history-photo-container", [ { name: "Фото для ИС" } ]) - legendFilesContainer.appendChild(imagesForHistoryContainer) + legendFilesContainer.append(imagesForHistoryContainer) let audioForLegendContainer = createFrom(rows, "files-container legend-files-container", [ { index: 27, name: "Файлы в легенде" } ]) - legendFilesContainer.appendChild(audioForLegendContainer) + legendFilesContainer.append(audioForLegendContainer) let audioForHistoryContainer = createFrom(rows, "files-container history-files-container", [ { name: "Файлы для ИС" } ]) - legendFilesContainer.appendChild(audioForHistoryContainer) + legendFilesContainer.append(audioForHistoryContainer) return legendFilesContainer } @@ -1473,12 +1519,12 @@ let imagesForAdminContainer = createFrom(rows, "files-container admin-photo-container", [ { index: 31, name: "Фото в админке" } ]) - adminFilesContainer.appendChild(imagesForAdminContainer) + adminFilesContainer.append(imagesForAdminContainer) let audioForAdminContainer = createFrom(rows, "files-container admin-files-container", [ { index: 35, name: "Файлы в админке" } ]) - adminFilesContainer.appendChild(audioForAdminContainer) + adminFilesContainer.append(audioForAdminContainer) return adminFilesContainer } @@ -1493,7 +1539,7 @@ function makeDialog() { let dialog = document.createElement("dialog") dialog.id = "dialog" - document.body.appendChild(dialog) + document.body.append(dialog) dialog.addEventListener("click", e => { if (e.target == dialog) { @@ -1511,19 +1557,23 @@ let content = document.querySelector("#content") let contentWrapper = document.createElement("div") contentWrapper.id = "content-wrapper" - contentWrapper.appendChild(document.querySelector("form")) - contentWrapper.appendChild(document.querySelector("#map-wrapper")) - content.appendChild(contentWrapper) + contentWrapper.append(document.querySelector("form")) + contentWrapper.append(document.querySelector("#map-wrapper")) + content.append(contentWrapper) - let panToCenter = document.createElement("button") - panToCenter.type = "button" - panToCenter.textContent = "В центр" - panToCenter.addEventListener("click", () => { - let lat = document.querySelector("input[name=\"cp\[lattitude\]\"").value - let lon = document.querySelector("input[name=\"cp\[longitude\]\"").value - map.setView(new L.LatLng(parseFloat(lat), parseFloat(lon)), 16) + let panToCenter = Tag.button({ + type: "button", + textContent: "В центр", + on: { + click: () => { + let lat = document.querySelector("input[name=\"cp\[lattitude\]\"").value + let lon = document.querySelector("input[name=\"cp\[longitude\]\"").value + map.setView(new L.LatLng(parseFloat(lat), parseFloat(lon)), 16) + } + } }) - document.querySelector("#map_controls").appendChild(panToCenter) + + document.querySelector("#map_controls").append(panToCenter) } } @@ -1898,18 +1948,18 @@ /* LEGEND */ let legendContainer = makeLegend(rows) - legendContainer.appendChild(makeLegendFiles(rows)) + legendContainer.append(makeLegendFiles(rows)) /* APPEND ALL */ - container.appendChild(topButtonsContainer) - container.appendChild(headerContainer) - container.appendChild(makeTopOptions(rows)) - container.appendChild(makeComment(rows)) - container.appendChild(legendContainer) - container.appendChild(makeAdminFiles(rows)) - container.appendChild(makeBottomOptions(rows)) - container.appendChild(bottomButtonsContainer) + container.append(topButtonsContainer) + container.append(headerContainer) + container.append(makeTopOptions(rows)) + container.append(makeComment(rows)) + container.append(legendContainer) + container.append(makeAdminFiles(rows)) + container.append(makeBottomOptions(rows)) + container.append(bottomButtonsContainer) /* MAP */ @@ -1998,9 +2048,10 @@ } function makeStatsSpan(text) { - let statsSpan = document.createElement("span") - statsSpan.textContent = text - statsSpan.classList.add("stats") + let statsSpan = Tag.span({ + textContent: text, + classes: "stats" + }) return statsSpan } @@ -2040,7 +2091,7 @@ } `) } - + changeColumnWidth() hideDescription() hideStartRow() @@ -2067,63 +2118,72 @@ function createPointsInpit() { let addPointsContainer = document.createElement("div") - let pointInput = document.createElement("input") - pointInput.id = "add-point" - pointInput.type = "text" - pointInput.addEventListener("focus", () => { - pointInput.classList.remove("success", "error") + let pointInput = Tag.input({ + id: "add-point", + type: "text", + on: { + focus: () => { + pointInput.classList.remove("success", "error") + } + } }) - let addButton = document.createElement("button") - addButton.type = "button" - addButton.textContent = "Добавить" - addButton.id = "add-button" - addButton.addEventListener("click", () => { - let point = pointInput.value - if (point == "") return + let addButton = Tag.button({ + type: "button", + textContent: "Добавить", + id: "add-button", + on: { + click: () => { + let point = pointInput.value + if (point == "") return - let availablePoints = matchNumberFromSelect("#cps_avail") - let addedPoints = matchNumberFromSelect("#cps_in") + let availablePoints = matchNumberFromSelect("#cps_avail") + let addedPoints = matchNumberFromSelect("#cps_in") - if (point in addedPoints) { - alert("Точка уже добавлена") - return + if (point in addedPoints) { + alert("Точка уже добавлена") + return + } + + if (!(point in availablePoints)) { + alert("Точка не найдена") + return + } + + unselectAll("#cps_avail") + let option = document.querySelector(`#cps_avail option[value="${availablePoints[point]}"]`) + option.selected = true + document.querySelector(`#cps_add`).click() + pointInput.classList.add("success") + pointInput.value = "" + option.selected = false + scrollSelectToBottom("#cps_in") + } } - - if (!(point in availablePoints)) { - alert("Точка не найдена") - return - } - - unselectAll("#cps_avail") - let option = document.querySelector(`#cps_avail option[value="${availablePoints[point]}"]`) - option.selected = true - document.querySelector(`#cps_add`).click() - pointInput.classList.add("success") - pointInput.value = "" - option.selected = false - scrollSelectToBottom("#cps_in") }) - let removeButton = document.createElement("button") - removeButton.type = "button" - removeButton.textContent = "Убрать" - removeButton.addEventListener("click", () => { - let point = pointInput.value - if (point == "") return + let removeButton = Tag.button({ + type: "button", + textContent: "Убрать", + on: { + click: () => { + let point = pointInput.value + if (point == "") return - let addedPoints = matchNumberFromSelect("#cps_in") + let addedPoints = matchNumberFromSelect("#cps_in") - if (!(point in addedPoints)) return + if (!(point in addedPoints)) return - unselectAll("#cps_in") - let option = document.querySelector(`#cps_in option[value="${addedPoints[point]}"]`) - option.selected = true - document.querySelector(`#cps_delete`).click() - pointInput.classList.add("error") - pointInput.value = "" - option.selected = false - scrollSelectToBottom("#cps_in") + unselectAll("#cps_in") + let option = document.querySelector(`#cps_in option[value="${addedPoints[point]}"]`) + option.selected = true + document.querySelector(`#cps_delete`).click() + pointInput.classList.add("error") + pointInput.value = "" + option.selected = false + scrollSelectToBottom("#cps_in") + } + } }) addPointsContainer.append(addButton) @@ -2160,11 +2220,11 @@ el.rows = 1 }) } - + function hideRedundantRows() { document.querySelectorAll(`table tr:is(:nth-child(3), :nth-child(4), :nth-child(5))`).forEach(el => el.classList.add("hidden")) } - + makeTextareasOneRow() hideRedundantRows() createPointsInpit() @@ -2191,16 +2251,19 @@ ` let rows = [...document.querySelectorAll(`#content tbody tr`)] - let collapseButton = document.createElement("button") - collapseButton.type = "button" - collapseButton.textContent = "Показать" - toggleStagePageRows(rows) - - collapseButton.addEventListener("click", () => { - toggleStagePageRows(rows) - collapseButton.textContent = toggleText(collapseButton.textContent, "Показать", "Скрыть") + let collapseButton = Tag.button({ + type: "button", + textContent: "Показать", + on: { + click: () => { + toggleStagePageRows(rows) + collapseButton.textContent = toggleText(collapseButton.textContent, "Показать", "Скрыть") + } + } }) + toggleStagePageRows(rows) + document.querySelector(`table tr:first-child th`).append(collapseButton) addStylesToHead(styles) @@ -2252,19 +2315,23 @@ } function addEnableButtons() { - let enableButton = document.createElement("button") - enableButton.type = "button" - enableButton.id = "enable-button" - enableButton.textContent = "Сделать красиво" + let enableButton = Tag.button({ + type: "button", + id: "enable-button", + textContent: "Сделать красиво", + on: { + click: () => { + if (pretty) return + + hide([enableButton]) + prettify() + } + } + }) + if (!isPageDisabled()) { hide([enableButton]) } - enableButton.addEventListener("click", () => { - if (pretty) return - - hide([enableButton]) - prettify() - }) let header = document.querySelector("#header") header.insertBefore(enableButton, header.querySelector("#globalmenu")) @@ -2282,48 +2349,57 @@ } function addDisableButton() { - let disableButton = document.createElement("button") - disableButton.id = "disable-button" - disableButton.type = "button" - disableButton.textContent = "Сделать как было" - disableButton.addEventListener("click", () => { - if (!pretty) return + let disableButton = Tag.button({ + id: "disable-button", + type: "button", + textContent: "Сделать как было", + on: { + click: () => { + if (!pretty) return - if (!confirm("Это действие отключит скрипт на этой странице и удалит все несохраненные данные")) return + if (!confirm("Это действие отключит скрипт на этой странице и удалит все несохраненные данные")) return - addDisabledPage() - pretty = false - location.reload() + addDisabledPage() + pretty = false + location.reload() + } + } }) header.querySelector("#enable-button").after(disableButton) } function createAlwaysEnable() { - let alwaysEnableContainer = document.createElement("div") - alwaysEnableContainer.id = "always-enable-container" - - let alwaysEnableLabel = document.createElement("label") - alwaysEnableLabel.setAttribute("for", "always-enable") - alwaysEnableLabel.textContent = "Всегда красиво" - alwaysEnableContainer.appendChild(alwaysEnableLabel) - - let alwaysEnableCheckbox = document.createElement("input") - alwaysEnableCheckbox.type = "checkbox" - alwaysEnableCheckbox.id = "always-enable" - alwaysEnableCheckbox.name = "always-enable" - alwaysEnableCheckbox.addEventListener("change", function () { - if (this.checked) - removeDisabledPage() - else - addDisabledPage() + let alwaysEnableContainer = Tag.div({ + id: "always-enable-container", + children: [ + Tag.make("label", { + _for: "always-enable", + textContent: "Всегда красиво" + }) + ] }) + let alwaysEnableCheckbox = Tag.input({ + type: "checkbox", + id: "always-enable", + name: "always-enable", + on: { + change: function () { + if (this.checked) + removeDisabledPage() + else + addDisabledPage() + } + } + }) + + if (!isPageDisabled()) { alwaysEnableCheckbox.checked = true } - alwaysEnableContainer.appendChild(alwaysEnableCheckbox) + alwaysEnableContainer.append(alwaysEnableCheckbox) return alwaysEnableContainer }