phantasmobile/app/static/js/comments.js

283 lines
10 KiB
JavaScript
Raw Normal View History

2023-07-24 21:00:43 +00:00
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
2023-07-24 11:05:45 +00:00
}
}
2023-07-24 21:00:43 +00:00
return cookieValue;
}
function getCSRF() {
return document.querySelector('input[name="csrfmiddlewaretoken"]').value
2023-07-24 11:05:45 +00:00
}
2023-07-24 21:00:43 +00:00
function serializeData(fields) {
var data = {};
2023-07-24 11:05:45 +00:00
document.querySelector('.add-comment').querySelectorAll('input,textarea').forEach(input => {
2023-07-24 21:00:43 +00:00
if (fields && !fields.includes(input.name)) {
return
}
if (input.name != 'csrfmiddlewaretoken') {
var value = input.value.trim()
if (value) {
data[input.name] = value
}
2023-07-24 11:05:45 +00:00
}
})
2023-07-24 21:00:43 +00:00
return data
}
async function api(url, data) {
var csrf = getCSRF()
return fetch(url, {
2023-07-24 11:05:45 +00:00
method: 'POST',
2023-07-25 09:49:59 +00:00
credentials: 'same-origin',
2023-07-24 11:05:45 +00:00
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrf
},
body: JSON.stringify(data)
}).then(response => {
return response.json()
})
}
2023-07-24 21:00:43 +00:00
async function publish_comment(id) {
return api("/comment/publish/", {
"comment": id
})
}
async function logout() {
return api("/logout/", {}).then(response => {
delete user.username
})
}
async function login(data) {
return api("/login/", data)
}
async function register(data) {
return api("/register/", data)
}
2023-07-24 11:05:45 +00:00
2023-07-16 05:56:14 +00:00
function renderComments(cdiv, data) {
2023-07-15 07:30:36 +00:00
cdiv.innerHTML = `
<h3>
<span class="icon">${icon.down}</span>
Comments
</h3>
2023-07-16 05:56:14 +00:00
<div class="block">
<div class="comments-content">
</div>
</div>
2023-07-15 07:30:36 +00:00
`
2023-07-16 05:56:14 +00:00
const content = cdiv.querySelector('.comments-content')
2023-07-15 07:30:36 +00:00
comments.forEach(comment => {
2023-07-24 11:05:45 +00:00
var extra = ''
if (!comment.published) {
if (user.is_moderator) {
extra += `<button class="publish-comment">${icon.publishComment}</button>`
} else {
extra += '(under review)'
}
}
2023-07-15 07:30:36 +00:00
var c = document.createElement('div')
c.className = 'comment'
c.innerHTML = `
2023-07-16 05:56:14 +00:00
<div class="text">${comment.text}</div>
<div class="meta">
by <span class="name">${comment.name}</span>
<span class="date">${comment.date}</span>
2023-07-24 11:05:45 +00:00
${extra}
2023-07-15 07:30:36 +00:00
</div>
`
2023-07-24 11:05:45 +00:00
c.querySelectorAll('button.publish-comment').forEach(button => {
button.title = "click to publish"
button.addEventListener("click", event => {
button.disabled = true
publish_comment(comment.id).then(response => {
button.remove()
})
})
})
2023-07-16 05:56:14 +00:00
content.append(c)
2023-07-15 07:30:36 +00:00
})
var add = document.querySelector('.add-comment')
add.style.display = 'block'
2023-07-16 05:56:14 +00:00
cdiv.querySelector('.block').append(add)
cdiv.querySelector('h3').addEventListener("click", event => {
var img = cdiv.querySelector('h3 .icon')
if (cdiv.classList.contains("collapsed")) {
cdiv.classList.remove("collapsed")
img.innerHTML = icon.down
} else {
cdiv.classList.add("collapsed")
img.innerHTML = icon.right
}
})
2023-07-15 07:30:36 +00:00
}
2023-07-24 21:00:43 +00:00
function clearInvalid() {
document.querySelectorAll('input.invalid, textarea.invalid').forEach(invalid => {
invalid.classList.remove('invalid')
})
}
2023-07-15 07:30:36 +00:00
document.querySelector('button#add-comment').addEventListener('click', event => {
2023-07-24 21:00:43 +00:00
event.preventDefault()
event.stopPropagation()
var valid = true, fields = ['text', 'name', 'email']
var element = document.querySelector('.add-comment .comment-fields')
element.querySelectorAll('input:invalid, textarea:invalid').forEach(invalid => {
if (fields.includes(invalid.name)) {
invalid.classList.add('invalid')
valid = false
console.log('invalid', invalid)
2023-07-15 07:30:36 +00:00
}
})
2023-07-24 21:00:43 +00:00
if (!valid) {
return
}
var data = serializeData(fields);
2023-07-15 07:30:36 +00:00
data.item = pandora.comment
2023-07-24 21:00:43 +00:00
api("/comment/", data).then(response => {
2023-07-16 05:56:14 +00:00
var comment = document.createElement('div')
2023-07-15 07:30:36 +00:00
comment.classList.add('comment')
2023-07-16 05:56:14 +00:00
comment.innerHTML = `
<div class="text"></div>
<div class="meta">
by <span class="name"></span>
<span class="date"></span>
</div>
`
comment.querySelector('.name').innerText = response.name
comment.querySelector('.date').innerText = response.date
2023-09-26 17:44:38 +00:00
comment.querySelector('.text').innerHTML = response.text
2023-07-16 05:56:14 +00:00
document.querySelector('.comments .comments-content').append(comment)
2023-07-15 07:30:36 +00:00
document.querySelector('.add-comment textarea').value = ''
2023-07-24 21:00:43 +00:00
if (!user.username) {
localStorage.name = data.name
localStorage.email = data.email
}
2023-07-15 07:30:36 +00:00
})
})
2023-07-24 21:00:43 +00:00
function afterLogin() {
document.querySelector('input[name="csrfmiddlewaretoken"]').value = getCookie('csrftoken')
user.username = data.username
document.querySelectorAll('.add-comment input[name="email"]').forEach(input => {
input.required = false
})
document.querySelectorAll('.add-comment input[type="password"]').forEach(input => {
input.value = ""
input.required = false
})
document.querySelector('.add-comment .buttons.login').classList.remove('active')
var buttons = document.querySelector('.add-comment .buttons.guest')
buttons.querySelector('#add-comment').innerText = "Add comment"
buttons.querySelector('#login').remove()
buttons.classList.remove('guest')
buttons.classList.add('active')
var textarea = document.querySelector('.add-comment textarea')
textarea.style.display = ""
textarea.required = true
document.querySelector('.add-comment .login .error').innerText = ''
delete localStorage.name
delete localStorage.email
}
var btnLogin = document.querySelector('.add-comment .buttons.guest button#login')
if (btnLogin) {
btnLogin.addEventListener('click', event => {
event.preventDefault()
event.stopPropagation()
clearInvalid()
document.querySelector('.add-comment .buttons.guest').classList.remove('active')
document.querySelector('.add-comment .user-info').style.display = "none"
document.querySelector('.add-comment .buttons.login').classList.add('active')
var textarea = document.querySelector('.add-comment textarea')
textarea.style.display = "none"
textarea.required = false
})
document.querySelector('.add-comment .buttons.login button#login').addEventListener("click", event => {
event.preventDefault()
event.stopPropagation()
clearInvalid()
if (!document.querySelectorAll('.add-comment .login input:invalid').length) {
event.target.disabled = true
var data = serializeData()
login({
"username": data.username,
"password": data.password,
}).then(response => {
if (response.error) {
document.querySelector('.add-comment .login .error').innerText = response.error
event.target.disabled = false
} else {
afterLogin()
}
})
} else {
document.querySelectorAll('.add-comment .login input:invalid').forEach(invalid => {
invalid.classList.add('invalid')
})
document.querySelector('.add-comment .login .error').innerText = ''
}
})
document.querySelector('.add-comment .buttons.login button#register').addEventListener("click", event => {
event.preventDefault()
event.stopPropagation()
clearInvalid()
var login = document.querySelector('.add-comment .buttons.login button#login')
var email = document.querySelector('.add-comment .buttons.login input[name="email"]')
if (login.style.display == "none") {
if (!document.querySelectorAll('.add-comment .login input:invalid').length) {
event.target.disabled = true
var data = serializeData()
register({
"username": data.username,
"password": data.password,
"email": data.email,
}).then(response => {
if (response.error) {
document.querySelector('.add-comment .login .error').innerText = response.error
event.target.disabled = false
} else {
afterLogin()
}
})
} else {
document.querySelectorAll('.add-comment .login input:invalid').forEach(invalid => {
invalid.classList.add('invalid')
})
document.querySelector('.add-comment .login .error').innerText = ''
}
} else {
document.querySelector('.add-comment .login .error').innerText = ''
login.style.display = "none"
email.required = true
email.style.display = "block"
}
})
}
if (!user.username) {
if (localStorage.name) {
document.querySelector('.add-comment .comment-fields input[name="name"]').value = localStorage.name
}
if (localStorage.email) {
document.querySelector('.add-comment .comment-fields input[name="email"]').value = localStorage.email
}
}