2016-09-27 16:30:48 +08:00
|
|
|
var Comments = (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');
|
2016-09-27 22:19:31 +08:00
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
$(data.html).insertAfter(listItem);
|
2016-09-27 23:31:37 +08:00
|
|
|
if (data.resultsNumber < data.perPage) {
|
2016-09-27 16:30:48 +08:00
|
|
|
moreBtn.remove();
|
|
|
|
} else {
|
2016-09-27 23:31:37 +08:00
|
|
|
moreBtn.attr('href', data.moreUrl);
|
2016-09-27 16:30:48 +08:00
|
|
|
moreBtn.trigger('blur');
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:42:32 +08:00
|
|
|
var date;
|
|
|
|
$.each(list.find('.comment-date-separator'), function() {
|
|
|
|
if ( $(this).find('p').html() === date ) {
|
|
|
|
$(this).remove();
|
|
|
|
} else {
|
|
|
|
date = $(this).find('p').html();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
// Reposition dropdown comment options
|
|
|
|
scrollCommentOptions(listItem
|
|
|
|
.closest('.content-comments')
|
|
|
|
.find('.dropdown-comment'));
|
2016-10-03 18:34:16 +08:00
|
|
|
} else {
|
|
|
|
$('.btn-more-comments').remove();
|
2016-08-30 17:39:50 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
// Initialize comment form.
|
|
|
|
function initCommentForm($el) {
|
2016-08-24 21:45:49 +08:00
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
var $form = $el.find('ul form');
|
2016-08-25 23:01:30 +08:00
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
$('.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();
|
|
|
|
|
2016-10-21 04:33:15 +08:00
|
|
|
// Find previous date separator
|
|
|
|
var dateSeparator = list.parent().find('.comment-date-separator:last');
|
|
|
|
if (dateSeparator.length > 0) {
|
|
|
|
// Parse string with creation date
|
|
|
|
var pr = dateSeparator.text().split('.');
|
|
|
|
var comm = data.date.split('.');
|
|
|
|
// Build Date objects and compare
|
|
|
|
var sepDate = new Date(pr[2], pr[1] - 1, pr[0]);
|
|
|
|
var commDate = new Date(comm[2], comm[1] - 1, comm[0]);
|
|
|
|
if (commDate > sepDate) {
|
|
|
|
// Add date separator
|
|
|
|
list.parent().find('.content-comments')
|
|
|
|
.append('<li class="comment-date-separator">\
|
|
|
|
<p class="text-center">' + data.date + '</p>\
|
|
|
|
</li>');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Comment is the first one so add date separator
|
|
|
|
list.parent().find('.content-comments')
|
|
|
|
.append('<li class="comment-date-separator">\
|
|
|
|
<p class="text-center">' + data.date + '</p>\
|
|
|
|
</li>');
|
|
|
|
}
|
|
|
|
|
2017-05-08 23:32:55 +08:00
|
|
|
CounterBadge.updateCounterBadge(data.counter,
|
|
|
|
data.linked_id, 'comments');
|
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-08-24 21:45:49 +08:00
|
|
|
}
|
2016-09-27 16:30:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initCommentOptions(scrollableContainer, useParentOffset) {
|
2016-10-11 21:00:59 +08:00
|
|
|
if ( ! _.isUndefined(useParentOffset) ) {
|
2016-09-27 23:31:37 +08:00
|
|
|
useParentOffset = useParentOffset;
|
|
|
|
} else {
|
|
|
|
useParentOffset = true;
|
|
|
|
}
|
2016-10-05 21:39:54 +08:00
|
|
|
scrollCommentOptions($('.dropdown-comment'), useParentOffset);
|
2016-08-24 21:45:49 +08:00
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
// 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;
|
2016-10-11 21:00:59 +08:00
|
|
|
var parentTopPos = $el.offset().top;
|
2016-09-27 16:30:48 +08:00
|
|
|
if (leftPos + menu.width() > $(window).width()) {
|
2016-10-11 21:00:59 +08:00
|
|
|
menu.offset({ left: leftPos - menu.width(),
|
2016-10-05 23:45:20 +08:00
|
|
|
top: (parentTopPos +
|
|
|
|
<%= Constants::DROPDOWN_TOP_OFFSET_PX %>)});
|
2016-08-24 21:45:49 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-09-27 16:30:48 +08:00
|
|
|
|
|
|
|
// Reposition dropdowns vertically on scroll events
|
|
|
|
document.addEventListener('scroll', function (event) {
|
|
|
|
var $target = $(event.target);
|
|
|
|
var parent = $(scrollableContainer);
|
|
|
|
|
|
|
|
if ($target.length) {
|
2016-10-05 21:39:54 +08:00
|
|
|
scrollCommentOptions(parent.find('.dropdown-comment'), useParentOffset);
|
2016-09-27 16:30:48 +08:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:39:54 +08:00
|
|
|
function scrollCommentOptions(selector, useParentOffset) {
|
2016-10-11 21:00:59 +08:00
|
|
|
if ( ! _.isUndefined(useParentOffset) ) {
|
2016-10-05 21:39:54 +08:00
|
|
|
useParentOffset = useParentOffset;
|
|
|
|
} else {
|
|
|
|
useParentOffset = true;
|
|
|
|
}
|
2016-09-27 16:30:48 +08:00
|
|
|
_.each(selector, function(el) {
|
|
|
|
var $el = $(el);
|
2016-10-05 21:39:54 +08:00
|
|
|
var offset = useParentOffset ? $el.offset().top : $el.position().top;
|
2016-09-27 16:30:48 +08:00
|
|
|
$el.find('.dropdown-menu-fixed')
|
2016-10-05 23:45:20 +08:00
|
|
|
.offset({ top: (offset + <%= Constants::DROPDOWN_TOP_OFFSET_PX %>) });
|
2016-09-27 16:30:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDeleteComments(parent) {
|
2016-09-27 17:03:10 +08:00
|
|
|
$(parent).on('click', '[data-action=delete-comment]', function() {
|
2016-09-27 16:30:48 +08:00
|
|
|
var $this = $(this);
|
|
|
|
if (confirm($this.attr('data-confirm-message'))) {
|
|
|
|
$.ajax({
|
|
|
|
url: $this.attr('data-url'),
|
|
|
|
type: 'DELETE',
|
|
|
|
dataType: 'json',
|
2017-05-08 23:32:55 +08:00
|
|
|
success: function(data) {
|
2016-09-27 16:30:48 +08:00
|
|
|
// There are 3 possible actions:
|
|
|
|
// - (A) comment is the last comment in project
|
2016-09-27 17:03:10 +08:00
|
|
|
// - (B) comment is the last comment inside specific date
|
|
|
|
// (remove the date separator)
|
2016-09-27 16:30:48 +08:00
|
|
|
// - (C) comment is a usual comment
|
|
|
|
|
|
|
|
var commentEl = $this.closest('.comment');
|
|
|
|
|
|
|
|
// Case A
|
2016-09-27 17:03:10 +08:00
|
|
|
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>');
|
2016-08-24 23:38:31 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
// Case B
|
2016-09-27 17:03:10 +08:00
|
|
|
if (commentEl.prev('.comment-date-separator').length > 0 &&
|
|
|
|
commentEl.next('.comment').length === 0) {
|
2016-09-27 16:30:48 +08:00
|
|
|
commentEl.prev('.comment-date-separator').remove();
|
|
|
|
}
|
|
|
|
commentEl.remove();
|
2016-08-24 23:38:31 +08:00
|
|
|
|
2017-05-08 23:32:55 +08:00
|
|
|
CounterBadge.updateCounterBadge(data.counter,
|
|
|
|
data.linked_id,
|
|
|
|
'comments');
|
2016-09-27 16:30:48 +08:00
|
|
|
scrollCommentOptions($(parent).find('.dropdown-comment'));
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
// Display alert
|
|
|
|
alert(data.responseJSON.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initEditComments(parent) {
|
2016-10-17 18:59:51 +08:00
|
|
|
$(parent).unbind('click').on('click', '[data-action=edit-comment]', function() {
|
2016-09-27 22:22:09 +08:00
|
|
|
|
2016-09-27 16:30:48 +08:00
|
|
|
var $this = $(this);
|
|
|
|
$.ajax({
|
|
|
|
url: $this.attr('data-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
var commentEl = $this.closest('.comment');
|
2016-09-27 17:03:10 +08:00
|
|
|
var container = commentEl
|
|
|
|
.find('[data-role=comment-message-container]');
|
2016-09-27 16:30:48 +08:00
|
|
|
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]');
|
2016-11-08 18:29:55 +08:00
|
|
|
var input = form.find('[data-role=message-input]');
|
2016-09-27 16:30:48 +08:00
|
|
|
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);
|
|
|
|
})
|
2016-12-24 03:41:23 +08:00
|
|
|
.on('ajax:success', function(ev, data) {
|
2016-09-27 16:30:48 +08:00
|
|
|
var newMessage = input.val();
|
2016-12-24 03:41:23 +08:00
|
|
|
if (!_.isUndefined(data.comment)) {
|
|
|
|
newMessage = data.comment;
|
|
|
|
}
|
2016-09-27 16:30:48 +08:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
},
|
2016-09-27 17:03:10 +08:00
|
|
|
error: function() {
|
2016-09-27 16:30:48 +08:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
initialize: initializeComments,
|
|
|
|
scrollBottom: scrollBottom,
|
|
|
|
moreComments: initCommentsLink,
|
|
|
|
form: initCommentForm,
|
|
|
|
bindNewElement: bindCommentInitializerToNewElement,
|
|
|
|
initCommentOptions: initCommentOptions,
|
|
|
|
initDeleteComments: initDeleteComments,
|
|
|
|
initEditComments: initEditComments
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|