neutral/public/javascripts/link.js

299 lines
10 KiB
JavaScript
Raw Normal View History

2023-11-28 20:05:44 +00:00
links.createWindow(() => {
2023-11-05 11:12:18 +00:00
const View = new ViewWindow({
title: `<i class="fa-solid fa-link"></i> Générateur de liens`,
width: "600px",
height: "600px",
})
View.setContent(`
<div class='ln-bar'>
<button id='${View.getViewTitle()}_add' class='btn blue'><span><i class='fa fa-add'></i> Ajouter un lien</span></button>
</div>
<div class='ln-links' id='${View.getViewTitle()}_links'>
<div style='font-size: 24px; margin-top: 225px;' class='t-center'>
<p><i class="fa-solid fa-rotate fa-spin"></i> Chargement en cours ...</p>
</div>
</div>
2023-11-05 11:12:18 +00:00
`)
const addBtn = getID(`${View.getViewTitle()}_add`)
const linksDiv = getID(`${View.getViewTitle()}_links`)
var links = new Array()
addBtn.addEventListener("click", () => {
View.createPopup({
title: `<i class='fa fa-add'></i> Ajouter un lien`,
content: `
<div class='ln-create'>
<input id='${View.getViewTitle()}_title' class='field' type='text' placeholder='Titre du lien'>
<input id='${View.getViewTitle()}_url' class='field' type='text' placeholder='URL du lien'>
<div class='ln-abstract-div'>
<p class='ln-abstract-label'>Abstraire le lien :</p>
<input id='${View.getViewTitle()}_ablink' type='checkbox'>
</div>
<span id='${View.getViewTitle()}_newlink_span'></span>
<p id='${View.getViewTitle()}_info'></p>
<button id='${View.getViewTitle()}_confirm' class='btn blue'><span><i class='fa fa-add'></i> Ajouter</span></button>
</div>
`
})
const titleInput = getID(`${View.getViewTitle()}_title`)
const urlInput = getID(`${View.getViewTitle()}_url`)
const addBtn = getID(`${View.getViewTitle()}_confirm`)
2023-11-28 20:05:44 +00:00
const info = new TextResponse(`${View.getViewTitle()}_info`)
2023-11-05 11:12:18 +00:00
const ablink = getID(`${View.getViewTitle()}_ablink`)
const newlinkSpan = getID(`${View.getViewTitle()}_newlink_span`)
ablink.checked = true
ablink.addEventListener("click", () => {
if(!ablink.checked) {
newlinkSpan.innerHTML = `
<input id='${View.getViewTitle()}_newlink' class='field' type='text' placeholder='URL du nouveau lien'>
`
} else {
newlinkSpan.innerHTML = ""
}
})
addBtn.addEventListener("click", () => {
info.clear()
console.log(ablink.checked)
if(!titleInput.value) {
info.err("Un titre est nécéssaire")
} else if(!urlInput.value) {
info.err("Une URL est nécéssaire")
} else {
var newlink = getID(`${View.getViewTitle()}_newlink`)
if(!newlink) {
newlink = { value: "" }
} else {
if(!newlink.value) {
info.err("Un nouveau lien est nécéssaire")
return false
}
2023-11-05 11:19:17 +00:00
if(!newlink.value.match(/^[a-zA-Z0-9-_]+$/)) {
info.err("L'URL n'est pas valide")
return false
}
2023-11-05 11:12:18 +00:00
}
2023-11-05 11:19:17 +00:00
2023-11-05 11:12:18 +00:00
const request = post(`LINKS_ADD`, {
title: titleInput.value,
url: urlInput.value,
abstractLink: ablink.checked,
dest: newlink.value
})
request.then((answer) => {
if(answer.answer == "OK") {
info.info("Le lien a bien été ajouté")
View.destroyPopup()
getLinks()
} else if(answer.answer == "ALREADY_EXISTS") {
info.err("Ce lien existe déjà")
} else {
info.err("Impossible d'ajouter le lien")
}
})
}
})
})
getLinks()
function getLinks() {
linksDiv.innerHTML = ""
links = new Array()
const request = post(`LINKS_GET_ALL`)
request.then((answer) => {
if(answer.answer == "OK") {
if(answer.links.length == 0) {
linksDiv.innerHTML = `<p class='lightred' style='text-align: center;'>Aucun lien disponible</p>`
}
answer.links.forEach((link) => {
links.push(link)
})
links.forEach((link) => {
linksDiv.innerHTML += `
<div class='ln-link'>
<p class='ln-link-title'>${link.title}</p>
<div>
<p class='ln-link-url'><i class="fa-solid fa-bookmark"></i> Lien original : <a href='${link.url}'>${link.url}</a></p>
<p class='ln-link-url'><i class="fa-solid fa-paperclip"></i> Lien modifié : <a href='https://neutral.raphix.fr/link/${link.dest}'> https://neutral.raphix.fr/link/${link.dest}</a></p>
</div>
<div class='ln-link-actions'>
<button id='${link.id}_edit' class='btn blue'><span><i class='fa fa-edit'></i> Modifier</span></button>
<button id='${link.id}_remove' class='btn red'><span><i class='fa fa-trash'></i> Supprimer</span></button>
</div>
</div>
`
})
links.forEach((link) => {
const editBtn = getID(`${link.id}_edit`)
editBtn.addEventListener("click", () => {
View.createPopup({
title: `<i class='fa fa-edit'></i> Modifier un lien`,
content: `
<div class='ln-create'>
<input id='${View.getViewTitle()}_edittitle' class='field' type='text' placeholder='Titre du lien' value='${link.title}'>
<input id='${View.getViewTitle()}_editurl' class='field' type='text' placeholder='URL du lien' value='${link.url}'>
<p id='${View.getViewTitle()}_editinfo'></p>
<button id='${View.getViewTitle()}_editconfirm' class='btn blue'><span><i class='fa fa-edit'></i> Modifier</span></button>
</div>
`
})
const titleInput = getID(`${View.getViewTitle()}_edittitle`)
const urlInput = getID(`${View.getViewTitle()}_editurl`)
const editBtnConfirm = getID(`${View.getViewTitle()}_editconfirm`)
2023-11-28 20:05:44 +00:00
const info = new TextResponse(`${View.getViewTitle()}_editinfo`)
2023-11-05 11:12:18 +00:00
titleInput.value = link.title
urlInput.value = link.url
editBtnConfirm.addEventListener("click", () => {
if(!titleInput.value) {
info.err("Un titre est nécéssaire")
} else if(!urlInput.value) {
info.err("Une URL est nécéssaire")
} else {
const request = post(`LINKS_EDIT`, {
id: link.id,
title: titleInput.value,
url: urlInput.value
})
request.then((answer) => {
if(answer == "OK") {
info.info("Le lien a bien été modifié")
View.destroyPopup()
getLinks()
} else {
info.err("Impossible de modifier le lien")
}
})
}
})
})
const delBtn = getID(`${link.id}_remove`)
delBtn.addEventListener("click", () => {
const request = post(`LINKS_DELETE`, link.id)
request.then((answer) => {
if(answer != "OK") {
View.createPopup({
title: `<i class='fa fa-warning'></i> Erreur`,
content: `
<p>Impossible de supprimer le lien</p>
`
})
} else {
getLinks()
}
})
})
})
} else {
info.err("Impossible de récupérer les liens")
}
})
}
2023-11-28 20:05:44 +00:00
})