hsbook_back.js
changeset 2 f5e18f8ed036
parent 0 54f784230511
equal deleted inserted replaced
1:5574cfc2b28d 2:f5e18f8ed036
     1 function qid(id) {
     1 function qid(id) {
     2   return id.replace(/([.:])/g, "\\$1");
     2   return id.replace(/([.:])/g, "\\$1");
     3 }
     3 }
     4 
     4 
     5 function beforeComment(formData, jqForm, options) {
     5 function beforeComment(formData, jqForm, options) {
     6   var form = jqForm[0];
     6   var form=jqForm[0];
     7   if (!form.comment.value) {
     7 
     8     $(options.target + " span.comment_error").empty().append(
     8   if (form.comment.textLength.toString()=='0') {
     9       "<span class=\"comment_error\">Your comment is empty</span>");
     9     $("span.comment_error").empty().append(
       
    10       "<span class=\"comment_error\">  Your comment is empty</span>");
    10     return false;
    11     return false;
    11   }
    12   }
    12   if (!form.name.value) {
    13   if (form.name.textLength.toString()=='0') {
    13     $(options.target + " span.comment_error").empty().append(
    14     $("span.comment_error").empty().append(
    14       "<span class=\"comment_error\">Please provide a name</span>");
    15       "<span class=\"comment_error\">   Please provide a name</span>");
    15     return false;
    16     return false;
    16   }
    17   }
    17   $(options.target + " span.comment_error").empty().after(
    18   $(options.target + " span.comment_error").empty().after(
    18     "<img src=\"/support/icons/throbber.gif\" style=\"vertical-align: middle\"/>");
    19     "<img src=\"/support/icons/throbber.gif\" style=\"vertical-align: middle\"/>");
    19   $(options.target + " input[@name=submit]").attr("disabled", true);
    20   $("input[@name=submit]").attr("disabled", true);
    20 }
    21 }
    21 
    22 
    22 function ajaxifyForm(id) {
    23 function ajaxifyForm(id) {
    23   var q = qid(id);
       
    24 
    24 
    25   $("#form_" + q).ajaxForm({ beforeSubmit: beforeComment,
    25 //  $('#form_func_1').replaceWith('something');
    26 			     success: function() { ajaxifyForm(id); },
    26 
    27 			     target: "#comments_" + q });
    27   var substring=id.substr(9);
    28 }
    28   $('#form_'+substring).ajaxForm({beforeSubmit: beforeComment,
       
    29 			      success: function(){ loadComments(id);}
       
    30 
       
    31 			     });}
       
    32 
    29 
    33 
    30 function toggleComment(id) {
    34 function toggleComment(id) {
    31   $("#toggle_" + qid(id)).nextAll().toggle();
    35   $("#toggle_" + qid(id)).nextAll().toggle();
    32   return false;
    36   return false;
    33 }
    37 }
    34 
    38 
    35 function loadComments(id) {
    39 
    36   $("#comments_" + qid(id)).load(location.protocol + "//" + location.host +
    40 function loadComments(id)
    37 				 "/comments/single/" + id + "/", function() {
    41 {
    38     ajaxifyForm(id);
    42 
    39   });
    43   var substring=id.substr(9);
    40   return false;
    44   $('#comments_'+substring).load("http://127.0.0.1:8000/single/"+ substring +'/',function() { ajaxifyForm(id);}
       
    45 				);
       
    46 
    41 }
    47 }
       
    48 
       
    49 
       
    50 
    42 
    51 
    43 function loadAllComments() {
    52 function loadAllComments() {
    44   $("a.commenttoggle").each(function() {
    53   $("a.commenttoggle").each(function() {
    45     var id = $(this).attr("pid");
    54     var id = $(this).attr("pid");
    46     if (id) {
    55     if (id) {
    59   }
    68   }
    60 
    69 
    61 $("p[@id]").each(function() {
    70 $("p[@id]").each(function() {
    62     $(this).append(loading($(this).attr("id")));
    71     $(this).append(loading($(this).attr("id")));
    63 		   });
    72 		   });
    64 $("p[@id]").each(function() {
       
    65 		   var url_string=window.location.pathname;
       
    66 		   var temp = new Array();
       
    67 		   temp = url_string.split('/');
       
    68 		   var chap_name=temp[temp.length-1].split('.')[0];
       
    69 
    73 
    70 		   $.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) {
    74 
    71 	      $.each(data , function(id) {
    75 var url_string=window.location.pathname;
    72 		       $("span.commenttoggle").replaceWith("<a class='commenttoggle'" + "href='comments: show / hide'>" + data.count +" "+"comments" + "</a>");
    76 var temp = new Array();
    73  		   });
    77 temp = url_string.split('/');
       
    78 var chap_name=temp[temp.length-1].split('.')[0];
       
    79 
       
    80 jQuery.getJSON("http://127.0.0.1:8000/count/"+chap_name, function(data) {
       
    81 
       
    82 		 $("span.comment").each(function(data_val) {
       
    83 		 var id = $(this).attr("id");
       
    84 		var substring=id.substr(9);
       
    85 
       
    86 		$(this).replaceWith("<span class='comment'" +  ' id='+ id+ " <a   +  href=javascript:loadComments('"+id+"');>" + data.count[substring] +' comments'+ "</a>");
       
    87 
       
    88 					});
       
    89 
       
    90 
       
    91 	       });
       
    92 
       
    93 		  });
    74 
    94 
    75 
    95 
    76 
    96 
    77 	    });
       
    78 		  });
       
    79 
    97 
    80 		  });
    98 
       
    99 
       
   100 
       
   101 
       
   102 
       
   103 
       
   104 
       
   105 
       
   106