From c5663d470c98b69456f82a113fbba53dbe20f38d Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 12 Oct 2016 20:21:16 +0300 Subject: [PATCH 1/5] Added comment thread wrapper Adds the ability to identify comment thread --- .../tickets/elements/snippets/snippet.comments.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/components/tickets/elements/snippets/snippet.comments.php b/core/components/tickets/elements/snippets/snippet.comments.php index f3e98201..9a653574 100644 --- a/core/components/tickets/elements/snippets/snippet.comments.php +++ b/core/components/tickets/elements/snippets/snippet.comments.php @@ -1,4 +1,4 @@ -' . $commentForm . $commentsThread . ''getOption('thread', $scriptProperties, 'resource-' . $modx->resource->id, @@ -193,8 +193,8 @@ ? $modx->lexicon('ticket_thread_err_closed') : $form; $output = !empty($formBefore) - ? $commentForm . $commentsThread - : $commentsThread . $commentForm; + ? '
' . $commentForm . $commentsThread . '
' + : '
' . $commentsThread . $commentForm . '
'; if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) { $output .= '
' . print_r($pdoFetch->getTime(), 1) . '
'; @@ -208,4 +208,4 @@ $modx->setPlaceholder($toPlaceholder, $output); } else { return $output; -} \ No newline at end of file +} From 95bdbdec6ceca1d8ef7333cb7903547f526ec5f5 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Oct 2016 15:32:56 +0300 Subject: [PATCH 2/5] Multiple comment threads Added ability to create multiple comment thread on one page. --- assets/components/tickets/js/web/default.js | 114 ++++++++++++-------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/assets/components/tickets/js/web/default.js b/assets/components/tickets/js/web/default.js index fe10274a..12128a8e 100644 --- a/assets/components/tickets/js/web/default.js +++ b/assets/components/tickets/js/web/default.js @@ -25,7 +25,13 @@ var Tickets = { return false; }); $(document).on('change', '#comments-subscribe', function () { - Tickets.comment.subscribe($('[name="thread"]', $('#comment-form'))); + var thread_id = $(this).parents('.comments-thread').attr('id'); + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+thread_id; + } + Tickets.comment.subscribe($(thread_id + ' [name="thread"]')); }); $(document).on('change', '#tickets-subscribe', function () { Tickets.ticket.subscribe($(this).data('id')); @@ -36,7 +42,8 @@ var Tickets = { return false; }); $(document).on('submit', '#comment-form', function (e) { - Tickets.comment.save(this, $(this).find('[type="submit"]')[0]); + var thread_id = $(this).parents('.comments-thread').attr('id'); + Tickets.comment.save(this, $(this).find('[type="submit"]')[0], thread_id); e.preventDefault(); return false; }); @@ -52,11 +59,12 @@ var Tickets = { return false; }); $(document).on('click touchend', '#comment-form .preview, #comment-form .submit', function (e) { + var thread_id = $(this).parents('.comments-thread').attr('id'); if ($(this).hasClass('preview')) { - Tickets.comment.preview(this.form, this); + Tickets.comment.preview(this.form, this, thread_id); } else { - Tickets.comment.save(this.form, this); + Tickets.comment.save(this.form, this, thread_id); } e.preventDefault(); return false; @@ -271,7 +279,12 @@ var Tickets = { }, comment: { - preview: function (form, button) { + preview: function(form,button, thread_id) { + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+thread_id; + } $(form).ajaxSubmit({ data: {action: 'comment/preview'}, url: TicketsConfig.actionUrl, @@ -285,7 +298,7 @@ var Tickets = { success: function (response) { $(button).removeAttr('disabled'); if (response.success) { - $('#comment-preview-placeholder').html(response.data.preview).show(); + $(thread_id+' #comment-preview-placeholder').html(response.data.preview).show(); prettyPrint(); } else { @@ -296,7 +309,12 @@ var Tickets = { return false; }, - save: function (form, button) { + save: function(form, button, thread_id) { + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+thread_id; + } $(form).ajaxSubmit({ data: {action: 'comment/save'}, url: TicketsConfig.actionUrl, @@ -313,20 +331,20 @@ var Tickets = { $(button).removeAttr('disabled'); if (response.success) { Tickets.forms.comment(false); - $('#comment-preview-placeholder').html('').hide(); - $('#comment-editor', form).val(''); - $('.ticket-comment .comment-reply a').show(); + $(thread_id+' #comment-preview-placeholder').html('').hide(); + $(thread_id+' #comment-editor',form).val(''); + $(thread_id+' .ticket-comment .comment-reply a').show(); // autoPublish = 0 if (!response.data.length && response.message) { Tickets.Message.info(response.message); } else { - Tickets.comment.insert(response.data.comment); + Tickets.comment.insert(thread_id, response.data.comment); Tickets.utils.goto($(response.data.comment).attr('id')); } - Tickets.comment.getlist(); + Tickets.comment.getlist(thread_id); prettyPrint(); } else { @@ -350,36 +368,36 @@ var Tickets = { } } if (response.data.captcha) { - $('input[name="captcha"]', form).val('').focus(); - $('#comment-captcha', form).text(response.data.captcha); + $(thread_id+' input[name="captcha"]', form).val('').focus(); + $(thread_id+' #comment-captcha', form).text(response.data.captcha); } } }); return false; }, - getlist: function () { - var form = $('#comment-form'); + getlist: function(thread_id) { + var form = $(thread_id + ' #comment-form'); var thread = $('[name="thread"]', form); if (!thread) { return false; } - Tickets.tpanel.start(); + Tickets.tpanel.start(thread_id); $.post(TicketsConfig.actionUrl, {action: 'comment/getlist', thread: thread.val()}, function (response) { for (var k in response.data.comments) { if (response.data.comments.hasOwnProperty(k)) { - Tickets.comment.insert(response.data.comments[k], true); + Tickets.comment.insert(thread_id, response.data.comments[k], true); } } - var count = $('.ticket-comment').length; - $('#comment-total, .ticket-comments-count').text(count); + var count = $(thread_id + ' .ticket-comment').size(); + $(thread_id + ' #comment-total, '+ thread_id + ' .ticket-comments-count').text(count); - Tickets.tpanel.stop(); + Tickets.tpanel.stop(thread_id); }, 'json'); return true; }, - insert: function (data, remove) { + insert: function(thread_id, data, remove) { var comment = $(data); var parent = $(comment).attr('data-parent'); var id = $(comment).attr('id'); @@ -401,13 +419,13 @@ var Tickets = { } if (parent == 0 && TicketsConfig.formBefore) { - $('#comments').prepend(data) + $(thread_id + ' #comments').prepend(data) } else if (parent == 0) { - $('#comments').append(data) + $(thread_id + ' #comments').append(data) } else { - var pcomm = $('#comment-' + parent); + var pcomm = $(thread_id + ' #comment-'+parent); if (pcomm.data('parent') != pcomm.data('newparent')) { parent = pcomm.data('newparent'); comment.attr('data-newparent', parent); @@ -421,7 +439,7 @@ var Tickets = { data = comment[0].outerHTML; } } - $('#comment-' + parent + ' > .comments-list').append(data); + $(thread_id + ' #comment-'+parent+' > .comments-list').append(data); } if (children.length > 0) { @@ -743,23 +761,29 @@ Tickets.Star = { Tickets.tpanel = { - wrapper: $('#comments-tpanel'), - refresh: $('#tpanel-refresh'), - new_comments: $('#tpanel-new'), + wrapper: '.comments-thread #comments-tpanel', + refresh: '.comments-thread #tpanel-refresh', + new_comments: '.comments-thread #tpanel-new', class_new: 'ticket-comment-new', initialize: function () { if (TicketsConfig.tpanel) { - this.wrapper.show(); + $(this.wrapper).show(); this.stop(); } - this.refresh.on('click', function () { + $(this.refresh).on('click', function() { + var thread_id = $(this).parents('.comments-thread ').attr('id'); + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+thread_id; + } $('.' + Tickets.tpanel.class_new).removeClass(Tickets.tpanel.class_new); - Tickets.comment.getlist(); + Tickets.comment.getlist(thread_id); }); - this.new_comments.on('click', function () { + $(this.new_comments).on('click', function() { var elem = $('.' + Tickets.tpanel.class_new + ':first'); $('html, body').animate({ scrollTop: elem.offset().top @@ -767,29 +791,35 @@ Tickets.tpanel = { elem.removeClass(Tickets.tpanel.class_new); }); - var count = parseInt(Tickets.tpanel.new_comments.text()); + var count = parseInt($(this).text()); if (count > 1) { - Tickets.tpanel.new_comments.text(count - 1); + $(this).text(count - 1); } else { - Tickets.tpanel.new_comments.text('').hide(); + $(this).text('').hide(); } }); }, - start: function () { - this.refresh.addClass('loading'); + start: function(thread_id) { + if (thread_id === undefined){ + thread_id = ''; + } + $(thread_id + '' + this.refresh).addClass('loading'); }, - stop: function () { + stop: function(thread_id) { + if (thread_id === undefined){ + thread_id = ''; + } var count = $('.' + this.class_new).length; if (count > 0) { - this.new_comments.text(count).show(); + $(thread_id + '' + this.new_comments).text(count).show(); } else { - this.new_comments.hide(); + $(thread_id + '' + this.new_comments).hide(); } - this.refresh.removeClass('loading'); + $(thread_id + '' + this.refresh).removeClass('loading'); } }; From 7114427036debe25d2217a370265d1d4def8b566 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 13 Oct 2016 17:52:57 +0300 Subject: [PATCH 3/5] Fixed own error at file start --- core/components/tickets/elements/snippets/snippet.comments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/components/tickets/elements/snippets/snippet.comments.php b/core/components/tickets/elements/snippets/snippet.comments.php index 9a653574..b8f3a8e2 100644 --- a/core/components/tickets/elements/snippets/snippet.comments.php +++ b/core/components/tickets/elements/snippets/snippet.comments.php @@ -1,4 +1,4 @@ -? '
' . $commentForm . $commentsThread . '
'getOption('thread', $scriptProperties, 'resource-' . $modx->resource->id, From 91823178a7b56b52dbc88663edf6636b963aade6 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 21 Oct 2016 12:13:11 +0300 Subject: [PATCH 4/5] Comment save\preview fix Users with empty 'fullname' field couldn't send comments - ['name'] was empty. --- core/components/tickets/model/tickets/tickets.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/components/tickets/model/tickets/tickets.class.php b/core/components/tickets/model/tickets/tickets.class.php index a7d2a58f..ab6b7418 100644 --- a/core/components/tickets/model/tickets/tickets.class.php +++ b/core/components/tickets/model/tickets/tickets.class.php @@ -399,7 +399,7 @@ public function previewComment($data = array()) /** @var modUser $user */ if ($this->authenticated && $user = $this->modx->getObject('modUser', $this->modx->user->id)) { - $comment['name'] = $this->modx->user->Profile->fullname; + $comment['name'] = ($this->modx->user->Profile->fullname == '') ? $this->modx->user->username : $this->modx->user->Profile->fullname; $comment['email'] = $this->modx->user->Profile->email; /** @var modUserProfile $profile */ $profile = $this->modx->user->Profile; @@ -439,7 +439,7 @@ public function saveComment($data = array()) : true; if ($this->authenticated) { if (empty($data['name'])) { - $data['name'] = $this->modx->user->Profile->get('fullname'); + $data['name'] = ($this->modx->user->Profile->get('fullname') == '') ? $this->modx->user->get('username') : $this->modx->user->Profile->get('fullname'); } $data['email'] = $this->modx->user->Profile->get('email'); } else { From 0c52775e77086e7c8a168a2c7cd6eccb76e5923a Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 21 Oct 2016 12:51:20 +0300 Subject: [PATCH 5/5] Multiple comment thread fixes Fixed bugs with text editor's show\hide on comment answer\change\create --- assets/components/tickets/js/web/default.js | 92 +++++++++++++++------ 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/assets/components/tickets/js/web/default.js b/assets/components/tickets/js/web/default.js index 12128a8e..d2aabda9 100644 --- a/assets/components/tickets/js/web/default.js +++ b/assets/components/tickets/js/web/default.js @@ -82,7 +82,13 @@ var Tickets = { }); // Show and hide forms $(document).on('click touchend', '#comment-new-link a', function (e) { - Tickets.forms.comment(); + var thread_id = $(this).parents('.comments-thread').attr('id'); + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } + Tickets.forms.comment(thread_id); e.preventDefault(); return false; }); @@ -143,22 +149,31 @@ var Tickets = { $('#ticket-editor').markItUp(TicketsConfig.editor.ticket); } if (TicketsConfig.enable_editor == true) { - $('#comment-editor').markItUp(TicketsConfig.editor.comment); + $('.comments-thread').each(function(){ + $(this).find('#comment-editor').markItUp(TicketsConfig.editor.comment); + }) } $.jGrowl.defaults.closerTemplate = '
[ ' + TicketsConfig.close_all_message + ' ]
'; - var count = $('.ticket-comment').length; - $('#comment-total, .ticket-comments-count').text(count); + $('.comments-thread').each(function(){ + var count = $(this).find('.ticket-comment').size(); + $(this).find('#comment-total, .ticket-comments-count').text(count); + }) $("#ticketForm.create").sisyphus({ excludeFields: $('#ticketForm .disable-sisyphus') }); + $('.comments-thread').each(function(){ // Auto hide new comment button - if ($('#comment-form').is(':visible')) { - $('#comment-new-link').hide(); - } + if ($(this).find('#comment-form').is(':visible')) { + $(this).find('#comment-new-link').hide(); + } else { + $(this).find('#comment-form').hide(); + $(this).find('#comment-new-link').show(); + } + }) }); // Link to parent comment @@ -283,7 +298,7 @@ var Tickets = { if (thread_id === undefined){ thread_id = ''; } else { - thread_id = '#'+thread_id; + thread_id = '#'+String(thread_id).replace('#', ''); } $(form).ajaxSubmit({ data: {action: 'comment/preview'}, @@ -313,7 +328,7 @@ var Tickets = { if (thread_id === undefined){ thread_id = ''; } else { - thread_id = '#'+thread_id; + thread_id = '#'+String(thread_id).replace('#', ''); } $(form).ajaxSubmit({ data: {action: 'comment/save'}, @@ -330,7 +345,7 @@ var Tickets = { success: function (response) { $(button).removeAttr('disabled'); if (response.success) { - Tickets.forms.comment(false); + Tickets.forms.comment(thread_id, false); $(thread_id+' #comment-preview-placeholder').html('').hide(); $(thread_id+' #comment-editor',form).val(''); $(thread_id+' .ticket-comment .comment-reply a').show(); @@ -377,6 +392,11 @@ var Tickets = { }, getlist: function(thread_id) { + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } var form = $(thread_id + ' #comment-form'); var thread = $('[name="thread"]', form); if (!thread) { @@ -398,6 +418,11 @@ var Tickets = { }, insert: function(thread_id, data, remove) { + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } var comment = $(data); var parent = $(comment).attr('data-parent'); var id = $(comment).attr('id'); @@ -466,14 +491,20 @@ var Tickets = { forms: { reply: function (comment_id) { - $('#comment-new-link').show(); + var thread_id = $('#comment-'+comment_id).parents('.comments-thread ').attr('id'); + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } + $(thread_id + ' #comment-new-link').show(); clearInterval(window.timer); - var form = $('#comment-form'); + var form = $(thread_id + ' #comment-form'); $('.time', form).text(''); - $('.ticket-comment .comment-reply a').show(); + $(thread_id + ' .ticket-comment .comment-reply a').show(); - $('#comment-preview-placeholder').hide(); + $(thread_id + ' #comment-preview-placeholder').hide(); $('input[name="parent"]', form).val(comment_id); $('input[name="id"]', form).val(0); @@ -487,21 +518,26 @@ var Tickets = { }, comment: function (focus) { + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } if (focus !== false) { focus = true; } clearInterval(window.timer); - $('#comment-new-link').hide(); + $(thread_id).find('#comment-new-link').hide(); - var form = $('#comment-form'); + var form = $(thread_id).find('#comment-form'); $('.time', form).text(''); - $('.ticket-comment .comment-reply a:hidden').show(); + $(thread_id).find('.ticket-comment .comment-reply a:hidden').show(); - $('#comment-preview-placeholder').hide(); + $(thread_id).find('#comment-preview-placeholder').hide(); $('input[name="parent"]', form).val(0); $('input[name="id"]', form).val(0); - $(form).insertAfter('#comment-form-placeholder').show(); + $(form).insertAfter(thread_id + ' #comment-form-placeholder').show(); $('#comment-editor', form).val(''); if (focus) { @@ -511,7 +547,13 @@ var Tickets = { }, edit: function (comment_id) { - $('#comment-new-link').show(); + var thread_id = $('#comment-'+comment_id).parents('.comments-thread ').attr('id'); + if (thread_id === undefined){ + thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); + } + $(thread_id).find('#comment-new-link').show(); var thread = $('#comment-form [name="thread"]').val(); $.post(TicketsConfig.actionUrl, { @@ -524,9 +566,9 @@ var Tickets = { } else { clearInterval(window.timer); - $('.ticket-comment .comment-reply a:hidden').show(); - var form = $('#comment-form'); - $('#comment-preview-placeholder').hide(); + $(thread_id + ' .ticket-comment .comment-reply a:hidden').show(); + var form = $(thread_id + ' #comment-form'); + $(thread_id + ' #comment-preview-placeholder').hide(); $('input[name="parent"]', form).val(0); $('input[name="id"]', form).val(comment_id); @@ -777,7 +819,7 @@ Tickets.tpanel = { if (thread_id === undefined){ thread_id = ''; } else { - thread_id = '#'+thread_id; + thread_id = '#'+String(thread_id).replace('#', ''); } $('.' + Tickets.tpanel.class_new).removeClass(Tickets.tpanel.class_new); Tickets.comment.getlist(thread_id); @@ -804,6 +846,8 @@ Tickets.tpanel = { start: function(thread_id) { if (thread_id === undefined){ thread_id = ''; + } else { + thread_id = '#'+String(thread_id).replace('#', ''); } $(thread_id + '' + this.refresh).addClass('loading'); },