feat: add link application support
This commit is contained in:
@@ -4,6 +4,7 @@ import './module/post.js'
|
||||
import './module/header.js'
|
||||
import './module/effect.js'
|
||||
import './module/moments.js'
|
||||
import './module/links.js'
|
||||
|
||||
terminal.refresh();
|
||||
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import {terminal} from "../terminal.js";
|
||||
|
||||
const SECTION_ID = "plugin-links-apply-section";
|
||||
const FORM_ID = "link-application-form";
|
||||
const CAPTCHA_URL = "/links/apply/captcha";
|
||||
const CAPTCHA_EXPIRE_TIME = 5 * 60 * 1000;
|
||||
|
||||
let captchaTimer = null;
|
||||
let listenersRegistered = false;
|
||||
|
||||
function clearCaptchaTimer() {
|
||||
if (!captchaTimer) return;
|
||||
|
||||
clearTimeout(captchaTimer);
|
||||
captchaTimer = null;
|
||||
}
|
||||
|
||||
function refreshCaptcha() {
|
||||
const section = document.getElementById(SECTION_ID);
|
||||
if (!section) {
|
||||
clearCaptchaTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
const captcha = section.querySelector("#link-application-captcha");
|
||||
const overlay = section.querySelector("#captcha-expired-overlay");
|
||||
|
||||
if (!captcha || !overlay) return;
|
||||
|
||||
captcha.src = `${CAPTCHA_URL}?t=${Date.now()}`;
|
||||
overlay.style.display = "none";
|
||||
|
||||
clearCaptchaTimer();
|
||||
|
||||
captchaTimer = setTimeout(() => {
|
||||
overlay.style.display = "flex";
|
||||
captchaTimer = null;
|
||||
}, CAPTCHA_EXPIRE_TIME);
|
||||
}
|
||||
|
||||
function showMessage(type, text) {
|
||||
const message = document.getElementById("link-application-message");
|
||||
if (!message) return;
|
||||
|
||||
message.textContent = text;
|
||||
message.dataset.type = type;
|
||||
message.hidden = false;
|
||||
}
|
||||
|
||||
function hideMessage() {
|
||||
const message = document.getElementById("link-application-message");
|
||||
if (!message) return;
|
||||
|
||||
message.hidden = true;
|
||||
message.textContent = "";
|
||||
delete message.dataset.type;
|
||||
}
|
||||
|
||||
async function submitApplication(form) {
|
||||
const section = document.getElementById(SECTION_ID);
|
||||
if (!section) return;
|
||||
|
||||
const submitButton = form.querySelector('[type="submit"]');
|
||||
|
||||
hideMessage();
|
||||
|
||||
if (submitButton) {
|
||||
submitButton.disabled = true;
|
||||
submitButton.setAttribute("aria-busy", "true");
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(form.action, {
|
||||
method: form.method || "POST",
|
||||
body: new FormData(form),
|
||||
credentials: "same-origin",
|
||||
});
|
||||
|
||||
const resultUrl = new URL(response.url);
|
||||
const applied = resultUrl.searchParams.get("applied");
|
||||
|
||||
if (applied === "success") {
|
||||
showMessage(
|
||||
"success",
|
||||
section.dataset.successMessage
|
||||
);
|
||||
|
||||
form.reset();
|
||||
} else {
|
||||
showMessage(
|
||||
"error",
|
||||
resultUrl.searchParams.get("message") ||
|
||||
section.dataset.errorMessage
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
showMessage(
|
||||
"error",
|
||||
section.dataset.errorMessage
|
||||
);
|
||||
} finally {
|
||||
refreshCaptcha();
|
||||
|
||||
const captchaCode = form.querySelector('[name="captchaCode"]');
|
||||
if (captchaCode) {
|
||||
captchaCode.value = "";
|
||||
}
|
||||
|
||||
if (submitButton) {
|
||||
submitButton.disabled = false;
|
||||
submitButton.removeAttribute("aria-busy");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleSubmit(event) {
|
||||
const form = event.target.closest(`#${FORM_ID}`);
|
||||
if (!form) return;
|
||||
|
||||
event.preventDefault();
|
||||
submitApplication(form);
|
||||
}
|
||||
|
||||
function handleClick(event) {
|
||||
const target = event.target.closest(
|
||||
'[data-link-apply-action="refresh-captcha"]'
|
||||
);
|
||||
|
||||
if (!target) return;
|
||||
if (!target.closest(`#${SECTION_ID}`)) return;
|
||||
|
||||
refreshCaptcha();
|
||||
}
|
||||
|
||||
function registerLinkApplicationActions() {
|
||||
if (listenersRegistered) return;
|
||||
|
||||
document.addEventListener("submit", handleSubmit);
|
||||
document.addEventListener("click", handleClick);
|
||||
|
||||
listenersRegistered = true;
|
||||
}
|
||||
|
||||
function initLinkApplication() {
|
||||
clearCaptchaTimer();
|
||||
|
||||
if (!document.getElementById(SECTION_ID)) return;
|
||||
|
||||
refreshCaptcha();
|
||||
}
|
||||
|
||||
terminal.registerInitFunc(registerLinkApplicationActions);
|
||||
terminal.registerRefresh(initLinkApplication);
|
||||
+119
-1
@@ -99,6 +99,107 @@
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
&-apply-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
column-gap: calc(var(--size-theme-font) * 1);
|
||||
row-gap: calc(var(--size-theme-font) * 0.9);
|
||||
}
|
||||
|
||||
&-apply-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: calc(var(--size-theme-font) * 0.3);
|
||||
min-width: 0;
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: calc(var(--size-theme-font) * 0.25);
|
||||
margin: 0;
|
||||
color: var(--color-theme-foreground);
|
||||
font-size: calc(var(--size-theme-font) * 0.85);
|
||||
}
|
||||
|
||||
&-full {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
&-submit {
|
||||
grid-column: 1 / -1;
|
||||
align-items: flex-start;
|
||||
margin-top: calc(var(--size-theme-font) * 0.3);
|
||||
}
|
||||
}
|
||||
|
||||
&-required {
|
||||
color: var(--color-theme-accent);
|
||||
font-style: normal;
|
||||
}
|
||||
|
||||
&-apply-captcha {
|
||||
.captcha-row {
|
||||
display: flex;
|
||||
align-items: stretch;
|
||||
gap: calc(var(--size-theme-font) * 0.5);
|
||||
|
||||
input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.captcha-img-wrapper {
|
||||
position: relative;
|
||||
flex-shrink: 0;
|
||||
width: 140px;
|
||||
height: 40px;
|
||||
}
|
||||
|
||||
img {
|
||||
width: 140px;
|
||||
max-width: none !important;
|
||||
height: 40px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
object-fit: contain;
|
||||
background: #fff;
|
||||
cursor: pointer;
|
||||
border: calc(var(--width-theme-border) / 2) var(--style-theme-border) var(--color-theme-foreground);
|
||||
|
||||
&:hover {
|
||||
border-color: var(--color-theme-accent);
|
||||
}
|
||||
}
|
||||
|
||||
.captcha-expired {
|
||||
display: none;
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
background: var(--color-theme-background);
|
||||
border: calc(var(--width-theme-border) / 2) var(--style-theme-border) var(--color-theme-accent);
|
||||
|
||||
span {
|
||||
color: var(--color-theme-accent);
|
||||
font-size: calc(var(--size-theme-font) * 0.75);
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&-apply-message {
|
||||
margin-bottom: calc(var(--size-theme-font) * 1);
|
||||
padding: calc(var(--size-theme-font) * 0.5) 0;
|
||||
color: var(--color-theme-accent);
|
||||
border-bottom: calc(var(--width-theme-border) / 2) var(--style-theme-table-border) var(--color-theme-accent);
|
||||
}
|
||||
|
||||
&-apply-submit-btn {
|
||||
width: auto;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
&-url {
|
||||
display: none;
|
||||
@@ -111,5 +212,22 @@
|
||||
&-container {
|
||||
grid-template-columns: calc(var(--size-theme-font) * 3) 1fr;
|
||||
}
|
||||
|
||||
&-apply-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
&-apply-item {
|
||||
&-full,
|
||||
&-submit {
|
||||
grid-column: auto;
|
||||
}
|
||||
}
|
||||
|
||||
&-apply-captcha {
|
||||
.captcha-row {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user