mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Reviewed and refactored next/previous navigation button implementation
- Updated styling to include item name. - Extracted used text to translations. - Updated the design to better suit the surrounding blocks. - Removed newly added model/repo methods. - Moved core logic out of controller and instead into a "NextPreviousContentLocator" helper with re-uses the output from the book-tree generation. - Also added the system to chapters. For #2511
This commit is contained in:
69
app/Entities/Tools/NextPreviousContentLocator.php
Normal file
69
app/Entities/Tools/NextPreviousContentLocator.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php namespace BookStack\Entities\Tools;
|
||||
|
||||
use BookStack\Entities\Models\BookChild;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Finds the next or previous content of a book element (page or chapter).
|
||||
*/
|
||||
class NextPreviousContentLocator
|
||||
{
|
||||
protected $relativeBookItem;
|
||||
protected $flatTree;
|
||||
protected $currentIndex = null;
|
||||
|
||||
/**
|
||||
* NextPreviousContentLocator constructor.
|
||||
*/
|
||||
public function __construct(BookChild $relativeBookItem, Collection $bookTree)
|
||||
{
|
||||
$this->relativeBookItem = $relativeBookItem;
|
||||
$this->flatTree = $this->treeToFlatOrderedCollection($bookTree);
|
||||
$this->currentIndex = $this->getCurrentIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next logical entity within the book hierarchy.
|
||||
*/
|
||||
public function getNext(): ?Entity
|
||||
{
|
||||
return $this->flatTree->get($this->currentIndex + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next logical entity within the book hierarchy.
|
||||
*/
|
||||
public function getPrevious(): ?Entity
|
||||
{
|
||||
return $this->flatTree->get($this->currentIndex - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the index of the current relative item.
|
||||
*/
|
||||
protected function getCurrentIndex(): ?int
|
||||
{
|
||||
$index = $this->flatTree->search(function (Entity $entity) {
|
||||
return get_class($entity) === get_class($this->relativeBookItem)
|
||||
&& $entity->id === $this->relativeBookItem->id;
|
||||
});
|
||||
return $index === false ? null : $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a book tree collection to a flattened version
|
||||
* where all items follow the expected order of user flow.
|
||||
*/
|
||||
protected function treeToFlatOrderedCollection(Collection $bookTree): Collection
|
||||
{
|
||||
$flatOrdered = collect();
|
||||
/** @var Entity $item */
|
||||
foreach ($bookTree->all() as $item) {
|
||||
$flatOrdered->push($item);
|
||||
$childPages = $item->visible_pages ?? [];
|
||||
$flatOrdered = $flatOrdered->concat($childPages);
|
||||
}
|
||||
return $flatOrdered;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user