123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698 |
- <?php
- namespace Dcat\Admin;
- use Closure;
- use Dcat\Admin\Contracts\TreeRepository;
- use Dcat\Admin\Exception\InvalidArgumentException;
- use Dcat\Admin\Repositories\EloquentRepository;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Traits\HasBuilderEvents;
- use Dcat\Admin\Traits\HasVariables;
- use Dcat\Admin\Tree\AbstractTool;
- use Dcat\Admin\Tree\Actions;
- use Dcat\Admin\Tree\Tools;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Str;
- use Illuminate\Support\Traits\Macroable;
- /**
- * Class Tree.
- *
- * @see https://github.com/dbushell/Nestable
- */
- class Tree implements Renderable
- {
- use HasBuilderEvents;
- use HasVariables;
- use Macroable;
- const SAVE_ORDER_NAME = '_order';
- /**
- * @var array
- */
- protected $items = [];
- /**
- * @var string
- */
- protected $elementId = 'tree-';
- /**
- * @var TreeRepository
- */
- protected $repository;
- /**
- * @var \Closure
- */
- protected $queryCallback;
- /**
- * View of tree to render.
- *
- * @var string
- */
- protected $view = 'admin::tree.container';
- /**
- * @var string
- */
- protected $branchView = 'admin::tree.branch';
- /**
- * @var \Closure
- */
- protected $callback;
- /**
- * @var null
- */
- protected $branchCallback = null;
- /**
- * @var string
- */
- public $path;
- /**
- * @var string
- */
- public $url;
- /**
- * @var bool
- */
- public $useCreate = true;
- /**
- * @var bool
- */
- public $useQuickCreate = true;
- /**
- * @var array
- */
- public $dialogFormDimensions = ['700px', '670px'];
- /**
- * @var bool
- */
- public $useSave = true;
- /**
- * @var bool
- */
- public $useRefresh = true;
- /**
- * @var array
- */
- protected $nestableOptions = [];
- /**
- * Header tools.
- *
- * @var Tools
- */
- public $tools;
- /**
- * @var string
- */
- protected $actionsClass;
- /**
- * @var \Closure[]
- */
- protected $actionCallbacks = [];
- /**
- * @var Closure
- */
- protected $wrapper;
- /**
- * Menu constructor.
- *
- * @param Model|TreeRepository|string|null $model
- */
- public function __construct($repository = null, ?\Closure $callback = null)
- {
- $this->repository = $this->makeRepository($repository);
- $this->path = $this->path ?: request()->getPathInfo();
- $this->url = url($this->path);
- $this->elementId .= Str::random(8);
- $this->setUpTools();
- if ($callback instanceof \Closure) {
- call_user_func($callback, $this);
- }
- $this->callResolving();
- }
- /**
- * Setup tree tools.
- */
- public function setUpTools()
- {
- $this->tools = new Tools($this);
- }
- /**
- * @param $repository
- *
- * @return TreeRepository
- */
- public function makeRepository($repository)
- {
- if (is_string($repository)) {
- $repository = new $repository();
- }
- if ($repository instanceof Model || $repository instanceof Builder) {
- $repository = EloquentRepository::make($repository);
- }
- if (! $repository instanceof TreeRepository) {
- $class = get_class($repository);
- throw new InvalidArgumentException("The class [{$class}] must be a type of [".TreeRepository::class.'].');
- }
- return $repository;
- }
- /**
- * Initialize branch callback.
- *
- * @return void
- */
- protected function setDefaultBranchCallback()
- {
- if (is_null($this->branchCallback)) {
- $this->branchCallback = function ($branch) {
- $key = $branch[$this->repository->getPrimaryKeyColumn()];
- $title = $branch[$this->repository->getTitleColumn()];
- return "$key - $title";
- };
- }
- }
- /**
- * Set branch callback.
- *
- * @param \Closure $branchCallback
- *
- * @return $this
- */
- public function branch(\Closure $branchCallback)
- {
- $this->branchCallback = $branchCallback;
- return $this;
- }
- /**
- * Set query callback this tree.
- *
- * @return $this
- */
- public function query(\Closure $callback)
- {
- $this->queryCallback = $callback;
- return $this;
- }
- /**
- * number of levels an item can be nested (default 5).
- *
- * @see https://github.com/dbushell/Nestable
- *
- * @param int $max
- *
- * @return $this
- */
- public function maxDepth(int $max)
- {
- return $this->nestable(['maxDepth' => $max]);
- }
- /**
- * Set nestable options.
- *
- * @param array $options
- *
- * @return $this
- */
- public function nestable($options = [])
- {
- $this->nestableOptions = array_merge($this->nestableOptions, $options);
- return $this;
- }
- /**
- * Disable create.
- *
- * @param bool $value
- *
- * @return void
- */
- public function disableCreateButton(bool $value = true)
- {
- $this->useCreate = ! $value;
- }
- public function showCreateButton(bool $value = true)
- {
- return $this->disableCreateButton(! $value);
- }
- public function disableQuickCreateButton(bool $value = true)
- {
- $this->useQuickCreate = ! $value;
- }
- public function showQuickCreateButton(bool $value = true)
- {
- return $this->disableQuickCreateButton(! $value);
- }
- /**
- * @param string $width
- * @param string $height
- *
- * @return $this
- */
- public function setDialogFormDimensions(string $width, string $height)
- {
- $this->dialogFormDimensions = [$width, $height];
- return $this;
- }
- /**
- * Disable save.
- *
- * @param bool $value
- *
- * @return void
- */
- public function disableSaveButton(bool $value = true)
- {
- $this->useSave = ! $value;
- }
- public function showSaveButton(bool $value = true)
- {
- return $this->disableSaveButton(! $value);
- }
- /**
- * Disable refresh.
- *
- * @param bool $value
- *
- * @return void
- */
- public function disableRefreshButton(bool $value = true)
- {
- $this->useRefresh = ! $value;
- }
- public function showRefreshButton(bool $value = true)
- {
- return $this->disableRefreshButton(! $value);
- }
- public function disableQuickEditButton(bool $value = true)
- {
- $this->actions(function (Actions $actions) use ($value) {
- $actions->disableQuickEdit($value);
- });
- }
- public function showQuickEditButton(bool $value = true)
- {
- return $this->disableQuickEditButton(! $value);
- }
- public function disableEditButton(bool $value = true)
- {
- $this->actions(function (Actions $actions) use ($value) {
- $actions->disableEdit($value);
- });
- }
- public function showEditButton(bool $value = true)
- {
- return $this->disableEditButton(! $value);
- }
- public function disableDeleteButton(bool $value = true)
- {
- $this->actions(function (Actions $actions) use ($value) {
- $actions->disableDelete($value);
- });
- }
- public function showDeleteButton(bool $value = true)
- {
- return $this->disableDeleteButton(! $value);
- }
- /**
- * @param Closure $closure
- *
- * @return $this;
- */
- public function wrap(\Closure $closure)
- {
- $this->wrapper = $closure;
- return $this;
- }
- /**
- * @return bool
- */
- public function hasWrapper()
- {
- return $this->wrapper ? true : false;
- }
- /**
- * Save tree order from a input.
- *
- * @param string $serialize
- *
- * @return bool
- */
- public function saveOrder($serialize)
- {
- $tree = json_decode($serialize, true);
- if (json_last_error() != JSON_ERROR_NONE) {
- throw new InvalidArgumentException(json_last_error_msg());
- }
- $this->repository->saveOrder($tree);
- return true;
- }
- /**
- * Set view of tree.
- *
- * @param string $view
- *
- * @return $this
- */
- public function view($view)
- {
- $this->view = $view;
- return $this;
- }
- /**
- * @param string $view
- *
- * @return $this
- */
- public function branchView($view)
- {
- $this->branchView = $view;
- return $this;
- }
- /**
- * @return \Closure
- */
- public function resolveAction()
- {
- return function ($branch) {
- $class = $this->actionsClass ?: Actions::class;
- $action = new $class();
- $action->setParent($this);
- $action->setRow($branch);
- $this->callActionCallbacks($action);
- return $action->render();
- };
- }
- protected function callActionCallbacks(Actions $actions)
- {
- foreach ($this->actionCallbacks as $callback) {
- $callback->call($actions->row, $actions);
- }
- }
- /**
- * 自定义行操作类.
- *
- * @param string $actionClass
- *
- * @return $this
- */
- public function setActionClass(string $actionClass)
- {
- $this->actionsClass = $actionClass;
- return $this;
- }
- /**
- * 设置行操作回调.
- *
- * @param \Closure|array $callback
- *
- * @return $this
- */
- public function actions($callback)
- {
- if ($callback instanceof \Closure) {
- $this->actionCallbacks[] = $callback;
- } else {
- $this->actionCallbacks[] = function (Actions $actions) use ($callback) {
- if (! is_array($callback)) {
- $callback = [$callback];
- }
- foreach ($callback as $value) {
- $actions->append(clone $value);
- }
- };
- }
- return $this;
- }
- /**
- * Return all items of the tree.
- *
- * @param array $items
- */
- public function getItems()
- {
- return $this->repository->withQuery($this->queryCallback)->toTree();
- }
- /**
- * Variables in tree template.
- *
- * @return array
- */
- public function defaultVariables()
- {
- return [
- 'id' => $this->elementId,
- 'tools' => $this->tools->render(),
- 'items' => $this->getItems(),
- 'useCreate' => $this->useCreate,
- 'useQuickCreate' => $this->useQuickCreate,
- 'useSave' => $this->useSave,
- 'useRefresh' => $this->useRefresh,
- 'createButton' => $this->renderCreateButton(),
- 'nestableOptions' => $this->nestableOptions,
- 'url' => $this->url,
- 'resolveAction' => $this->resolveAction(),
- ];
- }
- /**
- * @return mixed
- */
- public function getKeyName()
- {
- return $this->repository->getKeyName();
- }
- /**
- * @return string
- */
- public function resource()
- {
- return $this->url;
- }
- /**
- * Set resource path.
- *
- * @param string $path
- *
- * @return $this
- */
- public function setResource($path)
- {
- $this->url = admin_url($path);
- return $this;
- }
- /**
- * Setup tools.
- *
- * @param Closure|array|AbstractTool|Renderable|Htmlable|string $callback
- *
- * @return $this|Tools
- */
- public function tools($callback = null)
- {
- if ($callback === null) {
- return $this->tools;
- }
- if ($callback instanceof \Closure) {
- call_user_func($callback, $this->tools);
- return $this;
- }
- if (! is_array($callback)) {
- $callback = [$callback];
- }
- foreach ($callback as $tool) {
- $this->tools->add($tool);
- }
- return $this;
- }
- /**
- * @return string
- */
- protected function renderCreateButton()
- {
- if (! $this->useQuickCreate && ! $this->useCreate) {
- return '';
- }
- $url = $this->url.'/create';
- $new = trans('admin.new');
- $quickBtn = $btn = '';
- if ($this->useCreate) {
- $btn = "<a href='{$url}' class='btn btn-sm btn-primary'><i class='feather icon-plus'></i><span class='d-none d-sm-inline'> {$new}</span></a>";
- }
- if ($this->useQuickCreate) {
- $text = $this->useCreate ? '<i class=\' fa fa-clone\'></i>' : "<i class='feather icon-plus'></i><span class='d-none d-sm-inline'> $new</span>";
- $quickBtn = "<button data-url='$url' class='btn btn-sm btn-primary tree-quick-create'>$text</button>";
- }
- return " <div class='btn-group pull-right' style='margin-right:3px'>{$btn}{$quickBtn}</div>";
- }
- /**
- * @return void
- */
- protected function renderQuickCreateButton()
- {
- if ($this->useQuickCreate) {
- [$width, $height] = $this->dialogFormDimensions;
- Form::dialog(trans('admin.new'))
- ->click('.tree-quick-create')
- ->success('Dcat.reload()')
- ->dimensions($width, $height);
- }
- }
- /**
- * Render a tree.
- *
- * @return \Illuminate\Http\JsonResponse|string
- */
- public function render()
- {
- $this->callResolving();
- $this->setDefaultBranchCallback();
- $this->renderQuickCreateButton();
- view()->share([
- 'currentUrl' => $this->url,
- 'keyName' => $this->getKeyName(),
- 'branchView' => $this->branchView,
- 'branchCallback' => $this->branchCallback,
- ]);
- return $this->doWrap();
- }
- /**
- * @return string
- */
- protected function doWrap()
- {
- $view = view($this->view, $this->variables());
- if (! $wrapper = $this->wrapper) {
- $html = Admin::resolveHtml($view->render())['html'];
- return "<div class='card'>{$html}</div>";
- }
- return Admin::resolveHtml(Helper::render($wrapper($view)))['html'];
- }
- /**
- * Get the string contents of the grid view.
- *
- * @return string
- */
- public function __toString()
- {
- return $this->render();
- }
- /**
- * Create a tree instance.
- *
- * @param mixed ...$param
- *
- * @return $this
- */
- public static function make(...$param)
- {
- return new static(...$param);
- }
- }
|