Use Util::addInlineStyle in event listener
This commit is contained in:
@@ -4,13 +4,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace OCA\MinimalProfile\AppInfo;
|
namespace OCA\MinimalProfile\AppInfo;
|
||||||
|
|
||||||
|
use OCA\MinimalProfile\Service\Config;
|
||||||
use OCP\AppFramework\App;
|
use OCP\AppFramework\App;
|
||||||
use OCP\AppFramework\Bootstrap\IBootContext;
|
use OCP\AppFramework\Bootstrap\IBootContext;
|
||||||
use OCP\AppFramework\Bootstrap\IBootstrap;
|
use OCP\AppFramework\Bootstrap\IBootstrap;
|
||||||
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
||||||
use OCP\AppFramework\Http\TemplateResponse;
|
use OCP\AppFramework\Http\TemplateResponse;
|
||||||
use OCP\EventDispatcher\Event;
|
use OCP\EventDispatcher\Event;
|
||||||
use OCP\EventDispatcher\IEventListener;
|
use OCP\Util;
|
||||||
|
|
||||||
class Application extends App implements IBootstrap {
|
class Application extends App implements IBootstrap {
|
||||||
|
|
||||||
@@ -21,11 +22,23 @@ class Application extends App implements IBootstrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function register(IRegistrationContext $context): void {
|
public function register(IRegistrationContext $context): void {
|
||||||
|
$context->registerService(Config::class, function($c) {
|
||||||
|
return new Config($c->get(\OCP\IConfig::class));
|
||||||
|
});
|
||||||
|
|
||||||
$context->registerEventListener(
|
$context->registerEventListener(
|
||||||
TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS,
|
TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS,
|
||||||
function(Event $event) {
|
function(Event $event) {
|
||||||
if ($event instanceof TemplateResponse) {
|
if (!($event instanceof TemplateResponse)) {
|
||||||
$event->getStyleSheet()->load('minimalprofile', 'minimalprofile');
|
return;
|
||||||
|
}
|
||||||
|
$container = $event->getAppContainer();
|
||||||
|
$config = $container->query(Config::class);
|
||||||
|
$hidden = $config->getHiddenFields();
|
||||||
|
|
||||||
|
if (!empty($hidden)) {
|
||||||
|
$css = $config->generateCss($hidden);
|
||||||
|
Util::addInlineStyle($css);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
50
lib/Service/Config.php
Normal file
50
lib/Service/Config.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\MinimalProfile\Service;
|
||||||
|
|
||||||
|
use OCP\IConfig;
|
||||||
|
|
||||||
|
class Config {
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private IConfig $config,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHiddenFields(): array {
|
||||||
|
$value = $this->config->getAppValue('minimalprofile', 'hidden_fields', '');
|
||||||
|
if ($value === '') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return json_decode($value, true) ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateCss(array $fields): string {
|
||||||
|
$selectors = [
|
||||||
|
'pronouns' => '#account-property-pronouns',
|
||||||
|
'role' => '#account-property-role',
|
||||||
|
'headline' => '#account-property-headline',
|
||||||
|
'biography' => '#account-property-biography',
|
||||||
|
'organisation' => '#account-property-organisation',
|
||||||
|
'phone' => '#account-property-phone',
|
||||||
|
'address' => '#account-property-address',
|
||||||
|
'birthdate' => '#account-property-birthdate',
|
||||||
|
'website' => '#account-property-website',
|
||||||
|
'twitter' => '#account-property-twitter',
|
||||||
|
'fediverse' => '#account-property-fediverse',
|
||||||
|
'location' => '#account-property-location',
|
||||||
|
];
|
||||||
|
|
||||||
|
$css = '';
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
if (isset($selectors[$field])) {
|
||||||
|
$selector = $selectors[$field];
|
||||||
|
$css .= $selector . ' { display: none !important; }' . "\n";
|
||||||
|
$css .= '.personal-settings-setting-box:has(' . $selector . ') { display: none !important; }' . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $css;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user