form = $builder; $this->appends = new Collection(); $this->prepends = new Collection(); } /** * Append a tools. * * @param mixed $tool * * @return $this */ public function append($tool) { $this->appends->push($tool); return $this; } /** * Prepend a tool. * * @param mixed $tool * * @return $this */ public function prepend($tool) { $this->prepends->push($tool); return $this; } /** * Disable `list` tool. * * @return $this */ public function disableList(bool $disable = true) { $this->showList = !$disable; return $this; } /** * Disable `delete` tool. * * @return $this */ public function disableDelete(bool $disable = true) { $this->showDelete = !$disable; return $this; } /** * Disable `view` tool. * * @return $this */ public function disableView(bool $disable = true) { $this->showView = !$disable; return $this; } /** * Get request path for resource list. * * @return string */ protected function getListPath() { return $this->form->getResource(); } /** * Get request path for edit. * * @return string */ protected function getDeletePath() { return $this->getViewPath(); } /** * Get request path for delete. * * @return string */ protected function getViewPath() { $key = $this->form->getResourceId(); if ($key) { return $this->getListPath().'/'.$key; } else { return $this->getListPath(); } } /** * Get parent form of tool. * * @return Builder */ public function form() { return $this->form; } /** * Render list button. * * @return string */ protected function renderList() { if (!$this->showList) { return; } $text = trans('admin.list'); return << EOT; } /** * Render list button. * * @return string */ protected function renderView() { if (!$this->showView) { return; } $view = trans('admin.view'); return << HTML; } /** * Render `delete` tool. * * @return string */ protected function renderDelete() { if (!$this->showDelete) { return; } $delete = trans('admin.delete'); return << HTML; } /** * Render custom tools. * * @param Collection $tools * * @return mixed */ protected function renderCustomTools($tools) { if ($this->form->isCreating()) { $this->disableView(); $this->disableDelete(); } if (empty($tools)) { return ''; } return $tools->map([Helper::class, 'render'])->implode(' '); } /** * Render tools. * * @return string */ public function render() { $output = $this->renderCustomTools($this->prepends); foreach ($this->tools as $tool) { $renderMethod = 'render'.ucfirst($tool); $output .= $this->$renderMethod(); } return $output.$this->renderCustomTools($this->appends); } }