diff --git a/app/assets/javascripts/protocols/steps.js b/app/assets/javascripts/protocols/steps.js index 2158edf50..0ef69f70f 100644 --- a/app/assets/javascripts/protocols/steps.js +++ b/app/assets/javascripts/protocols/steps.js @@ -354,116 +354,6 @@ function initEditableHandsOnTable(root) { }); } -// Initialize comment form. -function initStepCommentForm(ev, $el) { - var $form = $el.find("ul form"); - - var $commentInput = $form.find("#comment_message"); - - $(".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 initStepCommentsLink($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); - moreBtn.trigger("blur"); - } - - // Reposition dropdown comment options - scrollCommentOptions(listItem.closest(".content-comments") - .find(".dropdown-comment")); - } - }); -} - -function initStepCommentTabAjax() { - $(".comment-tab-link") - .on("ajax:before", function (e) { - var $this = $(this); - var parentNode = $this.parents("li"); - var targetId = $this.attr("aria-controls"); - - if (parentNode.hasClass("active")) { - return false; - } - }) - .on("ajax:success", function (e, data) { - if (data.html) { - var $this = $(this); - var targetId = $this.attr("aria-controls"); - var target = $("#" + targetId); - var parentNode = $this.parents("ul").parent(); - - target.html(data.html); - initStepCommentForm(e, parentNode); - initStepCommentsLink(parentNode); - - parentNode.find(".active").removeClass("active"); - $this.parents("li").addClass("active"); - target.addClass("active"); - } - }) - .on("ajax:error", function(e, xhr, status, error) { - // TODO - }); -} - function applyCancelOnNew() { $("[data-action='cancel-new']").click(function() { var $form = $(this).closest("form"); @@ -486,7 +376,6 @@ function initCallBacks() { applyEditCallBack(); applyMoveStepCallBack(); applyCollapseLinkCallBack(); - initStepCommentTabAjax(); initDeleteStep(); } diff --git a/app/assets/javascripts/step_comments.js b/app/assets/javascripts/step_comments.js index 3a03cdca3..e0d81aa37 100644 --- a/app/assets/javascripts/step_comments.js +++ b/app/assets/javascripts/step_comments.js @@ -1,19 +1,135 @@ (function(){ + "use strict"; + + // // Initialize comment form. + // function initStepCommentForm(ev, $el) { + // var $form = $el.find("ul form"); + // + // var $commentInput = $form.find("#comment_message"); + // + // $(".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 initStepCommentsLink($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); + // moreBtn.trigger("blur"); + // } + // + // // Reposition dropdown comment options + // scrollCommentOptions(listItem.closest(".content-comments") + // .find(".dropdown-comment")); + // } + // }); + // } + // + // function initStepCommentTabAjax() { + // $(".comment-tab-link") + // .on("ajax:before", function (e) { + // var $this = $(this); + // var parentNode = $this.parents("li"); + // var targetId = $this.attr("aria-controls"); + // + // if (parentNode.hasClass("active")) { + // return false; + // } + // }) + // .on("ajax:success", function (e, data) { + // if (data.html) { + // var $this = $(this); + // var targetId = $this.attr("aria-controls"); + // var target = $("#" + targetId); + // var parentNode = $this.parents("ul").parent(); + // + // target.html(data.html); + // initStepCommentForm(e, parentNode); + // initStepCommentsLink(parentNode); + // + // parentNode.find(".active").removeClass("active"); + // $this.parents("li").addClass("active"); + // target.addClass("active"); + // } + // }) + // .on("ajax:error", function(e, xhr, status, error) { + // // TODO + // }); + // } function initializeComments(){ var steps = $(".step-comment"); $.each(steps, function(){ var that = $(this); var link = that.attr("data-href"); - + var parentNode = that.parents("ul").parent(); + debugger; $.ajax({ method: 'GET', url: link, beforeSend: animateSpinner(that, true) }) .done(function(data){ + debugger; updateCommentHTML(that, data); animateSpinner(that, false); }) - .always(animateSpinner(that, false)); + .always(function(data){ + debugger; + animateSpinner(that, false) + }); }); } @@ -24,6 +140,7 @@ url: link, beforeSend: animateSpinner(parent, true) }) .done(function(data){ + debugger; updateCommentHTML(parent, data); animateSpinner(parent, false); }) @@ -34,23 +151,33 @@ function commentFormOnSubmitAction(){ $(".comment-form") .each(function() { + debigger; bindCommentAjax("#" + $(this).attr("id")); }); } + // function bindMoreCommentButton(){ + // $(".btn-more-comments") + // .each(function(){ + // bindCommentAjax($(this)); + // }); + // } + function bindCommentAjax(form){ $(document) .on('ajax:success', function () { - refreshComments($(form)); + debugger; + refreshComments($(form)); }) .on('ajax:error', function () { - refreshComments(form); + refreshComments(form); }); } function updateCommentHTML(parent, data) { - $(parent.children()[0]).html(data.html); var id = "#" + $(parent.find(".comment-form")).attr("id"); + debugger; + $(parent.children()[0]).html(data.html); bindCommentAjax(id); } diff --git a/app/models/step.rb b/app/models/step.rb index 37c75e8b7..bb1d169ef 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -92,7 +92,7 @@ class Step < ActiveRecord::Base Comment.joins(:step_comment) .where(step_comments: {step_id: id}) .where('comments.id < ?', last_id) - .order(created_at: :desc) + .order(created_at: :asc) .limit(per_page) end diff --git a/app/views/step_comments/_index.html.erb b/app/views/step_comments/_index.html.erb index 1b8dc1397..70b0b9570 100644 --- a/app/views/step_comments/_index.html.erb +++ b/app/views/step_comments/_index.html.erb @@ -1,11 +1,6 @@
    <%= t('protocols.steps.comments_tab') %>

    <% if can_add_step_comment_in_protocol(@protocol) %>