Skip to content

Commit

Permalink
Pass new nav transformer tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Nov 22, 2024
1 parent e065976 commit d4b3cc3
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/CP/Navigation/NavTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ protected function removeEmptyCustomSections($submitted)
protected function transform()
{
$this->config['reorder'] = $this->getReorderedItems(
$this->coreNav->map(fn ($section) => $this->transformSectionKey($section)),
collect($this->submitted)->map(fn ($section) => $this->transformSectionKey($section)),
$this->transformReorderableSections($this->coreNav),
$this->transformReorderableSections($this->submitted),
);

$this->config['sections'] = collect($this->submitted)
Expand All @@ -72,6 +72,16 @@ protected function transform()
return $this;
}

/**
* Transform reorderable sections list.
*/
protected function transformReorderableSections(Collection|array $reorderableSections): Collection
{
return collect($reorderableSections)
->map(fn ($section) => $this->transformSectionKey($section))
->reject(fn ($section) => $section === 'top_level');
}

/**
* Transform section key.
*
Expand Down Expand Up @@ -281,24 +291,27 @@ protected function getReorderedItems($originalList, $newList): bool|array
*/
protected function calculateMinimumItemsForReorder(Collection $originalList, Collection $newList): int
{
$continueFiltering = true;
$originalList = $originalList->filter();

$newList = $this->rejectNewItemsFromEndOfNewList($originalList, $newList);
$newList = $this->rejectNewItemsFromEndOfNewList($originalList, $newList)->filter();

$redundantTailItems = $originalList
->filter()
->values()
->reverse()
->zip($newList->reverse())
->filter(function ($pair) use (&$continueFiltering) {
if ($continueFiltering && $pair->first() === $pair->last()) {
return true;
}
$minimumItemsCount = 0;

while (true) {
if ($originalList->all() == $newList->all()) {
break;
}

return $continueFiltering = false;
});
$originalList = $originalList
->reject(fn ($item) => $item === $newList->first())
->values();

$newList->shift();

$minimumItemsCount++;
}

return max(1, $newList->count() - $redundantTailItems->count() - 1);
return $minimumItemsCount;
}

/**
Expand Down Expand Up @@ -348,7 +361,6 @@ protected function minify()
->pipe(fn ($sections) => $this->rejectInherits($sections));

$reorder = collect(Arr::get($this->config, 'reorder') ?: [])
->reject(fn ($section) => $section === 'top_level')
->values()
->all();

Expand Down

0 comments on commit d4b3cc3

Please sign in to comment.