dynamic profile nav

This commit is contained in:
Sebastian Frank 2017-09-01 17:11:50 +02:00
parent e58a64335c
commit 451c9d14aa
No known key found for this signature in database
GPG Key ID: DC2BC5C506EBF6F3
6 changed files with 78 additions and 71 deletions

View File

@ -1,47 +1,81 @@
{ {
"title": "MyApp", "ui": {
"api": { "title": "MyApp",
"baseURL": "https://cx20.basehosts.de/api/v1/" "api": {
"baseURL": "https://cx20.basehosts.de/api/v1/"
},
"navigation": {
"main": [
{
"name": "Dashboard",
"to": "/",
"icon": "icon-compass"
},
{
"name": "Userlist",
"to": "/userlist",
"icon": "icon-contacts"
},
{
"name": "Domainlist",
"to": "/domainlist",
"icon": "icon-contacts"
},
{
"name": "FormDemo",
"to": "/formdemo",
"icon": "icon-contacts"
}
],
"profile": [
{
"name": "Profile anpassen",
"to": "/profile",
"icon": "icon-color-wand"
},
{
"name": "Logout",
"to": "/logout",
"icon": "icon-log-out"
}
]
}
}, },
"navigation": [ "routes": [
{ {
"name": "Login", "name": "Login",
"to": "/login", "to": "/login",
"icon": "fa-home", "content": "<login-form></login-form>"
"content": "<login-form></login-form>",
"show": {
"never": true
}
}, },
{ {
"name": "Dashboard", "name": "Dashboard",
"to": "/", "to": "/",
"icon": "icon-compass",
"content": "<dashboard></dashboard>" "content": "<dashboard></dashboard>"
}, },
{ {
"name": "Userlist", "name": "Userlist",
"to": "/userlist", "to": "/userlist",
"icon": "icon-contacts",
"content": "<userlist></userlist>" "content": "<userlist></userlist>"
}, },
{ {
"name": "Domainlist", "name": "Domainlist",
"to": "/domainlist", "to": "/domainlist",
"icon": "icon-contacts",
"content": "<domainlist></domainlist>" "content": "<domainlist></domainlist>"
}, },
{ {
"name": "FormDemo", "name": "FormDemo",
"to": "/formdemo", "to": "/formdemo",
"icon": "icon-contacts",
"content": "<form-demo></form-demo>" "content": "<form-demo></form-demo>"
}, },
{ {
"name": "Logout", "name": "Logout",
"to": "/logout", "to": "/logout",
"icon": "icon-log-out",
"content": "<logout></logout>" "content": "<logout></logout>"
},
{
"name": "Profil",
"to": "/profile",
"content": "<h3>Profil anpassen</h3>"
} }
] ]
} }

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="app"> <div class="app">
<topbar></topbar> <topbar :items="profileItems"></topbar>
<sidebar :items="navigationItems"></sidebar> <sidebar :items="navigationItems"></sidebar>
<div class="content_area"> <div class="content_area">
@ -30,7 +30,8 @@ export default {
}, },
data() { data() {
return { return {
navigationItems: this.$store.state.navigation navigationItems: this.$store.state.ui.navigation.main,
profileItems: this.$store.state.ui.navigation.profile
} }
}, },
created() { created() {

View File

@ -1,23 +0,0 @@
<template>
<div class="Navigation">
<ul>
<li v-for="(e, i) in items" :key="i">
<router-link :to="e.to">{{ e.name }}</router-link>
</li>
</ul>
</div>
</template>
<script>
export default {
name: "Navigation",
props: {
items: {
type: Array,
default() {
return [ ];
}
}
}
}
</script>

View File

@ -163,7 +163,6 @@ export default {
}, },
methods: { methods: {
showIf(conf) { showIf(conf) {
console.log(conf);
// show if not configured // show if not configured
if (!conf) { if (!conf) {
return true; return true;

View File

@ -12,18 +12,12 @@
</a> </a>
<nav class="user_menu"> <nav class="user_menu">
<ul> <ul>
<li><a href="#"> <router-link tag="li" v-for="(item, i) in items" :key="i" v-if="showIf(item.show)" :to="item.to" active-class="active" exact>
<i class="icon icon-color-wand" aria-hidden="true"></i> <a>
<div class="title"> <i :class="['icon', item.icon]"></i>
<router-link :to="'/users/' + loginID">Profil anpassen</router-link> <div class="title">{{ item.name }}</div>
</div> </a>
</a></li> </router-link>
<li><a href="#">
<i class="icon icon-log-out" aria-hidden="true"></i>
<div class="title">
<router-link to="/logout">Logout</router-link>
</div>
</a></li>
</ul> </ul>
</nav> </nav>
</div> </div>
@ -33,6 +27,9 @@
<script> <script>
export default { export default {
name: "Topbar", name: "Topbar",
props: [
"items"
],
data() { data() {
return { return {
} }
@ -67,6 +64,15 @@ export default {
methods: { methods: {
toggleMenu() { toggleMenu() {
this.$refs.user_profile.classList.toggle("open"); this.$refs.user_profile.classList.toggle("open");
},
showIf(conf) {
// show if not configured
if (!conf) {
return true;
}
if (conf.never) {
return false;
}
} }
} }
} }

View File

@ -14,8 +14,7 @@ const router = new VueRouter();
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
api: {}, ui: {},
navigation: [],
persist: { persist: {
login: {}, login: {},
authToken: '', authToken: '',
@ -24,13 +23,8 @@ const store = new Vuex.Store({
} }
}, },
mutations: { mutations: {
setAPI(state, payload) { setUI(state, payload) {
state.api = payload; state.ui = payload;
},
setNavigation(state, payload) {
for (var i = 0; i < payload.length; i++) {
state.navigation.push(payload[i]); // = payload;
}
}, },
setCredentials(state, payload) { setCredentials(state, payload) {
state.persist.credentials = { state.persist.credentials = {
@ -59,7 +53,7 @@ const store = new Vuex.Store({
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Axios({ Axios({
method: payload.method ? payload.method : 'get', method: payload.method ? payload.method : 'get',
baseURL: context.state.api.baseURL, baseURL: context.state.ui.api.baseURL,
url: payload.endpoint, url: payload.endpoint,
params: payload.params, params: payload.params,
data: payload.data, data: payload.data,
@ -173,21 +167,18 @@ if (persist) {
Axios.get('conf/init.json') Axios.get('conf/init.json')
.then(results => { .then(results => {
// set navigation // set navigation
if (!Array.isArray(results.data.navigation)) { if (!Array.isArray(results.data.routes)) {
alert('invalid data in init.json'); alert('invalid data in init.json, no routes');
return; return;
} }
const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}});
store.commit("setNavigation", navigation); // set ui config in store
store.commit("setUI", results.data.ui);
// set api config in store
store.commit("setAPI", results.data.api);
// add routes // add routes
let routes = []; let routes = [];
let rIdx = 0; let rIdx = 0;
results.data.navigation.forEach(({name, to, content, data}) => routes.push({ results.data.routes.forEach(({name, to, content, data}) => routes.push({
name: name, name: name,
path: to, path: to,
meta: { meta: {
@ -206,7 +197,6 @@ Axios.get('conf/init.json')
})); }));
router.addRoutes(routes); router.addRoutes(routes);
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
console.log(to);
document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title; document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title;
next(); next();
}); });