var list = []; //评论列表
var editorId = "#comment_content";
var verifycodeId = "#img_verifycode";
var listId = "#comment_list";
$(document).ready(init_comment);
function init_comment() {
load_comment_form();
editor = $(editorId);
var editor_inter = null;
if (editor.length > 0) {
// var v = ["html", "objc", "javascript", "css", "php", "csharp", "cpp", "java", "python", "ruby", "vb", "delphi", "sql", "plain"];
// var k = ["HTML/XML","Objective-C", "JavaScript", "CSS", "PHP", "C#", "C++", "Java", "Python", "Ruby", "Visual Basic", "Delphi", "SQL", "其它"];
// for (var i = 0; i < v.length; i++) {
// $("#lang_list").append('' + k[i] + '');
// }
$("#lang_list").append('HTML/XMLobjective-cDelphiRubyPHPC#C++JavaScriptVisual BasicPythonJavaCSSSQL其它');
editor.focus(function () {
editor_inter = setInterval(function () {
commentTip("还能输入" + (1000 - editor.val().length) + "个字符");
}, 200);
}).blur(function () {
if (editor_inter) clearInterval(editor_inter);
});
}
//加载列表
loadList(1);
}
function noComments() {
$(listId).html(' 暂无评论
');
}
function loadList(pageIndex, isSub) {
if (commentscount == 0) {
noComments();
return;
}
pageIndex = parseInt(pageIndex) || 1;
$("#comments_bar").html("正在加载评论...");
var cmtUrl = "../../comment/list/" + fileName + "?page=" + (pageIndex || 1);
if (isSub) cmtUrl += "&_" + Math.random();
$.get(cmtUrl, function (json) {
if (!json) {
noComments();
return;
}
var data = (typeof json == 'object') ? json : eval("(" + json + ")");
if (isSub) list = data.list;
else list = list.concat(data.list);
var listHtml = '';
//构造主题
var topics = getTopics(list);
var total = data.total > 0 ? data.total : topics.length;
//组装HTM
for (var i = 0; i < topics.length; i++) {
var comment = topics[i];
var layer = total - i;
listHtml += getItemHtml(comment, layer);
}
listHtml += '';
//输出列表
$(listId).html(listHtml);
//高亮评论中的代码段
dp.SyntaxHighlighter.HighlightAll('code2');
//展示昵称
new CNick(listId + ' a.username').showNickname();
//分页处理
if (data.page.PageIndex >= data.page.PageCount) {
$("#comment_bar").hide();
} else {
$("#comment_bar").html('
查看更多评论
');
}
//添加按钮事件
setBtnEvent();
});
};
//获取评论主题
function getTopics(list) {
var topics = [];
for (var i = 0; i < list.length; i++) {
var t = list[i];
if (t.ParentId == 0) {
t.Replies = getReplies(t, list);
topics.push(t);
}
}
return topics;
}
//获取评论回复
function getReplies(item, list) {
var replies = [];
for (var i = 0; i < list.length; i++) {
var r = list[i];
if (r.ParentId == item.CommentId) {
r.Replies = getReplies(r, list);
replies.push(r);
}
}
return replies;
}
//获取评论的HTML
function getItemHtml(comment, index, floor, deep) {
if (!deep) deep = 0;
var sty = deep > 3 ? ' style="margin-left:0;"' : '';
var html = '
' +
'
' + (comment.ParentId > 0 ? "Re:" : index + '楼') + ' ';
if (comment.UserName != null && comment.UserName != '')
html += '' + comment.UserName + '';
else
html += '[游客]';
html += " " + comment.PostTime + "发表 ";
html += ' [回复][引用][举报]';
if (username == currentUserName || comment.UserName == currentUserName) html += ' [删除]';
html += '
';
html += '
';
html += '
' + replaceUBBToHTML(comment) + '
';
html = html.replace(/\{id\}/g, comment.CommentId).replace(/\{username\}/g, comment.UserName);
if (comment.Replies != null) {
for (var j = 0; j < comment.Replies.length; j++) {
html += getItemHtml(comment.Replies[j], j + 1, index, deep + 1);
}
}
html += "
";
return html;
}
//获取评论对象
function getComment(commentId, list) {
for (var i = 0; i < list.length; i++) {
var comment = list[i];
if (comment.CommentId == commentId)
return comment;
}
return null;
}
function setBtnEvent() {
$("#load_comments").click(function () {
var page = $(this).attr("page");
loadList(page);
});
//评论按钮点击
$(".comment_head a").click(function () {
var action = $(this).attr("href");
action = action.substring(action.lastIndexOf('#'));
var commentId = $(this).parent().attr("commentid");
switch (action) {
case "#reply":
if (currentUserName) {
commentId = $(".comment_manage", $(this).parent()).attr("commentid");
replyComment(commentId, list);
setEditorFocus();
}
return true;
case "#quote":
if (currentUserName) {
quoteComment(commentId, list);
setEditorFocus();
}
return true;
case "#report":
reportComment(commentId, this);
break;
case "#delete":
deleteComment(commentId);
break;
default:
return true;
}
return false;
});
$(".comment_item").mouseover(function () {
$(".comment_manage", $(this)).eq(0).show();
}).mouseout(function () {
$(".comment_manage", $(this)).eq(0).hide();
});
}
/*使评论框获得焦点*/
function setEditorFocus() {
var val = editor.val();
editor.val('');
editor.focus();
editor.val(val);
}
//引用评论
function quoteComment(commentId, list) {
var comment = getComment(commentId, list);
var content = comment.Content;
if (comment.Content.length > 50) {
content = comment.Content.substring(0, 50) + "...";
}
editor.val("[quote=" + (comment.UserName == null ? "游客" : comment.UserName) + "]" + content + "[/quote]\r\n");
}
//回复评论
function replyComment(commentId, list) {
var comment = getComment(commentId, list);
editor.val('[reply]' + comment.UserName + "[/reply]\r\n");
$("#comment_replyId").val(commentId);
}
//举报评论
function reportComment(commentId, e) {
report(commentId, 3, e);
}
//删除评论
function deleteComment(commentId) {
if (!confirm("你确定要删除这篇评论吗?")) return;
var delUrl = blog_address + "/comment/delete?commentid=" + commentId + "&filename=" + fileName;
$.get(delUrl, function (data) {
if (data.result == 1) {
$("#comment_item_" + commentId).hide().remove();
} else {
alert("你没有删除该评论的权限!");
}
});
}
//替换评论的UBB代码
function replaceUBBToHTML(comment) {
var content = $.trim(comment.Content);
var re = /\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig;
var codelist = [];
while ((mc = re.exec(content)) != null) {
codelist.push(mc[0]);
content = content.replace(mc[0], "--code--");
}
content = replaceQuote(content);
//content = content.replace(/\[e(\d+)\]/g, "");
content = content.replace(/\[reply]([\s\S]*?)\[\/reply\][\r\n]{0,2}/gi, "回复$1:");
content = content.replace(/\[url=([^\]]+)]([\s\S]*?)\[\/url\]/gi, '$2');
content = content.replace(/\[img(=([^\]]+))?]([\s\S]*?)\[\/img\]/gi, '');
//content = content.replace(/\[(\/?)(b|i|u|p)\]/ig, "<$1$2>");
content = content.replace(/\r?\n/ig, " ");
if (codelist.length > 0) {
var re1 = /--code--/ig;
var i = 0;
while ((mc = re1.exec(content)) != null) {
content = content.replace(mc[0], codelist[i]);
i++;
}
}
content = content.replace(/\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig, function (m0, m1, m2) {
if ($.trim(m2) == "") return '';
return '
' + m2 + '
';
});
return content;
}
//替换评论的引用
function replaceQuote(str) {
var m = /\[quote=([^\]]+)]([\s\S]*)\[\/quote\]/gi.exec(str);
if (m) {
return str.replace(m[0], '');
} else {
return str;
}
}
function load_comment_form() {
var un = getcookie("UserName").toLowerCase();
if (islock) {
$("#comment_form").html("
该文章已被禁止评论!
");
} else if (currentUserName || (un != null&&un!=""&&un!=undefined)) {
var html = '';
$("#comment_form").html(html);
} else {
var curl = encodeURIComponent(location.href);
$("#comment_form").html('