links.createWindow(() => {
const View = new ViewWindow({
title: ` Générateur de liens`,
width: "600px",
height: "600px",
})
View.setContent(`
`)
const addBtn = getID(`${View.getViewTitle()}_add`)
const linksDiv = getID(`${View.getViewTitle()}_links`)
var links = new Array()
addBtn.addEventListener("click", () => {
View.createPopup({
title: ` Ajouter un lien`,
content: `
`
})
const titleInput = getID(`${View.getViewTitle()}_title`)
const urlInput = getID(`${View.getViewTitle()}_url`)
const addBtn = getID(`${View.getViewTitle()}_confirm`)
const info = new TextResponse(`${View.getViewTitle()}_info`)
const ablink = getID(`${View.getViewTitle()}_ablink`)
const newlinkSpan = getID(`${View.getViewTitle()}_newlink_span`)
ablink.checked = true
ablink.addEventListener("click", () => {
if(!ablink.checked) {
newlinkSpan.innerHTML = `
`
} 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
}
if(!newlink.value.match(/^[a-zA-Z0-9-_]+$/)) {
info.err("L'URL n'est pas valide")
return false
}
}
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 = `Aucun lien disponible
`
}
answer.links.forEach((link) => {
links.push(link)
})
links.forEach((link) => {
linksDiv.innerHTML += `
${link.title}
`
})
links.forEach((link) => {
const editBtn = getID(`${link.id}_edit`)
editBtn.addEventListener("click", () => {
View.createPopup({
title: ` Modifier un lien`,
content: `
`
})
const titleInput = getID(`${View.getViewTitle()}_edittitle`)
const urlInput = getID(`${View.getViewTitle()}_editurl`)
const editBtnConfirm = getID(`${View.getViewTitle()}_editconfirm`)
const info = new TextResponse(`${View.getViewTitle()}_editinfo`)
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: ` Erreur`,
content: `
Impossible de supprimer le lien
`
})
} else {
getLinks()
}
})
})
})
} else {
info.err("Impossible de récupérer les liens")
}
})
}
})