|
|
@@ -6,8 +6,10 @@ 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\Actions;
|
|
|
use Dcat\Admin\Tree\AbstractTool;
|
|
|
use Dcat\Admin\Tree\Tools;
|
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
|
@@ -23,6 +25,8 @@ class Tree implements Renderable
|
|
|
use HasVariables;
|
|
|
use Macroable;
|
|
|
|
|
|
+ const SAVE_ORDER_NAME = '_order';
|
|
|
+
|
|
|
/**
|
|
|
* @var array
|
|
|
*/
|
|
|
@@ -88,7 +92,7 @@ class Tree implements Renderable
|
|
|
/**
|
|
|
* @var array
|
|
|
*/
|
|
|
- public $dialogFormDimensions = ['700px', '620px'];
|
|
|
+ public $dialogFormDimensions = ['700px', '670px'];
|
|
|
|
|
|
/**
|
|
|
* @var bool
|
|
|
@@ -100,21 +104,6 @@ class Tree implements Renderable
|
|
|
*/
|
|
|
public $useRefresh = true;
|
|
|
|
|
|
- /**
|
|
|
- * @var bool
|
|
|
- */
|
|
|
- public $useEdit = true;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var bool
|
|
|
- */
|
|
|
- public $useQuickEdit = true;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var bool
|
|
|
- */
|
|
|
- public $useDelete = true;
|
|
|
-
|
|
|
/**
|
|
|
* @var array
|
|
|
*/
|
|
|
@@ -127,6 +116,16 @@ class Tree implements Renderable
|
|
|
*/
|
|
|
public $tools;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $actionsClass;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var \Closure[]
|
|
|
+ */
|
|
|
+ protected $actionCallbacks = [];
|
|
|
+
|
|
|
/**
|
|
|
* @var Closure
|
|
|
*/
|
|
|
@@ -255,16 +254,18 @@ class Tree implements Renderable
|
|
|
/**
|
|
|
* Disable create.
|
|
|
*
|
|
|
+ * @param bool $value
|
|
|
+ *
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function disableCreateButton()
|
|
|
+ public function disableCreateButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useCreate = false;
|
|
|
+ $this->useCreate = ! $value;
|
|
|
}
|
|
|
|
|
|
- public function disableQuickCreateButton()
|
|
|
+ public function disableQuickCreateButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useQuickCreate = false;
|
|
|
+ $this->useQuickCreate = ! $value;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -283,36 +284,46 @@ class Tree implements Renderable
|
|
|
/**
|
|
|
* Disable save.
|
|
|
*
|
|
|
+ * @param bool $value
|
|
|
+ *
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function disableSaveButton()
|
|
|
+ public function disableSaveButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useSave = false;
|
|
|
+ $this->useSave = ! $value;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Disable refresh.
|
|
|
*
|
|
|
+ * @param bool $value
|
|
|
+ *
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function disableRefreshButton()
|
|
|
+ public function disableRefreshButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useRefresh = false;
|
|
|
+ $this->useRefresh = ! $value;
|
|
|
}
|
|
|
|
|
|
- public function disableQuickEditButton()
|
|
|
+ public function disableQuickEditButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useQuickEdit = false;
|
|
|
+ $this->actions(function (Actions $actions) use ($value) {
|
|
|
+ $actions->disableQuickEdit($value);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public function disableEditButton()
|
|
|
+ public function disableEditButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useEdit = false;
|
|
|
+ $this->actions(function (Actions $actions) use ($value) {
|
|
|
+ $actions->disableEdit($value);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public function disableDeleteButton()
|
|
|
+ public function disableDeleteButton(bool $value = true)
|
|
|
{
|
|
|
- $this->useDelete = false;
|
|
|
+ $this->actions(function (Actions $actions) use ($value) {
|
|
|
+ $actions->disableDelete($value);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -355,59 +366,6 @@ class Tree implements Renderable
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Build tree grid scripts.
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function script()
|
|
|
- {
|
|
|
- $saveSucceeded = trans('admin.save_succeeded');
|
|
|
- $nestableOptions = json_encode($this->nestableOptions);
|
|
|
-
|
|
|
- return <<<JS
|
|
|
-(function () {
|
|
|
- var tree = $('#{$this->elementId}');
|
|
|
-
|
|
|
- tree.nestable($nestableOptions);
|
|
|
-
|
|
|
- $('.{$this->elementId}-save').on('click', function () {
|
|
|
- var serialize = tree.nestable('serialize'), _this = $(this);
|
|
|
- _this.buttonLoading();
|
|
|
- $.post('{$this->url}', {
|
|
|
- _order: JSON.stringify(serialize)
|
|
|
- },
|
|
|
- function (data) {
|
|
|
- _this.buttonLoading(false);
|
|
|
- Dcat.success('{$saveSucceeded}');
|
|
|
-
|
|
|
- if (typeof data.location !== "undefined") {
|
|
|
- return setTimeout(function () {
|
|
|
- if (data.location) {
|
|
|
- location.href = data.location;
|
|
|
- } else {
|
|
|
- location.reload();
|
|
|
- }
|
|
|
- }, 1500)
|
|
|
- }
|
|
|
-
|
|
|
- Dcat.reload();
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- $('.{$this->elementId}-tree-tools').on('click', function(e){
|
|
|
- var action = $(this).data('action');
|
|
|
- if (action === 'expand') {
|
|
|
- tree.nestable('expandAll');
|
|
|
- }
|
|
|
- if (action === 'collapse') {
|
|
|
- tree.nestable('collapseAll');
|
|
|
- }
|
|
|
- });
|
|
|
-})()
|
|
|
-JS;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Set view of tree.
|
|
|
*
|
|
|
@@ -434,6 +392,60 @@ JS;
|
|
|
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 $callback
|
|
|
+ *
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function actions(\Closure $callback)
|
|
|
+ {
|
|
|
+ $this->actionCallbacks[] = $callback;
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Return all items of the tree.
|
|
|
*
|
|
|
@@ -452,20 +464,46 @@ JS;
|
|
|
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,
|
|
|
- 'useEdit' => $this->useEdit,
|
|
|
- 'useQuickEdit' => $this->useQuickEdit,
|
|
|
- 'useDelete' => $this->useDelete,
|
|
|
- 'createButton' => $this->renderCreateButton(),
|
|
|
+ '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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $path
|
|
|
+ *
|
|
|
+ * @return $this|string
|
|
|
+ */
|
|
|
+ public function resource(string $path = null)
|
|
|
+ {
|
|
|
+ if ($path === null) {
|
|
|
+ return $this->url;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (! empty($path)) {
|
|
|
+ $this->url = admin_url($path);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Setup tools.
|
|
|
*
|
|
|
@@ -521,21 +559,6 @@ JS;
|
|
|
return " <div class='btn-group pull-right' style='margin-right:3px'>{$btn}{$quickBtn}</div>";
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @return void
|
|
|
- */
|
|
|
- protected function renderQuickEditButton()
|
|
|
- {
|
|
|
- if ($this->useQuickEdit) {
|
|
|
- [$width, $height] = $this->dialogFormDimensions;
|
|
|
-
|
|
|
- Form::dialog(trans('admin.edit'))
|
|
|
- ->click('.tree-quick-edit')
|
|
|
- ->success('Dcat.reload()')
|
|
|
- ->dimensions($width, $height);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* @return void
|
|
|
*/
|
|
|
@@ -563,14 +586,11 @@ JS;
|
|
|
|
|
|
$this->setDefaultBranchCallback();
|
|
|
|
|
|
- $this->renderQuickEditButton();
|
|
|
$this->renderQuickCreateButton();
|
|
|
|
|
|
- Admin::script($this->script());
|
|
|
-
|
|
|
view()->share([
|
|
|
'currentUrl' => $this->url,
|
|
|
- 'keyName' => $this->repository->getKeyName(),
|
|
|
+ 'keyName' => $this->getKeyName(),
|
|
|
'branchView' => $this->branchView,
|
|
|
'branchCallback' => $this->branchCallback,
|
|
|
]);
|
|
|
@@ -589,10 +609,12 @@ JS;
|
|
|
$view = view($this->view, $this->variables());
|
|
|
|
|
|
if (! $wrapper = $this->wrapper) {
|
|
|
- return "<div class='card'>{$view->render()}</div>";
|
|
|
+ $html = Admin::resolveHtml($view->render())['html'];
|
|
|
+
|
|
|
+ return "<div class='card'>{$html}</div>";
|
|
|
}
|
|
|
|
|
|
- return $wrapper($view);
|
|
|
+ return Admin::resolveHtml(Helper::render($wrapper($view)))['html'];
|
|
|
}
|
|
|
|
|
|
protected function handleException(\Throwable $e)
|