1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-19 21:29:41 +02:00

Don't allow to set child element as parent. (#19)

* filter available pages

* refactor

set function as method of Pages

* change wrong word

* change commentaries

* change method name

* refactor

refactor

* Update src/controllers/pages.js

Co-Authored-By: cabad11 <44175180+cabad11@users.noreply.github.com>

* refactor

change comments
change metod name
This commit is contained in:
cabad11 2019-01-14 17:53:10 +03:00 committed by GitHub
parent 9966131100
commit dbfc594e66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View file

@ -41,6 +41,38 @@ class Pages {
return Model.getAll(); return Model.getAll();
} }
/**
* @static
* Return all pages without children of passed page
*
* @param {string} parent - id of current page
* @returns {Promise<Page[]>}
*/
static async getAllExceptChildrens(parent) {
let pagesAvailable = this.removeChildren(await Pages.getAll(), parent);
return pagesAvailable.filter((item) => item !== null);
}
/**
* @static
* Set all children elements to null
*
* @param {Page[]} [pagesAvailable] - Array of all pages
* @param {string} parent - id of parent page
* @returns {Array<?Page>}
*/
static removeChildren(pagesAvailable, parent) {
pagesAvailable.forEach(async (item, index) => {
if (item === null || item._parent !== parent) {
return;
}
pagesAvailable[index] = null;
pagesAvailable = Pages.removeChildren(pagesAvailable, item._id);
});
return pagesAvailable;
}
/** /**
* Create new page model and save it in the database * Create new page model and save it in the database
* *

View file

@ -22,7 +22,7 @@ router.get('/page/edit/:id', async (req, res, next) => {
try { try {
let page = await Pages.get(pageId); let page = await Pages.get(pageId);
let pagesAvailable = await Pages.getAll(); let pagesAvailable = await Pages.getAllExceptChildrens(pageId);
res.render('pages/form', { res.render('pages/form', {
pagesAvailable, pagesAvailable,