2021-03-30 13:47:58 +02:00
|
|
|
const Routing = require('./routing')
|
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const Post = function (w) {
|
|
|
|
this.window = w
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
Post.prototype.commentsEvents = function () {
|
|
|
|
const document = this.window.document
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const parentCommentIdField = document.getElementById('user_comment_parentCommentId')
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
if (!parentCommentIdField) {
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const isAnswerAlert = document.getElementById('answer-alert')
|
|
|
|
const cancelAnswerButton = document.getElementById('cancel-answer')
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const toogleAnswerAlert = function () {
|
|
|
|
if (parentCommentIdField.value) {
|
|
|
|
isAnswerAlert.classList.remove('hidden')
|
|
|
|
} else {
|
|
|
|
isAnswerAlert.classList.add('hidden')
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
2021-06-15 14:26:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toogleAnswerAlert()
|
|
|
|
|
|
|
|
const answerButtons = document.querySelectorAll('a[data-answer]')
|
|
|
|
|
|
|
|
for (let i = 0, len = answerButtons.length; i < len; i++) {
|
|
|
|
answerButtons[i].addEventListener('click', function (e) {
|
|
|
|
parentCommentIdField.value = e.target.getAttribute('data-answer')
|
|
|
|
toogleAnswerAlert()
|
|
|
|
}, false)
|
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
cancelAnswerButton.addEventListener('click', function (e) {
|
|
|
|
e.preventDefault()
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
parentCommentIdField.value = null
|
|
|
|
toogleAnswerAlert()
|
|
|
|
}, false)
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const previewButton = document.querySelector('.preview-button')
|
|
|
|
const previewRender = document.querySelector('#preview')
|
|
|
|
|
|
|
|
previewButton.addEventListener('click', function () {
|
|
|
|
if (previewRender.innerHTML === '') {
|
|
|
|
previewRender.innerHTML = '<p>Chargement en cours…</p>'
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
const content = document.querySelector('#user_comment_content').value
|
|
|
|
const httpRequest = new XMLHttpRequest()
|
|
|
|
|
|
|
|
httpRequest.onreadystatechange = function (data) {
|
|
|
|
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
|
|
|
|
const json = JSON.parse(httpRequest.response)
|
|
|
|
previewRender.innerHTML = json.render
|
|
|
|
document.location.href = '#preview'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httpRequest.open('POST', Routing.generate('api_blog_comment_preview'))
|
|
|
|
httpRequest.setRequestHeader(
|
|
|
|
'Content-Type',
|
|
|
|
'application/x-www-form-urlencoded'
|
|
|
|
)
|
|
|
|
httpRequest.send('content=' + encodeURIComponent(content))
|
|
|
|
}, false)
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
Post.prototype.imagesEvents = function () {
|
|
|
|
const document = this.window.document
|
|
|
|
let isFullscreen = false
|
|
|
|
const images = document.querySelectorAll('.body img')
|
|
|
|
|
|
|
|
const handleClick = function (image) {
|
|
|
|
if (isFullscreen) {
|
|
|
|
if (document.exitFullscreen) {
|
|
|
|
document.exitFullscreen()
|
|
|
|
} else if (document.webkitExitFullscreen) {
|
|
|
|
document.webkitExitFullscreen()
|
|
|
|
} else if (document.mozCancelFullScreen) {
|
|
|
|
document.mozCancelFullScreen()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (image.requestFullscreen) {
|
|
|
|
image.requestFullscreen()
|
|
|
|
} else if (image.webkitRequestFullscreen) {
|
|
|
|
image.webkitRequestFullscreen()
|
|
|
|
} else if (image.mozRequestFullScreen) {
|
|
|
|
image.mozRequestFullScreen()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isFullscreen = !isFullscreen
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0, len = images.length; i < len; i++) {
|
|
|
|
const image = images[i]
|
|
|
|
|
|
|
|
if (image.parentNode.tagName === 'A') {
|
|
|
|
continue
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
2021-06-15 14:26:38 +02:00
|
|
|
|
|
|
|
(function (i) {
|
|
|
|
i.addEventListener('click', function () {
|
|
|
|
handleClick(i)
|
|
|
|
}, false)
|
|
|
|
})(image)
|
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:26:38 +02:00
|
|
|
Post.prototype.init = function () {
|
|
|
|
this.commentsEvents()
|
|
|
|
this.imagesEvents()
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Post
|