Navbar.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Support\Str;
  6. class Navbar extends Widget
  7. {
  8. const DROPDOWN_FLAG_KEY = '__dropdown__';
  9. protected $view = 'admin::widgets.navbar';
  10. protected $id;
  11. protected $title;
  12. /**
  13. * @var \Closure
  14. */
  15. protected $builder;
  16. /**
  17. * @var array
  18. */
  19. protected $items = [
  20. 'right' => [],
  21. 'left' => [],
  22. ];
  23. protected $active;
  24. protected $click = false;
  25. /**
  26. * Navbar constructor.
  27. */
  28. public function __construct(?string $title = '#', $items = [])
  29. {
  30. $this->title($title);
  31. $this->add($items);
  32. $this->class('navbar navbar-default');
  33. $this->id = 'navbar-'.Str::random(8);
  34. }
  35. public function title($title)
  36. {
  37. $this->title = $title;
  38. return $this;
  39. }
  40. public function noShadow()
  41. {
  42. return $this->class('no-shadow', true);
  43. }
  44. public function margin($value)
  45. {
  46. return $this->style('margin:'.$value);
  47. }
  48. public function add($items, bool $right = false)
  49. {
  50. if ($right) {
  51. $this->items['right'] = array_merge($this->items['right'], Helper::array($items));
  52. } else {
  53. $this->items['left'] = array_merge($this->items['left'], Helper::array($items));
  54. }
  55. return $this;
  56. }
  57. public function left($items)
  58. {
  59. return $this->add($items, false);
  60. }
  61. public function right($items)
  62. {
  63. return $this->add($items, true);
  64. }
  65. public function checked($key)
  66. {
  67. $this->active = $key;
  68. return $this;
  69. }
  70. public function click()
  71. {
  72. $this->click = true;
  73. return $this;
  74. }
  75. public function map(\Closure $closure)
  76. {
  77. $this->builder = $closure;
  78. return $this;
  79. }
  80. public function dropdown(
  81. ?string $text,
  82. array $options,
  83. \Closure $closure = null,
  84. bool $right = false
  85. ) {
  86. $dropdown = Dropdown::make($options)
  87. ->button($text)
  88. ->buttonClass('')
  89. ->template('%s<ul class="dropdown-menu">%s</ul>');
  90. if ($closure) {
  91. $closure($dropdown);
  92. }
  93. $key = $right ? 'right' : 'left';
  94. $this->items[$key][self::DROPDOWN_FLAG_KEY] = $dropdown;
  95. return $this;
  96. }
  97. public function variables()
  98. {
  99. foreach ($this->items['left'] as $k => &$item) {
  100. $item = $this->formatItem($k, $item);
  101. }
  102. foreach ($this->items['right'] as $k => &$item) {
  103. $item = $this->formatItem($k, $item);
  104. }
  105. if ($this->click) {
  106. Admin::script(
  107. <<<JS
  108. $('#{$this->id} li.nav-li').click(function () {
  109. $('#{$this->id} li.nav-li').removeClass('active');
  110. $(this).addClass('active');
  111. });
  112. JS
  113. );
  114. }
  115. return [
  116. 'id' => $this->id,
  117. 'title' => $this->title,
  118. 'items' => $this->items,
  119. 'attributes' => $this->formatHtmlAttributes(),
  120. 'actives' => $this->actives,
  121. ];
  122. }
  123. protected function formatItem($k, $item)
  124. {
  125. if ($k === self::DROPDOWN_FLAG_KEY) {
  126. return $item;
  127. }
  128. if ($builder = $this->builder) {
  129. $item = $builder($item, $k);
  130. }
  131. if (strpos($item, '</li>')) {
  132. return $item;
  133. }
  134. $active = $this->active === $k ? 'active' : '';
  135. $item = strpos($item, '</a>') ? $item : "<a href='javascript:void(0)'>$item</a>";
  136. return "<li class='nav-li $active'>$item</li>";
  137. }
  138. }