mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 07:25:21 +02:00
refactor: change method location and others
This commit is contained in:
parent
76f05fe274
commit
7603f727f0
4 changed files with 46 additions and 40 deletions
|
@ -62,6 +62,47 @@ class Pages {
|
|||
return nullFilteredPages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group given pages with descending order of creation time
|
||||
*
|
||||
* @param {pages[]} pages - pages to group
|
||||
* @returns {Page[]}
|
||||
*/
|
||||
public static group(pages: Page[]): Page[] {
|
||||
const result: Page[] = [];
|
||||
const obj:Record<string, Array<Page>> = {};
|
||||
|
||||
pages.sort((a, b) => {
|
||||
if (a.body.time > b.body.time) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (a.body.time < b.body.time) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}).forEach(doc => {
|
||||
if (doc._parent === '0' && typeof doc._id === 'string') {
|
||||
obj[doc._id] = [];
|
||||
obj[doc._id].push(doc);
|
||||
} else if (doc._parent !== '0' && doc._parent && doc._id) {
|
||||
obj[doc._id] = [];
|
||||
obj[doc._parent].push(doc);
|
||||
}
|
||||
});
|
||||
|
||||
Object.entries(obj).forEach(([, value]) => {
|
||||
if (value.length <=0) {
|
||||
return;
|
||||
} else {
|
||||
result.push(...value);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all children elements to null
|
||||
*
|
||||
|
|
|
@ -84,45 +84,9 @@ class Page {
|
|||
* @returns {Promise<Page[]>}
|
||||
*/
|
||||
public static async getAll(query: Record<string, unknown> = {}): Promise<Page[]> {
|
||||
const result: PageData[] = [];
|
||||
const obj = {};
|
||||
let docs = await pagesDb.find(query);
|
||||
const docs = await pagesDb.find(query);
|
||||
|
||||
docs = docs.sort((a, b) => {
|
||||
if (a.body.time > b.body.time) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (a.body.time < b.body.time) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
docs.forEach(doc => {
|
||||
if(doc.parent === '0') {
|
||||
// @ts-ignore
|
||||
obj[doc._id] = [];
|
||||
// @ts-ignore
|
||||
obj[doc._id].push(doc);
|
||||
} else {
|
||||
// @ts-ignore
|
||||
obj[doc._id] = [];
|
||||
// @ts-ignore
|
||||
obj[doc.parent].push(doc);
|
||||
}
|
||||
});
|
||||
Object.entries(obj).forEach(([key, value]) => {
|
||||
// @ts-ignore
|
||||
if(!key || value.length <=0) {
|
||||
return;
|
||||
} else {
|
||||
// @ts-ignore
|
||||
result.push(...value);
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.all(result.map(doc => new Page(doc)));
|
||||
return Promise.all(docs.map(doc => new Page(doc)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,9 +12,10 @@ const router = express.Router();
|
|||
router.get('/page/new', verifyToken, allowEdit, async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const pagesAvailable = await Pages.getAll();
|
||||
const groupedPagesAvailable = Pages.group(pagesAvailable);
|
||||
|
||||
res.render('pages/form', {
|
||||
pagesAvailable,
|
||||
groupedPagesAvailable,
|
||||
page: null,
|
||||
});
|
||||
} catch (error) {
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
{% endif %}
|
||||
<select name="parent">
|
||||
<option value="0">Root</option>
|
||||
{% for _page in pagesAvailable %}
|
||||
{% for _page in groupedPagesAvailable %}
|
||||
{% if _page._id != currentPageId %}
|
||||
<option value="{{ _page._id }}" {{ page is not empty and page._parent == _page._id ? 'selected' : ''}}>
|
||||
{% if _page._parent != "0" %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue