Dynamically load a response
authorAlexander Ebert <ebert@woltlab.com>
Mon, 6 Mar 2017 10:56:05 +0000 (11:56 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 6 Mar 2017 10:56:05 +0000 (11:56 +0100)
See #2231

wcfsetup/install/files/js/WCF.Comment.js
wcfsetup/install/files/lib/data/comment/CommentAction.class.php
wcfsetup/install/files/style/ui/comment.scss

index bb446f9fe0cf16170a3332aeaabcdb7b5db69e1d..da8751670c5013f51965448f0f4c48885528bb84 100644 (file)
@@ -757,6 +757,10 @@ WCF.Comment.Handler = Class.extend({
                                this._insertComments(data);
                        break;
                        
+                       case 'loadResponse':
+                               this._insertResponse(data);
+                               break;
+                       
                        case 'loadResponses':
                                this._insertResponses(data);
                        break;
@@ -781,7 +785,9 @@ WCF.Comment.Handler = Class.extend({
                if (data.returnValues.template === '') {
                        // comment id is invalid or there is a mismatch, silently ignore it
                        return;
-               }       
+               }
+               
+               // TODO: handle 'response' value
                
                $(data.returnValues.template).insertBefore(this._permalinkComment);
                var comment = this._permalinkComment.previousElementSibling;
@@ -793,7 +799,26 @@ WCF.Comment.Handler = Class.extend({
                //noinspection BadExpressionStatementJS
                comment.offsetTop;
                
-               comment.classList.add('fadeIn');
+               comment.classList.add('commentHighlightTarget');
+       },
+       
+       _insertResponse: function(data) {
+               if (data.returnValues.template === '') {
+                       // comment id is invalid or there is a mismatch, silently ignore it
+                       return;
+               }
+               
+               $(data.returnValues.template).insertBefore(this._permalinkResponse);
+               var response = this._permalinkResponse.previousElementSibling;
+               response.classList.add('commentResponsePermalinkContainer');
+               
+               elRemove(this._permalinkResponse);
+               this._permalinkResponse = response;
+               
+               //noinspection BadExpressionStatementJS
+               response.offsetTop;
+               
+               response.classList.add('commentHighlightTarget');
        },
        
        /**
index 4928ae4a2a848663b82cf018566149eaca458a76..f56f34c7b9cee8b89aba5e49d0ba5350a2f8cf37 100644 (file)
@@ -44,7 +44,7 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
        /**
         * @inheritDoc
         */
-       protected $allowGuestAccess = ['addComment', 'addResponse', 'loadComment', 'loadComments', 'getGuestDialog'];
+       protected $allowGuestAccess = ['addComment', 'addResponse', 'loadComment', 'loadComments', 'loadResponse', 'getGuestDialog'];
        
        /**
         * captcha object type used for comments
@@ -202,6 +202,7 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
        
        public function validateLoadComment() {
                $this->readInteger('objectID', false, 'data');
+               $this->readInteger('responseID', true, 'data');
                
                try {
                        $this->comment = $this->getSingleObject()->getDecoratedObject();
@@ -215,6 +216,13 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
                if (!$this->commentProcessor->isAccessible($this->parameters['data']['objectID'])) {
                        throw new PermissionDeniedException();
                }
+               
+               if (!empty($this->parameters['data']['responseID'])) {
+                       $this->response = new CommentResponse($this->parameters['data']['responseID']);
+                       if (!$this->response->responseID) {
+                               $this->response = null;
+                       }
+               }
        }
        
        public function loadComment() {
@@ -225,8 +233,24 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
                        return ['template' => ''];
                }
                
+               $returnValues = $this->renderComment($this->comment, $this->response);
+               return (is_array($returnValues)) ? $returnValues : ['template' => $returnValues];
+       }
+       
+       public function validateLoadResponse() {
+               $this->validateLoadComment();
+       }
+       
+       public function loadResponse() {
+               if ($this->comment === null || $this->response === null) {
+                       return ['template' => ''];
+               }
+               else if ($this->comment->objectTypeID != $this->parameters['data']['objectTypeID'] || $this->comment->objectID != $this->parameters['data']['objectID']) {
+                       return ['template' => ''];
+               }
+               
                return [
-                       'template' => $this->renderComment($this->comment)
+                       'template' => $this->renderResponse($this->response)
                ];
        }
        
@@ -746,18 +770,39 @@ class CommentAction extends AbstractDatabaseObjectAction implements IMessageInli
         * Renders a comment.
         * 
         * @param       Comment         $comment
-        * @return      string
+        * @param       CommentResponse $response
+        * @return      string|string[]
         */
-       protected function renderComment(Comment $comment) {
+       protected function renderComment(Comment $comment, CommentResponse $response = null) {
                $comment = new StructuredComment($comment);
                $comment->setIsDeletable($this->commentProcessor->canDeleteComment($comment->getDecoratedObject()));
                $comment->setIsEditable($this->commentProcessor->canEditComment($comment->getDecoratedObject()));
                
+               if ($response !== null) {
+                       // check if response is not visible
+                       /** @var CommentResponse $visibleResponse */
+                       foreach ($comment as $visibleResponse) {
+                               if ($visibleResponse->responseID == $response->responseID) {
+                                       $response = null;
+                                       break;
+                               }
+                       }
+               }
+               
                WCF::getTPL()->assign([
                        'commentList' => [$comment],
                        'commentManager' => $this->commentProcessor
                ]);
-               return WCF::getTPL()->fetch('commentList');
+               
+               $template = WCF::getTPL()->fetch('commentList');
+               if ($response === null) {
+                       return $template;
+               }
+               
+               return [
+                       'template' => $template,
+                       'response' => $this->renderResponse($response)
+               ];
        }
        
        /**
index 30429019c1498e0873185f6ffcef016345239990..c39e365f1786ba874bca7fc35d95f1f36c3bae02 100644 (file)
        }
 }
 
-.comment.commentHighlightTarget .commentContent:not(.commentResponseContent) {
+.comment.commentHighlightTarget .commentContent:not(.commentResponseContent),
+.commentResponse.commentHighlightTarget .commentResponseContent {
        animation: wcfCommentHighlight .96s linear;
 }