fixed project comments

This commit is contained in:
zmagod 2016-09-27 09:00:02 +02:00
parent 1c1a7c231e
commit 02bfa1949e
13 changed files with 206 additions and 273 deletions

View file

@ -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('<li class="comment">' + data.html + '</li>')
.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
};
})();

View file

@ -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("<li class='comment'>" + data.html + "</li>")
.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) {

View file

@ -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("<li class='comment'>" + data.html + "</li>")
.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();
})();

View file

@ -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)

View file

@ -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

View file

@ -1,11 +1,6 @@
<h5 class="text-center"><%= t('experiments.canvas.popups.comments_tab') %></h5>
<hr>
<ul class="no-style double-line content-comments">
<% if @comments.size == 0 then %>
<li class="no-comments"><em><%= t 'experiments.canvas.popups.no_comments' %></em></li>
<% else %>
<%= render 'my_module_comments/list.html.erb', comments: @comments %>
<% end %>
<% if @comments.length == @per_page %>
<li class="comment-more text-center">
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
@ -13,6 +8,11 @@
</a>
</li>
<% end %>
<% if @comments.size == 0 then %>
<li class="no-comments"><em><%= t 'experiments.canvas.popups.no_comments' %></em></li>
<% else %>
<%= render 'my_module_comments/list.html.erb', comments: @comments %>
<% end %>
</ul>
<% if can_add_comment_to_module(@my_module) %>
<ul class="no-style double-line">

View file

@ -1,4 +1,4 @@
<% day = 366 %>
<% day = 0 %>
<% current_day = DateTime.current.strftime('%j').to_i %>
<% comments.each do |comment| %>

View file

@ -43,5 +43,5 @@
<%= render partial: "protocols/import_export/import_elements.html.erb" %>
<%= stylesheet_link_tag 'datatables' %>
<%= javascript_include_tag("comments_helper") %>
<%= javascript_include_tag("my_modules/protocols") %>
<%= javascript_include_tag("step_result_comments") %>

View file

@ -50,7 +50,7 @@
<%= javascript_include_tag "handsontable.full.min" %>
<%= javascript_include_tag("canvas-to-blob.min") %>
<%= javascript_include_tag("direct-upload") %>
<%= javascript_include_tag("step_result_comments") %>
<%= javascript_include_tag("comments_helper") %>
<%= javascript_include_tag "my_modules/results" %>
<%= javascript_include_tag "results/result_texts" %>
<%= javascript_include_tag "results/result_tables" %>

View file

@ -1,17 +1,17 @@
<h5 class="text-center"><%= t('projects.index.comment_tab') %></h5>
<hr>
<ul class="no-style double-line content-comments">
<% if @comments.size == 0 then %>
<li class="no-comments"><em><%= t 'projects.index.no_comments' %></em></li>
<% else %>
<%= render 'project_comments/list.html.erb', comments: @comments %>
<% end %>
<% if @comments.length == @per_page %>
<li class="comment-more text-center">
<a class="btn btn-default btn-more-comments" href="<%= more_comments_url %>" data-remote="true">
<%= t'projects.index.more_comments' %></a>
</li>
<% end %>
<% if @comments.size == 0 then %>
<li class="no-comments"><em><%= t 'projects.index.no_comments' %></em></li>
<% else %>
<%= render 'project_comments/list.html.erb', comments: @comments %>
<% end %>
</ul>
<% if can_add_comment_to_project(@project) %>
<ul class="no-style double-line">

View file

@ -1,18 +1,13 @@
<%
day = 366
current_day = DateTime.current.strftime('%j').to_i
%>
<% day = 0 %>
<% current_day = DateTime.current.strftime('%j').to_i %>
<% comments.each do |comment| %>
<%
comment_day = comment.created_at.strftime('%j').to_i
if comment_day < current_day and comment_day < day then
day = comment.created_at.strftime('%j').to_i
%>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
</li>
<% end %>
<% comment_day = comment.created_at.strftime('%j').to_i %>
<% if comment_day < current_day && comment_day < day %>
<% day = comment.created_at.strftime('%j').to_i %>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
</li>
<% end %>
<li class="comment">
<%= render 'project_comments/comment.html.erb', comment: comment %>
</li>

View file

@ -141,4 +141,5 @@
<%= render partial: "projects/index/org_projects", locals: {org: org, projects: projects} %>
<% end %>
<% end %>
<%= javascript_include_tag('comments_helper') %>
<%= javascript_include_tag "projects/index", "data-turbolinks-track" => true %>

View file

@ -59,7 +59,7 @@ Rails.application.config.assets.precompile += %w(assets.js)
Rails.application.config.assets.precompile += %w(comments.js)
Rails.application.config.assets.precompile += %w(projects/show.js)
Rails.application.config.assets.precompile += %w(projects/introdutory_popup.js)
Rails.application.config.assets.precompile += %w(step_result_comments.js)
Rails.application.config.assets.precompile += %w(comments_helper.js)
# Libraries needed for Handsontable formulas
Rails.application.config.assets.precompile += %w(lodash.js)