Toolbar.vue 8.4 KB

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