]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/page-comments.js
76d11ac6c4a6d520bff13cfb443b18cb28d434d8
[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             this.$events.emit('error', getErrorMsg(resp));
54             return;
55         }
56         this.comments = resp.data.comments;
57         this.totalComments = +resp.data.total;
58         this.permissions = resp.data.permissions;
59         this.currentUserId = resp.data.user_id;
60         if (!linkedCommentId) {
61             return;
62         }
63         focusLinkedComment(linkedCommentId);
64     }).catch(err => {
65         this.$events.emit('error', 'errors.comment_list');
66     });
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 getErrorMsg(response) {
77     if (response.data) {
78         return response.data.message;
79     } else {
80         return trans('errors.comment_add');
81     }
82 }
83
84 function created() {
85     this.$on('new-comment', function (event, comment) {
86         this.comments.push(comment);
87     })
88 }
89
90 function beforeDestroy() {
91     this.$off('new-comment');
92 }
93
94 module.exports = {
95     data, methods, mounted, computed, components: {
96         comment, commentReply
97     },
98     created, beforeDestroy
99 };