]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/page-comments.js
Removes some unused 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 = getUrlParameter('cm');
48     this.$http.get(window.baseUrl(`/ajax/page/${this.pageId}/comments/`)).then(resp => {
49         if (!isCommentOpSuccess(resp)) {
50             // just show that no comments are available.
51             vm.totalComments = 0;
52             this.$events.emit('error', getErrorMsg(resp));
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
63         // adding a setTimeout to give the comment list some time to render
64         // before focusing the comment.
65         setTimeout(function() {
66             focusLinkedComment(linkedCommentId);
67         });
68     }).catch(err => {
69         this.$events.emit('error', trans('errors.comment_list'));
70     });
71 }
72
73 function isCommentOpSuccess(resp) {
74     if (resp && resp.data && resp.data.status === 'success') {
75         return true;
76     }
77     return false;
78 }
79
80 function getErrorMsg(response) {
81     if (response.data) {
82         return response.data.message;
83     } else {
84         return trans('errors.comment_add');
85     }
86 }
87
88 function created() {
89     this.$on('new-comment', function (event, comment) {
90         this.comments.push(comment);
91     })
92 }
93
94 function beforeDestroy() {
95     this.$off('new-comment');
96 }
97
98 function getUrlParameter(name) {
99     name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
100     var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
101     var results = regex.exec(location.hash);
102     return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
103 }
104
105 function focusLinkedComment(linkedCommentId) {
106     let comment = document.getElementById(linkedCommentId);
107     if (comment && comment.length !== 0) {
108         window.setupPageShow.goToText(linkedCommentId);
109     }
110 }
111
112 module.exports = {
113     data, methods, mounted, computed, components: {
114         comment, commentReply
115     },
116     created, beforeDestroy
117 };