mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-25 16:19:44 +02:00
refactor createMenuTree (#44)
* refactor createmenutree * Update src/routes/middlewares/pages.js Co-Authored-By: khaydarov <murod.haydarov@inbox.ru> * push migration * update * remove migration and order is optional * eslint fixes
This commit is contained in:
parent
529aca5e29
commit
7890d17255
5 changed files with 79 additions and 65 deletions
|
@ -23,6 +23,15 @@ class PagesOrder {
|
|||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all records about page's order
|
||||
*
|
||||
* @returns {Promise<PagesOrder[]>}
|
||||
*/
|
||||
static async getAll() {
|
||||
return Model.getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes the child page to the parent's order list
|
||||
*
|
||||
|
@ -65,10 +74,15 @@ class PagesOrder {
|
|||
* @return {Page[]}
|
||||
*/
|
||||
static async getOrderedChildren(pages, currentPageId, parentPageId, ignoreSelf = false) {
|
||||
const children = await PagesOrder.get(parentPageId);
|
||||
const children = await Model.get(parentPageId);
|
||||
const unordered = pages.filter(page => page._parent === parentPageId).map(page => page._id);
|
||||
|
||||
// Create unique array with ordered and unordered pages id
|
||||
const ordered = [ ...new Set([...children.order, ...unordered]) ];
|
||||
|
||||
const result = [];
|
||||
|
||||
children.order.forEach(pageId => {
|
||||
ordered.forEach(pageId => {
|
||||
pages.forEach(page => {
|
||||
if (page._id === pageId && (pageId !== currentPageId || !ignoreSelf)) {
|
||||
result.push(page);
|
||||
|
@ -80,13 +94,16 @@ class PagesOrder {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {string[]} unordered
|
||||
* @param {string} currentPageId - page's id that changes the order
|
||||
* @param {string} parentPageId - parent page's id that contains both two pages
|
||||
* @param {string} putAbovePageId - page's id above which we put the target page
|
||||
*/
|
||||
static async update(currentPageId, parentPageId, putAbovePageId) {
|
||||
static async update(unordered, currentPageId, parentPageId, putAbovePageId) {
|
||||
const pageOrder = await Model.get(parentPageId);
|
||||
|
||||
// Create unique array with ordered and unordered pages id
|
||||
pageOrder.order = [ ...new Set([...pageOrder.order, ...unordered]) ];
|
||||
pageOrder.putAbove(currentPageId, putAbovePageId);
|
||||
await pageOrder.save();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue