hgbook

diff web/javascript/hsbook.js @ 621:1ef7708b3b7f

Rename html to htdocs
author Dongsheng Song <dongsheng.song@gmail.com>
date Thu Mar 12 15:35:19 2009 +0800 (2009-03-12)
parents
children 2aebffe8609d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/web/javascript/hsbook.js	Thu Mar 12 15:35:19 2009 +0800
     1.3 @@ -0,0 +1,91 @@
     1.4 +function qid(id) {
     1.5 +  return id.replace(/([.:])/g, "\\$1");
     1.6 +}
     1.7 +
     1.8 +function beforeComment(formData, jqForm, options) {
     1.9 +  var form = jqForm[0];
    1.10 +  if (!form.comment.value) {
    1.11 +    $(options.target + " span.comment_error").empty().append(
    1.12 +      "<span class=\"comment_error\">Your comment is empty</span>");
    1.13 +    return false;
    1.14 +  }
    1.15 +  if (!form.name.value) {
    1.16 +    $(options.target + " span.comment_error").empty().append(
    1.17 +      "<span class=\"comment_error\">Please provide a name</span>");
    1.18 +    return false;
    1.19 +  }
    1.20 +  $(options.target + " span.comment_error").empty().after(
    1.21 +    "<img src=\"figs/throbber.gif\" style=\"vertical-align: middle\"/>");
    1.22 +  $(options.target + " input[@name=submit]").attr("disabled", true);
    1.23 +}
    1.24 +
    1.25 +function ajaxifyForm(id) {
    1.26 +  var q = qid(id);
    1.27 +  
    1.28 +  $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
    1.29 +			     success: function() { ajaxifyForm(id); },
    1.30 +			     target: "#comments_" + q });
    1.31 +}
    1.32 +
    1.33 +function toggleComment(id) {
    1.34 +  $("#toggle_" + qid(id)).nextAll().toggle();
    1.35 +  return false;
    1.36 +}
    1.37 +
    1.38 +function loadComments(id) {
    1.39 +  $("#comments_" + qid(id)).load(location.protocol + "//" + location.host +
    1.40 +				 "/comments/single/" + id + "/", function() {
    1.41 +    ajaxifyForm(id);
    1.42 +  });
    1.43 +  return false;
    1.44 +}
    1.45 +
    1.46 +function loadAllComments() {
    1.47 +  $("a.commenttoggle").each(function() {
    1.48 +    var id = $(this).attr("pid");
    1.49 +    if (id) {
    1.50 +      loadComments(id);
    1.51 +    }
    1.52 +  });
    1.53 +}
    1.54 +
    1.55 +$(document).ready(function() {
    1.56 +  function loading(id) {
    1.57 +    return " <span id=\"comments_" + id + "\" class=\"comment\">" +
    1.58 +      "<span pid=\"" + id + "\" class=\"commenttoggle\">Loading..." +
    1.59 +      "</span></span>";
    1.60 +  }
    1.61 +  $("div.toc>p")
    1.62 +    .after("<p style='display: none;'><a onclick='return loadAllComments()'>" +
    1.63 +	   "Load all comments (<b>slow</b>)</a></p>")
    1.64 +    .toggle(function() { $(this).nextAll().show("normal"); },
    1.65 +	    function() { $(this).nextAll().hide("normal"); })
    1.66 +    .hover(function() { $(this).fadeTo("normal", 0.8); },
    1.67 +	   function() { $(this).fadeTo("normal", 0.35); });
    1.68 +  $("p[@id]").each(function() {
    1.69 +    $(this).append(loading($(this).attr("id")));
    1.70 +  });
    1.71 +  $("pre[@id]").each(function() {
    1.72 +    $(this).after(loading($(this).attr("id")));
    1.73 +  });
    1.74 +  var chapid = $("div.preface, div.chapter, div.appendix, div.bibliography").attr("id");
    1.75 +  $("#chapterfeed").attr("href",
    1.76 +			 $("#chapterfeed").attr("href") + chapid + "/");
    1.77 +  $.getJSON(location.protocol + "//" + location.host + "/comments/chapter/" +
    1.78 +	    chapid + "/count/", function(data) {
    1.79 +    $.each(data, function(id, item) {
    1.80 +      var s = item == 1 ? "" : "s";
    1.81 +      $("#comments_" + qid(id) + " span.commenttoggle").replaceWith(
    1.82 +        "<a class='commenttoggle' id='toggle_" + id + "' " +
    1.83 +	"pid='" + id + "' " +
    1.84 +	"onclick='return loadComments(\"" + id + "\")' " +
    1.85 +	"href='comments: show / hide'>" + item + " comment" + s + "</a>");
    1.86 +    });
    1.87 +    $("span.commenttoggle").each(function() {
    1.88 +      var id = $(this).attr("pid");
    1.89 +      $(this).replaceWith("<a class='commenttoggle' id='toggle_" + id + "' " +
    1.90 +			  "onclick='return loadComments(\"" + id + "\")' " +
    1.91 +			  "href='comment: add'>No comments</a>");
    1.92 +    });
    1.93 +  });
    1.94 +});