ibt2/src/Toolbar.vue

217 lines
5.9 KiB
Vue
Raw Normal View History

2017-01-15 19:07:10 +01:00
<template>
<md-toolbar id="toolbar" class="md-dense">
2017-01-15 22:35:54 +01:00
<span v-if="currentPath == 'user'">
<md-button class="md-icon-button" @click="goBack()">
<md-icon>backspace</md-icon>&nbsp;
</md-button>
</span>
<span v-else class="button-spacer">&nbsp;</span>
2017-01-15 19:07:10 +01:00
<h2 id="toolbar-title" class="md-title">ibt2</h2>
<span v-if="loggedInUser.username">
2017-01-15 22:35:54 +01:00
<md-button id="logged-in-icon" class="md-icon-button" @click="toUserPage()">
<md-icon>person_pin</md-icon>
</md-button>
<span id="logged-in" class="md-subheading">
<router-link :to="userUrl" class="username-link">{{ loggedInUser.username }}</router-link>
2017-01-15 19:07:10 +01:00
</span>
2017-01-15 22:35:54 +01:00
<md-button id="logout-icon" class="md-icon-button" @click="logout()">
2017-01-15 19:07:10 +01:00
<md-icon>exit_to_app</md-icon>
</md-button>
</span>
<span v-else>
<md-button v-show="!showLoginForm" class="md-icon-button" @click="focusToLoginForm()">
<md-icon>power_settings_new</md-icon>
</md-button>
<span v-show="showLoginForm" id="login-form">
<md-input-container id="username-input" class="login-input" md-inline>
2017-01-15 22:35:54 +01:00
<md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />
</md-input-container>&nbsp;
<span id="password-block">
<md-input-container id="password-input" class="login-input" md-inline>
<md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" type="password" md-line />
</md-input-container>
<md-button id="login-button" class="md-icon-button" @click="login()">
<md-icon>play_circle_outline</md-icon>
</md-button>
</span>
2017-01-15 19:07:10 +01:00
</span>
</span>
</md-toolbar>
</template>
<script>
export default {
data () {
return {
username: '',
password: '',
2017-01-15 22:35:54 +01:00
showLoginForm: false
2017-01-15 19:07:10 +01:00
}
},
computed: {
userUrl: function() {
var id = this.loggedInUser._id;
if (!id) {
return '';
}
return '/user/' + this.loggedInUser._id;
2017-01-15 22:35:54 +01:00
},
loggedInUser() {
return this.$store.state.loggedInUser;
},
currentPath() {
return this.$route.name;
2017-01-15 19:07:10 +01:00
}
},
beforeCreate: function() {
2017-01-15 22:35:54 +01:00
this.usersUrl = this.$resource('users');
2017-01-15 19:07:10 +01:00
this.currentUserUrl = this.$resource('users/current');
this.loginUrl = this.$resource('login');
this.logoutUrl = this.$resource('logout');
},
mounted: function() {
this.getUserInfo();
},
methods: {
2017-01-15 22:35:54 +01:00
goBack() {
this.$router.back();
},
toUserPage() {
this.$router.push(this.userUrl);
},
2017-01-15 19:07:10 +01:00
focusToLoginForm() {
this.showLoginForm = true;
var that = this;
setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
},
focusToPassword() {
this.$refs.passwordInput.$el.focus();
},
login() {
2017-01-15 22:35:54 +01:00
var user_data = {username: this.username, password: this.password};
this.loginUrl.save(user_data).then((response) => {
2017-01-15 19:07:10 +01:00
return response.json();
}, (response) => {
2017-01-15 22:35:54 +01:00
if (response.status == 401) {
this.createUser(user_data);
}
2017-01-15 19:07:10 +01:00
}).then((data) => {
this.showLoginForm = false;
this.getUserInfo();
});
},
logout() {
this.logoutUrl.get().then((response) => {
return response.json();
}, (response) => {
alert('logout: failed to get resource');
}).then((json) => {
2017-01-15 22:35:54 +01:00
this.$store.commit('clearLoggedInUser');
});
},
createUser(user_data) {
user_data.username = user_data.username || {};
user_data.username = user_data.username || this.username;
user_data.password = user_data.password || this.password;
this.usersUrl.save(user_data).then((response) => {
return response.json();
}, (response) => {
alert('createUser: failed to get resource');
}).then((json) => {
this.showLoginForm = false;
this.getUserInfo();
2017-01-15 19:07:10 +01:00
});
2017-01-15 22:35:54 +01:00
2017-01-15 19:07:10 +01:00
},
getUserInfo(callback) {
this.currentUserUrl.get().then((response) => {
return response.json();
}, (response) => {
alert('getUserInfo: failed to get resource');
}).then((data) => {
2017-01-15 22:35:54 +01:00
data = data || {};
this.$store.commit('setLoggedInUser', data);
2017-01-15 19:07:10 +01:00
if (callback) {
2017-01-15 22:35:54 +01:00
callback(data);
2017-01-15 19:07:10 +01:00
}
});
}
}
}
</script>
<style>
#toolbar-title {
flex: 1;
}
.login-input {
width: 200px;
margin: 0px 0px 0px;
padding-top: 0px;
padding-left: 4px;
min-height: 24px;
line-height: 0px;
background-color: white;
}
#username-input {
display: inline;
float: left;
2017-01-15 22:35:54 +01:00
margin-right: 20px;
2017-01-15 19:07:10 +01:00
}
#password-input {
2017-01-15 22:35:54 +01:00
display: inline;
float: left;
}
#password-block {
2017-01-15 19:07:10 +01:00
display: inline;
float: right;
}
2017-01-15 22:35:54 +01:00
#login-button {
height: 32px;
}
#logged-in-icon {
margin-right: 0px;
padding-right: 0px;
color: #f6f72f !important;
}
2017-01-15 19:07:10 +01:00
#logged-in {
position: relative;
top: 10px;
}
2017-01-15 22:35:54 +01:00
#logout-icon {
margin-left: 0px;
padding-left: 0px;
}
.username-link {
font-weight: bold;
color: #f6f72f !important;
}
.button-spacer {
width: 52px;
}
2017-01-15 19:07:10 +01:00
</style>