|
| 1 | +// This file contains JS code for the paste view. |
| 2 | +// All files named admin_*.js will be merged together and minimised by calling |
| 3 | +// go generate ./... |
| 4 | + |
| 5 | +function pasteToggleDownloads(checkbox) { |
| 6 | + document.getElementById("paste-downloads").disabled = !checkbox.checked; |
| 7 | +} |
| 8 | + |
| 9 | +function pasteToggleExpiry(checkbox) { |
| 10 | + document.getElementById("paste-expiry").disabled = !checkbox.checked; |
| 11 | +} |
| 12 | + |
| 13 | +function pasteTogglePassword(checkbox) { |
| 14 | + document.getElementById("paste-password").disabled = !checkbox.checked; |
| 15 | +} |
| 16 | + |
| 17 | +function submitPaste() { |
| 18 | + const content = document.getElementById("paste-content").value.trim(); |
| 19 | + if (!content) { |
| 20 | + alert("Please enter some content before creating a paste."); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const title = document.getElementById("paste-title").value.trim(); |
| 25 | + const limitViews = document.getElementById("paste-enable-downloads").checked; |
| 26 | + const limitExpiry = document.getElementById("paste-enable-expiry").checked; |
| 27 | + const usePassword = document.getElementById("paste-enable-password").checked; |
| 28 | + |
| 29 | + const allowedDownloads = limitViews ? parseInt(document.getElementById("paste-downloads").value, 10) : 0; |
| 30 | + const expiryDays = limitExpiry ? parseInt(document.getElementById("paste-expiry").value, 10) : 0; |
| 31 | + const password = usePassword ? document.getElementById("paste-password").value : ""; |
| 32 | + |
| 33 | + apiAddPaste(content, title, allowedDownloads, expiryDays, password) |
| 34 | + .then(data => { |
| 35 | + const info = data.FileInfo; |
| 36 | + pasteInsertRow(info); |
| 37 | + document.getElementById("paste-content").value = ""; |
| 38 | + document.getElementById("paste-title").value = ""; |
| 39 | + pasteCopyUrl(info.UrlDownload, info.Id); |
| 40 | + }) |
| 41 | + .catch(error => { |
| 42 | + alert("Failed to create paste: " + error); |
| 43 | + console.error("Error:", error); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function pasteInsertRow(info) { |
| 48 | + const tbody = document.getElementById("paste-tbody"); |
| 49 | + const row = document.createElement("tr"); |
| 50 | + row.id = "pasterow-" + info.Id; |
| 51 | + |
| 52 | + const views = info.UnlimitedDownloads ? "Unlimited" : info.DownloadsRemaining; |
| 53 | + |
| 54 | + row.innerHTML = ` |
| 55 | + <td>${escapeHtml(info.Name)}</td> |
| 56 | + <td><span id="paste-created-${info.Id}"></span></td> |
| 57 | + <td><span id="paste-expiry-${info.Id}"></span></td> |
| 58 | + <td>${escapeHtml(String(views))}</td> |
| 59 | + <td> |
| 60 | + <div class="btn-group" role="group"> |
| 61 | + <button type="button" class="btn btn-outline-light btn-sm" title="Copy URL" |
| 62 | + onclick="pasteCopyUrl('${escapeHtml(info.UrlDownload)}', '${escapeHtml(info.Id)}')"> |
| 63 | + <i class="bi bi-copy"></i> |
| 64 | + </button> |
| 65 | + <button type="button" class="btn btn-outline-danger btn-sm" title="Delete" |
| 66 | + onclick="pasteDelete('${escapeHtml(info.Id)}')"> |
| 67 | + <i class="bi bi-trash3"></i> |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + </td>`; |
| 71 | + |
| 72 | + tbody.prepend(row); |
| 73 | + insertDateWithNegative(info.UploadDate, "paste-created-" + info.Id, "Unknown"); |
| 74 | + |
| 75 | + if (info.UnlimitedTime) { |
| 76 | + document.getElementById("paste-expiry-" + info.Id).innerText = "Never"; |
| 77 | + } else { |
| 78 | + insertFileRequestExpiry(info.ExpireAt, "paste-expiry-" + info.Id); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function pasteCopyUrl(url, id) { |
| 83 | + navigator.clipboard.writeText(url).then(() => { |
| 84 | + const toastEl = document.getElementById("paste-toast"); |
| 85 | + document.getElementById("paste-toast-body").innerText = "URL copied to clipboard!"; |
| 86 | + bootstrap.Toast.getOrCreateInstance(toastEl).show(); |
| 87 | + }).catch(() => { |
| 88 | + prompt("Copy this URL:", url); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function pasteDelete(id) { |
| 93 | + if (!confirm("Delete this paste?")) { |
| 94 | + return; |
| 95 | + } |
| 96 | + apiFilesDelete(id, 0) |
| 97 | + .then(() => { |
| 98 | + const row = document.getElementById("pasterow-" + id); |
| 99 | + if (row) { |
| 100 | + row.classList.add("rowDeleting"); |
| 101 | + setTimeout(() => row.remove(), 290); |
| 102 | + } |
| 103 | + }) |
| 104 | + .catch(error => { |
| 105 | + alert("Failed to delete paste: " + error); |
| 106 | + console.error("Error:", error); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +function escapeHtml(str) { |
| 111 | + return String(str) |
| 112 | + .replace(/&/g, "&") |
| 113 | + .replace(/</g, "<") |
| 114 | + .replace(/>/g, ">") |
| 115 | + .replace(/"/g, """) |
| 116 | + .replace(/'/g, "'"); |
| 117 | +} |
0 commit comments