From 02bfa1949e07bd580ee178727e391e8b9848830c Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 27 Sep 2016 09:00:02 +0200 Subject: [PATCH] fixed project comments --- app/assets/javascripts/comments_helper.js | 165 ++++++++++++++++++ app/assets/javascripts/projects/index.js | 82 +-------- .../javascripts/step_result_comments.js | 158 ----------------- app/models/my_module.rb | 11 +- app/models/project.rb | 11 +- app/views/my_module_comments/_index.html.erb | 10 +- app/views/my_module_comments/_list.html.erb | 2 +- app/views/my_modules/protocols.html.erb | 2 +- app/views/my_modules/results.html.erb | 2 +- app/views/project_comments/_index.html.erb | 10 +- app/views/project_comments/_list.html.erb | 23 +-- app/views/projects/index.html.erb | 1 + config/initializers/assets.rb | 2 +- 13 files changed, 206 insertions(+), 273 deletions(-) create mode 100644 app/assets/javascripts/comments_helper.js delete mode 100644 app/assets/javascripts/step_result_comments.js diff --git a/app/assets/javascripts/comments_helper.js b/app/assets/javascripts/comments_helper.js new file mode 100644 index 000000000..1e622033d --- /dev/null +++ b/app/assets/javascripts/comments_helper.js @@ -0,0 +1,165 @@ +var CommentsHelper = (function() { + 'use strict'; + + /** + * Initializes the comments + * + */ + function initializeComments(){ + var comments; + if ( $('.step-comment') && $('.step-comment').length > 0 ) { + comments = $('.step-comment'); + } else if ( $('.result-comment') && $('.result-comment').length > 0 ) { + comments = $('.result-comment'); + } + if(!_.isUndefined(comments)) { + $.each(comments, function(){ + var that = $(this); + var link = that.attr('data-href'); + $.ajax({ method: 'GET', + url: link, + beforeSend: animateSpinner(that, true) }) + .done(function(data) { + that.html(data.html); + initCommentForm(that); + initCommentsLink(that); + scrollBottom(that.find('.content-comments')); + }) + .always(function() { + animateSpinner(that, false); + }); + }); + } + } + + // scroll to the botttom + function scrollBottom(id) { + var list; + if ( id.hasClass('content-comments') ) { + list = id; + } else { + list = id.find('.content-comments'); + } + if ( list && list.length > 0) { + list.scrollTop($(list)[0].scrollHeight); + } + } + + // Initialize show more comments link. + function initCommentsLink($el) { + + $el.find('.btn-more-comments') + .on('ajax:success', function (e, data) { + if (data.html) { + var list = $(this).parents('ul'); + var moreBtn = list.find('.btn-more-comments'); + var listItem = moreBtn.parents('li'); + $(data.html).insertAfter(listItem); + if (data.results_number < data.per_page) { + moreBtn.remove(); + } else { + moreBtn.attr('href', data.more_url); + moreBtn.trigger('blur'); + } + + // Reposition dropdown comment options + scrollCommentOptions(listItem + .closest('.content-comments') + .find('.dropdown-comment')); + } + }); + } + + // Initialize comment form. + function initCommentForm($el) { + + var $form = $el.find('ul form'); + + $('.help-block', $form).addClass('hide'); + + $form.on('ajax:send', function () { + $('#comment_message', $form).attr('readonly', true); + }) + .on('ajax:success', function (e, data) { + if (data.html) { + var list = $form.parents('ul'); + + // Remove potential 'no comments' element + list.parent().find('.content-comments') + .find('li.no-comments').remove(); + + list.parent().find('.content-comments') + .append('
  • ' + data.html + '
  • ') + .scrollTop(0); + list.parents('ul').find('> li.comment:gt(8)').remove(); + $('#comment_message', $form).val(''); + $('.form-group', $form) + .removeClass('has-error'); + $('.help-block', $form) + .html('') + .addClass('hide'); + scrollBottom($el); + } + }) + .on('ajax:error', function (ev, xhr) { + if (xhr.status === 400) { + var messageError = xhr.responseJSON.errors.message; + + if (messageError) { + $('.form-group', $form) + .addClass('has-error'); + $('.help-block', $form) + .html(messageError[0]) + .removeClass('hide'); + } + } + }) + .on('ajax:complete', function () { + scrollBottom($('#comment_message', $form)); + $('#comment_message', $form) + .attr('readonly', false) + .focus(); + }); + } + + // restore comments after update or when new element is created + function bindCommentInitializerToNewElement() { + $(document) + .ready(function() { + if( document.getElementById('steps') !== null ) { + $('#steps') + .change(function() { + $('.step-save') + .on('click', function() { + $(document) + .on('ajax:success', function(){ + initializeComments(); + }); + }); + }); + } else if ( document.getElementById('results') !== null ) { + $('#results') + .change(function() { + $('.save-result') + .on('click', function() { + $(document) + .on('ajax:success', function(){ + initializeComments(); + }); + }); + }); + } + }); + } + + // bindCommentInitializerToNewElement(); + // initializeComments(); + return { + initialize: initializeComments, + scrollBottom: scrollBottom, + moreComments: initCommentsLink, + form: initCommentForm, + bindNewElement: bindCommentInitializerToNewElement + }; + +})(); diff --git a/app/assets/javascripts/projects/index.js b/app/assets/javascripts/projects/index.js index 560de9b13..0972f5b88 100644 --- a/app/assets/javascripts/projects/index.js +++ b/app/assets/javascripts/projects/index.js @@ -203,83 +203,9 @@ }); } - // Initialize comment form. - function initCommentForm($el) { - - var $form = $el.find("ul form"); - - $(".help-block", $form).addClass("hide"); - - $form.on("ajax:send", function (data) { - $("#comment_message", $form).attr("readonly", true); - }) - .on("ajax:success", function (e, data) { - if (data.html) { - var list = $form.parents("ul"); - - // Remove potential "no comments" element - list.parent().find(".content-comments") - .find("li.no-comments").remove(); - - list.parent().find(".content-comments") - .prepend("
  • " + data.html + "
  • ") - .scrollTop(0); - list.parents("ul").find("> li.comment:gt(8)").remove(); - $("#comment_message", $form).val(""); - $(".form-group", $form) - .removeClass("has-error"); - $(".help-block", $form) - .html("") - .addClass("hide"); - scrollCommentOptions( - list.parent().find(".content-comments .dropdown-comment") - ); - } - }) - .on("ajax:error", function (ev, xhr) { - if (xhr.status === 400) { - var messageError = xhr.responseJSON.errors.message; - - if (messageError) { - $(".form-group", $form) - .addClass("has-error"); - $(".help-block", $form) - .html(messageError[0]) - .removeClass("hide"); - } - } - }) - .on("ajax:complete", function () { - $("#comment_message", $form) - .attr("readonly", false) - .focus(); - }); - } - - // Initialize show more comments link. - function initCommentsLink($el) { - - $el.find(".btn-more-comments") - .on("ajax:success", function (e, data) { - if (data.html) { - var list = $(this).parents("ul"); - var moreBtn = list.find(".btn-more-comments"); - var listItem = moreBtn.parents('li'); - $(data.html).insertBefore(listItem); - if (data.results_number < data.per_page) { - moreBtn.remove(); - } else { - moreBtn.attr("href", data.more_url); - } - - // Reposition dropdown comment options - scrollCommentOptions(listItem.closest(".content-comments").find(".dropdown-comment")); - } - }); - } - // Initialize reloading manage user modal content after posting new // user. + function initAddUserForm() { manageUsersModalBody.find(".add-user-form") @@ -387,13 +313,15 @@ target.html(data.html); initUsersEditLink(parentNode); - initCommentForm(parentNode); - initCommentsLink(parentNode); + CommentsHelper.form(parentNode); + CommentsHelper.moreComments(parentNode); // TODO move to fn parentNode.find(".active").removeClass("active"); $this.parents("li").addClass("active"); target.addClass("active"); + + CommentsHelper.scrollBottom(parentNode); }) .on("ajax:error", function (e, xhr, status, error) { diff --git a/app/assets/javascripts/step_result_comments.js b/app/assets/javascripts/step_result_comments.js deleted file mode 100644 index 306a179a1..000000000 --- a/app/assets/javascripts/step_result_comments.js +++ /dev/null @@ -1,158 +0,0 @@ -(function(){ - "use strict"; - - /** - * Initializes the comments - * - */ - function initializeComments(){ - var comments; - if ( $(".step-comment") && $(".step-comment").length > 0 ) { - comments = $(".step-comment"); - } else if ( $(".result-comment") && $(".result-comment").length > 0 ) { - comments = $(".result-comment"); - } - if(!_.isUndefined(comments)) { - $.each(comments, function(){ - var that = $(this); - var link = that.attr('data-href'); - $.ajax({ method: 'GET', - url: link, - beforeSend: animateSpinner(that, true) }) - .done(function(data) { - that.html(data.html); - initCommentForm(that); - initCommentsLink(that); - scrollBottom(that.find('.content-comments')); - }) - .always(function() { - animateSpinner(that, false); - }); - }); - } - } - - // scroll to the botttom - function scrollBottom(id) { - var list; - if ( id.hasClass("content-comments")) { - list = id; - } else { - list = id.find(".content-comments"); - } - if ( list && list.length > 0) { - list.scrollTop($(list)[0].scrollHeight); - } - } - - // Initialize show more comments link. - function initCommentsLink($el) { - - $el.find(".btn-more-comments") - .on("ajax:success", function (e, data) { - if (data.html) { - var list = $(this).parents("ul"); - var moreBtn = list.find(".btn-more-comments"); - var listItem = moreBtn.parents("li"); - $(data.html).insertAfter(listItem); - if (data.results_number < data.per_page) { - moreBtn.remove(); - } else { - moreBtn.attr("href", data.more_url); - moreBtn.trigger("blur"); - } - - // Reposition dropdown comment options - scrollCommentOptions(listItem - .closest(".content-comments") - .find(".dropdown-comment")); - } - }); - } - - // Initialize comment form. - function initCommentForm($el) { - - var $form = $el.find("ul form"); - - $(".help-block", $form).addClass("hide"); - - $form.on("ajax:send", function () { - $("#comment_message", $form).attr("readonly", true); - }) - .on("ajax:success", function (e, data) { - if (data.html) { - var list = $form.parents("ul"); - - // Remove potential "no comments" element - list.parent().find(".content-comments") - .find("li.no-comments").remove(); - - list.parent().find(".content-comments") - .append("
  • " + data.html + "
  • ") - .scrollTop(0); - list.parents("ul").find("> li.comment:gt(8)").remove(); - $("#comment_message", $form).val(""); - $(".form-group", $form) - .removeClass("has-error"); - $(".help-block", $form) - .html("") - .addClass("hide"); - scrollBottom($el); - } - }) - .on("ajax:error", function (ev, xhr) { - if (xhr.status === 400) { - var messageError = xhr.responseJSON.errors.message; - - if (messageError) { - $(".form-group", $form) - .addClass("has-error"); - $(".help-block", $form) - .html(messageError[0]) - .removeClass("hide"); - } - } - }) - .on("ajax:complete", function () { - scrollBottom($("#comment_message", $form)); - $("#comment_message", $form) - .attr("readonly", false) - .focus(); - }); - } - - // restore comments after update or when new element is created - function bindCommentInitializerToNewElement() { - $(document) - .ready(function() { - if( document.getElementById("steps") !== null ) { - $("#steps") - .change(function() { - $(".step-save") - .on("click", function() { - $(document) - .on('ajax:success', function(){ - initializeComments(); - }); - }); - }); - } else if ( document.getElementById("results") !== null ) { - $("#results") - .change(function() { - $(".save-result") - .on("click", function() { - $(document) - .on('ajax:success', function(){ - initializeComments(); - }); - }); - }); - } - }); - } - - bindCommentInitializerToNewElement(); - initializeComments(); - -})(); diff --git a/app/models/my_module.rb b/app/models/my_module.rb index fcd4ba712..9c8c2e128 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -152,11 +152,12 @@ class MyModule < ActiveRecord::Base # using last comment id and per_page parameters. def last_comments(last_id = 1, per_page = 20) last_id = 9999999999999 if last_id <= 1 - Comment.joins(:my_module_comment) - .where(my_module_comments: {my_module_id: id}) - .where('comments.id < ?', last_id) - .order(created_at: :desc) - .limit(per_page) + comments = Comment.joins(:my_module_comment) + .where(my_module_comments: { my_module_id: id }) + .where('comments.id < ?', last_id) + .order(created_at: :desc) + .limit(per_page) + comments.reverse end def last_activities(last_id = 1, count = 20) diff --git a/app/models/project.rb b/app/models/project.rb index 2420e5270..e924ebec2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -81,11 +81,12 @@ class Project < ActiveRecord::Base # using last comment id and per_page parameters. def last_comments(last_id = 1, per_page = 20) last_id = 9999999999999 if last_id <= 1 - Comment.joins(:project_comment) - .where(project_comments: {project_id: id}) - .where('comments.id < ?', last_id) - .order(created_at: :desc) - .limit(per_page) + comments = Comment.joins(:project_comment) + .where(project_comments: { project_id: id }) + .where('comments.id < ?', last_id) + .order(created_at: :desc) + .limit(per_page) + comments.reverse end def unassigned_users diff --git a/app/views/my_module_comments/_index.html.erb b/app/views/my_module_comments/_index.html.erb index 393a55b45..31da2279d 100644 --- a/app/views/my_module_comments/_index.html.erb +++ b/app/views/my_module_comments/_index.html.erb @@ -1,11 +1,6 @@
    <%= t('experiments.canvas.popups.comments_tab') %>

    <% if can_add_comment_to_module(@my_module) %>