Navbar.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. use Dcat\Admin\Support\Helper;
  4. use Dcat\Admin\Traits\HasBuilderEvents;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\Support\Renderable;
  7. class Navbar implements Renderable
  8. {
  9. use HasBuilderEvents;
  10. /**
  11. * @var array
  12. */
  13. protected $elements = [];
  14. /**
  15. * Navbar constructor.
  16. */
  17. public function __construct()
  18. {
  19. $this->elements = [
  20. 'left' => collect(),
  21. 'right' => collect(),
  22. ];
  23. $this->callResolving();
  24. }
  25. /**
  26. * @param string|\Closure|Renderable|Htmlable $element
  27. *
  28. * @return $this
  29. */
  30. public function left($element)
  31. {
  32. $this->elements['left']->push($element);
  33. return $this;
  34. }
  35. /**
  36. * @param string|\Closure|Renderable|Htmlable $element
  37. *
  38. * @return $this
  39. */
  40. public function right($element)
  41. {
  42. $this->elements['right']->push($element);
  43. return $this;
  44. }
  45. /**
  46. * @param string $part
  47. *
  48. * @return mixed
  49. */
  50. public function render($part = 'right')
  51. {
  52. $this->callComposing($part);
  53. if (! isset($this->elements[$part]) || $this->elements[$part]->isEmpty()) {
  54. return '';
  55. }
  56. return $this->elements[$part]->map([Helper::class, 'render'])->implode('');
  57. }
  58. }