bos@574: function qid(id) { bos@574: return id.replace(/([.:])/g, "\\$1"); bos@574: } bos@574: bos@574: function beforeComment(formData, jqForm, options) { bos@574: var form = jqForm[0]; bos@574: if (!form.comment.value) { bos@574: $(options.target + " span.comment_error").empty().append( bos@574: "Your comment is empty"); bos@574: return false; bos@574: } bos@574: if (!form.name.value) { bos@574: $(options.target + " span.comment_error").empty().append( bos@574: "Please provide a name"); bos@574: return false; bos@574: } bos@574: $(options.target + " span.comment_error").empty().after( bos@574: ""); bos@574: $(options.target + " input[@name=submit]").attr("disabled", true); bos@574: } bos@574: bos@574: function ajaxifyForm(id) { bos@574: var q = qid(id); bos@574: bos@574: $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment, bos@574: success: function() { ajaxifyForm(id); }, bos@574: target: "#comments_" + q }); bos@574: } bos@574: bos@574: function toggleComment(id) { bos@574: $("#toggle_" + qid(id)).nextAll().toggle(); bos@574: return false; bos@574: } bos@574: bos@574: function loadComments(id) { bos@574: $("#comments_" + qid(id)).load(location.protocol + "//" + location.host + bos@574: "/comments/single/" + id + "/", function() { bos@574: ajaxifyForm(id); bos@574: }); bos@574: return false; bos@574: } bos@574: bos@574: function loadAllComments() { bos@574: $("a.commenttoggle").each(function() { bos@574: var id = $(this).attr("pid"); bos@574: if (id) { bos@574: loadComments(id); bos@574: } bos@574: }); bos@574: } bos@574: bos@574: $(document).ready(function() { bos@574: function loading(id) { bos@574: return " " + bos@574: "Loading..." + bos@574: ""; bos@574: } bos@574: $("div.toc>p") bos@574: .after("

" + bos@574: "Load all comments (slow)

") bos@574: .toggle(function() { $(this).nextAll().show("normal"); }, bos@574: function() { $(this).nextAll().hide("normal"); }) bos@574: .hover(function() { $(this).fadeTo("normal", 0.8); }, bos@574: function() { $(this).fadeTo("normal", 0.35); }); bos@574: $("p[@id]").each(function() { bos@574: $(this).append(loading($(this).attr("id"))); bos@574: }); bos@574: $("pre[@id]").each(function() { bos@574: $(this).after(loading($(this).attr("id"))); bos@574: }); bos@574: var chapid = $("div.preface, div.chapter, div.appendix, div.bibliography").attr("id"); bos@574: $("#chapterfeed").attr("href", bos@574: $("#chapterfeed").attr("href") + chapid + "/"); bos@574: $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" + bos@574: chapid + "/count/", function(data) { bos@574: $.each(data, function(id, item) { bos@574: var s = item == 1 ? "" : "s"; bos@574: $("#comments_" + qid(id) + " span.commenttoggle").replaceWith( bos@574: "" + item + " comment" + s + ""); bos@574: }); bos@574: $("span.commenttoggle").each(function() { bos@574: var id = $(this).attr("pid"); bos@574: $(this).replaceWith("No comments"); bos@574: }); bos@574: }); bos@574: });