hgbook

view web/javascript/hsbook.js @ 579:80928ea6e7ae

Add the ability to include text files and have them XML-mangled.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue Mar 17 21:47:12 2009 -0700 (2009-03-17)
parents
children 2aebffe8609d
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 $("pre[@id]").each(function() {
69 $(this).after(loading($(this).attr("id")));
70 });
71 var chapid = $("div.preface, div.chapter, div.appendix, div.bibliography").attr("id");
72 $("#chapterfeed").attr("href",
73 $("#chapterfeed").attr("href") + chapid + "/");
74 $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" +
75 chapid + "/count/", function(data) {
76 $.each(data, function(id, item) {
77 var s = item == 1 ? "" : "s";
78 $("#comments_" + qid(id) + " span.commenttoggle").replaceWith(
79 "<a class='commenttoggle' id='toggle_" + id + "' " +
80 "pid='" + id + "' " +
81 "onclick='return loadComments(\"" + id + "\")' " +
82 "href='comments: show / hide'>" + item + " comment" + s + "</a>");
83 });
84 $("span.commenttoggle").each(function() {
85 var id = $(this).attr("pid");
86 $(this).replaceWith("<a class='commenttoggle' id='toggle_" + id + "' " +
87 "onclick='return loadComments(\"" + id + "\")' " +
88 "href='comment: add'>No comments</a>");
89 });
90 });
91 });