CreateButton.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Grid;
  5. use Illuminate\Contracts\Support\Renderable;
  6. class CreateButton implements Renderable
  7. {
  8. /**
  9. * @var Grid
  10. */
  11. protected $grid;
  12. protected $mode;
  13. public function __construct(Grid $grid)
  14. {
  15. $this->grid = $grid;
  16. $this->mode = $grid->option('create_mode');
  17. }
  18. protected function renderDialogCreateButton()
  19. {
  20. if ($this->mode !== Grid::CREATE_MODE_DIALOG) {
  21. return;
  22. }
  23. $new = trans('admin.new');
  24. $url = $this->grid->getCreateUrl();
  25. $class = $this->grid->makeName('dialog-create');
  26. [$width, $height] = $this->grid->option('dialog_form_area');
  27. Form::dialog($new)
  28. ->click(".{$class}")
  29. ->success('Dcat.reload()')
  30. ->dimensions($width, $height);
  31. return "<button data-url='$url' class='btn btn-primary {$class}'><i class='feather icon-plus'></i><span class='d-none d-sm-inline'>&nbsp; $new</span></button>";
  32. }
  33. protected function renderCreateButton()
  34. {
  35. if ($this->mode && $this->mode !== Grid::CREATE_MODE_DEFAULT) {
  36. return;
  37. }
  38. $new = trans('admin.new');
  39. $url = $this->grid->getCreateUrl();
  40. return "<a href='{$url}' class='btn btn-primary'>
  41. <i class='feather icon-plus'></i><span class='d-none d-sm-inline'>&nbsp;&nbsp;{$new}</span>
  42. </a>";
  43. }
  44. public function render()
  45. {
  46. return $this->grid->tools()->format(
  47. "{$this->renderCreateButton()}{$this->renderDialogCreateButton()}"
  48. );
  49. }
  50. }