Toolbar.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <md-toolbar id="toolbar" class="md-dense">
  3. <h2 id="toolbar-title" class="md-title">ibt2</h2>
  4. <span v-if="loggedInUser.username">
  5. <span id="logged-in">
  6. Logged in: <router-link :to="userUrl">{{ loggedInUser.username }}</router-link>
  7. </span>
  8. <md-button class="md-icon-button" @click="logout()">
  9. <md-icon>exit_to_app</md-icon>
  10. </md-button>
  11. </span>
  12. <span v-else>
  13. <md-button v-show="!showLoginForm" class="md-icon-button" @click="focusToLoginForm()">
  14. <md-icon>power_settings_new</md-icon>
  15. </md-button>
  16. <span v-show="showLoginForm" id="login-form">
  17. <md-input-container id="username-input" class="login-input" md-inline>
  18. <md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />&nbsp;
  19. </md-input-container>
  20. <md-input-container id="password-input" class="login-input" md-inline>
  21. <md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" type="password" md-line />
  22. </md-input-container>
  23. </span>
  24. </span>
  25. </md-toolbar>
  26. </template>
  27. <script>
  28. export default {
  29. data () {
  30. return {
  31. username: '',
  32. password: '',
  33. showLoginForm: false,
  34. loggedInUser: {username: ''}
  35. }
  36. },
  37. computed: {
  38. userUrl: function() {
  39. var id = this.loggedInUser._id;
  40. if (!id) {
  41. return '';
  42. }
  43. return '/user/' + this.loggedInUser._id;
  44. }
  45. },
  46. beforeCreate: function() {
  47. this.currentUserUrl = this.$resource('users/current');
  48. this.loginUrl = this.$resource('login');
  49. this.logoutUrl = this.$resource('logout');
  50. },
  51. mounted: function() {
  52. this.getUserInfo();
  53. },
  54. methods: {
  55. focusToLoginForm() {
  56. this.showLoginForm = true;
  57. var that = this;
  58. setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
  59. },
  60. focusToPassword() {
  61. this.$refs.passwordInput.$el.focus();
  62. },
  63. login() {
  64. this.loginUrl.save({username: this.username, password: this.password}).then((response) => {
  65. return response.json();
  66. }, (response) => {
  67. alert('login: failed to get resource');
  68. }).then((data) => {
  69. this.showLoginForm = false;
  70. this.getUserInfo();
  71. });
  72. },
  73. logout() {
  74. this.logoutUrl.get().then((response) => {
  75. return response.json();
  76. }, (response) => {
  77. alert('logout: failed to get resource');
  78. }).then((json) => {
  79. this.loggedInUser = {};
  80. });
  81. },
  82. getUserInfo(callback) {
  83. this.currentUserUrl.get().then((response) => {
  84. return response.json();
  85. }, (response) => {
  86. alert('getUserInfo: failed to get resource');
  87. }).then((data) => {
  88. this.loggedInUser = data || {};
  89. if (callback) {
  90. callback(this.loggedInUser);
  91. }
  92. });
  93. }
  94. }
  95. }
  96. </script>
  97. <style>
  98. #toolbar-title {
  99. flex: 1;
  100. }
  101. .login-input {
  102. width: 200px;
  103. margin: 0px 0px 0px;
  104. padding-top: 0px;
  105. padding-left: 4px;
  106. min-height: 24px;
  107. line-height: 0px;
  108. margin-right: 20px;
  109. background-color: white;
  110. }
  111. #username-input {
  112. display: inline;
  113. float: left;
  114. }
  115. #password-input {
  116. display: inline;
  117. float: right;
  118. }
  119. #logged-in {
  120. position: relative;
  121. top: 10px;
  122. }
  123. </style>