Move mail javascript into its own file

This commit is contained in:
Booteille 2025-06-12 14:00:33 +02:00
parent b3d1368e9c
commit 7c417f3efd
No known key found for this signature in database
GPG Key ID: 0AB6C6CA01272646
2 changed files with 66 additions and 46 deletions

View File

@ -0,0 +1,56 @@
import { formatDate } from "~/lib/utils.js";
const retrieveItemFromStorage = (serverKey) =>
(JSON.parse(localStorage.getItem(`${prefix}files`)) || []).find(
(item) => item.short === serverKey
);
const updateMailtoLink = () => {
const ownSoftwareButtonDOM = document.querySelector(".action-own-software");
const emails = document.querySelector(".emails input").value;
const subject = document.querySelector(".subject input").value;
const body = document.querySelector(".body textarea").value;
ownSoftwareButtonDOM.href = `mailto:${encodeURIComponent(
emails
)}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
};
const populateBody = () => {
const links =
JSON.parse(new URL(window.location).searchParams.get("links")) || [];
let text = i18n.intro;
links.forEach((serverKey) => {
const item = retrieveItemFromStorage(serverKey);
if (item) {
const limit =
item.delay == 0
? null
: formatDate(item.delay * 86400 + item.created_at);
text += `- ${item.name}${i18n.colon} ${item.url}`;
if (limit !== null) {
text += `\n (${i18n.deadline}${limit})`;
}
text += "\n";
}
});
if (!isLdapDefined && !isHtpasswdDefined) {
text += `\n-- \n${i18n.footer}`;
}
document.querySelector(".body textarea").value = text;
updateMailtoLink();
};
document.addEventListener("DOMContentLoaded", () => {
populateBody();
document
.querySelectorAll(".control > *")
.forEach((node) => node.addEventListener("change", updateMailtoLink));
});

View File

@ -49,51 +49,15 @@
</div>
<script type="text/javascript">
const retrieveItemFromStorage = (serverKey) =>
(JSON.parse(localStorage.getItem(`${prefix}files`))|| [])
.find((item) => item.short === serverKey )
const isLdapDefined = <%= defined(config('ldap')) ? "true" : "false" %>;
const isHtpasswdDefined = <%= defined(config('htpasswd')) ? "true" : "false" %>;
const updateMailtoLink = () => {
const ownSoftwareButtonDOM = document.querySelector('.action-own-software');
const emails = document.querySelector('.emails input').value;
const subject = document.querySelector('.subject input').value;
const body = document.querySelector('.body textarea').value;
ownSoftwareButtonDOM.href = `mailto:${encodeURIComponent(emails)}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
}
const populateBody = () => {
const links = JSON.parse(new URL(window.location).searchParams.get("links")) || [];
let text = "<%== l('Hello,\n\nHere\'s some files I want to share with you:\n') %>";
links.forEach((serverKey, index) => {
const item = retrieveItemFromStorage(serverKey);
if (item) {
const limit = (item.delay == 0) ? null : formatDate(item.delay * 86400 + item.created_at);
text += `- ${item.name}<%= l(':') %> ${item.url}`;
if (limit !== null) {
text += `\n (<%= l('deadline: ') %>${limit})`;
}
text += "\n";
}
});
% if (!defined(config('ldap')) && !defined(config('htpasswd'))) {
text += "\n-- \n<%= l('Share your files in total privacy on %1', url_for('/')->to_abs) %>";
% }
document.querySelector('.body textarea').value = text;
updateMailtoLink();
}
document.addEventListener('DOMContentLoaded', () => {
populateBody();
document.querySelectorAll(".control > *")
.forEach(node => node.addEventListener('change', updateMailtoLink));
});
const i18n = {
intro: '<%= l('Hello,\n\nHere\'s some files I want to share with you:\n') %>',
colon: '<%= l(':') %>',
deadline: '<%= l('deadline: ') %>',
footer: '<%= l('Share your files in total privacy on %1', url_for('/')->to_abs) %>',
};
</script>
%= javascript '/js/minified/mail.min.js', type => 'module', defer => "true"