Add index page title, add mechanism to clear title from defaultRoute. (#2047)

* Add "All Descriptions title to index

* Added system to clear custom title if we're on the default route
This commit is contained in:
Alexander Skvortsov 2020-06-27 14:18:49 -04:00 committed by GitHub
parent d6de442b8a
commit 2f9beccf2a
5 changed files with 22 additions and 6 deletions

View File

@ -237,7 +237,10 @@ export default class Application {
} }
updateTitle() { updateTitle() {
document.title = (this.titleCount ? `(${this.titleCount}) ` : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title'); const count = this.titleCount ? `(${this.titleCount}) ` : '';
const pageTitleWithSeparator = this.title && m.route() !== '/' ? this.title + ' - ' : '';
const title = this.forum.attribute('title');
document.title = count + pageTitleWithSeparator + title;
} }
/** /**

View File

@ -79,7 +79,7 @@ export default class IndexPage extends Page {
extend(context, 'onunload', () => $('#app').css('min-height', '')); extend(context, 'onunload', () => $('#app').css('min-height', ''));
app.setTitle(''); app.setTitle(app.translator.trans('core.forum.index.meta_title_text'));
app.setTitleCount(0); app.setTitleCount(0);
// Work out the difference between the height of this hero and that of the // Work out the difference between the height of this hero and that of the

View File

@ -15,6 +15,7 @@ use Flarum\Frontend\Document;
use Flarum\Http\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -46,13 +47,15 @@ class Index
* @param Factory $view * @param Factory $view
* @param SettingsRepositoryInterface $settings * @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url * @param UrlGenerator $url
* @param Translator $translator
*/ */
public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url) public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url, Translator $translator)
{ {
$this->api = $api; $this->api = $api;
$this->view = $view; $this->view = $view;
$this->settings = $settings; $this->settings = $settings;
$this->url = $url; $this->url = $url;
$this->translator = $translator;
} }
public function __invoke(Document $document, Request $request) public function __invoke(Document $document, Request $request)
@ -74,6 +77,7 @@ class Index
$apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params); $apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params);
$defaultRoute = $this->settings->get('default_route'); $defaultRoute = $this->settings->get('default_route');
$document->title = $this->translator->trans('core.forum.index.meta_title_text');
$document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page')); $document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page'));
$document->payload['apiDocument'] = $apiDocument; $document->payload['apiDocument'] = $apiDocument;
$document->canonicalUrl = $defaultRoute === '/all' ? $this->url->to('forum')->base() : $request->getUri()->withQuery(''); $document->canonicalUrl = $defaultRoute === '/all' ? $this->url->to('forum')->base() : $request->getUri()->withQuery('');

View File

@ -13,6 +13,7 @@ use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
/** /**
* A view which renders a HTML skeleton for Flarum's frontend app. * A view which renders a HTML skeleton for Flarum's frontend app.
@ -131,14 +132,20 @@ class Document implements Renderable
*/ */
protected $forumApiDocument; protected $forumApiDocument;
/**
* @var Request
*/
protected $request;
/** /**
* @param Factory $view * @param Factory $view
* @param array $forumApiDocument * @param array $forumApiDocument
*/ */
public function __construct(Factory $view, array $forumApiDocument) public function __construct(Factory $view, array $forumApiDocument, Request $request)
{ {
$this->view = $view; $this->view = $view;
$this->forumApiDocument = $forumApiDocument; $this->forumApiDocument = $forumApiDocument;
$this->request = $request;
} }
/** /**
@ -173,7 +180,9 @@ class Document implements Renderable
*/ */
protected function makeTitle(): string protected function makeTitle(): string
{ {
return ($this->title ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title'); $onHomePage = rtrim($this->request->getUri()->getPath(), '/') === '';
return ($this->title && ! $onHomePage ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title');
} }
/** /**

View File

@ -50,7 +50,7 @@ class Frontend
{ {
$forumDocument = $this->getForumDocument($request); $forumDocument = $this->getForumDocument($request);
$document = new Document($this->view, $forumDocument); $document = new Document($this->view, $forumDocument, $request);
$this->populate($document, $request); $this->populate($document, $request);