]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/page-comments.js
Added code for permissions, removed unnecessary code.
[bookstack] / resources / assets / js / vues / page-comments.js
1 const comment = require('./components/comments/comment');
2 const commentReply = require('./components/comments/comment-reply');
3
4 let data = {
5   totalCommentsStr: trans('entities.comments_loading'),
6   comments: [],
7   permissions: null,
8   currentUserId: null,
9   trans: trans,
10   commentCount: 0
11 };
12
13 let methods = {
14   commentAdded: function () {
15     ++this.totalComments;
16   }
17 }
18
19 let computed = {
20   totalComments: {
21     get: function () {
22       return this.commentCount;
23     },
24     set: function (value) {
25       this.commentCount = value;
26       if (value === 0) {
27         this.totalCommentsStr = trans('entities.no_comments');
28       } else if (value === 1) {
29         this.totalCommentsStr = trans('entities.one_comment');
30       } else {
31         this.totalCommentsStr = trans('entities.x_comments', {
32             numComments: value
33         });
34       }
35     }
36   },
37   canComment: function () {
38     if (!this.permissions) {
39       return false;
40     }
41     return this.permissions.comment_create === true;
42   }
43 }
44
45 function mounted() {
46   this.pageId = Number(this.$el.getAttribute('page-id'));
47   // let linkedCommentId = this.$route.query.cm;
48   let linkedCommentId = null;
49   this.$http.get(window.baseUrl(`/ajax/page/${this.pageId}/comments/`)).then(resp => {
50     if (!isCommentOpSuccess(resp)) {
51         // just show that no comments are available.
52         vm.totalComments = 0;
53         return;
54     }
55     this.comments = resp.data.comments;
56     this.totalComments = +resp.data.total;
57     this.permissions = resp.data.permissions;
58     this.currentUserId = resp.data.user_id;
59     if (!linkedCommentId) {
60         return;
61     }
62     $timeout(function() {
63         // wait for the UI to render.
64         focusLinkedComment(linkedCommentId);
65     });
66   }, checkError('errors.comment_list'));
67 }
68
69 function isCommentOpSuccess(resp) {
70   if (resp && resp.data && resp.data.status === 'success') {
71       return true;
72   }
73   return false;
74 }
75
76 function checkError(msgKey) {
77   return function(response) {
78     let msg = null;
79     if (isCommentOpSuccess(response)) {
80         // all good
81         return;
82     } else if (response.data) {
83         msg = response.data.message;
84     } else {
85         msg = trans(msgKey);
86     }
87     if (msg) {
88         events.emit('success', msg);
89     }
90   }
91 }
92
93 function created () {
94   this.$on('new-comment', function (event, comment) {
95     this.comments.push(comment);
96   })
97 }
98
99 function beforeDestroy() {
100   this.$off('new-comment');
101 }
102
103 module.exports = {
104   data, methods, mounted, computed, components : {
105     comment, commentReply
106   },
107   created, beforeDestroy
108 };