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...
This commit is contained in:
Toby Zerner 2015-08-06 12:21:11 +09:30
parent bb89ef276f
commit 6f4d7a36b6
9 changed files with 128 additions and 42 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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()];

View File

@ -21,31 +21,27 @@
<div id="modal"></div>
<div id="alerts"></div>
@foreach ($scripts as $file)
<script src="{{ str_replace(public_path(), '', $file) }}"></script>
@endforeach
<script>
try {
var app = System.get('flarum/app').default;
babelHelpers._extends(app, {!! json_encode($app) !!});
@foreach ($bootstrappers as $bootstrapper)
System.get('{{ $bootstrapper }}');
@if (! $noJs)
@foreach ($scripts as $file)
<script src="{{ str_replace(public_path(), '', $file) }}"></script>
@endforeach
app.boot();
} catch (e) {
document.write('<div class="container">Something went wrong.</div>');
throw e;
}
</script>
<script>
try {
var app = System.get('flarum/app').default;
@if ($content)
<noscript>
{!! $content !!}
</noscript>
babelHelpers._extends(app, {!! json_encode($app) !!});
@foreach ($bootstrappers as $bootstrapper)
System.get('{{ $bootstrapper }}');
@endforeach
app.boot();
} catch (e) {
window.location = window.location + '?nojs=1';
throw e;
}
</script>
@endif
{!! $foot !!}

View File

@ -0,0 +1,6 @@
@if (! $noJs) <noscript> @endif
<div class="container">
<div class="Alert">You're viewing the HTML-only version of {{ $forum->attributes->title }}. Upgrade your browser for the full version.</div>
</div>
{!! $content !!}
@if (! $noJs) </noscript> @endif

View File

@ -1 +1,29 @@
discussion SEO content
<?php
$discussion = $document->data;
$postsCount = count($discussion->relationships->posts->data);
?>
<div class="container">
<h2>{{ $discussion->attributes->title }}</h2>
<div>
@foreach ($posts as $post)
<div>
<?php $user = $getResource($post->relationships->user->data); ?>
<h3>{{ $user ? $user->attributes->username : '[deleted]' }}</h3>
<div class="Post-body">
{!! $post->attributes->contentHtml !!}
</div>
</div>
<hr>
@endforeach
</div>
@if ($page > 1)
<a href="{{ $url(['page' => $page - 1]) }}">&laquo; Previous Page</a>
@endif
@if ($page < $postsCount / 20)
<a href="{{ $url(['page' => $page + 1]) }}">Next Page &raquo;</a>
@endif
</div>

View File

@ -45,6 +45,8 @@
<main class="App-content">
<div id="content"></div>
{!! $content !!}
<div class="App-composer">
<div class="container">
<div id="composer"></div>

View File

@ -1 +1,21 @@
index SEO content
<?php
$url = app('Flarum\Http\UrlGeneratorInterface');
?>
<div class="container">
<h2>All Discussions</h2>
<ul>
@foreach ($document->data as $discussion)
<li>
<a href="{{ $url->toRoute('flarum.forum.discussion', [
'id' => $discussion->id,
'slug' => $discussion->attributes->title
]) }}">
{{ $discussion->attributes->title }}
</a>
</li>
@endforeach
</ul>
<a href="{{ $url->toRoute('flarum.forum.index') }}?page={{ $page + 1 }}">Next Page &raquo;</a>
</div>