Commit 689f8ed4 by wulei@ldy.com

admin ext

1 parent 0b9a9233
<?php
namespace App\Admin\Extensions\Grid\Action;
use Dcat\Admin\Grid\RowAction;
class LinkAction extends RowAction
{
/**
* @return string
*/
protected $title = '链接';
protected $link;
public function __construct($title = null, $link = null)
{
$this->link = $link;
parent::__construct($title);
}
protected function href()
{
if ($this->link) {
if (is_array($this->link)) {
$this->parent->model()->setConstraints($this->link);
} else {
return $this->link;
}
}
return $this->parent->urlWithConstraints("{$this->resource()}/{$this->getKey()}");
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions\Grid;
use Dcat\Admin\Grid\Column;
use Illuminate\Support\Arr;
class ColumnExts
{
static $extends = [
'fen2yuan' => function ($value) {
return \bcdiv($value, 100, 2);
},
'unit' => function($value, $unit, $left = true) {
if (\is_callable($unit)) {
$unit = \call_user_func($unit, $this);
}
return $left ? ($unit . $value) : ($value . $unit);
},
'multiline' => function($value, ...$keys) {
return $value . '<br/>' . \implode('<br/>', \array_map(function($key) {
return Arr::get($this, $key, '');
}, $keys));
},
'concat' => function($value, $keys, $separator = "") {
return $value . $separator . \implode($separator, \array_map(function($key) {
return Arr::get($this, $key, '');
}, (array) $keys));
},
'ellipses' => function($value, $length = 5, $symbol = '...') {
$new = \mb_substr($value, 0, $length);
if (\mb_strlen($value) > $length) {
$new .= $symbol;
}
return "<span title={$value}>{$new}</span>";
},
'minute2hour' => function($value) {
return \intval($value/60) . '小时' . ($value % 60) . '分钟';
},
];
public static function extend()
{
foreach (static::$extends as $name => $disaplayer) {
Column::extend($name, $disaplayer);
}
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions\Grid;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\Displayers\Actions;
class SimpleGrid extends Grid
{
public function callBuilder()
{
$this->setActionClass(Actions::class);
$this->disableCreateButton();
$this->disableEditButton();
$this->disableViewButton();
$this->disableDeleteButton();
$this->disableBatchActions();
$this->disableRefreshButton();
$this->disableColumnSelector();
$this->disableRowSelector();
$this->simplePaginate();
$this->paginate(10);
$this->model()->latest($this->getKeyName());
parent::callBuilder();
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions\Grid;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Displayers\Actions;
class TextActions extends Actions
{
/**
* {@inheritdoc}
*/
public function display(array $callbacks = [])
{
Admin::style('.grid__actions__ a{margin:0 2px;}');
return parent::display($callbacks);
}
/**
* @return string
*/
protected function getViewLabel()
{
return trans('admin.show');
}
/**
* @return string
*/
protected function getEditLabel()
{
return trans('admin.edit');
}
/**
* @return string
*/
protected function getDeleteLabel()
{
return trans('admin.delete');
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions;
use App\Admin\Extensions\Widgets\ShowBox;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Database\Eloquent\Model;
trait HasCustomShow
{
protected function baseInfo(Model $model, $only = [])
{
return new ShowBox($model, $only);
}
protected function tabInfo(Model $model)
{
Admin::style('.show-tab .nav.nav-tabs .nav-item .nav-link{padding:7px 12px;}');
$tab = new Tab();
$tab->class('show-tab', true);
foreach ($this->getTabs() as $key => $title) {
if ($key == request('tab')) {
$content = method_exists($this, $key . 'Content') ? call_user_func([$this, $key . 'Content'], $model) : $this->getTabContent($model, $key);
$tab->add($title, $content, true);
} else {
$tab->addLink($title, '?tab=' . $key);
}
}
return new Card($tab);
}
protected function getTabs()
{
return $this->tabs;
}
protected function getTabContent(Model $model, $tab)
{
return '';
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions\Widgets;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Box;
class DataBox extends Box
{
public function __construct($title = '', $content = '')
{
parent::__construct($title, empty($content) ? ' ' : $content);
$this->class('data-box', true);
Admin::style(<<<STYLE
.data-box {background-color:#FEFEFE;}
.data-box .box-header {padding:0.5rem;justify-content:center;}
.data-box .box-body {padding:1rem;text-align:center;font-size:28px}
STYLE
);
}
}
\ No newline at end of file
<?php
namespace App\Admin\Extensions\Widgets;
use Dcat\Admin\Show;
use Dcat\Admin\Show\Tools;
use Dcat\Admin\Widgets\Widget;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
class ShowBox extends Widget
{
private $show;
public function __construct(Model $model, $fields = [], $width = 7, $edit = true)
{
$fields = $fields ?: array_keys($model->getAttributes());
$fields = Arr::isAssoc($fields) ? $fields : array_flip($fields);
$this->show = Show::make($model->getKey(), $model, function (Show $show) use ($fields, $width, $edit) {
foreach ($fields as $name => $label) {
$field = $show->field($name, \is_string($label) ? $label : '')->width($width, max(12 - $width, 0));
if (is_callable($label)) {
$field->as($label, $field, $this);
}
}
$show->tools(function(Tools $tools) use ($edit) {
$tools->disableList()->disableDelete();
$edit or $tools->disableEdit();
});
});
}
/**
* @param \Closure|array|AbstractTool|Renderable|Htmlable|string $callback
*/
public function tools($callback)
{
$this->show->tools($callback);
return $this;
}
public function title($title)
{
$this->show->panel()->title($title);
return $this;
}
public function fieldLabel($field, $label)
{
$setLabel = function () use ($label) {
$this->label = $label;
};
$setLabel->call($field);
return $this;
}
public function html()
{
$this->defaultCss();
return $this->show->render();
}
public function defaultCss()
{
admin_style('.box.box-solid.box-default{border:0;box-shadow:none}');
admin_style('.box-body{padding:0}');
admin_style('.control-label{padding-top:0;text-align:right}');
admin_style('.form-group{display:flex;justify-content:center;flex-wrap:nowrap;}');
}
}
\ No newline at end of file
<?php <?php
use App\Admin\Extensions\Grid\ColumnExts;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Grid\Filter;
use Dcat\Admin\Show;
/** /**
* Dcat-admin - admin builder based on Laravel. * Dcat-admin - admin builder based on Laravel.
...@@ -27,3 +24,5 @@ use Dcat\Admin\Show; ...@@ -27,3 +24,5 @@ use Dcat\Admin\Show;
Admin::navbar()->left('<a href="/admin" title="home"><i class="fa ml-1 fa-fw fa-home"></i></a>'); Admin::navbar()->left('<a href="/admin" title="home"><i class="fa ml-1 fa-fw fa-home"></i></a>');
Admin::navbar()->left('<a href="/admin/telescope" title="telescope" target="_blank"><i class="fa ml-1 fa-fw fa-telegram"></i></a>'); Admin::navbar()->left('<a href="/admin/telescope" title="telescope" target="_blank"><i class="fa ml-1 fa-fw fa-telegram"></i></a>');
ColumnExts::extend();
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!