Toolbar.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <md-toolbar id="toolbar" class="md-dense">
  3. <span v-if="currentPath == 'user'">
  4. <md-button class="md-icon-button" @click="goBack()">
  5. <md-icon>backspace</md-icon>&nbsp;
  6. </md-button>
  7. </span>
  8. <span v-else class="button-spacer">&nbsp;</span>
  9. <h2 id="toolbar-title" class="md-title">ibt2</h2>
  10. <span v-if="loggedInUser.username">
  11. <md-button id="logged-in-icon" class="md-icon-button" @click="toUserPage()">
  12. <md-icon>person_pin</md-icon>
  13. </md-button>
  14. <span id="logged-in" class="md-subheading">
  15. <router-link :to="userUrl" class="username-link">{{ loggedInUser.username }}</router-link>
  16. </span>
  17. <md-button id="logout-icon" class="md-icon-button" @click="logout()">
  18. <md-icon>exit_to_app</md-icon>
  19. </md-button>
  20. </span>
  21. <span v-else>
  22. <md-button v-show="!showLoginForm" class="md-icon-button" @click="focusToLoginForm()">
  23. <md-icon>power_settings_new</md-icon>
  24. </md-button>
  25. <span v-show="showLoginForm" id="login-form">
  26. <md-input-container id="username-input" class="login-input" md-inline>
  27. <md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />
  28. </md-input-container>&nbsp;
  29. <span id="password-block">
  30. <md-input-container id="password-input" class="login-input" md-inline>
  31. <md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" type="password" md-line />
  32. </md-input-container>
  33. <md-button id="login-button" class="md-icon-button" @click="login()">
  34. <md-icon>play_circle_outline</md-icon>
  35. </md-button>
  36. </span>
  37. </span>
  38. </span>
  39. </md-toolbar>
  40. </template>
  41. <script>
  42. export default {
  43. data () {
  44. return {
  45. username: '',
  46. password: '',
  47. showLoginForm: false
  48. }
  49. },
  50. computed: {
  51. userUrl: function() {
  52. var id = this.loggedInUser._id;
  53. if (!id) {
  54. return '';
  55. }
  56. return '/user/' + this.loggedInUser._id;
  57. },
  58. loggedInUser() {
  59. return this.$store.state.loggedInUser;
  60. },
  61. currentPath() {
  62. return this.$route.name;
  63. }
  64. },
  65. beforeCreate: function() {
  66. this.usersUrl = this.$resource('users');
  67. this.currentUserUrl = this.$resource('users/current');
  68. this.loginUrl = this.$resource('login');
  69. this.logoutUrl = this.$resource('logout');
  70. },
  71. mounted: function() {
  72. this.getUserInfo();
  73. },
  74. methods: {
  75. goBack() {
  76. this.$router.back();
  77. },
  78. toUserPage() {
  79. this.$router.push(this.userUrl);
  80. },
  81. focusToLoginForm() {
  82. this.showLoginForm = true;
  83. var that = this;
  84. setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
  85. },
  86. focusToPassword() {
  87. this.$refs.passwordInput.$el.focus();
  88. },
  89. login() {
  90. var user_data = {username: this.username, password: this.password};
  91. this.loginUrl.save(user_data).then((response) => {
  92. return response.json();
  93. }, (response) => {
  94. if (response.status == 401) {
  95. this.createUser(user_data);
  96. }
  97. }).then((data) => {
  98. this.showLoginForm = false;
  99. this.getUserInfo();
  100. });
  101. },
  102. logout() {
  103. this.logoutUrl.get().then((response) => {
  104. return response.json();
  105. }, (response) => {
  106. alert('logout: failed to get resource');
  107. }).then((json) => {
  108. this.$store.commit('clearLoggedInUser');
  109. });
  110. },
  111. createUser(user_data) {
  112. user_data.username = user_data.username || {};
  113. user_data.username = user_data.username || this.username;
  114. user_data.password = user_data.password || this.password;
  115. this.usersUrl.save(user_data).then((response) => {
  116. return response.json();
  117. }, (response) => {
  118. alert('createUser: failed to get resource');
  119. }).then((json) => {
  120. this.showLoginForm = false;
  121. this.getUserInfo();
  122. });
  123. },
  124. getUserInfo(callback) {
  125. this.currentUserUrl.get().then((response) => {
  126. return response.json();
  127. }, (response) => {
  128. alert('getUserInfo: failed to get resource');
  129. }).then((data) => {
  130. data = data || {};
  131. this.$store.commit('setLoggedInUser', data);
  132. if (callback) {
  133. callback(data);
  134. }
  135. });
  136. }
  137. }
  138. }
  139. </script>
  140. <style>
  141. #toolbar-title {
  142. flex: 1;
  143. }
  144. .login-input {
  145. width: 200px;
  146. margin: 0px 0px 0px;
  147. padding-top: 0px;
  148. padding-left: 4px;
  149. min-height: 24px;
  150. line-height: 0px;
  151. background-color: white;
  152. }
  153. #username-input {
  154. display: inline;
  155. float: left;
  156. margin-right: 20px;
  157. }
  158. #password-input {
  159. display: inline;
  160. float: left;
  161. }
  162. #password-block {
  163. display: inline;
  164. float: right;
  165. }
  166. #login-button {
  167. height: 32px;
  168. }
  169. #logged-in-icon {
  170. margin-right: 0px;
  171. padding-right: 0px;
  172. color: #f6f72f !important;
  173. }
  174. #logged-in {
  175. position: relative;
  176. top: 10px;
  177. }
  178. #logout-icon {
  179. margin-left: 0px;
  180. padding-left: 0px;
  181. }
  182. .username-link {
  183. font-weight: bold;
  184. color: #f6f72f !important;
  185. }
  186. .button-spacer {
  187. width: 52px;
  188. }
  189. </style>