2020-09-08 21:42:30 +08:00
|
|
|
/* global inlineEditing PerfectScrollbar HelperModule I18n */
|
2019-06-04 20:40:21 +08:00
|
|
|
/* eslint-disable no-restricted-globals, no-alert */
|
|
|
|
var Comments = (function() {
|
|
|
|
function changeCounter(comment, value) {
|
2019-06-21 16:43:41 +08:00
|
|
|
var currentCount = $('#comment-counter-' + comment.closest('.comments-container').attr('data-object-id'));
|
|
|
|
var newValue = parseInt(currentCount.html(), 10) + value;
|
2023-03-09 18:38:47 +08:00
|
|
|
currentCount.text(newValue);
|
2019-06-21 16:43:41 +08:00
|
|
|
if (newValue === 0) {
|
|
|
|
currentCount.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
currentCount.removeClass('hidden');
|
|
|
|
}
|
2019-06-04 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function scrollBottom(container) {
|
|
|
|
container.scrollTop(container[0].scrollHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
function newCommentValidation(textarea, submitBtn) {
|
|
|
|
textarea.off().on('focus', function() {
|
|
|
|
$(this).addClass('border');
|
2020-05-22 22:51:11 +08:00
|
|
|
if (this.value.trim().length > 0) {
|
2019-06-04 20:40:21 +08:00
|
|
|
submitBtn.addClass('show');
|
|
|
|
}
|
|
|
|
}).on('blur', function() {
|
2020-05-22 22:51:11 +08:00
|
|
|
if (this.value.trim().length === 0) {
|
2019-06-04 20:40:21 +08:00
|
|
|
$(this).removeClass('border');
|
|
|
|
submitBtn.removeClass('show');
|
|
|
|
}
|
|
|
|
}).on('keyup', function() {
|
2020-05-22 22:51:11 +08:00
|
|
|
if (this.value.trim().length > 0) {
|
2019-06-04 20:40:21 +08:00
|
|
|
submitBtn.addClass('show');
|
|
|
|
} else {
|
|
|
|
submitBtn.removeClass('show');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDeleteComment() {
|
|
|
|
$('.comment-container .delete-button').off().click(function(e) {
|
|
|
|
var $this = $(this);
|
|
|
|
if (confirm($this.attr('data-confirm-message'))) {
|
|
|
|
$.ajax({
|
|
|
|
url: $this.attr('data-url'),
|
|
|
|
type: 'DELETE',
|
|
|
|
dataType: 'json',
|
|
|
|
success: () => {
|
|
|
|
changeCounter($this, -1);
|
|
|
|
$this.closest('.comment-container').remove();
|
|
|
|
},
|
|
|
|
error: (error) => {
|
2020-09-08 21:42:30 +08:00
|
|
|
if (error.status === 403) {
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
|
|
|
|
} else {
|
|
|
|
alert(error.responseJSON.errors.message);
|
|
|
|
}
|
2019-06-04 20:40:21 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initCreateComment() {
|
|
|
|
$.each($('.new-message-container'), (index, el) => {
|
|
|
|
var $el = $(el);
|
|
|
|
newCommentValidation(
|
|
|
|
$el.find('textarea'),
|
|
|
|
$el.find('.new-comment-button')
|
|
|
|
);
|
|
|
|
$el.find('.new-comment-button').off('click').click(() => {
|
|
|
|
var errorField = $el.find('.new-message-error');
|
|
|
|
var newButton = this;
|
|
|
|
if (newButton.disable) return;
|
|
|
|
newButton.disable = true;
|
|
|
|
$.post(el.dataset.createUrl, {
|
|
|
|
comment: { message: $el.find('#message').val() }
|
|
|
|
}, (result) => {
|
|
|
|
$el.parent().find('.comments-list').append(result.html);
|
|
|
|
changeCounter($el, 1);
|
2019-11-12 18:26:18 +08:00
|
|
|
inlineEditing.init();
|
2019-06-04 20:40:21 +08:00
|
|
|
initDeleteComment();
|
|
|
|
scrollBottom($el.parent().find('.content-comments'));
|
|
|
|
errorField.html('');
|
|
|
|
$el.find('#message').val('');
|
|
|
|
$el.find('.new-comment-button').removeClass('show');
|
|
|
|
newButton.disable = false;
|
2019-06-14 22:20:47 +08:00
|
|
|
$el.find('textarea').focus().blur();
|
2019-06-04 20:40:21 +08:00
|
|
|
})
|
|
|
|
.error((error) => {
|
2020-09-08 21:42:30 +08:00
|
|
|
if (error.status === 403) {
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
|
|
|
|
}
|
2023-03-09 18:38:47 +08:00
|
|
|
errorField.text(error.responseJSON.errors.message);
|
2019-06-04 20:40:21 +08:00
|
|
|
newButton.disable = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initMoreButton() {
|
|
|
|
$('.comments-container .btn-more-comments-new').off()
|
|
|
|
.on('ajax:success', function(e, result) {
|
|
|
|
var moreBtn = $(this);
|
|
|
|
if (result.moreUrl) {
|
|
|
|
moreBtn.closest('.comments-container').find('.comments-list').prepend(result.html);
|
2019-11-12 18:26:18 +08:00
|
|
|
inlineEditing.init();
|
2019-06-04 20:40:21 +08:00
|
|
|
initDeleteComment();
|
|
|
|
if (result.resultsNumber < result.perPage) {
|
|
|
|
moreBtn.remove();
|
|
|
|
} else {
|
|
|
|
moreBtn.attr('href', result.moreUrl);
|
|
|
|
moreBtn.trigger('blur');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
moreBtn.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkContainerSize(mode) {
|
|
|
|
if (mode === 'simple') {
|
|
|
|
$('.comments-container').addClass('simple');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initPerfectScroll() {
|
|
|
|
$.each($('.comments-container .content-comments'), function(index, object) {
|
|
|
|
var ps = new PerfectScrollbar(object, { wheelSpeed: 0.5 });
|
|
|
|
$(document).ajaxComplete(() => {
|
|
|
|
ps.update();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: (mode) => {
|
|
|
|
if ($('.comments-container').length > 0) {
|
|
|
|
initCreateComment();
|
|
|
|
initDeleteComment();
|
|
|
|
initMoreButton();
|
|
|
|
checkContainerSize(mode);
|
|
|
|
setTimeout(() => {
|
|
|
|
scrollBottom($('.content-comments'));
|
|
|
|
}, 500);
|
|
|
|
initPerfectScroll();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
scrollBottom: () => {
|
|
|
|
scrollBottom($('.content-comments'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
$(document).on('turbolinks:load', function() {
|
|
|
|
Comments.init();
|
|
|
|
});
|