提交评论功能

This commit is contained in:
2024-05-06 12:35:09 +08:00
parent 645aafcadb
commit e48d34c52c
10 changed files with 215 additions and 8 deletions
+107
View File
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章详情</title>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/jsmind.js"></script>
<script src="/static/js/jsmind.draggable.js"></script>
<script src="/static/js/jsmind.screenshot.js"></script>
<script src="/static/js/jsmind.menu.js"></script>
<script src="/static/js/jsmind.coop.js?d="></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="/static/js/mind/c_m_m.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons@4.28.0/dist/feather.min.js"></script> <!-- 图标相关-->
<style>
.article_detail{
width: 80%;
margin-left: 100px;
}
.comment_post{
width: 80%;
margin-left: 100px;
}
.comment_show{
width: 80%;
margin-left: 100px;
}
#commentform_title {
background-repeat: no-repeat;
padding: 0 0 0 25px;
margin-bottom: 10px;
}
.feedback_area_title {
border-bottom: 1px solid #ddd;
font-size: 14px;
font-weight: bold;
margin: 20px 0 10px;
}
#p{
border: deepskyblue 1px dashed;
background-color: antiquewhite;
}
</style>
</head>
<body>
<div class="comment_post"> {# 提交评论区域的div #}
<div id="commentform_title">发表评论</div>
<textarea rows="10" cols="60" id="comment_content"></textarea>
<p><button>提交评论</button></p>
</div>
<div class="comment_show"> {# 评论展示区域的div #}
<div class="feedback_area_title">评论列表</div>
<div class="comment_list">
{% for comment in comment_list %} {# 循环展示评论的数据 #}
<div>
<p>第{{ forloop.counter}}楼 -> By:{{ comment.user.username }} -> {{ comment.comment_time }} -> <button class="reply" username={{ comment.user.username }} pk={{ comment.pk }}>回复</button></p> {# 在此处定义一个回复按钮,用户实现子评论,并且自定义属性username和pk,用于下面回复功能的实现 #}
{% if comment.pre_comment_id %} {# 判断评论是否有父级评论 #}
<p id="p">原评论内容:{{ comment.pre_comment.comment_content }}</p> {# 如果有父级评论,则在中间显示父级评论的评论内容 #}
{% endif %}
<p>评论内容:{{ comment.comment_content }}</p>
<hr>
</div>
{% endfor %}
</div>
</div>
{# 提交评论的JS,发送Ajax请求 #}
<script>
var pid = "" {# 设置一个变量pid默认为空,用于后面作为数据库存储的父级评论的ID,如果没有父级评论则为空,子评论有父级评论 #}
{# 提交评论按钮的点击事件 #}
$(".comment_post p button").click(function (){
$.ajax({
url: '/comment_control/',
headers: {"X-CSRFToken": $.cookie("csrftoken")},
type: 'post',
data: {
comment_content: $("#comment_content").val(), {# 提交的数据内容data #}
article_id: {{ path }},
pid: pid
},
success: function (res){ {# 本例中返回的数据仅仅用于在控制台打印而已 #}
console.log(res) {# 控制台打印返回的数据 #}
$("#comment_content").val("") {# 提交完成后,清空评论输入框的内容 #}
pid = "" {# 子评论提交完成后,将pid默认设置为空,恢复为默认的父评论 #}
}
})
})
{# 回复按钮的点击事件 #}
$(".reply").click(function (){
$("#comment_content").focus() {# 回复按钮的事件,点击时,将光标聚集到评论输入框中 #}
var val = "@" + $(this).attr("username") + "\n" {# $(this)指代".reply"标签本身,获取这个标签的username值 #}
$("#comment_content").val(val) {# 回复时,自动在输入框加入:@要回复的人 #}
pid = $(this).attr("pk") {# 当点击回复时,父评论ID不再为空,此时修改为:对应评论的主键值ID #}
})
</script>
</body>
</html>
+19 -3
View File
@@ -106,6 +106,7 @@
if (page_tag === 'b9b101e') {
sub_btn = `<a class="dropdown-item" href="javascript:void(0);" onclick="window.open('2_d?id=${item.id}&s_p=b9b101e')">协同脑图</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.open_bianji(${item.id})">协同编辑</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.open_pinglun(${item.id})">评论</a>
<a class="dropdown-item" href="javascript:void(0);" onclick="mind.del_mind(${item.id},'card_${uuid}')">删除</a>`
@@ -144,9 +145,9 @@
});
},
/**显示分享链接*/
show_share_url: function (u, h) {
const url = `s_2_d?u=${u}&h=${h}`
$('#share_mind_modal .modal-body').html(`分享成功,请复制以下链接进行分享:<div>${window.location.protocol}//${window.location.host}/${url}</div>`)
show_share_url: function (u) {
const url = `2_pinglun?id=${u}`
$('#share_mind_modal .modal-body').html(`评论链接生成成功,请打开下面链接进行评论:<div>${window.location.protocol}//${window.location.host}/${url}</div>`)
$('#share_mind_modal_a').click()
},
/**分页组件*/
@@ -233,6 +234,20 @@
async: false
});
},
open_pinglun: function (mind_id) {
const me = this
$.ajax({
type: "post",
url: "pinglun",
headers: {"X-CSRFToken": $.cookie("csrftoken")},
data: {'id': mind_id},
dataType: "json",
success: function (resp) {
me.show_share_url(mind_id)
},
async: false
});
},
/**取消分享*/
share_cancel: function (mind_id) {
const me = this
@@ -671,6 +686,7 @@
<!-- 右侧内容 end-->
</div>
</div>
<!--导航 和 内容 end-->
</body>
<script src="/static/js/mind/modal.js"></script>
+16 -1
View File
@@ -200,6 +200,7 @@
async: false
});
},
/**分享脑图*/
share_mind: function (mind_id) {
const me = this
@@ -230,7 +231,21 @@
async: false
});
},
/**评论*/
open_pinglun: function (mind_id) {
const me = this
$.ajax({
type: "post",
url: "bianji",
headers: {"X-CSRFToken": $.cookie("csrftoken")},
data: {'id': mind_id},
dataType: "json",
success: function (resp) {
me.show_share_url(`${resp.data.u}`, `${resp.data.h}`)
},
async: false
});
},
/**取消分享*/
share_cancel: function (mind_id) {
const me = this