From 6f4d7a36b69035a4b20083f6bb507c586fe78fae Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 6 Aug 2015 12:21:11 +0930 Subject: [PATCH] Build very rough HTML-only content And redirect to the "no JS" mode if the JS app crashes on boot. ClientView/ClientAction is all a bit of a mess and will need to be radically cleaned up at some point... --- .../js/forum/src/components/DiscussionPage.js | 9 +---- .../src/Forum/Actions/DiscussionAction.php | 40 +++++++++++++++++-- .../core/src/Forum/Actions/IndexAction.php | 8 ++-- framework/core/src/Support/ClientView.php | 13 +++++- framework/core/views/app.blade.php | 40 +++++++++---------- framework/core/views/content.blade.php | 6 +++ framework/core/views/discussion.blade.php | 30 +++++++++++++- framework/core/views/forum.blade.php | 2 + framework/core/views/index.blade.php | 22 +++++++++- 9 files changed, 128 insertions(+), 42 deletions(-) create mode 100644 framework/core/views/content.blade.php diff --git a/framework/core/js/forum/src/components/DiscussionPage.js b/framework/core/js/forum/src/components/DiscussionPage.js index 79d3169e9..ac2294eeb 100644 --- a/framework/core/js/forum/src/components/DiscussionPage.js +++ b/framework/core/js/forum/src/components/DiscussionPage.js @@ -174,13 +174,6 @@ export default class DiscussionPage extends mixin(Component, evented) { * @param {Discussion} discussion */ init(discussion) { - // If the slug in the URL doesn't match up, we'll redirect so we have the - // correct one. - if (m.route.param('id') === discussion.id() && m.route.param('slug') !== discussion.slug()) { - m.route(app.route.discussion(discussion, m.route.param('near')), null, true); - return; - } - this.discussion = discussion; app.setTitle(discussion.title()); @@ -207,7 +200,7 @@ export default class DiscussionPage extends mixin(Component, evented) { // the specific post that was routed to. this.stream = new PostStream({discussion, includedPosts}); this.stream.on('positionChanged', this.positionChanged.bind(this)); - this.stream.goToNumber(m.route.param('near') || 1, true); + this.stream.goToNumber(m.route.param('near') || includedPosts[0].number(), true); this.trigger('loaded', discussion); } diff --git a/framework/core/src/Forum/Actions/DiscussionAction.php b/framework/core/src/Forum/Actions/DiscussionAction.php index fff5a3b3b..ee545cdca 100644 --- a/framework/core/src/Forum/Actions/DiscussionAction.php +++ b/framework/core/src/Forum/Actions/DiscussionAction.php @@ -11,18 +11,50 @@ class DiscussionAction extends ClientAction { $view = parent::render($request, $routeParams); + $queryParams = $request->getQueryParams(); + $page = max(1, array_get($queryParams, 'page')); + $params = [ 'id' => array_get($routeParams, 'id'), - 'page.near' => array_get($routeParams, 'near') + 'page' => [ + 'near' => array_get($routeParams, 'near'), + 'offset' => ($page - 1) * 20, + 'limit' => 20 + ] ]; - // FIXME: make sure this is extensible. 404s, pagination. - $document = $this->preload($params); + $getResource = function ($link) use ($document) { + return array_first($document->included, function ($key, $value) use ($link) { + return $value->type === $link->type && $value->id === $link->id; + }); + }; + + $url = function ($newQueryParams) use ($queryParams, $document) { + $newQueryParams = array_merge($queryParams, $newQueryParams); + $queryString = []; + + foreach ($newQueryParams as $k => $v) { + $queryString[] = $k . '=' . $v; + } + + return app('Flarum\Http\UrlGeneratorInterface') + ->toRoute('flarum.forum.discussion', ['id' => $document->data->id]) . + ($queryString ? '?' . implode('&', $queryString) : ''); + }; + + $posts = []; + + foreach ($document->included as $resource) { + if ($resource->type === 'posts' && isset($resource->relationships->discussion) && isset($resource->attributes->contentHtml)) { + $posts[] = $resource; + } + } + $view->setTitle($document->data->attributes->title); $view->setDocument($document); - $view->setContent(app('view')->make('flarum.forum::discussion', compact('document'))); + $view->setContent(app('view')->make('flarum.forum::discussion', compact('document', 'page', 'getResource', 'posts', 'url'))); return $view; } diff --git a/framework/core/src/Forum/Actions/IndexAction.php b/framework/core/src/Forum/Actions/IndexAction.php index 3894f118d..45e9d0de4 100644 --- a/framework/core/src/Forum/Actions/IndexAction.php +++ b/framework/core/src/Forum/Actions/IndexAction.php @@ -27,18 +27,18 @@ class IndexAction extends ClientAction $sort = array_pull($queryParams, 'sort'); $q = array_pull($queryParams, 'q'); + $page = array_pull($queryParams, 'page', 1); $params = [ 'sort' => $sort && isset($this->sortMap[$sort]) ? $this->sortMap[$sort] : '', - 'filter' => ['q' => $q] + 'filter' => compact('q'), + 'page' => ['offset' => ($page - 1) * 20, 'limit' => 20] ]; - // FIXME: make sure this is extensible. Support pagination. - $document = $this->preload($params); $view->setDocument($document); - $view->setContent(app('view')->make('flarum.forum::index', compact('document'))); + $view->setContent(app('view')->make('flarum.forum::index', compact('document', 'page', 'forum'))); return $view; } diff --git a/framework/core/src/Support/ClientView.php b/framework/core/src/Support/ClientView.php index 2af78d9c1..9a6592d95 100644 --- a/framework/core/src/Support/ClientView.php +++ b/framework/core/src/Support/ClientView.php @@ -237,10 +237,19 @@ class ClientView implements Renderable ] + $this->variables; $view->bootstrappers = $this->bootstrappers; + $noJs = array_get($this->request->getQueryParams(), 'nojs'); + $view->title = ($this->title ? $this->title . ' - ' : '') . $forum->data->attributes->title; $view->forum = $forum->data; - $view->layout = app('view')->file($this->layout, ['forum' => $forum->data]); - $view->content = $this->content; + $view->layout = app('view')->file($this->layout, [ + 'forum' => $forum->data, + 'content' => app('view')->file(__DIR__.'/../../views/content.blade.php', [ + 'content' => $this->content, + 'noJs' => $noJs, + 'forum' => $forum->data + ]) + ]); + $view->noJs = $noJs; $view->styles = [$this->assets->getCssFile()]; $view->scripts = [$this->assets->getJsFile(), $this->locale->getFile()]; diff --git a/framework/core/views/app.blade.php b/framework/core/views/app.blade.php index 7230ca661..0505e5264 100644 --- a/framework/core/views/app.blade.php +++ b/framework/core/views/app.blade.php @@ -21,31 +21,27 @@
- @foreach ($scripts as $file) - - @endforeach - - @endforeach - app.boot(); - } catch (e) { - document.write('
Something went wrong.
'); - throw e; - } - + @endif {!! $foot !!} diff --git a/framework/core/views/content.blade.php b/framework/core/views/content.blade.php new file mode 100644 index 000000000..05fc8ae15 --- /dev/null +++ b/framework/core/views/content.blade.php @@ -0,0 +1,6 @@ +@if (! $noJs) @endif diff --git a/framework/core/views/discussion.blade.php b/framework/core/views/discussion.blade.php index 93f5f2632..d5b73831e 100644 --- a/framework/core/views/discussion.blade.php +++ b/framework/core/views/discussion.blade.php @@ -1 +1,29 @@ -discussion SEO content +data; +$postsCount = count($discussion->relationships->posts->data); +?> +
+

{{ $discussion->attributes->title }}

+ +
+ @foreach ($posts as $post) +
+ relationships->user->data); ?> +

{{ $user ? $user->attributes->username : '[deleted]' }}

+
+ {!! $post->attributes->contentHtml !!} +
+
+ +
+ @endforeach +
+ + @if ($page > 1) + « Previous Page + @endif + + @if ($page < $postsCount / 20) + Next Page » + @endif +
diff --git a/framework/core/views/forum.blade.php b/framework/core/views/forum.blade.php index f816ca8dc..1c30c5bc9 100644 --- a/framework/core/views/forum.blade.php +++ b/framework/core/views/forum.blade.php @@ -45,6 +45,8 @@
+ {!! $content !!} +
diff --git a/framework/core/views/index.blade.php b/framework/core/views/index.blade.php index 16922c081..2593660dd 100644 --- a/framework/core/views/index.blade.php +++ b/framework/core/views/index.blade.php @@ -1 +1,21 @@ -index SEO content + +
+

All Discussions

+ + + + Next Page » +