Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Nov 5, 2024
1 parent 35bab0a commit d9ff159
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
34 changes: 21 additions & 13 deletions src/CP/Navigation/NavBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ protected function applyPreferenceOverrides($preferences = null)
->each(fn ($overrides) => $this->createPendingItemsForSection($overrides))
->each(fn ($overrides) => $this->applyPreferenceOverridesForSection($overrides));

if ($navPreferencesConfig['reorder']) {
$this->setSectionOrder($sections);
if ($reorder = $navPreferencesConfig['reorder']) {
$this->setSectionOrder($sections, $reorder);
}

return $this;
Expand Down Expand Up @@ -317,8 +317,8 @@ protected function applyPreferenceOverridesForSection($sectionNav)
->filter()
->each(fn ($item) => $item->isChild(false));

if ($sectionNav['reorder']) {
$this->setSectionItemOrder($section, $sectionNav['items']);
if ($reorder = $sectionNav['reorder']) {
$this->setSectionItemOrder($section, $sectionNav['items'], $reorder);
}
}

Expand Down Expand Up @@ -442,23 +442,31 @@ protected function renameSection($sectionKey)
/**
* Set section order.
*
* @param array $sections
* @param array $reorder
*/
protected function setSectionOrder($sections)
protected function setSectionOrder($sections, $reorder)
{
// Get conconfigured core sections...
// Get unconfigured core sections...
$unconfiguredCoreSections = $this->sections;

// Get unconfigured sections...
$unconfiguredRegisteredSections = collect($this->items)->map->section()->filter()->unique();

// Merge unconfigured sections onto the end of the list and map their order...
$this->sectionsOrder = collect($sections)
->pluck('display')
->merge($unconfiguredRegisteredSections)
ray($sections, $this->sections, $unconfiguredRegisteredSections);
// Get merged unique list of sections...
$order = collect()
->merge($unconfiguredCoreSections)
->merge($unconfiguredRegisteredSections)
->unique()
->values()
->values();

ray($reorder)->purple();
$reorder = collect($reorder)
->map(fn ($sectionKey) => $sections->pluck('display')->flip()->get($sectionKey));

ray($reorder)->purple();

// exit;
return $order
->mapWithKeys(fn ($section, $index) => [$section => $index + 1])
->all();
}
Expand Down
4 changes: 2 additions & 2 deletions src/CP/Navigation/NavPreferencesNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function __construct($navPreferences)
*/
public static function fromPreferences($navPreferences)
{
return (new static($navPreferences))
return ray()->pass((new static($navPreferences))
->normalize()
->get();
->get());
}

/**
Expand Down
55 changes: 41 additions & 14 deletions src/CP/Navigation/NavTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\CP\Navigation;

use Facades\Statamic\CP\Navigation\NavItemIdHasher;
use Illuminate\Support\Collection;
use Statamic\Facades\CP\Nav;
use Statamic\Support\Arr;
use Statamic\Support\Str;
Expand Down Expand Up @@ -30,6 +31,8 @@ public function __construct(array $submitted)
*/
public static function fromVue(array $submitted)
{
ray($submitted)->orange();

return (new static($submitted))
->transform()
->minify()
Expand Down Expand Up @@ -256,10 +259,12 @@ protected function transformItemUrl($url)
*/
protected function getReorderedItems($originalList, $newList): bool|array
{
$itemsAreReordered = collect($originalList)
$comparableLists = collect($originalList)
->intersect($newList)
->values()
->zip($newList)
->zip($newList);

$itemsAreReordered = $comparableLists
->reject(fn ($pair) => is_null($pair->first()))
->reject(fn ($pair) => $pair->first() === $pair->last())
->isNotEmpty();
Expand All @@ -269,33 +274,55 @@ protected function getReorderedItems($originalList, $newList): bool|array
}

return collect($newList)
->take($this->calculateMinimumItemsForReorder($originalList, $newList))
->take($this->calculateMinimumItemsForReorder($comparableLists->map->first(), $comparableLists->map->last()))
->all();
}

/**
* Calculate minimum number of items needed for reorder config.
*
* @param array $originalList
* @param array $newList
*/
protected function calculateMinimumItemsForReorder($originalList, $newList): int
protected function calculateMinimumItemsForReorder(Collection $originalList, Collection $newList): int
{
$continueFiltering = true;

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

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

return $continueFiltering = false;
});

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

/**
* Reject new items from the tail end end of our new list by checking to see if they existed in the old list.
*/
protected function rejectNewItemsFromEndOfNewList(Collection $originalList, Collection $newList): Collection
{
$continueRejecting = true;

$minimumItemsCount = collect($originalList)
ray($originalList, $newList)->purple();

return $newList
->reverse()
->zip(collect($newList)->reverse())
->reject(function ($pair) use (&$continueRejecting) {
if ($continueRejecting && $pair->first() === $pair->last()) {
->reject(function ($item) use ($originalList, &$continueRejecting) {
if ($continueRejecting && ! $originalList->contains($item)) {
return true;
}

return $continueRejecting = false;
})
->count();

return max(1, $minimumItemsCount - 1);
->reverse()
->values();
}

/**
Expand Down

0 comments on commit d9ff159

Please sign in to comment.