navigation sorting working again

This commit is contained in:
Ro 2019-11-29 13:43:55 -08:00
parent 03a96dfed1
commit d329522091
7 changed files with 94 additions and 60 deletions

View file

@ -87,40 +87,22 @@ router.post('/sync', (req, res) => {
}
});
router.get('/nav', function(req, res) {
fs.readJson('config/site-settings.json')
.then(obj => {
fs.writeJson('config/site-settings.json', obj)
.then(() => {
res.json(obj.menu);
})
.catch(() => {
//console.error(err);
});
})
.catch(() => {
//console.error(err);
});
});
router.post('/nav-sync', (req, res) => {
let payload = req.body;
if (req.session.user) {
fs.readJson('config/site-settings.json')
.then(obj => {
obj.menu = payload;
fs.writeJson('config/site-settings.json', obj)
.then(() => {
res.json({
message: DataEvent.SETTINGS_UPDATED
});
})
.catch(() => {
//console.error(err);
});
settings.menu = payload;
fs.writeJson('site/settings.json', settings)
.then(() => {
res.json({
type: DataEvent.SETTINGS_UPDATED,
message: 'Menu order saved, champ'
});
})
.catch(() => {
//console.error(err);
.catch(err => {
res.json({
type: DataEvent.REQUEST_LAME,
message: err
});
});
}
});

View file

@ -45,7 +45,7 @@ app.use(flash());
var dash = require('./routes/dash/index');
var page = require('./routes/dash/pages');
var settings = require('./routes/dash/settings');
//var navDashboard = require('./routes/dash/nav');
var nav = require('./routes/dash/nav');
//api
var pages = require('./api/v1/pages');
var setting = require('./api/v1/settings');
@ -58,11 +58,10 @@ app.use('/api/v1/settings', setting);
//app.use('/api/mail', mailer);
app.use('/api/v1/auth', auth);
// PAGES
//app.use('/', front);
app.use('/@/dashboard', dash);
app.use('/@/dashboard/page', page);
app.use('/@/dashboard/settings', settings);
//app.use('/@/dashboard/navigation', navDashboard);
app.use('/@/dashboard/navigation', nav);
//app.use('/mailer', mailer);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
@ -76,7 +75,7 @@ app.use(function(req, res, next) {
if (app.get('env') === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render(theme + '/error', {
res.render('error', {
message: err.message,
error: err
});
@ -86,7 +85,7 @@ if (app.get('env') === 'development') {
// no stacktraces leaked to user
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render(theme + '/error', {
res.render('error', {
message: err.message,
error: {}
});

View file

@ -1,21 +1,65 @@
import Book from '../../../brain/data/Book';
const express = require('express');
const router = express.Router();
const fs = require('fs-extra');
const _ = require('lodash');
var settings = [];
const settings = require('../../../site/settings.json');
const book = new Book();
//--------------------------
// SETTINGS
//--------------------------
router.get('/', function(req, res) {
fs.readJson('config/site-settings.json')
.then(obj => {
settings = [];
settings = obj;
})
.catch(() => {
//console.error(err)
});
if (req.session.user) {
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
});
}
}
} 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
});
}
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
});
}
}
}
res.render('navigation', {
menu: nav,
welcome: 'Edit Navigation',
status: true,
title: 'Dashboard | Navigation'
});
})
.catch(err => {
//console.log('ERROR', err);
});
} else {
res.redirect('/@/dashboard');
}

View file

@ -4,13 +4,13 @@ block main-content
#nav-index-wrapper
#nav-pages
- var index = 0;
- for ( index; index < pages.length; index++)
.nav-item(id=pages[index].id, data-slug=pages[index].slug, data-uuid=pages[index].uuid)
- for ( index; index < menu.length; index++)
.nav-item(id=menu[index].id, data-slug=menu[index].slug, data-uuid=menu[index].uuid)
svg#item-arrows(viewBox="0 0 20 20" class="icons")
use(xlink:href='/dash/assets/images/sprite.svg#entypo-select-arrows')
use(xlink:href='/assets/images/global/sprite.svg#entypo-select-arrows')
label
= pages[index].title
= menu[index].title
#nav-btns
button.nav-btn#edit-item(data-id=pages[index].uuid) EDIT
button.nav-btn#remove-item(data-id=pages[index].id) REMOVE
button.nav-btn#edit-item(data-id=menu[index].uuid) EDIT
button.nav-btn#remove-item(data-id=menu[index].id) REMOVE

View file

@ -1,17 +1,15 @@
import DataUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/DataUtils';
import * as DataEvent from '../../../src/com/events/DataEvent';
const utils = new DataUtils();
export default class NavActions {
//--------------------------
// constructor
//--------------------------
constructor() {
this.dataUtils = new DataUtils();
}
constructor() {}
//--------------------------
// methods
//--------------------------
save() {
let self = this;
let navData = [];
let items = document.getElementById('nav-pages').children;
for (let index = 0; index < items.length; index++) {
@ -23,9 +21,9 @@ export default class NavActions {
});
}
return new Promise(function(resolve, reject) {
self.dataUtils
utils
.request(
'/api/settings/nav-sync',
'/api/v1/settings/nav-sync',
DataEvent.SETTINGS_UPDATED,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
@ -40,6 +38,7 @@ export default class NavActions {
});
}
removeItem(id) {
/**
let self = this;
this.dbutils.getPost(id).then(post => {
post.post.page = 'false';
@ -48,6 +47,7 @@ export default class NavActions {
self.save();
});
});
*/
}
//--------------------------
// event handlers

View file

@ -1,4 +1,7 @@
import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications';
const notify = new Notifications();
export default class NavIndex {
//--------------------------
// constructor
@ -15,8 +18,13 @@ export default class NavIndex {
onUpdate: () => {
new NavActions()
.save()
.then(() => {
//console.log(r);
.then(response => {
let r = JSON.parse(response.request['response']);
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {
notify.alert(r.message, true);
}
})
.catch(() => {
//console.log(err);
@ -39,7 +47,7 @@ export default class NavIndex {
new NavActions().removeItem(id);
break;
case 'edit-item':
window.location = '/@/dashboard/posts/edit/' + e.target.getAttribute('data-id');
window.location = '/@/dashboard/pages/edit/' + e.target.getAttribute('data-id');
break;
}
}

View file

@ -10,6 +10,7 @@ export const PAGE_ADDED = 'postAdded';
export const PAGE_UPDATED = 'postUpdated';
export const PAGE_DELETED = 'postImageAdded';
export const SETTINGS_UPDATED = 'settingsUpdated';
export const MENU_UPDATED = 'menuUpdated';
export const AVATAR_UPLOADED = 'avatarUploaded';
export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded';
export const API_PAGE_WRITE = 'writingItDown';