if (!('ifCond' in Handlebars.helpers)) { Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { switch (operator) { case '==': return (v1 == v2) ? options.fn(this) : options.inverse(this); case '===': return (v1 === v2) ? options.fn(this) : options.inverse(this); case '<': return (v1 < v2) ? options.fn(this) : options.inverse(this); case '<=': return (v1 <= v2) ? options.fn(this) : options.inverse(this); case '>': return (v1 > v2) ? options.fn(this) : options.inverse(this); case '>=': return (v1 >= v2) ? options.fn(this) : options.inverse(this); case '&&': return (v1 && v2) ? options.fn(this) : options.inverse(this); case '||': return (v1 || v2) ? options.fn(this) : options.inverse(this); case '!=': return (v1 != v2) ? options.fn(this) : options.inverse(this); default: return options.inverse(this); } }); } var Comment = Comment || {}; Comment.url = '/comment/comment.php'; Comment.page = { comment: 0, announcement: 0 }; /** * Initialize */ Comment.init = function () { var commentBlock = $(this); // bind event when user type tin search input var findInput = commentBlock.find('input.find-comment'); findInput.keyup(function () { var $thisInput = $(this); var container = $thisInput.parents('.tab-pane').find('.comment-container'); var clearInput = $thisInput.parent().find('.clear-input'); Comment.resetPage(); if ($thisInput.val()) { clearInput.css('visibility', 'visible'); clearTimeout($thisInput.data('timeoutid')); $thisInput.data('timeoutid', setTimeout(function(){ Comment.getComments(container, false, $thisInput.val()); }, 300)); } else { clearInput.css('visibility', 'hidden'); Comment.getComments(container, false); } }); // Bind event clear search input button commentBlock.find('.clear-input').find('a').click(function(e) { e.preventDefault(); $(this).parents('.find-comment-wrap').find('.find-comment').val('').keyup(); $(this).parent().css('visibility', 'hidden'); }); // bind event click to close add comment form button commentBlock.find('.cf-close').click(function (e) { e.preventDefault(); $(this).parents('.ac-form').hide(); }); // bind event click to button show popup form add comment var addCommentButton = commentBlock.find('.ac-button'); addCommentButton.click(function (e) { var button = $(this); var formWrap = button.parents('.add-comment').find('.ac-form'); formWrap.show(); }); // init editor for form add comment var addCommentForm = commentBlock.find('.form-comment'); var contentArea = addCommentForm.find('textarea.redactor'); contentArea.redactor({ buttons: ['bold', 'italic', 'unorderedlist', 'orderedlist', 'link','image'], minHeight: 100, imageUpload: false, s3: false, plugins: ['imagelink'], placeholder: 'Nội dung', keyupCallback: function(e) { var formWrap = this.$textarea.parents('.form-comment'); if(this.$textarea.val()) { formWrap.find('.fc-add-button').prop('disabled', false); } else { formWrap.find('.fc-add-button').prop('disabled', true); } } }); // add comment submit handle addCommentForm.submit(function (e) { e.preventDefault(); var form = $(this); $.post(Comment.url, $(this).serialize(), function (response) { if (response.status == 'error') { alert(response.message); } else if (response.status == 'success'){ var container = form.parents('.tab-pane').find('.comment-container'); Comment.addComment(response.comment, container, false); form.parents('.ac-form').hide(); Comment.resetForm(form); } }, 'JSON'); }); // bind event //show/hide action button commentBlock.on('mouseenter', '.c-action' , function (e) { $(this).find('.owner-action').show(); }); commentBlock.on('mouseleave', '.c-action' , function (e) { $(this).find('.owner-action').hide(); }); // init comment // get active tab var $tabPanel = commentBlock.parent(); if ($tabPanel.hasClass('active')) { Comment.getComments($tabPanel.find('.comment-container'), false); } // change tab $('a[data-toggle="tab"][href="javascript:void(0)"' + $tabPanel.attr('id') + '"]').on('shown.bs.tab', function (e) { var tab = $(e.target).attr('href'); var container = $(tab).find('.comment-container'); if (!$(tab).hasClass('loaded')) { Comment.getComments(container, false); } }); // load more button commentBlock.on('click', '.tcl-more', function (e) { e.preventDefault(); var container = $(this).parents('.tab-pane').find('.comment-container'); Comment.getComments(container, true); }); }; /** * Add comment * @param data * @param container * @param isAppend */ Comment.addComment = function (data, container, isAppend) { var source = $("#hm-comment-item").html(); var reply = $('#hm-reply-item').html(); var template = Handlebars.compile(source); var html = template(data); var comment = $(html); // add comment if (isAppend) { container.append(comment); } else { container.prepend(comment); } // add replies var replyContainer = comment.find('.reply-container'); if (data.replies) { for (var i in data.replies) { template = Handlebars.compile(reply); html = template(data.replies[i]); replyContainer.append(html); } } // init editor comment.find('textarea.redactor').redactor({ buttons: ['bold', 'italic', 'unorderedlist', 'orderedlist', 'link', 'image'], minHeight: 100, placeholder: 'Nội dung', keyupCallback: function(e) { var formWrap = this.$textarea.parents('form'); if(this.$textarea.val()) { formWrap.find('.fc-add-button').prop('disabled', false); } else { formWrap.find('.fc-add-button').prop('disabled', true); } }, initCallback: function(e) { var formWrap = this.$textarea.parents('form'); if(this.$textarea.val()) { formWrap.find('.fc-add-button').prop('disabled', false); } } }); // show hide reply comment.find('.comment-reply').click(function (e) { function toggleReply(target) { var reply = target.parents('.comment-item').find('.reply'); reply.toggleClass('hidden'); reply.find('.edit-comment').hide(); reply.find('.ci-wrapper').show(); var count = target.find('.r-count').text(); if (parseInt(count)) { target.find('.r-show').toggleClass('hidden'); } } var replyButton = $(this); e.preventDefault(); if ($(this).hasClass('login')) { toggleReply(replyButton); return false; } Comment.checkLogin(function() { toggleReply(replyButton); replyButton.addClass('login'); }); }); // add reply event comment.find('.reply-form').submit(function (e) { e.preventDefault(); var form = $(this); $.post(Comment.url, $(this).serialize(), function (response) { if (response.status == 'error') { alert(response.message); } else if (response.status == 'success'){ var container = form.parents('.reply').find('.reply-container'); Comment.addReply(response.comment, container); Comment.resetForm(form); } }, 'JSON'); }); // add like event comment.find('.like-action').click(function (e) { e.preventDefault(); Comment.likeComment($(this)); }); // bind delete event comment.find('.owner-delete').click(function (e) { e.preventDefault(); Comment.deleteComment($(this)); }); // bind edit event comment.find('.owner-edit').click(function (e) { e.preventDefault(); Comment.editComment($(this)); }); }; /** * Add reply * @param data * @param container */ Comment.addReply = function (data, container) { var reply = $('#hm-reply-item').html(); var template = Handlebars.compile(reply); var html = template(data); var comment = $(html); container.append(comment); // update reply count if (data.comment) { var commentItem = comment.parents('.comment-item'); var replyAction = commentItem.find('.comment-reply'); replyAction.find('.r-count').text(data.comment.replies_count); replyAction.find('.r-hide').removeClass('hidden'); replyAction.find('.r-show:not(.r-hide)').addClass('hidden'); } // init editor comment.find('textarea.redactor').redactor({ buttons: ['bold', 'italic', 'unorderedlist', 'orderedlist', 'link', 'image'], minHeight: 100, placeholder: 'Nội dung', keyupCallback: function(e) { var formWrap = this.$textarea.parents('form'); if(this.$textarea.val()) { formWrap.find('.fc-add-button').prop('disabled', false); } else { formWrap.find('.fc-add-button').prop('disabled', true); } } }); // bind delete event comment.find('.owner-delete').click(function (e) { e.preventDefault(); Comment.deleteComment($(this)); }); // bind edit event comment.find('.owner-edit').click(function (e) { e.preventDefault(); Comment.editComment($(this)); }); }; /** * Delete comment * @param target */ Comment.deleteComment = function(target) { var commentId = target.data('comment'); if (!confirm("Bạn có chắc muốn xóa nhận xét này?")) { return false; } $.post(Comment.url, { commentid: commentId, action: 'delete' }, function (response) { if (response.status == 'error') { alert(response.message); } else if (response.status == 'success') { // update count comment if (response.isReply) { var count = target.parents('.comment-item').find('.comment-reply .r-count'); count.text(parseInt(count.text()) - 1); } // remove comment target.closest('.hm-item').remove(); } }, 'JSON'); }; /** * Edit comment * @param target */ Comment.editComment = function(target) { var form = $(target.data('target')); var commentId = target.data('comment'); var wrapper = form.closest('div').find('.ci-wrapper-' + commentId); wrapper.hide(); form.show(); // event cancel button form.find('.cancel-button').click(function (e) { e.preventDefault(); wrapper.show(); form.hide(); }); // submit form edit form.unbind('submit').submit(function (e) { e.preventDefault(); $.post(Comment.url, $(this).serialize(), function (response) { if (response.status == 'error') { alert(response.message); } else if (response.status == 'success') { // update comment wrapper.find('.title').html(response.title); wrapper.find('.content').html(response.content); wrapper.show(); form.hide(); } }, 'JSON'); }) }; /** * Add comment like * @param target */ Comment.likeComment = function(target) { var commentId = target.parents('.comment-like').data('comment'); $.post(Comment.url, { commentid: commentId, action: 'like' }, function (response) { if (response.status == 'error') { alert(response.message); } else if (response.status == 'success') { // update like and like count if (response.isLike) { target.find('.cb-like').text('Đã thích '); } else { target.find('.cb-like').text('Thích '); } target.find('.cb-like-count').text(response.likeCount); } }, 'JSON'); }; /** * Get comments. Use for search, paginator, load * @param container * @param isLoadMore * @param query */ Comment.getComments = function(container, isLoadMore, query) { // show loading gif var loading = container.parents('.tab-pane').find('.tc-loading'); loading.show(); // post data var data = { scormid: container.data('scorm'), type: container.data('type'), page: (container.data('type') == '1') ? Comment.page.announcement : Comment.page.comment, action: 'list' }; // use for search comments // reset page and container when search if (typeof query !== 'undefined') { data.query = query; container.empty(); Comment.resetPage(); } $.post(Comment.url, data, function(response) { if (response.status == 'error') { if (isLoadMore) { alert(response.message); } } else if (response.status == 'success'){ container.parents('.tab-pane').addClass('loaded'); if (response.data) { // if not load more reset page and comment if (!isLoadMore) { container.empty(); Comment.resetPage(); } // add comment for (var i in response.data) { Comment.addComment(response.data[i], container, true); } // show/hide button load more var load = container.parents('.tab-pane').find('.tc-load'); if (response.isMore) { load.show(); } else { load.hide(); } // Pagination if (response.isMore) { if(data.type == '0') { Comment.page.comment = Comment.page.comment + 1; } else { Comment.page.announcement = Comment.page.announcement +1; } } } else { // show empty comment notice container.parents('.tab-pane').find('.tc-load').hide(); if (!isLoadMore) { container.find('.empty').show(); } } } loading.hide(); }, 'JSON'); }; /** * Reset pagination page */ Comment.resetPage = function () { Comment.page.announcement = 0; Comment.page.comment = 0; }; /** * Check user id login * @param callback */ Comment.checkLogin = function (callback) { $.post('/comment/check-login.php', {}, function (response) { if (response.login == 'false') { alert('Hãy đăng nhập để tham gia thảo luận nhé!'); } else { if (typeof callback == 'function') { callback(); } } }, 'JSON'); }; /** * Reset form * @param form */ Comment.resetForm = function (form) { form.find('input[type="text"]').val(''); form.find('textarea.redactor').val('').parents('.redactor-box').find('.redactor-editor').html(''); form.find('.fc-add-button').prop('disabled', true); }; $(function () { $.fn.comment = function(opts) { // Initialize for all elements in jQuery collection this.each(function() { Comment.init.call(this); }); return this; }; $('#hm-comment').comment(); });