2011-07-21 19:13:07 +03:00
|
|
|
|
var ls = ls || {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Обработка комментариев
|
|
|
|
|
*/
|
|
|
|
|
ls.comments = (function ($) {
|
|
|
|
|
/**
|
|
|
|
|
* Опции
|
|
|
|
|
*/
|
|
|
|
|
this.options = {
|
|
|
|
|
type: {
|
|
|
|
|
topic: {
|
|
|
|
|
url_add: aRouter.blog+'ajaxaddcomment/',
|
|
|
|
|
url_response: aRouter.blog+'ajaxresponsecomment/'
|
|
|
|
|
},
|
|
|
|
|
talk: {
|
|
|
|
|
url_add: aRouter.talk+'ajaxaddcomment/',
|
|
|
|
|
url_response: aRouter.talk+'ajaxresponsecomment/'
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
classes: {
|
|
|
|
|
form_loader: 'loader',
|
|
|
|
|
comment_new: 'new',
|
|
|
|
|
comment_current: 'current',
|
|
|
|
|
comment_deleted: 'deleted',
|
|
|
|
|
comment_self: 'self',
|
|
|
|
|
comment: 'comment',
|
|
|
|
|
comment_goto_parent: 'goto-comment-parent',
|
|
|
|
|
comment_goto_child: 'goto-comment-child'
|
2011-07-22 13:43:16 +03:00
|
|
|
|
},
|
|
|
|
|
wysiwyg: null
|
2011-07-21 19:13:07 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.iCurrentShowFormComment=0;
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Добавляет комментарий
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.add = function(formObj, targetId, targetType) {
|
2011-07-22 13:43:16 +03:00
|
|
|
|
if (this.options.wysiwyg) {
|
|
|
|
|
$('#'+formObj+' textarea').val(tinyMCE.activeEditor.getContent());
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
formObj = $('#'+formObj);
|
|
|
|
|
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$('#form_comment_text').addClass(this.options.classes.form_loader).attr('readonly',true);
|
2011-07-22 13:43:16 +03:00
|
|
|
|
$('#comment-button-submit').attr('disabled', 'disabled');
|
|
|
|
|
|
2011-07-21 19:13:07 +03:00
|
|
|
|
ls.ajax(this.options.type[targetType].url_add, formObj.serializeJSON(), function(result){
|
2011-04-01 10:49:36 +03:00
|
|
|
|
if (!result) {
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.enableFormComment();
|
|
|
|
|
ls.msg.error('Error','Please try again later');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (result.bStateError) {
|
|
|
|
|
this.enableFormComment();
|
|
|
|
|
ls.msg.error(null,result.sMsg);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
} else {
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.enableFormComment();
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$('#form_comment_text').val('');
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
2011-04-01 10:49:36 +03:00
|
|
|
|
// Load new comments
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.load(targetId, targetType, result.sCommentId, true);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
}
|
2011-07-22 13:43:16 +03:00
|
|
|
|
$('#comment-button-submit').attr('disabled', '');
|
2011-07-21 19:13:07 +03:00
|
|
|
|
}.bind(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Активирует форму
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.enableFormComment = function() {
|
|
|
|
|
$('#form_comment_text').removeClass(this.options.classes.form_loader).attr('readonly',false);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Показывает/скрывает форму комментирования
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.toggleCommentForm = function(idComment, bNoFocus) {
|
2011-07-22 13:43:16 +03:00
|
|
|
|
if (this.options.wysiwyg) {
|
|
|
|
|
tinyMCE.execCommand('mceRemoveControl',true,'form_comment_text');
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$('#form_comment').appendTo("#reply_"+idComment);
|
|
|
|
|
$('#form_comment_text').val('');
|
|
|
|
|
if (!bNoFocus) $('#form_comment_text').focus();
|
|
|
|
|
$('#form_comment_reply').val(idComment);
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$("#reply_"+idComment).css('display','block');
|
|
|
|
|
$('#comment_preview_'+this.iCurrentShowFormComment).html('').css('display','none');
|
|
|
|
|
this.iCurrentShowFormComment=idComment;
|
2011-07-22 13:43:16 +03:00
|
|
|
|
if (this.options.wysiwyg) {
|
|
|
|
|
tinyMCE.execCommand('mceAddControl',true,'form_comment_text');
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Подгружает новые комментарии
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.load = function(idTarget, typeTarget, selfIdComment, bNotFlushNew) {
|
2011-04-01 10:49:36 +03:00
|
|
|
|
var idCommentLast = $("#comment_last_id").val();
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Удаляем подсветку у комментариев
|
2011-07-21 19:13:07 +03:00
|
|
|
|
if (!bNotFlushNew) {
|
|
|
|
|
$('.comment').each(function(index, item){
|
|
|
|
|
$(item).removeClass(this.options.classes.comment_new+' '+this.options.classes.comment_current);
|
|
|
|
|
}.bind(this));
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-01 10:49:36 +03:00
|
|
|
|
objImg = $('#update-comments');
|
2011-07-24 15:25:01 +03:00
|
|
|
|
objImg.addClass('active');
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
var params = { idCommentLast: idCommentLast, idTarget: idTarget, typeTarget: typeTarget };
|
|
|
|
|
if (selfIdComment) {
|
|
|
|
|
params.selfIdComment = selfIdComment;
|
|
|
|
|
}
|
|
|
|
|
if ($('#comment_use_paging').val()) {
|
|
|
|
|
params.bUsePaging = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ls.ajax(this.options.type[typeTarget].url_response, params, function(result) {
|
2011-07-24 15:25:01 +03:00
|
|
|
|
objImg.removeClass('active');
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
if (!result) { ls.msg.error('Error','Please try again later'); }
|
2011-04-01 10:49:36 +03:00
|
|
|
|
if (result.bStateError) {
|
2011-07-21 19:13:07 +03:00
|
|
|
|
ls.msg.error(null,result.sMsg);
|
|
|
|
|
} else {
|
|
|
|
|
var aCmt = result.aComments;
|
|
|
|
|
if (aCmt.length > 0 && result.iMaxIdComment) {
|
|
|
|
|
$("#comment_last_id").val(result.iMaxIdComment);
|
|
|
|
|
}
|
|
|
|
|
if (selfIdComment) {
|
|
|
|
|
this.toggleCommentForm(0, true);
|
|
|
|
|
} else {
|
|
|
|
|
this.setCountNewComment(aCmt.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$.each(aCmt, function(index, item) {
|
|
|
|
|
this.inject(item.idParent, item.id, item.html);
|
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
|
|
if (selfIdComment && $('#comment_id_'+selfIdComment).length) {
|
|
|
|
|
this.scrollToComment(selfIdComment);
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
}
|
2011-07-21 19:13:07 +03:00
|
|
|
|
}.bind(this));
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Вставка комментария
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.inject = function(idCommentParent, idComment, sHtml) {
|
|
|
|
|
var newComment = $('<div>', {'class': 'comment-wrapper', id: 'comment_wrapper_id_'+idComment}).html(sHtml);
|
|
|
|
|
if (idCommentParent) {
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$('#comment_wrapper_id_'+idCommentParent).append(newComment);
|
|
|
|
|
} else {
|
|
|
|
|
$('#comments').append(newComment);
|
2011-07-21 19:13:07 +03:00
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Удалить/восстановить комментарий
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.toggle = function(obj, commentId) {
|
|
|
|
|
ls.ajax(aRouter['ajax']+'comment/delete/', { idComment: commentId }, function(result){
|
2011-04-01 10:49:36 +03:00
|
|
|
|
if (!result) {
|
2011-07-21 19:13:07 +03:00
|
|
|
|
ls.msg.error('Error','Please try again later');
|
|
|
|
|
}
|
|
|
|
|
if (result.bStateError) {
|
|
|
|
|
ls.msg.error(null,result.sMsg);
|
|
|
|
|
} else {
|
|
|
|
|
ls.msg.notice(null,result.sMsg);
|
|
|
|
|
|
|
|
|
|
$('#comment_id_'+commentId).removeClass(this.options.classes.comment_self+' '+this.options.classes.comment_new+' '+this.options.classes.comment_deleted+' '+this.options.classes.comment_current);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
if (result.bState) {
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$('#comment_id_'+commentId).addClass(this.options.classes.comment_deleted);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
}
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$(obj).text(result.sTextToggle);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
}
|
2011-07-21 19:13:07 +03:00
|
|
|
|
}.bind(this));
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Предпросмотр комментария
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.preview = function() {
|
2011-07-22 13:43:16 +03:00
|
|
|
|
if (this.options.wysiwyg) {
|
|
|
|
|
$("#form_comment_text").val(tinyMCE.activeEditor.getContent());
|
|
|
|
|
}
|
2011-04-01 10:49:36 +03:00
|
|
|
|
if ($("#form_comment_text").val() == '') return;
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$("#comment_preview_"+this.iCurrentShowFormComment).css('display', 'block');
|
2011-07-06 10:11:50 +03:00
|
|
|
|
ls.tools.textPreview('form_comment_text', false, 'comment_preview_'+this.iCurrentShowFormComment);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Устанавливает число новых комментариев
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.setCountNewComment = function(count) {
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
$('#new_comments_counter').css('display','block').text(count);
|
|
|
|
|
} else {
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$('#new_comments_counter').text(0).hide();
|
|
|
|
|
}
|
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Вычисляет кол-во новых комментариев
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.calcNewComments = function() {
|
|
|
|
|
this.setCountNewComment($('.'+this.options.classes.comment+'.'+this.options.classes.comment_new).length);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Переход к следующему комментарию
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.goToNextComment = function() {
|
|
|
|
|
var aCommentsNew = $('.'+this.options.classes.comment+'.'+this.options.classes.comment_new);
|
|
|
|
|
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$.scrollTo($(aCommentsNew[0]), 1000, {offset: -250});
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$('[id^=comment_id_]').removeClass(this.options.classes.comment_current);
|
|
|
|
|
$(aCommentsNew[0]).removeClass(this.options.classes.comment_new).addClass(this.options.classes.comment_current);
|
|
|
|
|
|
2011-04-01 10:49:36 +03:00
|
|
|
|
this.setCountNewComment(aCommentsNew.length - 1);
|
|
|
|
|
},
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Прокрутка к комментарию
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.scrollToComment = function(idComment) {
|
2011-04-01 10:49:36 +03:00
|
|
|
|
$.scrollTo('#comment_id_'+idComment, 1000, {offset: -250});
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$('[id^=comment_id_]').removeClass(this.options.classes.comment_current);
|
|
|
|
|
$('#comment_id_'+idComment).addClass(this.options.classes.comment_current);
|
2011-04-01 10:49:36 +03:00
|
|
|
|
},
|
|
|
|
|
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
2011-04-01 12:54:47 +03:00
|
|
|
|
// Прокрутка к родительскому комментарию
|
2011-07-21 19:13:07 +03:00
|
|
|
|
this.goToParentComment = function(id, pid) {
|
2011-04-01 10:49:36 +03:00
|
|
|
|
thisObj = this;
|
2011-07-21 19:13:07 +03:00
|
|
|
|
$('.'+this.options.classes.comment_goto_child).hide().find('a').unbind();
|
|
|
|
|
|
|
|
|
|
$("#comment_id_"+pid).find('.'+this.options.classes.comment_goto_child).show().find("a").bind("click", function(){
|
|
|
|
|
$(this).parent('.'+thisObj.options.classes.comment_goto_child).hide();
|
2011-04-01 10:49:36 +03:00
|
|
|
|
thisObj.scrollToComment(id);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
this.scrollToComment(pid);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
2011-07-22 13:43:16 +03:00
|
|
|
|
this.init = function() {
|
|
|
|
|
this.initEvent();
|
|
|
|
|
this.calcNewComments();
|
|
|
|
|
|
|
|
|
|
if (typeof(this.options.wysiwyg)!='number') {
|
|
|
|
|
this.options.wysiwyg = Boolean(BLOG_USE_TINYMCE && tinyMCE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.initEvent = function() {
|
|
|
|
|
$('#form_comment_text').bind('keyup', function(e) {
|
|
|
|
|
key = e.keyCode || e.which;
|
|
|
|
|
if(e.ctrlKey && (key == 13)) {
|
|
|
|
|
$('#comment-button-submit').click();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2011-07-21 19:13:07 +03:00
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}).call(ls.comments || {},jQuery);
|
|
|
|
|
|
2011-04-01 10:49:36 +03:00
|
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
2011-07-22 13:43:16 +03:00
|
|
|
|
ls.comments.init();
|
|
|
|
|
});
|