4 commentTree.run = function (params) {
5 /* define exotic parameters */
7 var userConfig = params.userConfig || {};
8 var siteConfig = params.siteConfig || {};
9 var pageInfo = params.pageInfo || {};
10 var user = params.user || {};
13 console.log('error in commentTree.run(): no element given');
18 * register <comment-tree>
20 Vue.component('comment-tree', {
21 template: '#comment-tree-template',
27 commentAllowed: pageInfo.comment_allowed,
28 archived: pageInfo.archived,
29 discussionId: pageInfo.discussion_id,
31 parentId: pageInfo.parent_id || 0,
34 created: function commentTreeCreated() {
35 newslash.getComments(this.discussionId, this.parentId).then(
36 (resp) => { // success
37 var comments = resp.comments;
40 var commentIndex = {};
41 comments.forEach(comment => {
42 comment._children = [];
43 commentIndex[comment.cid] = comment;
44 if (comment.pid && commentIndex[comment.pid]) {
45 commentIndex[comment.pid]._children.push(comment);
48 this.comments = resp.comments;
51 statusIndicator.error("comment_loading_error");
58 * register <comment-header>
60 Vue.component('comment-header', {
61 template: '#comment-header-template',
64 config: userConfig || {},
65 savable: user.is_login || 0,
66 commentAllowed: pageInfo.comment_allowed,
67 archived: pageInfo.archived,
72 saveConfig: function saveConfig() {
75 config: this.$data.config,
77 statusIndicator.loading("saving");
78 this.$newslash.post('user', data,
79 (response) => { // success
80 statusIndicator.done("saved");
82 (response) => { // fail
83 statusIndicator.error("save_failed");
93 Vue.component('comment', {
94 template: '#comment-template',
97 modReasons: siteConfig.modReasons,
100 metamoderateMessage: '',
105 showShareButtons: false,
108 commentAllowed: pageInfo.comment_allowed,
109 archived: pageInfo.archived || false,
111 isFormVisible: false,
116 preview: { type: Boolean, default: false },
118 created: function () {
119 if (this.comment._displayForce) {
120 this.displayForce = true;
124 commentLocalTime: function () {
125 if (this.comment.create_time == "") return "";
126 var dt = newslash.util.decodeMySQLDateTime(this.comment.create_time);
127 return newslash.util.formatToLocalDateTime(dt);
129 isIpidVisible: function () { return user.is_admin; },
130 isM2able: function () { return user.is_login; },
131 signedPoints: function () {
132 var num = this.comment.points;
135 } else if (num > 0) {
140 reasonText: function () {
141 if (this.comment.reason != 0) {
142 return ":" + siteConfig.modReasons[this.comment.reason];
146 isScoreVisible: function () {
147 return !userConfig.comment.hide_score
148 && this.comment.lastmod != 0
149 && this.comment.points != 0;
151 isFoldedComment: function () {
152 return this.comment.points < userConfig.comment.fold_threshold
153 && !this.comment.isPreview
154 && !this.displayForce;
156 isHiddenComment: function () {
157 return this.comment.points < userConfig.comment.show_threshold
158 && !this.comment.isPreview
159 && !this.displayForce;
161 isHiddenBoxMode: function () {
162 if (this.comment.isPreview
164 || this.comment.points >= userConfig.comment.show_threshold) {
167 // 子アイテムの少なくとも1つが表示なら -> 「1件の非表示コメント」表示
168 var count = _countHiddenItems(this.comment);
169 if (count[0] != count[1]) {
174 hiddenCount: function () {
175 // when point >= threshold: hide "hidden" box.
176 if (this.comment.points >= userConfig.comment.show_threshold) {
179 // 子アイテムの少なくとも1つが表示なら -> 「1件の非表示コメント」表示
180 const count = _countHiddenItems(this.comment);
181 if (count[0] != count[1]) {
184 // 子アイテムがすべて非表示なら -> 「n件の非表示コメント」表示
187 isModerateEnabled: function () {
188 return this.comment.cid && (user.is_admin || (user.is_moderator && this.commentAllowed));
190 isReplyEnabled: function () {
191 return this.commentAllowed && !this.comment.isPreview;
193 isSignatureVisible: function () {
194 return !userConfig.comment.hide_signature && this.comment.signature;
198 //isM2able: isM2able,
200 showModInfo: showModInfo,
202 metamoderate: metamoderate,
203 showReplyForm: showReplyForm,
204 hideReplyForm: hideReplyForm,
205 toggleShareButtons: toggleShareButtons,
209 /* methods for comment */
211 function isM2able(mod) {
212 return typeof mod.m2val == 'undefined' ? 1 : 0;
216 function showModInfo() {
217 this.loadingMods = 1;
218 this.displayModInfo = !this.displayModInfo;
219 if (!this.displayModInfo) {
222 // get moderation data
223 //this.$http.get(this.urls.moderate + "?cid=" + this.comment.cid).then(
224 this.$newslash.getModerations(this.comment.cid,
225 (response) => { // success
226 if (response.body.sec_token) {
227 this.secToken = response.body.sec_token;
229 for (var i = 0; i < response.body.moderations.length; i++) {
230 response.body.moderations[i].status = '';
232 if (response.body.moderations.length > 0) {
233 Vue.set(this.comment, 'moderations', response.body.moderations);
235 Vue.set(this.comment, 'moderations', []);
238 this.loadingMods = 0;
240 (response) => { // fail
245 function showReplyForm() {
246 this.isFormVisible = true;
249 function hideReplyForm() {
250 this.isFormVisible = false;
253 function toggleShareButtons() {
254 this.showShareButtons = !this.showShareButtons;
257 function showItem() {
258 this.displayForce = true;
261 function moderate() {
263 reason: Number(this.moderateReason),
264 discussion_id: pageInfo.discussion_id,
265 cid: Number(this.comment.cid),
268 this.loadingMods = 1;
269 this.$http.post(this.urls.moderate, data).then(
270 (response) => { // success
271 this.moderateMessage = "保存しました";
273 (response) => { // fail
274 if (response.body.message) {
275 this.moderateMessage = "エラー:" + response.body.message;
277 this.moderateMessage = "エラー";
283 function metamoderate(mod, value) {
284 if (mod.m2val == 1 || mod.m2val == -1) {
289 moderation_id: mod.id,
290 action: 'metamoderate',
291 sec_token: this.secToken,
294 mod.status = 'm2saving';
295 this.$http.post(this.urls.metamoderate, data).then(
296 (response) => { // success
297 mod.status = 'm2saved';
300 (response) => { // fail
301 mod.status = 'error';
302 if (response.body.message) {
303 console.log(response.body.message);
309 function _countHiddenItems(comment) {
310 if (!comment || !comment._children) {
311 console.log("_countHiddenItems: invalid parameter given");
315 for (var i = 0; i < comment._children.length; i++) {
316 var ret = _countHiddenItems(comment._children[i]);
320 if (comment.points < userConfig.comment.show_threshold) {
327 * register <comment-form>
329 Vue.component('comment-form', {
330 template: '#comment-form-template',
334 isPreviewVisible: false,
343 preview: new Comment(),
351 created: function () {
353 this.rawTitle = "Re: " + this.replyTo.title;
355 this.cancelable = false;
359 isItemFilled: function isItemFilled () { return this.rawTitle.length && this.rawComment.length;}
362 cancelEdit: cancelEdit,
363 doPreview: doPreview,
364 leavePreview: leavePreview,
365 postComment: postComment,
370 function cancelEdit() {
371 this.$emit("finish-edit");
374 function doPreview() {
376 title: this.rawTitle,
377 comment: this.rawComment,
378 discussion_id: pageInfo.discussion_id,
379 stoid: pageInfo.stoid,
380 pid: this.replyTo ? this.replyTo.pid : 0,
383 this.$newslash.post("comment", data, {noCaptcha: 1},
384 (response) => { // success
385 this.preview.title = response.body.title;
386 this.preview.comment = response.body.comment;
387 this.preview.author = response.body.author;
388 this.preview.date = response.body.date;
389 this.preview.signature = response.body.signature;
390 this.csrfToken = response.body.csrf_token || "";
391 this.preview.isPreview = true;
393 this.isFormVisible = false;
394 this.isPreviewVisible = true;
397 (response) => { // fail
398 if (response.body.message) {
399 statusIndicator.error(response.body.message);
405 function leavePreview() {
406 this.isFormVisible = true;
407 this.isPreviewVisible = false;
410 function postComment() {
412 title: this.rawTitle,
413 comment: this.rawComment,
414 discussion_id: pageInfo.discussion_id,
415 stoid: pageInfo.stoid,
418 csrf_token: this.csrfToken,
422 data.pid = this.replyTo.cid;
423 } else if (pageInfo.comment_id) {
424 data.pid = pageInfo.comment_id;
427 this.$newslash.post("comment", data,
428 (response) => { // success
429 var newComment = response.body.new_comment;
430 newComment._children = [];
431 newComment._displayForce = true;
432 //comments.initComment(newComment, this.reply.parent);
434 this.isFormVisible = true;
435 this.isPreviewVisible = false;
438 this.replyTo._children.unshift(newComment);
440 this.target.push(newComment);
442 this.rawTitle = this.replyTo ? ("Re: " + this.replyTo.title) : "";
443 this.rawComment = "";
444 this.$emit("finish-edit");
446 (response) => { // fail
447 if (response.body.message) {
448 statusIndicator.error(response.body.message);
454 this.vm = new Vue({el: params.el});