web/html/support/hsbook.js
changeset 0 8083d21c0020
equal deleted inserted replaced
-1:000000000000 0:8083d21c0020
       
     1 function qid(id) {
       
     2   return id.replace(/([.:])/g, "\\$1");
       
     3 }
       
     4 
       
     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=\"/support/icons/throbber.gif\" style=\"vertical-align: middle\"/>");
       
    19   $(options.target + " input[@name=submit]").attr("disabled", true);
       
    20 }
       
    21 
       
    22 function ajaxifyForm(id) {
       
    23   var q = qid(id);
       
    24   
       
    25   $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
       
    26 			     success: function() { ajaxifyForm(id); },
       
    27 			     target: "#comments_" + q });
       
    28 }
       
    29 
       
    30 function toggleComment(id) {
       
    31   $("#toggle_" + qid(id)).nextAll().toggle();
       
    32   return false;
       
    33 }
       
    34 
       
    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 }
       
    42 
       
    43 function loadAllComments() {
       
    44   $("a.commenttoggle").each(function() {
       
    45     var id = $(this).attr("pid");
       
    46     if (id) {
       
    47       loadComments(id);
       
    48     }
       
    49   });
       
    50 }
       
    51 
       
    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   $(".chapter p[@id]").each(function() {
       
    66     $(this).append(loading($(this).attr("id")));
       
    67   });
       
    68   $(".chapter table[@id].equation").each(function() {
       
    69     id = $(this).attr("id");
       
    70     $("#" + id + " tr").after('<tr><td colspan="4">' + loading($(this).attr("id")) + '</td></tr>');
       
    71   });
       
    72   $(".chapter pre[@id]").each(function() {
       
    73     $(this).after(loading($(this).attr("id")));
       
    74   });
       
    75   var chapid = $("div.preface, div.chapter, div.appendix, div.bibliography").attr("id");
       
    76   $("#chapterfeed").attr("href",
       
    77 			 $("#chapterfeed").attr("href") + chapid + "/");
       
    78   $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" +
       
    79 	    chapid + "/count/", function(data) {
       
    80     $.each(data, function(id, item) {
       
    81       var s = item == 1 ? "" : "s";
       
    82       $("#comments_" + qid(id) + " span.commenttoggle").replaceWith(
       
    83         "<a class='commenttoggle' id='toggle_" + id + "' " +
       
    84 	"pid='" + id + "' " +
       
    85 	"onclick='return loadComments(\"" + id + "\")' " +
       
    86 	"href='comments: show / hide'>" + item + " comment" + s + "</a>");
       
    87     });
       
    88     $("span.commenttoggle").each(function() {
       
    89       var id = $(this).attr("pid");
       
    90       $(this).replaceWith("<a class='commenttoggle' id='toggle_" + id + "' " +
       
    91 			  "onclick='return loadComments(\"" + id + "\")' " +
       
    92 			  "href='comment: add'>No comments</a>");
       
    93     });
       
    94   });
       
    95 });