Merge pull request #176 from ZmagoD/zd_SCI_474

Unify the comments
This commit is contained in:
Zmago Devetak 2016-10-03 15:26:17 +02:00 committed by GitHub
commit 91c7d8a952
22 changed files with 425 additions and 574 deletions

View file

@ -1,151 +1,336 @@
function initCommentOptions(scrollableContainer, useParentOffset) {
useParentOffset = (typeof useParentOffset !== 'undefined') ? useParentOffset : true;
scrollCommentOptions($(".dropdown-comment"), useParentOffset);
var Comments = (function() {
'use strict';
// Reposition dropdown to the left
// (only do this when using parent offset)
if (useParentOffset) {
$(document).on("shown.bs.dropdown", ".dropdown-comment", function() {
var $el = $(this);
var menu = $el.find(".dropdown-menu");
var leftPos = $el.offset().left;
if (leftPos + menu.width() > $(window).width()) {
menu.offset({ left: leftPos - menu.width() });
/**
* 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.resultsNumber < data.perPage) {
moreBtn.remove();
} else {
moreBtn.attr('href', data.moreUrl);
moreBtn.trigger('blur');
}
// Reposition dropdown comment options
scrollCommentOptions(listItem
.closest('.content-comments')
.find('.dropdown-comment'));
} else {
$('.btn-more-comments').remove();
}
});
}
// Reposition dropdowns vertically on scroll events
document.addEventListener('scroll', function (event) {
var $target = $(event.target);
var parent = $(scrollableContainer);
// Initialize comment form.
function initCommentForm($el) {
if ($target.length) {
scrollCommentOptions(parent.find(".dropdown-comment"), useParentOffset);
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();
});
});
});
}
});
}
function initCommentOptions(scrollableContainer, useParentOffset) {
if ( ! _.isUndefined(typeof useParentOffset) ) {
useParentOffset = useParentOffset;
} else {
useParentOffset = true;
}
}, true);
}
scrollCommentOptions($('.dropdown-comment'), useParentOffset);
function scrollCommentOptions(selector, useParentOffset) {
useParentOffset = (typeof useParentOffset !== 'undefined') ? useParentOffset : true;
_.each(selector, function(el) {
var $el = $(el);
var offset = useParentOffset ? $el.offset().top : $el.position().top;
$el.find(".dropdown-menu-fixed")
.offset({ top: (offset + 20) });
});
}
function initDeleteComments(parent) {
$(parent).on("click", "[data-action=delete-comment]", function(e) {
var $this = $(this);
if (confirm($this.attr("data-confirm-message"))) {
$.ajax({
url: $this.attr("data-url"),
type: "DELETE",
dataType: "json",
success: function(data) {
// There are 3 possible actions:
// - (A) comment is the last comment in project
// - (B) comment is the last comment inside specific date (remove the date separator)
// - (C) comment is a usual comment
var commentEl = $this.closest(".comment");
// Case A
if (commentEl.prevAll(".comment").length == 0 && commentEl.next().length == 0) {
commentEl.after("<li class='no-comments'><em>" + I18n.t("projects.index.no_comments") + "</em></li>");
}
// Case B
if (commentEl.prev(".comment-date-separator").length > 0 && commentEl.next(".comment").length == 0) {
commentEl.prev(".comment-date-separator").remove();
}
commentEl.remove();
scrollCommentOptions($(parent).find(".dropdown-comment"));
},
error: function(data) {
// Display alert
alert(data.responseJSON.message);
// Reposition dropdown to the left
// (only do this when using parent offset)
if (useParentOffset) {
$(document).on('shown.bs.dropdown', '.dropdown-comment', function() {
var $el = $(this);
var menu = $el.find('.dropdown-menu');
var leftPos = $el.offset().left;
if (leftPos + menu.width() > $(window).width()) {
menu.offset({ left: leftPos - menu.width() });
}
});
}
});
}
function initEditComments(parent) {
$(parent).on("click", "[data-action=edit-comment]", function(e) {
var $this = $(this);
$.ajax({
url: $this.attr("data-url"),
type: "GET",
dataType: "json",
success: function(data) {
var commentEl = $this.closest(".comment");
var container = commentEl.find("[data-role=comment-message-container]");
var oldMessage = container.find("[data-role=comment-message]");
var optionsBtn = commentEl.find("[data-role=comment-options]");
// Reposition dropdowns vertically on scroll events
document.addEventListener('scroll', function (event) {
var $target = $(event.target);
var parent = $(scrollableContainer);
// Hide old message, append new HTML
oldMessage.hide();
optionsBtn.hide();
container.append(data.html);
if ($target.length) {
scrollCommentOptions(parent.find('.dropdown-comment'), useParentOffset);
}
}, true);
}
var form = container.find("[data-role=edit-comment-message-form]");
var input = form.find("input[data-role=message-input]");
var submitBtn = form.find("[data-action=save]");
var cancelBtn = form.find("[data-action=cancel]");
function scrollCommentOptions(selector, useParentOffset) {
if ( ! _.isUndefined(typeof useParentOffset) ) {
useParentOffset = useParentOffset;
} else {
useParentOffset = true;
}
_.each(selector, function(el) {
var $el = $(el);
var offset = useParentOffset ? $el.offset().top : $el.position().top;
$el.find('.dropdown-menu-fixed')
.offset({ top: (offset + 20) });
});
}
input.focus();
function initDeleteComments(parent) {
$(parent).on('click', '[data-action=delete-comment]', function() {
var $this = $(this);
if (confirm($this.attr('data-confirm-message'))) {
$.ajax({
url: $this.attr('data-url'),
type: 'DELETE',
dataType: 'json',
success: function() {
// There are 3 possible actions:
// - (A) comment is the last comment in project
// - (B) comment is the last comment inside specific date
// (remove the date separator)
// - (C) comment is a usual comment
form
.on("ajax:send", function() {
input.attr("readonly", true);
})
.on("ajax:success", function(e, data) {
var newMessage = input.val();
oldMessage.html(newMessage);
var commentEl = $this.closest('.comment');
form.off("ajax:send ajax:success ajax:error ajax:complete");
submitBtn.off("click");
cancelBtn.off("click");
form.remove();
oldMessage.show();
optionsBtn.show();
})
.on("ajax:error", function(ev, xhr) {
if (xhr.status === 422) {
var messageError = xhr.responseJSON.errors.message;
if (messageError) {
$(".form-group", form)
.addClass("has-error");
$(".help-block", form)
.html(messageError[0] + " |")
.removeClass("hide");
}
// Case A
if (commentEl.prevAll('.comment').length === 0 &&
commentEl.next().length === 0) {
commentEl.after('<li class="no-comments"><em>' +
I18n.t('projects.index.no_comments') + '</em></li>');
}
})
.on("ajax:complete", function() {
input.attr("readonly", false).focus();
});
submitBtn.on("click", function() {
form.submit();
});
// Case B
if (commentEl.prev('.comment-date-separator').length > 0 &&
commentEl.next('.comment').length === 0) {
commentEl.prev('.comment-date-separator').remove();
}
commentEl.remove();
cancelBtn.on("click", function() {
form.off("ajax:send ajax:success ajax:error ajax:complete");
submitBtn.off("click");
cancelBtn.off("click");
form.remove();
oldMessage.show();
optionsBtn.show();
});
},
error: function(data) {
// TODO
}
});
});
}
scrollCommentOptions($(parent).find('.dropdown-comment'));
},
error: function(data) {
// Display alert
alert(data.responseJSON.message);
}
});
}
});
}
function initEditComments(parent) {
$(parent).on('click', '[data-action=edit-comment]', function() {
var $this = $(this);
$.ajax({
url: $this.attr('data-url'),
type: 'GET',
dataType: 'json',
success: function(data) {
var commentEl = $this.closest('.comment');
var container = commentEl
.find('[data-role=comment-message-container]');
var oldMessage = container.find('[data-role=comment-message]');
var optionsBtn = commentEl.find('[data-role=comment-options]');
// Hide old message, append new HTML
oldMessage.hide();
optionsBtn.hide();
container.append(data.html);
var form = container.find('[data-role=edit-comment-message-form]');
var input = form.find('input[data-role=message-input]');
var submitBtn = form.find('[data-action=save]');
var cancelBtn = form.find('[data-action=cancel]');
input.focus();
form
.on('ajax:send', function() {
input.attr('readonly', true);
})
.on('ajax:success', function() {
var newMessage = input.val();
oldMessage.html(newMessage);
form.off('ajax:send ajax:success ajax:error ajax:complete');
submitBtn.off('click');
cancelBtn.off('click');
form.remove();
oldMessage.show();
optionsBtn.show();
})
.on('ajax:error', function(ev, xhr) {
if (xhr.status === 422) {
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() {
input.attr('readonly', false).focus();
});
submitBtn.on('click', function() {
form.submit();
});
cancelBtn.on('click', function() {
form.off('ajax:send ajax:success ajax:error ajax:complete');
submitBtn.off('click');
cancelBtn.off('click');
form.remove();
oldMessage.show();
optionsBtn.show();
});
},
error: function() {
// TODO
}
});
});
}
return {
initialize: initializeComments,
scrollBottom: scrollBottom,
moreComments: initCommentsLink,
form: initCommentForm,
bindNewElement: bindCommentInitializerToNewElement,
initCommentOptions: initCommentOptions,
scrollCommentOptions: scrollCommentOptions,
initDeleteComments: initDeleteComments,
initEditComments: initEditComments
};
})();

View file

@ -1,5 +1,6 @@
//= require protocols/import_export/import
//= require protocols/import_export/export
//= require comments
//= require datatables
// Currently selected row in "load from protocol" modal
@ -554,4 +555,6 @@ initLoadFromRepository();
initRefreshStatusBar();
initImport();
initExport();
Comments.bindNewElement();
Comments.initialize();
initTutorial();

View file

@ -30,38 +30,6 @@ function initHandsOnTables(root) {
});
}
function initResultCommentTabAjax() {
$(".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);
initCommentForm(parentNode);
initCommentsLink(parentNode);
parentNode.find(".active").removeClass("active");
$this.parents("li").addClass("active");
target.addClass("active");
}
})
.on("ajax:error", function(e, xhr, status, error) {
// TODO
});
}
function applyCollapseLinkCallBack() {
$(".result-panel-collapse-link")
.on("ajax:success", function() {
@ -265,14 +233,16 @@ function processResult(ev, resultTypeEnum, editMode, forS3) {
// rendered url and opens the comment tab
$(document).ready(function(){
initHandsOnTables($(document));
initResultCommentTabAjax();
expandAllResults();
initTutorial();
applyCollapseLinkCallBack();
initCommentOptions("ul.content-comments");
initEditComments("#results");
initDeleteComments("#results");
Comments.bindNewElement();
Comments.initialize();
Comments.initCommentOptions("ul.content-comments");
Comments.initEditComments("#results");
Comments.initDeleteComments("#results");
$(function () {
$("#results-collapse-btn").click(function () {

View file

@ -324,9 +324,9 @@ function initializeFullZoom() {
restoreDraggablePosition($("#diagram"), $("#canvas-container"));
// Initialize comments
initCommentOptions("ul.content-comments", false);
initEditComments(".panel.module-large .tab-content");
initDeleteComments(".panel.module-large .tab-content");
Comments.initCommentOptions("ul.content-comments", false);
Comments.initEditComments(".panel.module-large .tab-content");
Comments.initDeleteComments(".panel.module-large .tab-content");
}
function destroyFullZoom() {
@ -656,85 +656,6 @@ function bindFullZoomAjaxTabs() {
});
}
// 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"),
false
);
}
})
.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"),
false
);
}
});
}
// Initialize reloading manage user modal content after posting new
// user.
function initAddUserForm() {
@ -838,14 +759,20 @@ function bindFullZoomAjaxTabs() {
} else if (targetContents === "users") {
initUsersEditLink(parentNode);
} else if (targetContents === "comments") {
initCommentForm(parentNode);
initCommentsLink(parentNode);
Comments.form(parentNode);
Comments.moreComments(parentNode);
}
$this.parents("ul").parent().find(".active").removeClass("active");
$this.parents("li").addClass("active");
target.addClass("active");
$this.parents(".module-large").addClass("expanded");
// Call scrollBotton after the comments are displayed
// so that the scrollHight can be calculated
if ( targetContents === 'comments' ) {
Comments.scrollBottom(parentNode);
}
})
.on("ajax:error", function (e, xhr, status, error) {
// TODO

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")
@ -358,9 +284,9 @@
initNewProjectModal();
initEditProjectModal();
initManageUsersModal();
initCommentOptions("ul.content-comments");
initEditComments(".panel-project .tab-content");
initDeleteComments(".panel-project .tab-content");
Comments.initCommentOptions("ul.content-comments");
Comments.initEditComments(".panel-project .tab-content");
Comments.initDeleteComments(".panel-project .tab-content");
// initialize project tab remote loading
$(".panel-project .panel-footer [role=tab]")
@ -387,13 +313,15 @@
target.html(data.html);
initUsersEditLink(parentNode);
initCommentForm(parentNode);
initCommentsLink(parentNode);
Comments.form(parentNode);
Comments.moreComments(parentNode);
// TODO move to fn
parentNode.find(".active").removeClass("active");
$this.parents("li").addClass("active");
target.addClass("active");
Comments.scrollBottom(parentNode);
})
.on("ajax:error", function (e, xhr, status, error) {

View file

@ -553,9 +553,9 @@ $(document).ready(function() {
setupAssetsLoading();
// Init comments edit/delete
initCommentOptions("ul.content-comments");
initEditComments("#steps");
initDeleteComments("#steps");
Comments.initCommentOptions("ul.content-comments");
Comments.initEditComments("#steps");
Comments.initDeleteComments("#steps");
$(function () {

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

@ -1,7 +1,7 @@
class MyModuleCommentsController < ApplicationController
before_action :load_vars
before_action :check_view_permissions, only: [ :index ]
before_action :check_add_permissions, only: [ :new, :create ]
before_action :check_view_permissions, only: :index
before_action :check_add_permissions, only: [:new, :create]
before_action :check_edit_permissions, only: [:edit, :update]
before_action :check_destroy_permissions, only: [:destroy]
@ -9,31 +9,32 @@ class MyModuleCommentsController < ApplicationController
@comments = @my_module.last_comments(@last_comment_id, @per_page)
respond_to do |format|
format.json {
format.json do
# 'index' partial includes header and form for adding new
# messages. 'list' partial is used for showing more
# comments.
partial = "index.html.erb"
partial = "list.html.erb" if @last_comment_id > 0
more_url = ""
partial = 'index.html.erb'
partial = 'list.html.erb' if @last_comment_id > 0
more_url = ''
if @comments.count > 0
more_url = url_for(my_module_my_module_comments_url(@my_module,
format: :json,
from: @comments.last.id))
format: :json,
from: @comments
.first.id))
end
render :json => {
per_page: @per_page,
results_number: @comments.length,
more_url: more_url,
html: render_to_string({
render json: {
perPage: @per_page,
resultsNumber: @comments.length,
moreUrl: more_url,
html: render_to_string(
partial: partial,
locals: {
comments: @comments,
more_comments_url: more_url
}
})
)
}
}
end
end
end

View file

@ -1,7 +1,7 @@
class ProjectCommentsController < ApplicationController
before_action :load_vars
before_action :check_view_permissions, only: [ :index ]
before_action :check_add_permissions, only: [ :new, :create ]
before_action :check_view_permissions, only: :index
before_action :check_add_permissions, only: [:new, :create]
before_action :check_edit_permissions, only: [:edit, :update]
before_action :check_destroy_permissions, only: [:destroy]
@ -9,30 +9,31 @@ class ProjectCommentsController < ApplicationController
@comments = @project.last_comments(@last_comment_id, @per_page)
respond_to do |format|
format.json {
format.json do
# 'index' partial includes header and form for adding new
# messages. 'list' partial is used for showing more
# comments.
partial = "index.html.erb"
partial = "list.html.erb" if @last_comment_id > 0
more_url = ""
partial = 'index.html.erb'
partial = 'list.html.erb' if @last_comment_id > 0
more_url = ''
if @comments.count > 0
more_url = url_for(project_project_comments_url(format: :json,
from: @comments.last.id))
from: @comments
.first.id))
end
render :json => {
:per_page => @per_page,
:results_number => @comments.length,
:more_url => more_url,
:html => render_to_string({
:partial => partial,
:locals => {
:comments => @comments,
:more_comments_url => more_url
render json: {
perPage: @per_page,
resultsNumber: @comments.length,
moreUrl: more_url,
html: render_to_string(
partial: partial,
locals: {
comments: @comments,
more_comments_url: more_url
}
})
)
}
}
end
end
end

View file

@ -24,9 +24,9 @@ class ResultCommentsController < ApplicationController
.first.id))
end
render json: {
per_page: @per_page,
results_number: @comments.length,
more_url: more_url,
perPage: @per_page,
resultsNumber: @comments.length,
moreUrl: more_url,
html: render_to_string(partial: partial,
locals: { comments: @comments,
more_comments_url: more_url })

View file

@ -23,9 +23,9 @@ class StepCommentsController < ApplicationController
from: @comments.first.id))
end
render json: {
per_page: @per_page,
results_number: @comments.length,
more_url: more_url,
perPage: @per_page,
resultsNumber: @comments.length,
moreUrl: more_url,
html: render_to_string(partial: partial,
locals: { comments: @comments,
more_comments_url: more_url })

View file

@ -153,11 +153,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,9 +1,9 @@
<% day = 366 %>
<% 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 %>
<% 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>

View file

@ -44,4 +44,3 @@
<%= stylesheet_link_tag 'datatables' %>
<%= javascript_include_tag("my_modules/protocols") %>
<%= javascript_include_tag("step_result_comments") %>

View file

@ -50,7 +50,6 @@
<%= 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 "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

@ -1,9 +1,9 @@
<% day = 366 %>
<% 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 %>
<% 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>

View file

@ -1,9 +1,9 @@
<% day = 366 %>
<% 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 %>
<% 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>

View file

@ -59,7 +59,6 @@ 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)
# Libraries needed for Handsontable formulas
Rails.application.config.assets.precompile += %w(lodash.js)