diff --git a/app/assets/v2/js/activity.js b/app/assets/v2/js/activity.js index 89c5be32d13..79a44d713dd 100644 --- a/app/assets/v2/js/activity.js +++ b/app/assets/v2/js/activity.js @@ -536,45 +536,91 @@ $(document).ready(function() { return; } - // user input - var comment = $parent.parents('.box').find('.comment_container textarea').val().trim(); + if ($('.editableComment')[0]) { + // edited content + const comment = $parent.parents('.box').find('.editableComment').val(); - // validation - if (!comment) { - return; - } + $('.editableComment').prop('disabled', true); - $parent.parents('.box').find('.comment_container textarea').prop('disabled', true); - $('.post_comment').prop('disabled', true); + // validation + if (!comment) { + $('.editableComment').prop('disabled', false); + return; + } - $parent.parents('.activity.box').find('.loading').removeClass('hidden'); - var has_hidden_comments = $parent.parents('.activity.box').find('.row.comment_row.hidden').length; - // increment number - var num = $parent.find('span.num').html(); + $parent.parents('.activity.box').find('.loading').removeClass('hidden'); - num = parseInt(num) + 1; - $parent.find('span.num').html(num); + const has_hidden_comments = $parent.parents('.activity.box').find('.row.comment_row.hidden').length; + const num = $parent.find('span.num').html(); + const comment_id = $('.editableComment').parent().closest('.comment_row').data('id'); + const url = '/api/v0.1/comment/' + comment_id; + const params = { + 'method': 'EDIT', + 'comment': comment, + 'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val() + }; - // remote post - var params = { - 'method': 'comment', - 'comment': comment, - 'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val() - }; - var url = '/api/v0.1/activity/' + $parent.data('pk'); + $.post(url, params, function(response) { + const success_callback = function($parent) { + $.noop(); // do nothing + }; - $.post(url, params, function(response) { - var success_callback = function($parent) { - $parent.parents('.box').find('.comment_container textarea').val(''); - $parent.find('textarea').focus(); + view_comments($parent, allow_close_comment_container, success_callback); + }).always(function() { + $parent.parents('.activity.box').find('.loading').addClass('hidden'); + $parent.parents('.box').find('.comment_container textarea').prop('disabled', false); + $('.editableComment').prop('disabled', false); + $(`.comment_row[data-id="${comment_id}"] .comment_options`).prop('disabled', false).removeClass('hidden'); + $(`.comment_row[data-id="${comment_id}"]`).parent().closest('.box') + .find('#container-post_comment') + .find('*').prop('disabled', false).removeClass('hidden'); + // TODO: automatically cancel editing when another edit_comment button is clicked + $('.edit_comment').prop('disabled', false); + }); + } else { + // user input + const comment = $parent.parents('.box').find('.comment_container textarea').val().trim(); + + $parent.parents('.box').find('.comment_container textarea').prop('disabled', true); + $('.post_comment').prop('disabled', true); + + // validation + if (!comment) { + $parent.parents('.box').find('.comment_container textarea').prop('disabled', false); + $('.post_comment').prop('disabled', false); + return; + } + + + $parent.parents('.activity.box').find('.loading').removeClass('hidden'); + const has_hidden_comments = $parent.parents('.activity.box').find('.row.comment_row.hidden').length; + + // increment number + let num = $parent.find('span.num').html(); + num = parseInt(num) + 1; + $parent.find('span.num').html(num); + + // remote post + const params = { + 'method': 'comment', + 'comment': comment, + 'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val() }; - var override_hide_comments = !has_hidden_comments; + const url = '/api/v0.1/activity/' + $parent.data('pk'); - view_comments($parent, allow_close_comment_container, success_callback, override_hide_comments); - }).done(function() { - // pass - }) + $.post(url, params, function(response) { + const success_callback = function($parent) { + $parent.parents('.box').find('.comment_container textarea').val(''); + $parent.find('textarea').focus(); + + }; + const override_hide_comments = !has_hidden_comments; + + view_comments($parent, allow_close_comment_container, success_callback, override_hide_comments); + }).done(function() { + // pass + }) .fail(function() { $parent.parents('.activity.box').find('.error').removeClass('hidden'); }) @@ -583,6 +629,7 @@ $(document).ready(function() { $parent.parents('.box').find('.comment_container textarea').prop('disabled', false); $('.post_comment').prop('disabled', false); }); + } }; // converts an object to a dict @@ -638,6 +685,7 @@ $(document).ready(function() { const timeAgo = timedifferenceCvrt(new Date(comment['created_on'])); const show_tip = true; const is_comment_owner = document.contxt.github_handle == comment['profile_handle']; + const is_edited = typeof comment['is_edited'] !== 'undefined' ? comment['is_edited'] : false; var sorted_match_curve_html = ''; if (comment['sorted_match_curve']) { @@ -647,6 +695,7 @@ $(document).ready(function() { for (let j = 0; j < match_curve.length; j++) { let ele = match_curve[j]; + sorted_match_curve_html += '