fipamo/brain/routes/dash/nav.js

80 lines
2 KiB
JavaScript
Raw Normal View History

2019-11-29 22:43:55 +01:00
import Book from '../../../brain/data/Book';
import Settings, { SETTINGS_FILE } from '../../data/Settings';
const express = require('express');
const router = express.Router();
const _ = require('lodash');
//const settings = require('../../../site/settings.json');
2019-11-29 22:43:55 +01:00
const book = new Book();
const settings = new Settings();
//--------------------------
// SETTINGS
//--------------------------
router.get('/', function (req, res) {
if (req.session.user) {
settings
.load(SETTINGS_FILE)
.then(settings => {
var nav = [];
book.getPage()
.then(pages => {
if (settings.menu.length === 0) {
for (let index = 0; index < pages.length; index++) {
let item = pages[index].metadata;
if (item.menu) {
nav.push({
id: item.id,
uuid: item.uuid,
title: item.title,
slug: item.slug,
path: item.path
});
}
}
2019-11-29 22:43:55 +01:00
} else {
let newpages = [];
nav = settings.menu;
for (let index = 0; index < pages.length; index++) {
let item = pages[index].metadata;
if (item.menu)
newpages.push({
id: item.id,
uuid: item.uuid,
title: item.title,
slug: item.slug,
path: item.path
});
}
for (let i = 0; i < newpages.length; i++) {
if (_.find(nav, { uuid: newpages[i].uuid })) {
//menu item already exists
} else {
nav.push({
id: newpages[i].id,
uuid: newpages[i].uuid,
title: newpages[i].title,
slug: newpages[i].slug,
path: newpages[i].path
});
}
}
2019-11-29 22:43:55 +01:00
}
res.render('navigation', {
menu: nav,
welcome: 'Edit Navigation',
status: true,
title: 'Dashboard | Navigation'
});
})
.catch(err => {
res.render('error', { error: err });
});
2019-11-29 22:43:55 +01:00
})
.catch(err => {
res.render('error', { error: err });
2019-11-29 22:43:55 +01:00
});
} else {
res.redirect('/@/dashboard');
}
});
module.exports = router;