hgbook

annotate web/support/javascript/hsbook.js @ 906:92f13a597d93

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