1
0
Fork 0
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:
Y-k-Y 2022-04-28 22:36:30 +09:00
parent 76f05fe274
commit 7603f727f0
4 changed files with 46 additions and 40 deletions

View file

@ -62,6 +62,47 @@ class Pages {
return nullFilteredPages; 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 * Set all children elements to null
* *

View file

@ -84,45 +84,9 @@ class Page {
* @returns {Promise<Page[]>} * @returns {Promise<Page[]>}
*/ */
public static async getAll(query: Record<string, unknown> = {}): Promise<Page[]> { public static async getAll(query: Record<string, unknown> = {}): Promise<Page[]> {
const result: PageData[] = []; const docs = await pagesDb.find(query);
const obj = {};
let docs = await pagesDb.find(query);
docs = docs.sort((a, b) => { return Promise.all(docs.map(doc => new Page(doc)));
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)));
} }
/** /**

View file

@ -12,9 +12,10 @@ const router = express.Router();
router.get('/page/new', verifyToken, allowEdit, async (req: Request, res: Response, next: NextFunction) => { router.get('/page/new', verifyToken, allowEdit, async (req: Request, res: Response, next: NextFunction) => {
try { try {
const pagesAvailable = await Pages.getAll(); const pagesAvailable = await Pages.getAll();
const groupedPagesAvailable = Pages.group(pagesAvailable);
res.render('pages/form', { res.render('pages/form', {
pagesAvailable, groupedPagesAvailable,
page: null, page: null,
}); });
} catch (error) { } catch (error) {

View file

@ -22,7 +22,7 @@
{% endif %} {% endif %}
<select name="parent"> <select name="parent">
<option value="0">Root</option> <option value="0">Root</option>
{% for _page in pagesAvailable %} {% for _page in groupedPagesAvailable %}
{% if _page._id != currentPageId %} {% if _page._id != currentPageId %}
<option value="{{ _page._id }}" {{ page is not empty and page._parent == _page._id ? 'selected' : ''}}> <option value="{{ _page._id }}" {{ page is not empty and page._parent == _page._id ? 'selected' : ''}}>
{% if _page._parent != "0" %} {% if _page._parent != "0" %}