Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Manages post in view for comments
- jQuery(function ($) {
- $(document).ready(function () {
- $("body").on("submit", ".dynamic-form", function (e) {
- //removes the 'post_' part from id
- var id = this.id.replace('post_', '');
- var new_comment_class = '.new_comment_' + id;
- var new_comment_class_removal = 'new_comment_' + id;
- var comments_id = '#comments_' + id;
- var temp_class = '.temp_class_' + id;
- var temp_class_removal = 'temp_class_' + id;
- var form = $(this);
- var textarea = '#text_' + id;
- var cmnt_button = '#cmntbtn_' + id;
- $.ajax({
- url: form.prop('action'),
- type: 'post',
- dataType: 'json',
- data: $(this).serialize(),
- success: function (data) {
- var new_comment_id = 'comment_' + data.comment_id;
- var new_delete_comment_id = 'deletecmnt_' + data.comment_id;
- var resultStr = "<a href="#">" + data.user_name + " </a>" + data.body +
- "<button id="" + new_delete_comment_id + "" class="fa fa-minus pull-right" aria-hidden="true" data-token="" + data.token + ""></button>";
- var newElement = "<p class="card-text " + temp_class_removal + "" style="display:none"></p>";
- $(comments_id).find(new_comment_class).html(resultStr).slideToggle(150).promise().done(function () {
- $(new_comment_class).fadeIn("fast").promise().done(function () {
- $(newElement).insertBefore(new_comment_class).promise().done(function () {
- $(comments_id).find(new_comment_class).toggleClass(new_comment_class_removal).attr('id', new_comment_id);
- $(comments_id).find(temp_class).toggleClass(temp_class_removal).addClass(new_comment_class_removal);
- });
- });
- });
- //slides form up and empties textarea
- $(form).slideToggle(150).promise().done(function () {
- $(form).fadeOut("fast").promise().done(function () {
- $(form).find(textarea).val('');
- $(cmnt_button).removeClass('fa-commenting').addClass('fa-comment');
- });
- });
- }
- });
- e.preventDefault();
- });
- });
- });
- $(document).ready(function () {
- $("button[id*='deletecmnt_']").click(function () {
- var id = this.id.replace('deletecmnt_', '');
- var comment_card_id = ('#comment_' + id);
- var token = $(this).data('token');
- $.ajax({
- url: '../comment/' + id,
- type: 'post',
- data: {_method: 'delete', _token: token},
- success: function () {
- // Checks for display of comment card and removes it
- if ($(comment_card_id).is(":visible")) {
- $(comment_card_id).fadeOut("fast");
- }
- }
- })
- })
- });
- $(document).ready(function () {
- $("button[id*='deletecmnt_']").on("click", (function () {
- var id = this.id.replace('deletecmnt_', '');
- var comment_card_id = ('#comment_' + id);
- var token = $(this).data('token');
- $.ajax({
- url: '../comment/' + id,
- type: 'post',
- data: {_method: 'delete', _token: token},
- success: function () {
- // Checks for display of comment card and removes it
- if ($(comment_card_id).is(":visible")) {
- $(comment_card_id).fadeOut("fast");
- }
- }
- })
- }))
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement