New plugin API: addUserBadge

This commit is contained in:
Pig Fang 2019-07-02 23:34:27 +08:00
parent be9f6011bb
commit e8108959e3
6 changed files with 48 additions and 2 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace App\Events;
class RenderingBadges extends Event
{
public $badges;
public function __construct(array &$badges)
{
$this->badges = &$badges;
}
}

View File

@ -53,8 +53,6 @@ class Hook
/**
* Add routes. A router instance will be passed to the given callback.
*
* @param Closure $callback
*/
public static function addRoute(Closure $callback): void
{
@ -118,4 +116,14 @@ class Hook
}
}, $priority);
}
public static function addUserBadge(string $text, $color = 'primary'): void
{
Event::listen(
Events\RenderingBadges::class,
function (Events\RenderingBadges $event) use ($text, $color) {
$event->badges[] = [$text, $color];
}
);
}
}

View File

@ -5,6 +5,7 @@
- Added configurable "Explore" menu.
- Custom `PLUGINS_DIR` with relative path is allowed.
- Added link for editing announcement.
- New plugin API: [`Hook::addUserBadge`](https://bs-plugin.netlify.com/guide/bootstrap.html#%E6%98%BE%E7%A4%BA%E7%94%A8%E6%88%B7-badge).
## Tweaked

View File

@ -5,6 +5,7 @@
- 添加可自定义的「浏览」菜单
- 允许在 `PLUGINS_DIR` 配置项中使用相对路径
- 添加「编辑公告」的链接
- 新插件 API[`Hook::addUserBadge`](https://bs-plugin.netlify.com/guide/bootstrap.html#%E6%98%BE%E7%A4%BA%E7%94%A8%E6%88%B7-badge)
## 调整

View File

@ -7,6 +7,11 @@ $roles = [
];
$role = $roles[$user->permission];
@endphp
@php
$badges = [];
event(new \App\Events\RenderingBadges($badges));
@endphp
<div class="user-panel">
<div class="pull-left image">
<img src="{{ url("avatar/45/".base64_encode($user->email).'.png?tid='.$user->avatar) }}" alt="User Image">
@ -14,5 +19,15 @@ $role = $roles[$user->permission];
<div class="pull-left info">
<p class="nickname">{{ $user->nickname ?? $user->email }}</p>
<i class="fas fa-circle text-success"></i> @lang("admin.users.status.$role")
@if (count($badges) === 1)
<small class="label bg-{{ $badges[0][1] }}">{{ $badges[0][0] }}</small>
@endif
</div>
</div>
@if (count($badges) > 1)
<div class="user-panel" style="padding-top: 0">
@foreach ($badges as $badge)
<small class="label bg-{{ $badge[1] }}">{{ $badge[0] }}</small>
@endforeach
</div>
@endif

View File

@ -84,4 +84,12 @@ class HookTest extends TestCase
$this->get('/skinlib')
->assertSee('<script src="/script/pattern"></script>');
}
public function testAddUserBadge()
{
Hook::addUserBadge('hi', 'green');
$this->actAs('normal')
->get('/user')
->assertSee('<small class="label bg-green">hi</small>');
}
}