blog-app/public/js/main.js
2021-11-13 14:19:55 +08:00

65 lines
1.7 KiB
JavaScript

/* global marked */
(function () {
var forms = document.querySelectorAll('form')
forms.forEach(function (node) {
node.addEventListener(
'submit',
function () {
node.querySelector('button[type="submit"]').disabled = true
},
false
)
})
var deleter = document.querySelectorAll(
'a[data-toggle][data-target="#confirm-modal"], ' +
'a[data-toggle][data-target="#comment-confirm-modal"]'
)
deleter.forEach(function (node) {
node.addEventListener(
'click',
function () {
document
.getElementById('delete-form')
.setAttribute('action', node.dataset.action)
},
false
)
})
var search = document.querySelector('#search')
search.addEventListener('keydown', function (e) {
if (e.keyCode === 13) {
window.location = '/search/' + encodeURIComponent(search.value)
return false
}
})
var editor = document.querySelectorAll(
'a[data-toggle][data-target="#comment-edit-modal"]'
)
editor.forEach(function (node) {
node.addEventListener(
'click',
function (e) {
e.preventDefault()
var form = document.getElementById('comment-edit-form')
form.setAttribute('action', node.dataset.action)
var textarea = form.querySelector('textarea')
textarea.textContent = textarea.value =
node.parentNode.nextElementSibling.textContent
},
false
)
})
var previewBtn = document.querySelector('#preview-post-btn')
if (previewBtn != null) {
previewBtn.addEventListener(
'click',
function () {
document.getElementById('preview-content').innerHTML = marked(
document.getElementById('source-content').value
)
},
false
)
}
})()