2021-12-25 13:43:41 +01:00
|
|
|
import get from 'lodash.get'
|
|
|
|
import orderBy from 'lodash.orderby'
|
|
|
|
import Link from 'next/link'
|
2022-05-30 16:26:19 +02:00
|
|
|
import LeftIcon from 'shared/components/icons/left.js'
|
|
|
|
import RightIcon from 'shared/components/icons/right.js'
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
// helper method to order nav entries
|
2022-10-04 19:25:57 +02:00
|
|
|
const order = (obj) => orderBy(obj, ['__order', '__title'], ['asc', 'asc'])
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
// Helper method to filter out the real children
|
2022-10-04 19:25:57 +02:00
|
|
|
const currentChildren = (current) =>
|
|
|
|
Object.values(order(current)).filter((entry) => typeof entry === 'object')
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
// Helper method to get the siblings
|
2022-10-04 19:25:57 +02:00
|
|
|
const currentSiblings = (app) =>
|
|
|
|
currentChildren(get(app.navigation, app.slug.split('/').slice(0, -1)))
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
// Helper method to get the parents
|
2022-10-04 19:25:57 +02:00
|
|
|
const currentParents = (app) =>
|
|
|
|
currentChildren(get(app.navigation, app.slug.split('/').slice(0, -2)))
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
// Helper method to get current node
|
2022-10-04 19:25:57 +02:00
|
|
|
const current = (app) => get(app.navigation, app.slug.split('/'))
|
2021-12-25 13:43:41 +01:00
|
|
|
|
2022-10-04 19:25:57 +02:00
|
|
|
const previous = (app) => {
|
2021-12-25 13:43:41 +01:00
|
|
|
// Previous sibling (aside)
|
|
|
|
const aside = currentSiblings(app)
|
|
|
|
if (aside.length > 0) {
|
|
|
|
let next = false
|
|
|
|
for (const node of aside.reverse()) {
|
|
|
|
if (next) return node
|
2022-06-01 19:57:39 +02:00
|
|
|
if (node?.__slug && node.__slug === app.slug) next = true
|
2021-12-25 13:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Previous parent (up)
|
|
|
|
const up = currentParents(app)
|
|
|
|
if (up.length > 0) {
|
|
|
|
let next = false
|
|
|
|
for (const node of up.reverse()) {
|
|
|
|
if (next) return node
|
2022-06-01 19:57:39 +02:00
|
|
|
if (node?.__slug && node.__slug === app.slug.slice(0, node.__slug.length)) next = true
|
2021-12-25 13:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:25:57 +02:00
|
|
|
const next = (app) => {
|
2021-12-25 13:43:41 +01:00
|
|
|
// Next child (down)
|
|
|
|
const down = currentChildren(current(app))
|
|
|
|
if (down.length > 0) return down[0]
|
|
|
|
|
|
|
|
// Next sibling (aside)
|
|
|
|
const aside = currentSiblings(app)
|
|
|
|
if (aside.length > 0) {
|
|
|
|
let next = false
|
|
|
|
for (const node of aside) {
|
|
|
|
if (next) return node
|
2022-06-01 19:57:39 +02:00
|
|
|
if (node?.__slug && node.__slug === app.slug) next = true
|
2021-12-25 13:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next parent (up)
|
|
|
|
const up = currentParents(app)
|
|
|
|
if (up.length > 0) {
|
|
|
|
let next = false
|
|
|
|
for (const node of up) {
|
|
|
|
if (next) return node
|
2022-06-01 19:57:39 +02:00
|
|
|
if (node?.__slug && node.__slug === app.slug.slice(0, node.__slug.length)) next = true
|
2021-12-25 13:43:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:25:57 +02:00
|
|
|
const renderPrevious = (node) =>
|
|
|
|
node ? (
|
2022-05-30 16:26:19 +02:00
|
|
|
<div className="flex flex-row gap-2 items-center">
|
2022-10-12 22:06:41 +02:00
|
|
|
<LeftIcon className="w-6 h-6" />
|
2022-10-04 19:25:57 +02:00
|
|
|
<Link href={'/' + node.__slug}>
|
|
|
|
<a className="text-secondary break-words">{node.__linktitle}</a>
|
2021-12-27 14:59:32 +01:00
|
|
|
</Link>
|
|
|
|
</div>
|
2022-10-04 19:25:57 +02:00
|
|
|
) : (
|
|
|
|
<span></span>
|
|
|
|
)
|
2021-12-25 13:43:41 +01:00
|
|
|
|
2022-10-04 19:25:57 +02:00
|
|
|
const renderNext = (node) =>
|
|
|
|
node ? (
|
2022-10-12 22:06:41 +02:00
|
|
|
<div className="flex flex-row gap-2 items-center justify-end">
|
2022-10-04 19:25:57 +02:00
|
|
|
<Link href={'/' + node.__slug}>
|
|
|
|
<a className="text-right break-words">{node.__linktitle}</a>
|
2021-12-27 14:59:32 +01:00
|
|
|
</Link>
|
2022-10-12 22:06:41 +02:00
|
|
|
<RightIcon className="w-6 h-6" />
|
2021-12-27 14:59:32 +01:00
|
|
|
</div>
|
2022-10-04 19:25:57 +02:00
|
|
|
) : (
|
|
|
|
<span></span>
|
|
|
|
)
|
2021-12-25 13:43:41 +01:00
|
|
|
|
|
|
|
const PrevNext = ({ app }) => {
|
|
|
|
return (
|
2022-10-12 22:06:41 +02:00
|
|
|
<div className="grid grid-cols-2 gap-4 border-t mt-12 py-2">
|
2021-12-25 13:43:41 +01:00
|
|
|
{renderPrevious(previous(app))}
|
|
|
|
{renderNext(next(app))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PrevNext
|