Skip to content

Commit

Permalink
feat: NoJs Admin View (#3059)
Browse files Browse the repository at this point in the history
Adds a nojs blade template to be able to enable/disable extensions when one of them misbehaves.
  • Loading branch information
SychO9 authored Aug 31, 2021
1 parent 55d8af4 commit e8153cc
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 70 deletions.
1 change: 1 addition & 0 deletions less/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
@import "admin/ExtensionWidget";
@import "admin/AppearancePage";
@import "admin/MailPage";
@import "admin/NoJs";
@import "admin/UsersListPage.less";
17 changes: 17 additions & 0 deletions less/admin/NoJs.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Minimal NoJs specific styles
.NoJs-ExtensionsTable {
td&-icon {
padding-top: 0;
padding-bottom: 0;
}

.ExtensionListItem-Dot {
position: relative;
right: 0;
margin: 0;
}

.ExtensionIcon {
--size: 25px;
}
}
69 changes: 69 additions & 0 deletions less/common/Table.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.Table {
background: @control-bg;
border-radius: @border-radius;
border-collapse: collapse;
border-spacing: 0;

caption {
text-align: start;
}

td, th {
border-bottom: 1px solid @body-bg;
color: @control-color;
}

td, th, .Checkbox, &-controls-item {
padding: 10px 15px;
}

& &-checkbox, & &-controls {
padding: 0;
}

thead {
th {
text-align: center;
padding: 15px 25px;
}

.icon {
display: block;
font-size: 14px;
width: auto;
margin-bottom: 5px;
}
}

&-groupToggle {
cursor: pointer;

.icon {
font-size: 14px;
margin-right: 2px;
.fa-fw();
}
}

&-checkbox {
.Checkbox {
display: block;
}

.Checkbox-display {
text-align: center;
cursor: pointer;
}

&.highlighted .Checkbox, .Checkbox:hover {
&:not(.disabled) {
background: darken(@control-bg, 4%);
}
}
}

&-controls-item {
width: 100%;
border-radius: 0;
}
}
1 change: 1 addition & 0 deletions less/common/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@import "Placeholder";
@import "Search";
@import "Select";
@import "Table";
@import "TextEditor";
@import "Tooltip";
@import "ValidationError";
51 changes: 1 addition & 50 deletions less/forum/NotificationGrid.less
Original file line number Diff line number Diff line change
@@ -1,52 +1,3 @@
.NotificationGrid {
background: @control-bg;
border-radius: @border-radius;
border-collapse: collapse;
border-spacing: 0;

td, th {
border-bottom: 1px solid @body-bg;
color: @control-color;
}
td, th, .Checkbox {
padding: 10px 15px;
}
.NotificationGrid-checkbox {
padding: 0;
}
thead {
th {
text-align: center;
padding: 15px 25px;
}
.icon {
display: block;
font-size: 14px;
width: auto;
margin-bottom: 5px;
}
}
}
.NotificationGrid-groupToggle {
cursor: pointer;

.icon {
font-size: 14px;
margin-right: 2px;
.fa-fw();
}
}
.NotificationGrid-checkbox {
.Checkbox {
display: block;
}
.Checkbox-display {
text-align: center;
cursor: pointer;
}
&.highlighted .Checkbox, .Checkbox:hover {
&:not(.disabled) {
background: darken(@control-bg, 4%);
}
}
.Table();
}
15 changes: 15 additions & 0 deletions locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,20 @@ core:

# Translations in this namespace are used in views other than Flarum's normal JS client.
views:
# Translations in this namespace are displayed by the basic HTML admin index.
admin:
extensions:
caption: => core.ref.extensions
disable: Disable
empty: No installed extensions
enable: Enable
name: Extension Name
package_name: Package Name
version: Version
info:
caption: Application Info
title: Administration

# Translations in this namespace are displayed by the Confirm Email interface.
confirm_email:
submit_button: => core.ref.confirm_email
Expand Down Expand Up @@ -673,6 +687,7 @@ core:
edit: Edit
edit_user: Edit User
email: Email
extensions: Extensions
icon: Icon
icon_text: "Enter the name of any <a>FontAwesome</a> icon class, <em>including</em> the <code>fas fa-</code> prefix."
load_more: Load More
Expand Down
60 changes: 60 additions & 0 deletions src/Admin/Content/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Admin\Content;

use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Frontend\Document;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\View\Factory;
use Psr\Http\Message\ServerRequestInterface as Request;

class Index
{
/**
* @var Factory
*/
protected $view;

/**
* @var ExtensionManager
*/
protected $extensions;

/**
* @var SettingsRepositoryInterface
*/
protected $settings;

public function __construct(Factory $view, ExtensionManager $extensions, SettingsRepositoryInterface $settings)
{
$this->view = $view;
$this->extensions = $extensions;
$this->settings = $settings;
}

public function __invoke(Document $document, Request $request): Document
{
$extensions = $this->extensions->getExtensions();
$extensionsEnabled = json_decode($this->settings->get('extensions_enabled', '{}'), true);
$csrfToken = $request->getAttribute('session')->token();

$mysqlVersion = $document->payload['mysqlVersion'];
$phpVersion = $document->payload['phpVersion'];
$flarumVersion = Application::VERSION;

$document->content = $this->view->make(
'flarum.admin::frontend.content.admin',
compact('extensions', 'extensionsEnabled', 'csrfToken', 'flarumVersion', 'phpVersion', 'mysqlVersion')
);

return $document;
}
}
55 changes: 55 additions & 0 deletions src/Admin/Controller/UpdateExtensionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Admin\Controller;

use Flarum\Bus\Dispatcher;
use Flarum\Extension\Command\ToggleExtension;
use Flarum\Http\RequestUtil;
use Flarum\Http\UrlGenerator;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;

class UpdateExtensionController implements RequestHandlerInterface
{
/**
* @var UrlGenerator
*/
protected $url;

/**
* @var Dispatcher
*/
protected $bus;

public function __construct(UrlGenerator $url, Dispatcher $bus)
{
$this->url = $url;
$this->bus = $bus;
}

/**
* {@inheritdoc}
*/
public function handle(Request $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$enabled = (bool) (int) Arr::get($request->getParsedBody(), 'enabled');
$name = Arr::get($request->getQueryParams(), 'name');

$this->bus->dispatch(
new ToggleExtension($actor, $name, $enabled)
);

return new RedirectResponse($this->url->to('admin')->base());
}
}
10 changes: 9 additions & 1 deletion src/Admin/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
* LICENSE file that was distributed with this source code.
*/

use Flarum\Admin\Content\Index;
use Flarum\Admin\Controller\UpdateExtensionController;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;

return function (RouteCollection $map, RouteHandlerFactory $route) {
$map->get(
'/',
'index',
$route->toAdmin()
$route->toAdmin(Index::class)
);

$map->post(
'/extensions/{name}',
'extensions.update',
$route->toController(UpdateExtensionController::class)
);
};
27 changes: 11 additions & 16 deletions src/Api/Controller/UpdateExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace Flarum\Api\Controller;

use Flarum\Extension\ExtensionManager;
use Flarum\Bus\Dispatcher;
use Flarum\Extension\Command\ToggleExtension;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
Expand All @@ -20,33 +21,27 @@
class UpdateExtensionController implements RequestHandlerInterface
{
/**
* @var ExtensionManager
* @var Dispatcher
*/
protected $extensions;
protected $bus;

/**
* @param ExtensionManager $extensions
*/
public function __construct(ExtensionManager $extensions)
public function __construct(Dispatcher $bus)
{
$this->extensions = $extensions;
$this->bus = $bus;
}

/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
RequestUtil::getActor($request)->assertAdmin();

$enabled = Arr::get($request->getParsedBody(), 'enabled');
$actor = RequestUtil::getActor($request);
$enabled = (bool) (int) Arr::get($request->getParsedBody(), 'enabled');
$name = Arr::get($request->getQueryParams(), 'name');

if ($enabled === true) {
$this->extensions->enable($name);
} elseif ($enabled === false) {
$this->extensions->disable($name);
}
$this->bus->dispatch(
new ToggleExtension($actor, $name, $enabled)
);

return new EmptyResponse;
}
Expand Down
Loading

0 comments on commit e8153cc

Please sign in to comment.