|
1 function qid(id) { |
|
2 return id.replace(/([.:])/g, "\\$1"); |
|
3 } |
|
4 |
|
5 function beforeComment(formData, jqForm, options) { |
|
6 var form = jqForm[0]; |
|
7 if (!form.comment.value) { |
|
8 $(options.target + " span.comment_error").empty().append( |
|
9 "<span class=\"comment_error\">Your comment is empty</span>"); |
|
10 return false; |
|
11 } |
|
12 if (!form.name.value) { |
|
13 $(options.target + " span.comment_error").empty().append( |
|
14 "<span class=\"comment_error\">Please provide a name</span>"); |
|
15 return false; |
|
16 } |
|
17 $(options.target + " span.comment_error").empty().after( |
|
18 "<img src=\"/support/icons/throbber.gif\" style=\"vertical-align: middle\"/>"); |
|
19 $(options.target + " input[@name=submit]").attr("disabled", true); |
|
20 } |
|
21 |
|
22 function ajaxifyForm(id) { |
|
23 var q = qid(id); |
|
24 |
|
25 $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment, |
|
26 success: function() { ajaxifyForm(id); }, |
|
27 target: "#comments_" + q }); |
|
28 } |
|
29 |
|
30 function toggleComment(id) { |
|
31 $("#toggle_" + qid(id)).nextAll().toggle(); |
|
32 return false; |
|
33 } |
|
34 |
|
35 function loadComments(id) { |
|
36 $("#comments_" + qid(id)).load(location.protocol + "//" + location.host + |
|
37 "/comments/single/" + id + "/", function() { |
|
38 ajaxifyForm(id); |
|
39 }); |
|
40 return false; |
|
41 } |
|
42 |
|
43 function loadAllComments() { |
|
44 $("a.commenttoggle").each(function() { |
|
45 var id = $(this).attr("pid"); |
|
46 if (id) { |
|
47 loadComments(id); |
|
48 } |
|
49 }); |
|
50 } |
|
51 |
|
52 |
|
53 |
|
54 $(document).ready(function() { |
|
55 function loading(id) { |
|
56 return " <span id=\"comments_" + id + "\" class=\"comment\">" + |
|
57 "<span pid=\"" + id + "\" class=\"commenttoggle\"><p>Loading...." + |
|
58 "</span>"; |
|
59 } |
|
60 |
|
61 $("p[@id]").each(function() { |
|
62 $(this).append(loading($(this).attr("id"))); |
|
63 }); |
|
64 $("p[@id]").each(function() { |
|
65 var url_string=window.location.pathname; |
|
66 var temp = new Array(); |
|
67 temp = url_string.split('/'); |
|
68 var chap_name=temp[temp.length-1].split('.')[0]; |
|
69 |
|
70 $.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) { |
|
71 $.each(data , function(id) { |
|
72 $("span.commenttoggle").replaceWith("<a class='commenttoggle'" + "href='comments: show / hide'>" + data.count +" "+"comments" + "</a>"); |
|
73 }); |
|
74 |
|
75 |
|
76 |
|
77 }); |
|
78 }); |
|
79 |
|
80 }); |