Toolbar.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <div>
  3. <md-toolbar id="toolbar" class="md-dense">
  4. <span v-if="currentPath != 'home' && currentPath != 'day' && currentPath != 'days'">
  5. <md-button class="md-icon-button" @click="goBack()">
  6. <md-tooltip md-direction="right">back</md-tooltip>
  7. <md-icon>backspace</md-icon>&nbsp;
  8. </md-button>
  9. </span>
  10. <span v-else class="button-spacer">&nbsp;</span>
  11. <h2 id="toolbar-title" class="md-title">
  12. <router-link :to="{name: 'home'}" class="home-link">ibt2</router-link>
  13. </h2>
  14. <span v-if="loggedInUser.username">
  15. <md-button v-if="loggedInUser.isAdmin" id="users-icon" class="md-icon-button" @click="toSettingsPage()">
  16. <md-tooltip md-direction="left">global settings</md-tooltip>
  17. <md-icon>settings</md-icon>
  18. </md-button>
  19. <md-button v-if="loggedInUser.isAdmin" id="users-icon" class="md-icon-button" @click="toUsersPage()">
  20. <md-tooltip md-direction="left">list of users</md-tooltip>
  21. <md-icon>people_outline</md-icon>
  22. </md-button>
  23. <md-button id="logged-in-icon" class="md-icon-button" @click="toUserPage()">
  24. <md-tooltip md-direction="left">personal page</md-tooltip>
  25. <md-icon>person_pin</md-icon>
  26. </md-button>
  27. <span id="logged-in" class="md-subheading">
  28. <router-link :to="userUrl" class="username-link">{{ loggedInUser.username }}</router-link>
  29. </span>
  30. <md-button id="logout-icon" class="md-icon-button" @click="logout()">
  31. <md-tooltip md-direction="left">logout</md-tooltip>
  32. <md-icon>exit_to_app</md-icon>
  33. </md-button>
  34. </span>
  35. <span v-else>
  36. <span id="login-form">
  37. <md-input-container id="username-input" class="login-input" md-inline>
  38. <md-tooltip md-direction="bottom">login name or create a new user if it doesn't exist</md-tooltip>
  39. <md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />
  40. </md-input-container>&nbsp;
  41. <span id="password-block">
  42. <md-input-container id="password-input" class="login-input" md-has-password md-inline>
  43. <md-tooltip md-direction="bottom">login password or create a new user if it doesn't exist</md-tooltip>
  44. <md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" type="password" md-line />
  45. </md-input-container>
  46. <md-button id="login-button" class="md-icon-button" @click="login()">
  47. <md-tooltip md-direction="left">login or create a new user if it doesn't exist</md-tooltip>
  48. <md-icon>play_circle_outline</md-icon>
  49. </md-button>
  50. </span>
  51. </span>
  52. </span>
  53. <ibt-snackbar ref="snackbarObj" />
  54. <ibt-dialog ref="dialogObj" />
  55. </md-toolbar>
  56. <vue-markdown v-if="settings.showMotd && settings.motd" class="motd" :source="settings.motd"></vue-markdown>
  57. </div>
  58. </template>
  59. <script>
  60. import VueMarkdown from 'vue-markdown';
  61. import IbtDialog from './IbtDialog.vue';
  62. import IbtSnackbar from './IbtSnackbar.vue';
  63. export default {
  64. data () {
  65. return {
  66. username: '',
  67. password: '',
  68. dialog: {
  69. text: 'some error',
  70. ok: 'ok'
  71. }
  72. }
  73. },
  74. computed: {
  75. userUrl: function() {
  76. var id = this.loggedInUser._id;
  77. if (!id) {
  78. return '';
  79. }
  80. return '/user/' + this.loggedInUser._id;
  81. },
  82. settings() {
  83. return this.$store.state.settings || {};
  84. },
  85. loggedInUser() {
  86. return this.$store.state.loggedInUser;
  87. },
  88. currentPath() {
  89. return this.$route.name;
  90. }
  91. },
  92. beforeCreate: function() {
  93. this.usersUrl = this.$resource('users');
  94. this.currentUserUrl = this.$resource('users/current');
  95. this.loginUrl = this.$resource('login');
  96. this.logoutUrl = this.$resource('logout');
  97. },
  98. mounted: function() {
  99. this.getUserInfo();
  100. },
  101. methods: {
  102. goBack() {
  103. this.$router.back();
  104. },
  105. toUserPage() {
  106. this.$router.push(this.userUrl);
  107. },
  108. toUsersPage() {
  109. this.$router.push('/user/');
  110. },
  111. toSettingsPage() {
  112. this.$router.push('/settings/');
  113. },
  114. focusToLoginForm() {
  115. var that = this;
  116. setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
  117. },
  118. focusToPassword() {
  119. this.$refs.passwordInput.$el.focus();
  120. },
  121. login(opt) {
  122. opt = opt || {};
  123. var user_data = {username: this.username, password: this.password};
  124. this.loginUrl.save(user_data).then((response) => {
  125. return response.json();
  126. }, (response) => {
  127. // Unable to login? Let's try to create the user!
  128. if (response.status == 401) {
  129. if (opt.stopHere) {
  130. this.$refs.dialogObj.show({text: 'failed to login and create a new user. Wrong username and password?'});
  131. } else {
  132. this.createUser(user_data);
  133. }
  134. }
  135. }).then((data) => {
  136. this.getUserInfo();
  137. });
  138. },
  139. logout() {
  140. this.logoutUrl.get().then((response) => {
  141. return response.json();
  142. }, (response) => {
  143. this.$refs.dialogObj.show({text: 'failed to logout'});
  144. }).then((json) => {
  145. this.$store.commit('clearLoggedInUser');
  146. this.$refs.snackbarObj.show('logged out');
  147. });
  148. },
  149. createUser(user_data) {
  150. user_data.username = user_data.username || {};
  151. user_data.username = user_data.username || this.username;
  152. user_data.password = user_data.password || this.password;
  153. this.usersUrl.save(user_data).then((response) => {
  154. return response.json();
  155. }, (response) => {
  156. }).then((json) => {
  157. this.login({stopHere: true});
  158. });
  159. },
  160. getUserInfo(callback) {
  161. this.currentUserUrl.get().then((response) => {
  162. return response.json();
  163. }, (response) => {
  164. this.$refs.dialogObj.show({text: 'unable to get user info'});
  165. }).then((data) => {
  166. data = data || {};
  167. this.$store.commit('setLoggedInUser', data);
  168. if (callback) {
  169. callback(data);
  170. }
  171. });
  172. }
  173. },
  174. components: { IbtDialog, IbtSnackbar, VueMarkdown }
  175. }
  176. </script>
  177. <style>
  178. #toolbar-title {
  179. flex: 1;
  180. }
  181. .login-input {
  182. width: 200px;
  183. margin: 0px 0px 0px;
  184. padding-top: 0px;
  185. padding-left: 4px;
  186. min-height: 24px;
  187. line-height: 0px;
  188. background-color: white;
  189. }
  190. #username-input {
  191. display: inline;
  192. float: left;
  193. margin-right: 20px;
  194. }
  195. #password-input {
  196. display: inline;
  197. float: left;
  198. }
  199. #password-block {
  200. display: inline;
  201. float: right;
  202. }
  203. #login-button {
  204. height: 32px;
  205. }
  206. #logged-in-icon {
  207. margin-right: 0px;
  208. padding-right: 0px;
  209. color: #f6f72f !important;
  210. }
  211. #logged-in {
  212. position: relative;
  213. top: 10px;
  214. }
  215. #logout-icon {
  216. margin-left: 0px;
  217. padding-left: 0px;
  218. }
  219. .username-link {
  220. font-weight: bold;
  221. color: #f6f72f !important;
  222. }
  223. .button-spacer {
  224. width: 52px;
  225. }
  226. .home-link {
  227. font-weight: bold;
  228. color: white !important;
  229. }
  230. .motd p {
  231. margin-top: 0px;
  232. margin-bottom: 0px;
  233. padding-top: 4px;
  234. padding-bottom: 4px;
  235. text-align: center;
  236. background-color: #ffcdd2;
  237. }
  238. </style>