comment.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. var list = []; //评论列表
  2. var editorId = "#comment_content";
  3. var verifycodeId = "#img_verifycode";
  4. var listId = "#comment_list";
  5. $(document).ready(init_comment);
  6. function init_comment() {
  7. load_comment_form();
  8. editor = $(editorId);
  9. var editor_inter = null;
  10. if (editor.length > 0) {
  11. // var v = ["html", "objc", "javascript", "css", "php", "csharp", "cpp", "java", "python", "ruby", "vb", "delphi", "sql", "plain"];
  12. // var k = ["HTML/XML","Objective-C", "JavaScript", "CSS", "PHP", "C#", "C++", "Java", "Python", "Ruby", "Visual Basic", "Delphi", "SQL", "其它"];
  13. // for (var i = 0; i < v.length; i++) {
  14. // $("#lang_list").append('<a href="#' + v[i] + '">' + k[i] + '</a>');
  15. // }
  16. $("#lang_list").append('<a class="long_name" href="#html">HTML/XML</a><a class="long_name" href="#objc">objective-c</a><a class="zhong_name" href="#delphi">Delphi</a><a class="zhong_name" href="#ruby">Ruby</a><a href="#php">PHP</a><a class="duan_name" href="#csharp">C#</a><a style=" border-right: none;" class="duan_name" href="#cpp">C++</a><a style=" border-bottom:none;"class="long_name" href="#javascript">JavaScript</a><a style=" border-bottom:none;" class="long_name" href="#vb">Visual Basic</a><a style=" border-bottom:none;" class="zhong_name" href="#python">Python</a><a style=" border-bottom:none;" class="zhong_name" href="#java">Java</a><a style="border-bottom:none;" class="duan_name" href="#css">CSS</a><a style="border-bottom:none;" class="duan_name" href="#sql">SQL</a><a style="border:none;" class="duan_name" href="#plain">其它</a>');
  17. editor.focus(function () {
  18. editor_inter = setInterval(function () {
  19. commentTip("还能输入" + (1000 - editor.val().length) + "个字符");
  20. }, 200);
  21. }).blur(function () {
  22. if (editor_inter) clearInterval(editor_inter);
  23. });
  24. }
  25. //加载列表
  26. loadList(1);
  27. }
  28. function noComments() {
  29. $(listId).html('<br />&nbsp;&nbsp;暂无评论<br /><br /><div class="clear"></div>');
  30. }
  31. function loadList(pageIndex, isSub) {
  32. if (commentscount == 0) {
  33. noComments();
  34. return;
  35. }
  36. pageIndex = parseInt(pageIndex) || 1;
  37. $("#comments_bar").html("正在加载评论...");
  38. var cmtUrl = "../../comment/list/" + fileName + "?page=" + (pageIndex || 1);
  39. if (isSub) cmtUrl += "&_" + Math.random();
  40. $.get(cmtUrl, function (json) {
  41. if (!json) {
  42. noComments();
  43. return;
  44. }
  45. var data = (typeof json == 'object') ? json : eval("(" + json + ")");
  46. if (isSub) list = data.list;
  47. else list = list.concat(data.list);
  48. var listHtml = '';
  49. //构造主题
  50. var topics = getTopics(list);
  51. var total = data.total > 0 ? data.total : topics.length;
  52. //组装HTM
  53. for (var i = 0; i < topics.length; i++) {
  54. var comment = topics[i];
  55. var layer = total - i;
  56. listHtml += getItemHtml(comment, layer);
  57. }
  58. listHtml += '<div class="clear"></div>';
  59. //输出列表
  60. $(listId).html(listHtml);
  61. //高亮评论中的代码段
  62. dp.SyntaxHighlighter.HighlightAll('code2');
  63. //展示昵称
  64. new CNick(listId + ' a.username').showNickname();
  65. //分页处理
  66. if (data.page.PageIndex >= data.page.PageCount) {
  67. $("#comment_bar").hide();
  68. } else {
  69. $("#comment_bar").html('<div id="load_comments" page="' + (pageIndex + 1) + '">查看更多评论</div>');
  70. }
  71. //添加按钮事件
  72. setBtnEvent();
  73. });
  74. };
  75. //获取评论主题
  76. function getTopics(list) {
  77. var topics = [];
  78. for (var i = 0; i < list.length; i++) {
  79. var t = list[i];
  80. if (t.ParentId == 0) {
  81. t.Replies = getReplies(t, list);
  82. topics.push(t);
  83. }
  84. }
  85. return topics;
  86. }
  87. //获取评论回复
  88. function getReplies(item, list) {
  89. var replies = [];
  90. for (var i = 0; i < list.length; i++) {
  91. var r = list[i];
  92. if (r.ParentId == item.CommentId) {
  93. r.Replies = getReplies(r, list);
  94. replies.push(r);
  95. }
  96. }
  97. return replies;
  98. }
  99. //获取评论的HTML
  100. function getItemHtml(comment, index, floor, deep) {
  101. if (!deep) deep = 0;
  102. var sty = deep > 3 ? ' style="margin-left:0;"' : '';
  103. var html = '<dl class="comment_item comment_' + (comment.ParentId > 0 ? "reply" : "topic") + '" id="comment_item_{id}"' + sty + '>' +
  104. '<dt class="comment_head" floor=' + (floor || index) + '>' + (comment.ParentId > 0 ? "Re:" : index + '楼') + ' <span class="user">';
  105. if (comment.UserName != null && comment.UserName != '')
  106. html += '<a class="username" href="/' + comment.UserName + '" target="_blank">' + comment.UserName + '</a>';
  107. else
  108. html += '[游客]';
  109. html += " <span class='ptime'>" + comment.PostTime + "发表</span> ";
  110. html += ' <a href="#reply" class="cmt_btn reply" title="回复">[回复]</a> <span class="comment_manage" style="display:none;" commentid={id} username={username}> <a href="#quote" class="cmt_btn quote" title="引用">[引用]</a> <a href="#report" class="cmt_btn report" title="举报">[举报]</a>';
  111. if (username == currentUserName || comment.UserName == currentUserName) html += ' <a href="#delete" class="cmt_btn delete" title="删除">[删除]</a>';
  112. html += '</span></dt>';
  113. html += '<dd class="comment_userface"><a href="/' + comment.UserName + '" target="_blank"><img src="' + comment.Userface + '" width="40" height="40" /></a></dd>';
  114. html += '<dd class="comment_body">' + replaceUBBToHTML(comment) + '</dd>';
  115. html = html.replace(/\{id\}/g, comment.CommentId).replace(/\{username\}/g, comment.UserName);
  116. if (comment.Replies != null) {
  117. for (var j = 0; j < comment.Replies.length; j++) {
  118. html += getItemHtml(comment.Replies[j], j + 1, index, deep + 1);
  119. }
  120. }
  121. html += "</dl>";
  122. return html;
  123. }
  124. //获取评论对象
  125. function getComment(commentId, list) {
  126. for (var i = 0; i < list.length; i++) {
  127. var comment = list[i];
  128. if (comment.CommentId == commentId)
  129. return comment;
  130. }
  131. return null;
  132. }
  133. function setBtnEvent() {
  134. $("#load_comments").click(function () {
  135. var page = $(this).attr("page");
  136. loadList(page);
  137. });
  138. //评论按钮点击
  139. $(".comment_head a").click(function () {
  140. var action = $(this).attr("href");
  141. action = action.substring(action.lastIndexOf('#'));
  142. var commentId = $(this).parent().attr("commentid");
  143. switch (action) {
  144. case "#reply":
  145. if (currentUserName) {
  146. commentId = $(".comment_manage", $(this).parent()).attr("commentid");
  147. replyComment(commentId, list);
  148. setEditorFocus();
  149. }
  150. return true;
  151. case "#quote":
  152. if (currentUserName) {
  153. quoteComment(commentId, list);
  154. setEditorFocus();
  155. }
  156. return true;
  157. case "#report":
  158. reportComment(commentId, this);
  159. break;
  160. case "#delete":
  161. deleteComment(commentId);
  162. break;
  163. default:
  164. return true;
  165. }
  166. return false;
  167. });
  168. $(".comment_item").mouseover(function () {
  169. $(".comment_manage", $(this)).eq(0).show();
  170. }).mouseout(function () {
  171. $(".comment_manage", $(this)).eq(0).hide();
  172. });
  173. }
  174. /*使评论框获得焦点*/
  175. function setEditorFocus() {
  176. var val = editor.val();
  177. editor.val('');
  178. editor.focus();
  179. editor.val(val);
  180. }
  181. //引用评论
  182. function quoteComment(commentId, list) {
  183. var comment = getComment(commentId, list);
  184. var content = comment.Content;
  185. if (comment.Content.length > 50) {
  186. content = comment.Content.substring(0, 50) + "...";
  187. }
  188. editor.val("[quote=" + (comment.UserName == null ? "游客" : comment.UserName) + "]" + content + "[/quote]\r\n");
  189. }
  190. //回复评论
  191. function replyComment(commentId, list) {
  192. var comment = getComment(commentId, list);
  193. editor.val('[reply]' + comment.UserName + "[/reply]\r\n");
  194. $("#comment_replyId").val(commentId);
  195. }
  196. //举报评论
  197. function reportComment(commentId, e) {
  198. report(commentId, 3, e);
  199. }
  200. //删除评论
  201. function deleteComment(commentId) {
  202. if (!confirm("你确定要删除这篇评论吗?")) return;
  203. var delUrl = blog_address + "/comment/delete?commentid=" + commentId + "&filename=" + fileName;
  204. $.get(delUrl, function (data) {
  205. if (data.result == 1) {
  206. $("#comment_item_" + commentId).hide().remove();
  207. } else {
  208. alert("你没有删除该评论的权限!");
  209. }
  210. });
  211. }
  212. //替换评论的UBB代码
  213. function replaceUBBToHTML(comment) {
  214. var content = $.trim(comment.Content);
  215. var re = /\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig;
  216. var codelist = [];
  217. while ((mc = re.exec(content)) != null) {
  218. codelist.push(mc[0]);
  219. content = content.replace(mc[0], "--code--");
  220. }
  221. content = replaceQuote(content);
  222. //content = content.replace(/\[e(\d+)\]/g, "<img src=\"" + static_host + "/images/emotions/e$1.gif\"\/>");
  223. content = content.replace(/\[reply]([\s\S]*?)\[\/reply\][\r\n]{0,2}/gi, "回复$1:");
  224. content = content.replace(/\[url=([^\]]+)]([\s\S]*?)\[\/url\]/gi, '<a href="$1" target="_blank">$2</a>');
  225. content = content.replace(/\[img(=([^\]]+))?]([\s\S]*?)\[\/img\]/gi, '<img src="$3" style="max-width:400px;max-height:200px;" border="0" title="$2" />');
  226. //content = content.replace(/\[(\/?)(b|i|u|p)\]/ig, "<$1$2>");
  227. content = content.replace(/\r?\n/ig, "<br />");
  228. if (codelist.length > 0) {
  229. var re1 = /--code--/ig;
  230. var i = 0;
  231. while ((mc = re1.exec(content)) != null) {
  232. content = content.replace(mc[0], codelist[i]);
  233. i++;
  234. }
  235. }
  236. content = content.replace(/\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig, function (m0, m1, m2) {
  237. if ($.trim(m2) == "") return '';
  238. return '<pre name="code2" class="' + m1 + '">' + m2 + '</pre>';
  239. });
  240. return content;
  241. }
  242. //替换评论的引用
  243. function replaceQuote(str) {
  244. var m = /\[quote=([^\]]+)]([\s\S]*)\[\/quote\]/gi.exec(str);
  245. if (m) {
  246. return str.replace(m[0], '<fieldset><legend>引用“' + m[1] + '”的评论:</legend>' + replaceQuote(m[2]) + '</fieldset>');
  247. } else {
  248. return str;
  249. }
  250. }
  251. function load_comment_form() {
  252. var un = getcookie("UserName").toLowerCase();
  253. if (islock) {
  254. $("#comment_form").html("<div class='notice'>该文章已被禁止评论!</div>");
  255. } else if (currentUserName || (un != null&&un!=""&&un!=undefined)) {
  256. var html = '<a name="commentbox"></a><a name="reply"></a><a name="quote"></a><form action="/' + username + '/comment/submit?id=' + fileName + '" method="post" onsubmit="return subform(this);">' +
  257. '<div class="commentform"><div class="panel_head">发表评论</div>' +
  258. '<ul><li class="left">用 户 名:</li><li class="right">' + currentUserName + '</li></ul>' +
  259. '<ul><li class="left">评论内容:</li><li class="right" style="position:relative;">' +
  260. '<div id="ubbtools">' +
  261. '<a href="#insertcode" code="code"><img src="' + static_host + '/images/ubb/code.gif" border="0" alt="插入代码" title="插入代码"/></a>' +
  262. '</div>' +
  263. '<div id="lang_list" style="position: absolute; top: 28px; left: 0px; display: none;"></div>' +
  264. '<textarea class="comment_content" name="comment_content" id="comment_content" style="width: 400px; height: 200px;"></textarea>' +
  265. '</li></ul>' +
  266. '<ul><input type="hidden" id="comment_replyId" name="comment_replyId" />' +
  267. '<input type="hidden" id="comment_userId" name="comment_userId" value="521203" />' +
  268. '<input type="hidden" id="commentId" name="commentId" value="" />' +
  269. '<input type="submit" class="comment_btn" value="提交" />&nbsp;&nbsp;<span id="tip_comment" style="color: Red; display: none;"></span>' +
  270. '</ul></div></form>';
  271. $("#comment_form").html(html);
  272. } else {
  273. var curl = encodeURIComponent(location.href);
  274. $("#comment_form").html('<div class="guest_link">您还没有登录,请' +
  275. '<a href="javascript:void(0);" onclick="javascript:csdn.showLogin(function (dat) {js_logined(dat.data.userName);});">[登录]</a>或' +
  276. '<a href="http://passport.csdn.net/account/register?from=' + curl + '">[注册]</a></div>');
  277. }
  278. ubb_event();
  279. }
  280. function getcookie(name) {
  281. var cookie_start = document.cookie.indexOf(name);
  282. var cookie_end = document.cookie.indexOf(";", cookie_start);
  283. return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
  284. }
  285. var c_doing = false;
  286. function subform(e) {
  287. if (c_doing) return false;
  288. var content = $.trim($(editorId).val());
  289. if (content == "") {
  290. commentTip("评论内容没有填写!");
  291. return false;
  292. } else if (content.length > 1000) {
  293. commentTip("评论内容太长了,不能超过1000个字符!");
  294. return false;
  295. }
  296. var commentId = $("#commentId").val();
  297. commentTip("正在发表评论...");
  298. var beginTime = new Date();
  299. $(editorId).attr("disabled", true);
  300. $("button[type=submit]", e).attr("disabled", true);
  301. c_doing = true;
  302. $.ajax({
  303. type: "POST",
  304. url: $(e).attr("action"),
  305. data: {
  306. "commentid": commentId,
  307. "content": content,
  308. "replyId": $("#comment_replyId").val()
  309. },
  310. success: function (data) {
  311. c_doing = false;
  312. commentTip(data.content);
  313. if (data.result) {
  314. var rcommentid=$("#comment_replyId").val()
  315. $(editorId).val('');
  316. $("#comment_replyId,#comment_verifycode").val('');
  317. commentscount++;
  318. loadList(1, true);
  319. $(editorId).attr("disabled", false);
  320. $("button[type=submit]", e).attr("disabled", false);
  321. commentTip("发表成功!评论耗时:" + (new Date() - beginTime) + "毫秒")
  322. if (rcommentid!=undefined && rcommentid != "")
  323. {
  324. $("html,body").animate({ scrollTop: $("#comment_item_" + rcommentid).offset().top }, 1000);
  325. }
  326. }
  327. }
  328. });
  329. return false;
  330. }
  331. //操作提示
  332. var _c_t;
  333. function commentTip(message) {
  334. $("#tip_comment").html(message).show();
  335. clearTimeout(_c_t);
  336. _c_t = setTimeout(function () {
  337. $("#tip_comment").hide();
  338. }, 10000);
  339. }
  340. function ubb_event() {
  341. //ubb按钮事件
  342. $("#ubbtools").children().click(function () {
  343. var selectedValue = editor.selection();
  344. editor.focus();
  345. var code = $(this).attr("code");
  346. switch (code) {
  347. case "code":
  348. var lang_list = $("#lang_list");
  349. lang_list.show();
  350. lang_list.children().each(function () {
  351. $(this).unbind("click").click(function () {
  352. editor.val("[code=" + $.trim(this.href.split('#')[1]) + "]\n" + selectedValue + "\n[/code]");
  353. lang_list.hide();
  354. });
  355. });
  356. editor.click(function (e) {
  357. lang_list.hide();
  358. });
  359. break;
  360. default:
  361. editor.val("[" + code + "]" + selectedValue + "[/" + code + "]");
  362. break;
  363. }
  364. return false;
  365. });
  366. }