From fd153265e9b1b425af2e689f14ee616c1c504e9f Mon Sep 17 00:00:00 2001 From: Gordon Anderson Date: Wed, 10 Feb 2016 11:56:14 +0700 Subject: [PATCH] ENHANCEMENT: Ajax spam/ham/approve/delete ressurected. Added confirm box for delete --- code/extensions/CommentsExtension.php | 5 +-- javascript/CommentsInterface.js | 44 ++++++++++++++++----------- javascript/lang/en.js | 10 ++++++ tests/CommentsExtensionTest.php | 4 ++- 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 javascript/lang/en.js diff --git a/code/extensions/CommentsExtension.php b/code/extensions/CommentsExtension.php index 89636b3..403c9b6 100644 --- a/code/extensions/CommentsExtension.php +++ b/code/extensions/CommentsExtension.php @@ -80,7 +80,7 @@ class CommentsExtension extends DataExtension { */ public function populateDefaults() { $defaults = $this->owner->config()->defaults; - + // Set if comments should be enabled by default if(isset($defaults['ProvideComments'])) { $this->owner->ProvideComments = $defaults['ProvideComments']; @@ -292,7 +292,7 @@ class CommentsExtension extends DataExtension { public function getCommentsEnabled() { // Don't display comments form for pseudo-pages (such as the login form) if(!$this->owner->exists()) return false; - + // Determine which flag should be used to determine if this is enabled if($this->owner->getCommentsOption('enabled_cms')) { return $this->owner->ProvideComments; @@ -422,6 +422,7 @@ class CommentsExtension extends DataExtension { Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/lib/jquery.form.js'); Requirements::javascript(COMMENTS_THIRDPARTY . '/jquery-validate/jquery.validate.min.js'); + Requirements::add_i18n_javascript('comments/javascript/lang'); Requirements::javascript('comments/javascript/CommentsInterface.js'); } diff --git a/javascript/CommentsInterface.js b/javascript/CommentsInterface.js index d934db3..255b517 100755 --- a/javascript/CommentsInterface.js +++ b/javascript/CommentsInterface.js @@ -9,7 +9,7 @@ */ $('.comments-holder-container form').entwine({ onmatch: function() { - + // @todo Reinstate preview-comment functionality /** @@ -56,7 +56,7 @@ this._super(); } }); - + /** * Comment reply form */ @@ -74,7 +74,7 @@ this._super(); } }); - + /** * Toggle on/off reply form */ @@ -83,7 +83,7 @@ var allForms = $( ".comment-reply-form-holder" ), formID = $( this ).prop('href').replace(/^[^#]*#/, '#'), form = $(formID).closest('.comment-reply-form-holder'); - + // Prevent focus e.preventDefault(); if(form.is(':visible')) { @@ -94,7 +94,7 @@ } } }); - + /** * Preview comment by fetching it from the server via ajax. @@ -132,16 +132,24 @@ $(':input', form).on('change keydown', function() { previewEl.removeClass('loading').hide(); });*/ - + /** * Clicking one of the metalinks performs the operation via ajax * this inclues the spam and approve links */ - /* @todo Migrate to work with nested comments - commentsList.on('click', '.action-links a', function(e) { + + $('.comments-holder .comments-list').on('click', 'div.comment-moderation-options a', function(e) { var link = $(this); + if (link.hasClass('delete')) { + var confirmationMsg = ss.i18n._t('CommentsInterface_singlecomment_ss.DELETE_CONFIRMATION'); + var confirmation = window.confirm(confirmationMsg); + if (!confirmation) { + e.preventDefault(); + return false; + } + } var comment = link.parents('.comment:first'); - + $.ajax({ url: $(this).attr('href'), cache: false, @@ -149,34 +157,34 @@ if(link.hasClass('ham')) { // comment has been marked as not spam comment.html(html); - comment.removeClass('spam').hide().fadeIn(); + comment.removeClass('spam'); } else if(link.hasClass('approve')) { // comment has been approved comment.html(html); - comment.removeClass('unmoderated').hide().fadeIn(); + comment.removeClass('unmoderated'); } else if(link.hasClass('delete')) { comment.fadeOut(1000, function() { - comment.remove(); - - if(commentsList.children().length == 0) { + comment.remove(); + + if(commentsList.children().length === 0) { noCommentsYet.show(); } }); } else if(link.hasClass('spam')) { - comment.html(html).addClass('spam').hide().fadeIn(); + comment.html(html).addClass('spam'); } }, failure: function(html) { - alert(html) + var errorMsg = ss.i18n._t('CommentsInterface_singlecomment_ss.AJAX_ERROR'); + alert(errorMsg); } }); - + e.preventDefault(); }); - */ /** * Ajax pagination diff --git a/javascript/lang/en.js b/javascript/lang/en.js new file mode 100644 index 0000000..00d6876 --- /dev/null +++ b/javascript/lang/en.js @@ -0,0 +1,10 @@ +// This file was generated by silverstripe/cow from javascript/lang/src/en.js. +// See https://github.com/tractorcow/cow for details +if (typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') { + if (typeof(console) != 'undefined') console.error('Class ss.i18n not defined'); +} else { + ss.i18n.addDictionary('en', { + "CommentsInterface_singlecomment_ss.DELETE_CONFIRMATION": "Are you sure?", + "CommentsInterface_singlecomment_ss.AJAX_ERROR": "An error occurred whilst updating the comment", + }); +} diff --git a/tests/CommentsExtensionTest.php b/tests/CommentsExtensionTest.php index f705189..f08c901 100644 --- a/tests/CommentsExtensionTest.php +++ b/tests/CommentsExtensionTest.php @@ -243,12 +243,14 @@ class CommentsExtensionTest extends SapphireTest { $cf = $item->CommentsForm(); $backend = Requirements::backend(); - $this->assertEquals( + $this->assertEquals( array( 'framework/thirdparty/jquery/jquery.js', 'framework/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js', 'framework/thirdparty/jquery-validate/lib/jquery.form.js', 'comments/thirdparty/jquery-validate/jquery.validate.min.js', + 'framework/javascript/i18n.js', + 'comments/javascript/lang/en.js', 'comments/javascript/CommentsInterface.js' ), $backend->get_javascript()