您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PanelShiftDropdown.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Filament\Components;
  3. use Filament\Contracts\Plugin;
  4. use Filament\Panel;
  5. use Filament\Panel\Concerns\HasNavigation;
  6. use Filament\View\PanelsRenderHook;
  7. class PanelShiftDropdown implements Plugin
  8. {
  9. use HasNavigation;
  10. protected string $view = 'components.panel-shift-dropdown';
  11. protected string $renderHook = PanelsRenderHook::USER_MENU_BEFORE;
  12. protected bool $hasDisplayAndAccessibility = true;
  13. protected bool $hasCompanySettings = true;
  14. protected bool $hasLogoutItem = true;
  15. protected int $groupIndex = 0;
  16. public function displayAndAccessibility(bool $condition = true): static
  17. {
  18. $this->hasDisplayAndAccessibility = $condition;
  19. return $this;
  20. }
  21. public function hasDisplayAndAccessibility(): bool
  22. {
  23. return $this->hasDisplayAndAccessibility;
  24. }
  25. public function companySettings(bool $condition = true): static
  26. {
  27. $this->hasCompanySettings = $condition;
  28. return $this;
  29. }
  30. public function hasCompanySettings(): bool
  31. {
  32. return $this->hasCompanySettings;
  33. }
  34. public function logoutItem(bool $condition = true): static
  35. {
  36. $this->hasLogoutItem = $condition;
  37. return $this;
  38. }
  39. public function hasLogoutItem(): bool
  40. {
  41. return $this->hasLogoutItem;
  42. }
  43. public function getNavigation(): array
  44. {
  45. if ($this->hasNavigationBuilder()) {
  46. return $this->buildNavigation();
  47. }
  48. return [];
  49. }
  50. public static function make(): static
  51. {
  52. return app(static::class);
  53. }
  54. public function getId(): string
  55. {
  56. return 'panel-shift-dropdown';
  57. }
  58. public function register(Panel $panel): void
  59. {
  60. if ($this->hasNavigation()) {
  61. $panel->renderHook($this->getRenderHook(), function () {
  62. return view($this->view, [
  63. 'component' => $this,
  64. ]);
  65. });
  66. }
  67. }
  68. public function getNavigationAsHierarchyArray(): array
  69. {
  70. $navigation = $this->getNavigation();
  71. $panels = $this->initializePanels();
  72. foreach ($navigation as $item) {
  73. $this->processItem($item, $panels);
  74. }
  75. $this->addCompanySettingsItems($panels);
  76. $this->addAccessibilityItems($panels);
  77. return $panels;
  78. }
  79. protected function initializePanels(): array
  80. {
  81. return [
  82. 'main' => [
  83. 'panelId' => 'main',
  84. 'label' => 'Main',
  85. 'items' => [],
  86. 'renderItems' => true,
  87. ],
  88. ];
  89. }
  90. protected function processItem($item, array &$panels, $parentId = 'main'): void
  91. {
  92. if (method_exists($item, 'getItems') && ! empty($item->getLabel())) {
  93. $this->processGroupItem($item, $panels, $parentId);
  94. } elseif (method_exists($item, 'getItems') && empty($item->getLabel())) {
  95. foreach ($item->getItems() as $groupItem) {
  96. $this->processItem($groupItem, $panels, $parentId);
  97. }
  98. } elseif (method_exists($item, 'getChildItems') && ! empty($item->getChildItems())) {
  99. $this->processNavigationItem($item, $panels, $parentId);
  100. } else {
  101. $this->addStandaloneItem($item, $panels, $parentId);
  102. }
  103. }
  104. protected function processGroupItem($item, array &$panels, $parentId): void
  105. {
  106. $uniqueId = 'group-' . ++$this->groupIndex;
  107. $panels[$uniqueId] = $this->createPanel($uniqueId, $item);
  108. $panels[$parentId]['items'][] = $this->createPanelReference($uniqueId, $item);
  109. foreach ($item->getItems() as $groupItem) {
  110. $this->processItem($groupItem, $panels, $uniqueId);
  111. }
  112. }
  113. protected function processNavigationItem($item, array &$panels, $parentId): void
  114. {
  115. $uniqueId = 'group-' . ++$this->groupIndex;
  116. $panels[$uniqueId] = $this->createPanel($uniqueId, $item);
  117. $panels[$parentId]['items'][] = $this->createPanelReference($uniqueId, $item);
  118. foreach ($item->getChildItems() as $childItem) {
  119. $this->processItem($childItem, $panels, $uniqueId);
  120. }
  121. }
  122. protected function addStandaloneItem($item, array &$panels, $parentId): void
  123. {
  124. $panels[$parentId]['items'][] = [
  125. 'url' => $item->getUrl(),
  126. 'label' => $item->getLabel(),
  127. 'icon' => $item->getIcon(),
  128. ];
  129. }
  130. protected function addAccessibilityItems(array &$panels): void
  131. {
  132. if ($this->hasDisplayAndAccessibility()) {
  133. $displayAndAccessibilityId = 'display-and-accessibility';
  134. $panels['main']['items'][] = [
  135. 'panelId' => $displayAndAccessibilityId,
  136. 'label' => 'Display & Accessibility',
  137. 'icon' => 'heroicon-s-moon',
  138. ];
  139. $panels[$displayAndAccessibilityId] = [
  140. 'panelId' => $displayAndAccessibilityId,
  141. 'label' => 'Display & Accessibility',
  142. 'items' => [],
  143. 'renderItems' => false,
  144. ];
  145. }
  146. }
  147. protected function addCompanySettingsItems(array &$panels): void
  148. {
  149. if ($this->hasCompanySettings()) {
  150. $companySettingsId = 'company-settings';
  151. $panels['main']['items'][] = [
  152. 'panelId' => $companySettingsId,
  153. 'label' => 'Company Settings',
  154. 'icon' => 'heroicon-s-building-office-2',
  155. ];
  156. $panels[$companySettingsId] = [
  157. 'panelId' => $companySettingsId,
  158. 'label' => 'Company Settings',
  159. 'items' => [],
  160. 'renderItems' => false,
  161. ];
  162. $switchCompanyPanelId = 'company-switcher';
  163. $panels[$companySettingsId]['items'][] = [
  164. 'panelId' => $switchCompanyPanelId,
  165. 'label' => 'Switch Company',
  166. 'icon' => '',
  167. ];
  168. $panels[$switchCompanyPanelId] = [
  169. 'panelId' => $switchCompanyPanelId,
  170. 'label' => 'Switch Company',
  171. 'items' => [],
  172. 'renderItems' => false,
  173. ];
  174. }
  175. }
  176. protected function createPanel($uniqueId, $item, $renderItems = true): array
  177. {
  178. return [
  179. 'panelId' => $uniqueId,
  180. 'label' => $item->getLabel(),
  181. 'icon' => $item->getIcon(),
  182. 'items' => [],
  183. 'renderItems' => $renderItems,
  184. ];
  185. }
  186. protected function createPanelReference($uniqueId, $item): array
  187. {
  188. return [
  189. 'panelId' => $uniqueId,
  190. 'label' => $item->getLabel(),
  191. 'icon' => $item->getIcon(),
  192. ];
  193. }
  194. public function boot(Panel $panel): void
  195. {
  196. // TODO: Implement boot() method.
  197. }
  198. public function renderHook(string $hook): static
  199. {
  200. $this->renderHook = $hook;
  201. return $this;
  202. }
  203. public function getRenderHook(): string
  204. {
  205. return $this->renderHook;
  206. }
  207. }