hgbook

view web/javascript/hsbook.js @ 1013:44946b10a4b3

merge with André Sintzoff
author Romain PELISSE <belaran@gmail.com>
date Tue Nov 24 11:44:49 2009 +0100 (2009-11-24)
parents 2aebffe8609d
children
line source
1 function qid(id) {
2 return id.replace(/([.:])/g, "\\$1");
3 }
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=\"figs/throbber.gif\" style=\"vertical-align: middle\"/>");
19 $(options.target + " input[@name=submit]").attr("disabled", true);
20 }
22 function ajaxifyForm(id) {
23 var q = qid(id);
25 $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
26 success: function() { ajaxifyForm(id); },
27 target: "#comments_" + q });
28 }
30 function toggleComment(id) {
31 $("#toggle_" + qid(id)).nextAll().toggle();
32 return false;
33 }
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 }
43 function loadAllComments() {
44 $("a.commenttoggle").each(function() {
45 var id = $(this).attr("pid");
46 if (id) {
47 loadComments(id);
48 }
49 });
50 }
52 $(document).ready(function() {
53 function loading(id) {
54 return " <span id=\"comments_" + id + "\" class=\"comment\">" +
55 "<span pid=\"" + id + "\" class=\"commenttoggle\">Loading..." +
56 "</span></span>";
57 }
58 $("div.toc>p")
59 .after("<p style='display: none;'><a onclick='return loadAllComments()'>" +
60 "Load all comments (<b>slow</b>)</a></p>")
61 .toggle(function() { $(this).nextAll().show("normal"); },
62 function() { $(this).nextAll().hide("normal"); })
63 .hover(function() { $(this).fadeTo("normal", 0.8); },
64 function() { $(this).fadeTo("normal", 0.35); });
65 $("p[@id]").each(function() {
66 $(this).append(loading($(this).attr("id")));
67 });
68 $("table[@id].equation").each(function() {
69 $(this).after(loading($(this).attr("id")));
70 });
71 $("pre[@id]").each(function() {
72 $(this).after(loading($(this).attr("id")));
73 });
74 var chapid = $("body, div.preface, div.chapter, div.appendix, div.bibliography").attr("id");
75 $("#chapterfeed").attr("href",
76 $("#chapterfeed").attr("href") + chapid + "/");
77 $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" +
78 chapid + "/count/", function(data) {
79 $.each(data, function(id, item) {
80 var s = item == 1 ? "" : "s";
81 $("#comments_" + qid(id) + " span.commenttoggle").replaceWith(
82 "<a class='commenttoggle' id='toggle_" + id + "' " +
83 "pid='" + id + "' " +
84 "onclick='return loadComments(\"" + id + "\")' " +
85 "href='comments: show / hide'>" + item + " comment" + s + "</a>");
86 });
87 $("span.commenttoggle").each(function() {
88 var id = $(this).attr("pid");
89 $(this).replaceWith("<a class='commenttoggle' id='toggle_" + id + "' " +
90 "onclick='return loadComments(\"" + id + "\")' " +
91 "href='comment: add'>No comments</a>");
92 });
93 });
94 });