forked from felixhahnweilheim/humhub-dark-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.php
More file actions
executable file
·78 lines (69 loc) · 2.55 KB
/
Copy pathEvents.php
File metadata and controls
executable file
·78 lines (69 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace humhub\modules\darkMode;
use humhub\modules\darkMode\widgets\SwitchButton;
use humhub\modules\darkMode\models\UserSetting;
use humhub\modules\ui\menu\MenuLink;
use humhub\components\View;
use Yii;
use yii\helpers\Url;
class Events
{
public static function onViewBeginBody($event)
{
if (
Yii::$app->request->isConsoleRequest
|| Yii::$app->request->isAjax
) {
return;
}
$mode = (new UserSetting())->darkMode;
if ($mode === UserSetting::OPTION_DARK) {
Yii::$app->view->registerJs("
(function() {
$('html').attr('data-bs-theme', 'dark');
})();
", View::POS_HEAD);
} elseif ($mode === UserSetting::OPTION_DEFAULT) {
Yii::$app->view->registerJs("
(function() {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
$('html').attr('data-bs-theme', 'dark');
$('html').attr('data-dark-mode-default', true);
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
if (!$('html').attr('data-dark-mode-default') || $('html').attr('data-dark-mode-default') == 'false') return;
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
$('html').attr('data-bs-theme', 'dark');
} else {
$('html').attr('data-bs-theme', 'light');
}
});
})();
", View::POS_HEAD);
}
}
public static function onNotificationAddonInit($event)
{
if (Yii::$app->getModule('dark-mode')->settings->get('showButton', true)) {
try {
$event->sender->addWidget(SwitchButton::class, [], ['sortOrder' => 200]);
} catch (\Throwable $e) {
Yii::error($e, 'dark-mode');
}
}
}
public static function onAccountSettingsMenuInit($event)
{
$menu = $event->sender;
$menu->addEntry(new MenuLink([
'icon' => 'fa-moon-o',
'label' => Yii::t('DarkModeModule.base', 'Dark Mode'),
'url' => Url::toRoute('/dark-mode/user/'),
'sortOrder' => 900,
'isActive' => (Yii::$app->controller->module
&& Yii::$app->controller->module->id === 'dark-mode'
&& Yii::$app->controller->id === 'user'),
]));
return true;
}
}