From c3ea0d333e869975e49bf6da4165c9e0f6b0d2fa Mon Sep 17 00:00:00 2001 From: Abijeet Date: Thu, 27 Apr 2017 02:35:29 +0530 Subject: [PATCH] #47 - Adds functionality to display child comments. Also has some code towards the reply functionality. --- app/Comment.php | 11 +++-- app/Http/Controllers/CommentController.php | 6 ++- app/Repos/CommentRepo.php | 17 ++++--- gulpfile.js | 2 + resources/assets/js/controllers.js | 18 +++++-- resources/assets/js/directives.js | 47 +++++++++++++++++++ resources/views/comments/add.blade.php | 6 +-- .../views/comments/comment-reply.blade.php | 10 ++++ resources/views/comments/comments.blade.php | 19 ++++++++ resources/views/comments/list-item.blade.php | 15 ++++-- resources/views/pages/comments.blade.php | 8 ---- resources/views/pages/show.blade.php | 2 +- 12 files changed, 127 insertions(+), 34 deletions(-) create mode 100644 resources/views/comments/comment-reply.blade.php create mode 100644 resources/views/comments/comments.blade.php delete mode 100644 resources/views/pages/comments.blade.php diff --git a/app/Comment.php b/app/Comment.php index 4a9c48c74..74fcc3fdc 100644 --- a/app/Comment.php +++ b/app/Comment.php @@ -34,8 +34,8 @@ class Comment extends Ownable return $this->belongsTo(User::class); } - public function getParentCommentsByPage($pageId, $pageNum = 0, $limit = 0) { - $data = ['pageId' => $pageId]; + public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) { + $query = static::newQuery(); $query->join('users AS u', 'comments.created_by', '=', 'u.id'); $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id'); @@ -44,7 +44,12 @@ class Comment extends Ownable . 'u.name AS created_by_name, u1.name AS updated_by_name, ' . '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ', [$pageId]); - $query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]); + + if (empty($commentId)) { + $query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]); + } else { + $query->whereRaw('page_id = ? AND parent_id = ?', [$pageId, $commentId]); + } $query->orderBy('created_at'); return $query; } diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 3df441766..b8cf77621 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -85,7 +85,11 @@ class CommentController extends Controller $this->checkOwnablePermission('page-view', $page); $comments = $this->commentRepo->getCommentsForPage($pageId, $commentId); - + if (empty($commentId)) { + // requesting for parent level comments, send the total count as well. + $totalComments = $this->commentRepo->getCommentCount($pageId); + return response()->json(array('success' => true, 'comments'=> $comments, 'total' => $totalComments)); + } return response()->json(array('success' => true, 'comments'=> $comments)); } } diff --git a/app/Repos/CommentRepo.php b/app/Repos/CommentRepo.php index c2a19ec0b..ba34617ed 100644 --- a/app/Repos/CommentRepo.php +++ b/app/Repos/CommentRepo.php @@ -38,14 +38,13 @@ class CommentRepo { return $comment; } - public function getCommentsForPage($pageId, $commentId, $count = 20) { - if (empty($commentId)) { - // requesting parent comments - $query = $this->comment->getParentCommentsByPage($pageId); - return $query->paginate($count); - } else { - // requesting the child comments. - return Comment::whereRaw("page_id = $pageId AND parent_id = $commentId")->get(); - } + public function getCommentsForPage($pageId, $commentId, $count = 20) { + // requesting parent comments + $query = $this->comment->getCommentsByPage($pageId, $commentId); + return $query->paginate($count); + } + + public function getCommentCount($pageId) { + return $this->comment->where('page_id', '=', $pageId)->count(); } } \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index b72bb366d..580db00cc 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,3 +1,5 @@ +'use strict'; + const argv = require('yargs').argv; const gulp = require('gulp'), plumber = require('gulp-plumber'); diff --git a/resources/assets/js/controllers.js b/resources/assets/js/controllers.js index 65dc50e99..d92c5538b 100644 --- a/resources/assets/js/controllers.js +++ b/resources/assets/js/controllers.js @@ -731,14 +731,14 @@ module.exports = function (ngApp, events) { } $timeout(function() { - console.log($scope.pageId); $http.get(window.baseUrl(`/ajax/page/${$scope.pageId}/comments/`)).then(resp => { if (!resp.data || resp.data.success !== true) { // TODO : Handle error return; - } + } vm.comments = resp.data.comments.data; - vm.totalComments = resp.data.comments.total; + vm.totalComments = resp.data.total; + // TODO : Fetch message from translate. if (vm.totalComments === 0) { vm.totalCommentsStr = 'No comments found.'; } else if (vm.totalComments === 1) { @@ -749,6 +749,18 @@ module.exports = function (ngApp, events) { }, checkError('app')); }); + vm.loadSubComments = function(event, comment) { + event.preventDefault(); + $http.get(window.baseUrl(`/ajax/page/${$scope.pageId}/comments/${comment.id}/sub-comments`)).then(resp => { + console.log(resp); + if (!resp.data || resp.data.success !== true) { + return; + } + comment.is_loaded = true; + comment.comments = resp.data.comments.data; + }, checkError('app')); + }; + function checkError(errorGroupName) { $scope.errors[errorGroupName] = {}; return function(response) { diff --git a/resources/assets/js/directives.js b/resources/assets/js/directives.js index f30a09778..d119d2e92 100644 --- a/resources/assets/js/directives.js +++ b/resources/assets/js/directives.js @@ -865,5 +865,52 @@ module.exports = function (ngApp, events) { } } }]); + + ngApp.directive('commentReply', ['$timeout', function ($timeout) { + return { + restrict: 'E', + templateUrl: 'comment-reply.html', + scope: { + + }, + link: function (scope, element, attr) { + + } + } + + }]); + ngApp.directive('commentReplyLink', ['$document', '$compile', function ($document, $compile) { + return { + link: function (scope, element, attr) { + element.on('$destroy', function () { + element.off('click'); + scope.$destroy(); + }); + + element.on('click', function () { + var $container = element.parents('.comment-box').first(); + if (!$container.length) { + console.error('commentReplyLink directive should be placed inside a container with class comment-box!'); + return; + } + if (attr.noCommentReplyDupe) { + removeDupe(); + } + var compiledHTML = $compile('')(scope); + $container.append(compiledHTML); + }); + } + }; + + + function removeDupe() { + let $existingElement = $document.find('comment-reply'); + if (!$existingElement.length) { + return; + } + + $existingElement.remove(); + } + }]); }; diff --git a/resources/views/comments/add.blade.php b/resources/views/comments/add.blade.php index f7d9c41fc..c221cdbd4 100644 --- a/resources/views/comments/add.blade.php +++ b/resources/views/comments/add.blade.php @@ -1,8 +1,4 @@ -@section('head') - -@stop - -
+
+
+ + +
\ No newline at end of file diff --git a/resources/views/comments/comments.blade.php b/resources/views/comments/comments.blade.php new file mode 100644 index 000000000..107052e87 --- /dev/null +++ b/resources/views/comments/comments.blade.php @@ -0,0 +1,19 @@ +@section('head') + +@stop + + +
+

@{{vm.totalCommentsStr}}

+
+
+
+ +
+
+
+@include('comments/add', ['pageId' => $pageId]) \ No newline at end of file diff --git a/resources/views/comments/list-item.blade.php b/resources/views/comments/list-item.blade.php index 1a72d7e84..aecc0c26b 100644 --- a/resources/views/comments/list-item.blade.php +++ b/resources/views/comments/list-item.blade.php @@ -1,19 +1,26 @@
- user avatar + user avatar
\ No newline at end of file diff --git a/resources/views/pages/comments.blade.php b/resources/views/pages/comments.blade.php deleted file mode 100644 index ea9dd57d6..000000000 --- a/resources/views/pages/comments.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -
-

@{{vm.totalCommentsStr}}

-
-
- @include('comments/list-item') -
-
-@include('comments/add', ['pageId' => $pageId]) diff --git a/resources/views/pages/show.blade.php b/resources/views/pages/show.blade.php index 244a47c02..db1d1e5cd 100644 --- a/resources/views/pages/show.blade.php +++ b/resources/views/pages/show.blade.php @@ -113,7 +113,7 @@
- @include('pages/comments', ['pageId' => $page->id]) + @include('comments/comments', ['pageId' => $page->id])