hgbook

annotate web/javascript/hsbook.js @ 652:751ee9bf2e8d

ch00-preface.xml: Typo fix
author Dongsheng Song <dongsheng.song@gmail.com>
date Fri Mar 20 16:59:07 2009 +0800 (2009-03-20)
parents
children 2aebffe8609d
rev   line source
bos@574 1 function qid(id) {
bos@574 2 return id.replace(/([.:])/g, "\\$1");
bos@574 3 }
bos@574 4
bos@574 5 function beforeComment(formData, jqForm, options) {
bos@574 6 var form = jqForm[0];
bos@574 7 if (!form.comment.value) {
bos@574 8 $(options.target + " span.comment_error").empty().append(
bos@574 9 "<span class=\"comment_error\">Your comment is empty</span>");
bos@574 10 return false;
bos@574 11 }
bos@574 12 if (!form.name.value) {
bos@574 13 $(options.target + " span.comment_error").empty().append(
bos@574 14 "<span class=\"comment_error\">Please provide a name</span>");
bos@574 15 return false;
bos@574 16 }
bos@574 17 $(options.target + " span.comment_error").empty().after(
bos@574 18 "<img src=\"figs/throbber.gif\" style=\"vertical-align: middle\"/>");
bos@574 19 $(options.target + " input[@name=submit]").attr("disabled", true);
bos@574 20 }
bos@574 21
bos@574 22 function ajaxifyForm(id) {
bos@574 23 var q = qid(id);
bos@574 24
bos@574 25 $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
bos@574 26 success: function() { ajaxifyForm(id); },
bos@574 27 target: "#comments_" + q });
bos@574 28 }
bos@574 29
bos@574 30 function toggleComment(id) {
bos@574 31 $("#toggle_" + qid(id)).nextAll().toggle();
bos@574 32 return false;
bos@574 33 }
bos@574 34
bos@574 35 function loadComments(id) {
bos@574 36 $("#comments_" + qid(id)).load(location.protocol + "//" + location.host +
bos@574 37 "/comments/single/" + id + "/", function() {
bos@574 38 ajaxifyForm(id);
bos@574 39 });
bos@574 40 return false;
bos@574 41 }
bos@574 42
bos@574 43 function loadAllComments() {
bos@574 44 $("a.commenttoggle").each(function() {
bos@574 45 var id = $(this).attr("pid");
bos@574 46 if (id) {
bos@574 47 loadComments(id);
bos@574 48 }
bos@574 49 });
bos@574 50 }
bos@574 51
bos@574 52 $(document).ready(function() {
bos@574 53 function loading(id) {
bos@574 54 return " <span id=\"comments_" + id + "\" class=\"comment\">" +
bos@574 55 "<span pid=\"" + id + "\" class=\"commenttoggle\">Loading..." +
bos@574 56 "</span></span>";
bos@574 57 }
bos@574 58 $("div.toc>p")
bos@574 59 .after("<p style='display: none;'><a onclick='return loadAllComments()'>" +
bos@574 60 "Load all comments (<b>slow</b>)</a></p>")
bos@574 61 .toggle(function() { $(this).nextAll().show("normal"); },
bos@574 62 function() { $(this).nextAll().hide("normal"); })
bos@574 63 .hover(function() { $(this).fadeTo("normal", 0.8); },
bos@574 64 function() { $(this).fadeTo("normal", 0.35); });
bos@574 65 $("p[@id]").each(function() {
bos@574 66 $(this).append(loading($(this).attr("id")));
bos@574 67 });
bos@574 68 $("pre[@id]").each(function() {
bos@574 69 $(this).after(loading($(this).attr("id")));
bos@574 70 });
bos@574 71 var chapid = $("div.preface, div.chapter, div.appendix, div.bibliography").attr("id");
bos@574 72 $("#chapterfeed").attr("href",
bos@574 73 $("#chapterfeed").attr("href") + chapid + "/");
bos@574 74 $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" +
bos@574 75 chapid + "/count/", function(data) {
bos@574 76 $.each(data, function(id, item) {
bos@574 77 var s = item == 1 ? "" : "s";
bos@574 78 $("#comments_" + qid(id) + " span.commenttoggle").replaceWith(
bos@574 79 "<a class='commenttoggle' id='toggle_" + id + "' " +
bos@574 80 "pid='" + id + "' " +
bos@574 81 "onclick='return loadComments(\"" + id + "\")' " +
bos@574 82 "href='comments: show / hide'>" + item + " comment" + s + "</a>");
bos@574 83 });
bos@574 84 $("span.commenttoggle").each(function() {
bos@574 85 var id = $(this).attr("pid");
bos@574 86 $(this).replaceWith("<a class='commenttoggle' id='toggle_" + id + "' " +
bos@574 87 "onclick='return loadComments(\"" + id + "\")' " +
bos@574 88 "href='comment: add'>No comments</a>");
bos@574 89 });
bos@574 90 });
bos@574 91 });