From 026cb5f32706735b5e944cca848c3403b50de880 Mon Sep 17 00:00:00 2001 From: hylom Date: Tue, 30 Jan 2018 20:28:19 +0900 Subject: [PATCH] change 'page' object to set values in each Controllers --- .../lib/Newslash/Plugin/DiscussionHelper.pm | 12 ++++++- .../lib/Newslash/Web/Controller/Comment.pm | 10 +++++- .../lib/Newslash/Web/Controller/Journal.pm | 5 ++- .../lib/Newslash/Web/Controller/Poll.pm | 6 +++- .../lib/Newslash/Web/Controller/Story.pm | 3 ++ src/newslash_web/public/js/comment-tree.js | 24 ++++++------- src/newslash_web/public/js/vue-newslash.js | 2 +- src/newslash_web/templates/common/js_vars.html.tt2 | 42 +++------------------- 8 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/newslash_web/lib/Newslash/Plugin/DiscussionHelper.pm b/src/newslash_web/lib/Newslash/Plugin/DiscussionHelper.pm index 7391e9cd..2deed7fe 100644 --- a/src/newslash_web/lib/Newslash/Plugin/DiscussionHelper.pm +++ b/src/newslash_web/lib/Newslash/Plugin/DiscussionHelper.pm @@ -7,7 +7,13 @@ sub register { $app->hook(before_render => sub { my ($c, $args) = @_; my $discus = $args->{discussion} || $c->stash('discussion'); - return if !$discus; + + my $page = $args->{page} || {}; + if (!$discus) { + $page->{archived} = 0; + $page->{discussion_id} = 0; + return; + } my $user = $c->stash('user'); my $discussions = $c->model('discussions'); @@ -15,6 +21,10 @@ sub register { uid => $user->{uid}); $c->stash(comment_allowed => $comment_allowed); + $page->{archived} = ($discus->{type} eq 'archived') ? 1 : 0; + $page->{discussion_id} = $discus->{id} || 0; + $page->{comment_allowed} = $comment_allowed; + }); } diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Comment.pm b/src/newslash_web/lib/Newslash/Web/Controller/Comment.pm index d8542fb2..52b517bc 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/Comment.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/Comment.pm @@ -47,7 +47,15 @@ sub single { } my $page = { content_type => "comment", - type => "single" }; + type => "single", + comment_id => $comment->{cid}, + cid => $comment->{cid}, + parent_id => $comment->{pid}, + pid => $comment->{pid}, + stoid => $comment->{stoid}, + story_id => $comment->{stoid}, + sid => $comment->{sid}, + }; $c->render(item => $comment, parent => $parent, diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Journal.pm b/src/newslash_web/lib/Newslash/Web/Controller/Journal.pm index de730622..36b96dd0 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/Journal.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/Journal.pm @@ -26,7 +26,10 @@ sub single { } my $page = { content_type => "journal", - type => "single" }; + type => "single", + id => $journal->{journal_id}, + journal_id => $journal->{journal_id}, + }; $c->render(item => $journal, discussion => $discuss, diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Poll.pm b/src/newslash_web/lib/Newslash/Web/Controller/Poll.pm index 66b75fe8..31865017 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/Poll.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/Poll.pm @@ -50,7 +50,11 @@ sub single { my $related_polls = $c->model('polls')->select(limit => 10, order_by => {create_time => "DESC"}); my $page = { content_type => "poll", - type => "single" }; + type => "single", + qid => $poll->{qid}, + question_id => $poll->{qid}, + id => $poll->{qid}, + }; $c->render(item => $poll, page => $page, diff --git a/src/newslash_web/lib/Newslash/Web/Controller/Story.pm b/src/newslash_web/lib/Newslash/Web/Controller/Story.pm index e3366193..000a4796 100644 --- a/src/newslash_web/lib/Newslash/Web/Controller/Story.pm +++ b/src/newslash_web/lib/Newslash/Web/Controller/Story.pm @@ -49,6 +49,9 @@ sub single { my $page = { type => "single", content_type => "story", + stoid => $story->{stoid}, + sid => $story->{sid}, + id => $stpry->{stoid}, }; $c->render(item => $story, discussion => $discuss, diff --git a/src/newslash_web/public/js/comment-tree.js b/src/newslash_web/public/js/comment-tree.js index 00f8318d..0dda379d 100644 --- a/src/newslash_web/public/js/comment-tree.js +++ b/src/newslash_web/public/js/comment-tree.js @@ -12,7 +12,7 @@ commentTree.init = function () { return { userConfig: userConfig || {}, archived: page.archived || false, - commentAllowed: page.commentAllowed || false, + commentAllowed: page.comment_allowed || false, savable: user.is_login || false, isConfigVisible: false, }; @@ -47,7 +47,7 @@ commentTree.init = function () { template: '#comment-tree-template', data: function () { return { - commentAllowed: page.commentAllowed, + commentAllowed: page.comment_allowed, archived: page.archived || false, comments: [], rootID: 0, @@ -57,16 +57,16 @@ commentTree.init = function () { }); function commentTreeCreated() { - if (!page.discussionID) { + if (!page.discussion_id) { return; } var parentID; if (page.type == "comment") { - parentID = page.commentID; - this.rootID = page.commentID; + parentID = page.comment_id; + this.rootID = page.comment_id; } - newslash.getComments(page.discussionID, parentID).then( + newslash.getComments(page.discussion_id, parentID).then( (resp) => { // success var comments = resp.comments; @@ -103,7 +103,7 @@ commentTree.init = function () { secToken: '', displayForce: false, - commentAllowed: page.commentAllowed, + commentAllowed: page.comment_allowed, archived: page.archived || false, isFormVisible: false, @@ -254,7 +254,7 @@ commentTree.init = function () { function moderate() { var data = { reason: Number(this.moderateReason), - discussion_id: page.discussionID, + discussion_id: page.discussion_id, cid: Number(this.comment.cid), action: 'moderate', }; @@ -369,7 +369,7 @@ commentTree.init = function () { var data = { title: this.rawTitle, comment: this.rawComment, - discussion_id: page.discussionID, + discussion_id: page.discussion_id, stoid: page.stoid, pid: this.replyTo ? this.replyTo.pid : 0, action: 'preview', @@ -405,7 +405,7 @@ commentTree.init = function () { var data = { title: this.rawTitle, comment: this.rawComment, - discussion_id: page.discussionID, + discussion_id: page.discussion_id, stoid: page.stoid, pid: 0, action: 'post', @@ -414,8 +414,8 @@ commentTree.init = function () { if (this.replyTo) { data.pid = this.replyTo.cid; - } else if (page.commentID) { - data.pid = page.commentID; + } else if (page.comment_id) { + data.pid = page.comment_id; } this.$newslash.post("comment", data, diff --git a/src/newslash_web/public/js/vue-newslash.js b/src/newslash_web/public/js/vue-newslash.js index 6a6e8e39..ec49402f 100644 --- a/src/newslash_web/public/js/vue-newslash.js +++ b/src/newslash_web/public/js/vue-newslash.js @@ -128,7 +128,7 @@ VueNewslash.prototype.getComments = function getComments(discusID, onSuccess, on if (typeof discusID === "function") { onFail = onSuccess; onSuccess = discusID; - discusID = page.discussionID || 0; + discusID = page.discussion_id || 0; } if (!discusID) { onFail({}); diff --git a/src/newslash_web/templates/common/js_vars.html.tt2 b/src/newslash_web/templates/common/js_vars.html.tt2 index 28df906b..f223ac9e 100644 --- a/src/newslash_web/templates/common/js_vars.html.tt2 +++ b/src/newslash_web/templates/common/js_vars.html.tt2 @@ -9,46 +9,12 @@ is_admin: 1, [%- END %] }; - var page = {}; - [%- IF discussion.type != 'archived' && comment_allowed %] - page.commentAllowed = 1; - [%- ELSE %] - page.commentAllowed = 0; - [%- END %] - [%- IF discussion %] - page.archived = [% IF discussion.type == 'archived'; 1; ELSE; 0; END %]; - page.discussionID = [% IF discussion.id; discussion.id; ELSE; 0; END %]; - [%- ELSE %] - page.archived = 0; - page.discussionID = 0; - [%- END %] - [%- IF page -%] - page.contentType = "[% page.content_type %]"; + [%- IF page %] + var page = [% page.json() %]; + [%- ELSE %] + var page = {}; [%- END -%] - [%- IF page.content_type == "story" %] - page.type = 'story'; - page.stoid = [% item.stoid %]; - page.sid = [% item.sid %]; - [%- END -%] - - [%- IF page.content_type == "journal" %] - page.type = 'journal'; - page.id = [% item.journal_id %]; - [%- END -%] - - [%- IF page.content_type == "comment" %] - page.type = 'comment'; - page.commentID = [% item.comment_id %]; - page.parentID = [% item.parent_id %]; - page.stoid = [% discussion.stoid %]; - page.sid = '[% discussion.sid %]'; - [%- END -%] - - [%- IF page.content_type == "poll" %] - page.type = 'poll'; - page.qid = [% item.qid %]; - [%- END -%] -- 2.11.0