hsbook_back.js
author amit
Thu, 30 Sep 2010 11:36:30 +0530
changeset 0 54f784230511
child 2 f5e18f8ed036
permissions -rw-r--r--
Initial commit of django based commenting system for sphinx. Already has most of the boiler plate code.

function qid(id) {
  return id.replace(/([.:])/g, "\\$1");
}

function beforeComment(formData, jqForm, options) {
  var form = jqForm[0];
  if (!form.comment.value) {
    $(options.target + " span.comment_error").empty().append(
      "<span class=\"comment_error\">Your comment is empty</span>");
    return false;
  }
  if (!form.name.value) {
    $(options.target + " span.comment_error").empty().append(
      "<span class=\"comment_error\">Please provide a name</span>");
    return false;
  }
  $(options.target + " span.comment_error").empty().after(
    "<img src=\"/support/icons/throbber.gif\" style=\"vertical-align: middle\"/>");
  $(options.target + " input[@name=submit]").attr("disabled", true);
}

function ajaxifyForm(id) {
  var q = qid(id);

  $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
			     success: function() { ajaxifyForm(id); },
			     target: "#comments_" + q });
}

function toggleComment(id) {
  $("#toggle_" + qid(id)).nextAll().toggle();
  return false;
}

function loadComments(id) {
  $("#comments_" + qid(id)).load(location.protocol + "//" + location.host +
				 "/comments/single/" + id + "/", function() {
    ajaxifyForm(id);
  });
  return false;
}

function loadAllComments() {
  $("a.commenttoggle").each(function() {
    var id = $(this).attr("pid");
    if (id) {
      loadComments(id);
    }
  });
}



$(document).ready(function() {
  function loading(id) {
    return " <span id=\"comments_" + id + "\" class=\"comment\">" +
      "<span pid=\"" + id + "\" class=\"commenttoggle\"><p>Loading...." +
      "</span>";
  }

$("p[@id]").each(function() {
    $(this).append(loading($(this).attr("id")));
		   });
$("p[@id]").each(function() {
		   var url_string=window.location.pathname;
		   var temp = new Array();
		   temp = url_string.split('/');
		   var chap_name=temp[temp.length-1].split('.')[0];

		   $.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) {
	      $.each(data , function(id) {
		       $("span.commenttoggle").replaceWith("<a class='commenttoggle'" + "href='comments: show / hide'>" + data.count +" "+"comments" + "</a>");
 		   });



	    });
		  });

		  });