Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. //Manages post in view for comments
  2. jQuery(function ($) {
  3. $(document).ready(function () {
  4.  
  5. $("body").on("submit", ".dynamic-form", function (e) {
  6.  
  7. //removes the 'post_' part from id
  8. var id = this.id.replace('post_', '');
  9. var new_comment_class = '.new_comment_' + id;
  10. var new_comment_class_removal = 'new_comment_' + id;
  11. var comments_id = '#comments_' + id;
  12. var temp_class = '.temp_class_' + id;
  13. var temp_class_removal = 'temp_class_' + id;
  14. var form = $(this);
  15. var textarea = '#text_' + id;
  16. var cmnt_button = '#cmntbtn_' + id;
  17.  
  18.  
  19. $.ajax({
  20. url: form.prop('action'),
  21. type: 'post',
  22. dataType: 'json',
  23. data: $(this).serialize(),
  24. success: function (data) {
  25.  
  26. var new_comment_id = 'comment_' + data.comment_id;
  27.  
  28. var new_delete_comment_id = 'deletecmnt_' + data.comment_id;
  29.  
  30. var resultStr = "<a href="#">" + data.user_name + " </a>" + data.body +
  31.  
  32. "<button id="" + new_delete_comment_id + "" class="fa fa-minus pull-right" aria-hidden="true" data-token="" + data.token + ""></button>";
  33.  
  34. var newElement = "<p class="card-text " + temp_class_removal + "" style="display:none"></p>";
  35.  
  36. $(comments_id).find(new_comment_class).html(resultStr).slideToggle(150).promise().done(function () {
  37. $(new_comment_class).fadeIn("fast").promise().done(function () {
  38.  
  39. $(newElement).insertBefore(new_comment_class).promise().done(function () {
  40.  
  41. $(comments_id).find(new_comment_class).toggleClass(new_comment_class_removal).attr('id', new_comment_id);
  42. $(comments_id).find(temp_class).toggleClass(temp_class_removal).addClass(new_comment_class_removal);
  43.  
  44. });
  45.  
  46. });
  47. });
  48. //slides form up and empties textarea
  49. $(form).slideToggle(150).promise().done(function () {
  50.  
  51.  
  52. $(form).fadeOut("fast").promise().done(function () {
  53.  
  54. $(form).find(textarea).val('');
  55. $(cmnt_button).removeClass('fa-commenting').addClass('fa-comment');
  56.  
  57. });
  58.  
  59.  
  60. });
  61.  
  62. }
  63. });
  64.  
  65. e.preventDefault();
  66. });
  67. });
  68. });
  69.  
  70. $(document).ready(function () {
  71.  
  72. $("button[id*='deletecmnt_']").click(function () {
  73.  
  74. var id = this.id.replace('deletecmnt_', '');
  75.  
  76. var comment_card_id = ('#comment_' + id);
  77.  
  78. var token = $(this).data('token');
  79.  
  80. $.ajax({
  81. url: '../comment/' + id,
  82. type: 'post',
  83. data: {_method: 'delete', _token: token},
  84. success: function () {
  85.  
  86.  
  87. // Checks for display of comment card and removes it
  88. if ($(comment_card_id).is(":visible")) {
  89.  
  90. $(comment_card_id).fadeOut("fast");
  91.  
  92. }
  93. }
  94. })
  95.  
  96. })
  97.  
  98. });
  99.  
  100. $(document).ready(function () {
  101.  
  102. $("button[id*='deletecmnt_']").on("click", (function () {
  103.  
  104. var id = this.id.replace('deletecmnt_', '');
  105.  
  106. var comment_card_id = ('#comment_' + id);
  107.  
  108. var token = $(this).data('token');
  109.  
  110. $.ajax({
  111. url: '../comment/' + id,
  112. type: 'post',
  113. data: {_method: 'delete', _token: token},
  114. success: function () {
  115.  
  116.  
  117. // Checks for display of comment card and removes it
  118. if ($(comment_card_id).is(":visible")) {
  119.  
  120. $(comment_card_id).fadeOut("fast");
  121.  
  122. }
  123. }
  124. })
  125.  
  126. }))
  127.  
  128. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement