/plugins/VanillaCommentReplies/replies.js

https://github.com/Emaratilicious/Addons · JavaScript · 157 lines · 116 code · 19 blank · 22 comment · 16 complexity · 6ea3724ef9a16eb4b93aaa25103b1a05 MD5 · raw file

  1. jQuery(document).ready(function($) {
  2. // Show delete button when hovering over replies.
  3. $('.Reply').livequery(function() {
  4. $(this).hover(function() {
  5. $(this).find('a.DeleteReply').show();
  6. }, function() {
  7. $(this).find('a.DeleteReply').hide();
  8. });
  9. });
  10. /* Replies */
  11. // Hide replies other than first and last
  12. $.fn.hideReplies = function() {
  13. return this.each(function() {
  14. var children = $(this).children();
  15. var rowCount = children.length - 1; // Subtract the replyform row
  16. // If there are more than 3 comments, hide the middle ones
  17. if (rowCount > 3) {
  18. // Don't bother if replies have already been hidden
  19. if ($(children[1]).attr('class') != 'Reply Reveal') {
  20. // Hide the middle comments
  21. for (i = 1; i < rowCount - 1; i++) {
  22. $(children[i]).hide();
  23. }
  24. // Add a link to reveal hidden replies
  25. var text = (rowCount > 3) ? gdn.definition('Replies').replace('%s', rowCount - 2) : gdn.definition('Reply');
  26. $(children[0]).after('<li class="Reply Reveal"><a href="#">' + text + '</a></li>');
  27. // bind to the click event of the anchor and re-reveal the replies when it is clicked
  28. $(children[0]).next().find('a').click(function() {
  29. $(this).parent().hide();
  30. children.slideDown('fast');
  31. return false;
  32. });
  33. }
  34. }
  35. });
  36. }
  37. // Hide/reveal the replies when the comment reply link is clicked
  38. $('li.Comment ul.Info li.ReplyCount a').live('click', function() {
  39. var replies = $(this).parents('.Comment').find('.Replies');
  40. replies.toggle();
  41. replies.find('a.ReplyLink').click();
  42. return false;
  43. });
  44. // Hijack reply form link clicks
  45. $('ul.Replies a.ReplyLink').live('click', function() {
  46. // Hide the anchor
  47. var anchor = this;
  48. $(anchor).hide();
  49. var row = $(anchor).parent();
  50. // Reveal the form
  51. var frm = $(anchor).parent().find('form');
  52. frm.show();
  53. // Focus on the textbox
  54. var textbox = frm.find('.TextBox');
  55. textbox.focus().blur(function() {
  56. // Hide the form onblur if empty
  57. if (this.value == '') {
  58. var replies = $(anchor).parents('.Replies');
  59. var children = $(replies).children();
  60. var rowCount = children.length - 1; // Subtract the replyform row
  61. $(replies).find('.Errors').remove();
  62. $(frm).hide();
  63. $(anchor).show();
  64. }
  65. });
  66. return false;
  67. });
  68. // Hijack reply form button clicks
  69. $('ul.Replies form input.Button').live('click', function() {
  70. var button = this;
  71. var frm = $(button).parents('form');
  72. var row = $(frm).parents('.ReplyForm');
  73. var textbox = $(frm).find('textarea');
  74. // Post the form and place the results above the input and erase the textbox
  75. var postValues = frm.serialize() + '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW
  76. // Get the most recent reply CommentID to be added
  77. var replyCommentID = frm.find('[name$=ReplyCommentID]');
  78. var prefix = replyCommentID.attr('name').replace('ReplyCommentID', '');
  79. replyCommentID = replyCommentID.val();
  80. var lastCommentID = frm.parent().parent().prev().attr('id');
  81. lastCommentID = lastCommentID == undefined ? replyCommentID : lastCommentID.replace('Comment_', '');
  82. postValues += '&' + prefix + 'LastCommentID=' + lastCommentID;
  83. var action = frm.attr('action') + '/' + replyCommentID;
  84. $.ajax({
  85. type: "POST",
  86. url: action,
  87. data: postValues,
  88. dataType: 'json',
  89. error: function(XMLHttpRequest, textStatus, errorThrown) {
  90. $.popup({}, XMLHttpRequest.responseText);
  91. },
  92. success: function(json) {
  93. $('.ReplyForm .Errors').remove();
  94. if (json.FormSaved == false) {
  95. if (json.StatusMessage != null && json.StatusMessage != '') {
  96. $(row).prepend(json.StatusMessage);
  97. }
  98. } else {
  99. gdn.definition('LastCommentID', json.CommentID, true);
  100. $(row).before(json.Data);
  101. $(row).parents('.Comment').find('ul.Info li.ReplyCount a').text(json.Replies);
  102. textbox.val('').blur();
  103. }
  104. }
  105. });
  106. return false;
  107. });
  108. function setVisibilities() {
  109. // Hide all reply forms and reveal all showform buttons
  110. $('ul.Replies a.ReplyLink').show();
  111. $('.ReplyForm form').hide();
  112. // Hide middle replies
  113. $('.Replies').hideReplies();
  114. }
  115. setVisibilities();
  116. // Bind the setvisibilities to a couple of different events
  117. $('body').bind('CommentAdded', setVisibilities);
  118. $('body').bind('CommentEdited', setVisibilities);
  119. $('body').bind('CommentPagingComplete', setVisibilities);
  120. // Delete reply
  121. $('a.DeleteReply').popup({
  122. confirm: true,
  123. followConfirm: false,
  124. deliveryType: 'BOOL', // DELIVERY_TYPE_BOOL
  125. afterConfirm: function(json, sender) {
  126. var row = $(sender).parents('li.Reply');
  127. if (json.ErrorMessage) {
  128. $.popup({}, json.ErrorMessage);
  129. } else {
  130. // Remove the affected row
  131. $(row).slideUp('fast', function() { $(this).remove(); });
  132. }
  133. }
  134. });
  135. $('a.DeleteReply').livequery(function() {
  136. $(this).hide();
  137. });
  138. });