Add ability to fetch menu items for a virtual root
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2020-11-23 10:57:32 -06:00
parent 7f8f0a1dd7
commit 97b4bf662c
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

View File

@ -12,6 +12,46 @@ class Menu extends Controller {
}
async get_items(req, res) {
const virtual_root = req.query.virtualRootPageId
const Page = this.models.get('api:Page')
const page_only = req.query.type === 'page'
if ( !virtual_root ) {
return this.get_top_level_menu(req, res);
} else {
const root_page = await Page.findOne({ UUID: virtual_root })
if ( !root_page || !(await root_page.is_accessible_by(req.user, 'view')) ) {
return res.status(404)
.message('Invalid virtual root page ID.')
.api()
}
const nodes = await this._build_menu_object(root_page, [], page_only)
const menu = []
const virtual_page = {
id: root_page.UUID,
children: [...(await root_page.get_menu_items(page_only)), ...nodes],
name: root_page.is_shared() ? root_page.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : root_page.Name,
shared: root_page.is_shared(),
type: PageType.Note,
};
menu.push({
id: 0,
name: 'Virtual Tree Root',
children: [virtual_page],
noDelete: true,
noChildren: true,
virtual: true,
type: PageType.Branch,
})
return res.api(menu)
}
}
async get_top_level_menu(req, res) {
const Page = this.models.get('api:Page')
const page_only = req.query.type === 'page'