'admin::tree', 'branch' => 'admin::tree.branch', ]; /** * @var \Closure */ protected $callback; /** * @var null */ protected $branchCallback = null; /** * @var bool */ public $useCreate = true; /** * @var bool */ public $useSave = true; /** * @var bool */ public $useRefresh = true; /** * @var array */ protected $nestableOptions = []; /** * Header tools. * * @var Tools */ public $tools; /** * Menu constructor. * * @param Model|null $model */ public function __construct(Model $model = null, \Closure $callback = null) { $this->model = $model; $this->path = app('request')->getPathInfo(); $this->elementId .= uniqid(); $this->setupTools(); if ($callback instanceof \Closure) { call_user_func($callback, $this); } $this->initBranchCallback(); } /** * Setup tree tools. */ public function setupTools() { $this->tools = new Tools($this); } /** * Initialize branch callback. * * @return void */ protected function initBranchCallback() { if (is_null($this->branchCallback)) { $this->branchCallback = function ($branch) { $key = $branch[$this->model->getKeyName()]; $title = $branch[$this->model->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 Model */ public function query(\Closure $callback) { $this->queryCallback = $callback; return $this; } /** * Set nestable options. * * @param array $options * * @return $this */ public function nestable($options = []) { $this->nestableOptions = array_merge($this->nestableOptions, $options); return $this; } /** * Disable create. * * @return void */ public function disableCreate() { $this->useCreate = false; } /** * Disable save. * * @return void */ public function disableSave() { $this->useSave = false; } /** * Disable refresh. * * @return void */ public function disableRefresh() { $this->useRefresh = 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->model->saveOrder($tree); return true; } /** * Build tree grid scripts. * * @return string */ protected function script() { $deleteConfirm = trans('admin.delete_confirm'); $saveSucceeded = trans('admin.save_succeeded'); $refreshSucceeded = trans('admin.refresh_succeeded'); $deleteSucceeded = trans('admin.delete_succeeded'); $confirm = trans('admin.confirm'); $cancel = trans('admin.cancel'); $nestableOptions = json_encode($this->nestableOptions); return <<