App.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div id="app">
  3. <md-toolbar class="md-dense">
  4. <h2 id="toolbar-title" class="md-title">ibt2</h2>
  5. <span v-if="loggedInUser.username">
  6. Logged in: {{ loggedInUser.username }}
  7. <md-button class="md-icon-button" @click="logout()">
  8. <md-icon>exit_to_app</md-icon>
  9. </md-button>
  10. </span>
  11. <span v-else>
  12. <md-button v-show="!showLoginForm" class="md-icon-button" @click="focusToLoginForm()">
  13. <md-icon>power_settings_new</md-icon>
  14. </md-button>
  15. <span v-show="showLoginForm">
  16. <md-input-container id="login-form" md-inline>
  17. <md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />
  18. <md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" md-line />
  19. </md-input-container>
  20. </span>
  21. </span>
  22. </md-toolbar>
  23. <md-layout md-gutter md-row>
  24. <md-layout md-column md-flex="20" md-gutter>
  25. <datepicker id="datepicker" :value="date" :inline="true" @selected="getDay"></datepicker>
  26. </md-layout>
  27. <md-layout id="panel" md-column>
  28. <md-layout md-row>
  29. <group v-for="group in day.groups || []" :group="group" :day="day.day" new-attendee="" @updated="reload" />
  30. <group :add-new-group="true" :day="day.day" new-attendee="" new-group="" @updated="reload" />
  31. </md-layout>
  32. </md-layout>
  33. </md-layout>
  34. </div>
  35. </template>
  36. <script>
  37. import Datepicker from 'vuejs-datepicker';
  38. import Group from './Group';
  39. export default {
  40. data () {
  41. return {
  42. date: null, // a Date object representing the selected date
  43. day: {},
  44. daysSummary: {},
  45. username: '',
  46. password: '',
  47. showLoginForm: false,
  48. loggedInUser: {username: ''}
  49. }
  50. },
  51. beforeCreate: function() {
  52. this.daysUrl = this.$resource('days{/day}');
  53. this.attendeesUrl = this.$resource('attendees{/id}');
  54. this.currentUserUrl = this.$resource('users/current');
  55. this.loginUrl = this.$resource('login');
  56. this.logoutUrl = this.$resource('logout');
  57. },
  58. mounted: function() {
  59. this.getUserInfo();
  60. var [year, month, day] = (this.$route.params.day || '').split('-');
  61. year = parseInt(year);
  62. month = parseInt(month) - 1;
  63. day = parseInt(day);
  64. if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
  65. this.date = new Date(year, month, day);
  66. }
  67. if (!(this.date && !isNaN(this.date.getTime()))) {
  68. this.date = new Date();
  69. }
  70. this.reload();
  71. },
  72. methods: {
  73. focusToLoginForm() {
  74. this.showLoginForm = true;
  75. var that = this;
  76. setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
  77. },
  78. focusToPassword() {
  79. this.$refs.passwordInput.$el.focus();
  80. },
  81. reload() {
  82. var ym = this.dateToString(this.date, true);
  83. this.getSummary({start: ym, end: ym});
  84. this.getDay();
  85. },
  86. dateToString(date, excludeDay) {
  87. var year = '' + date.getFullYear();
  88. var month = '' + (date.getMonth() + 1);
  89. month = '00'.substring(0, 2 - month.length) + month;
  90. var ym = year + '-' + month;
  91. if (excludeDay) {
  92. return ym;
  93. }
  94. var day = '' + (date.getDate());
  95. day = '00'.substring(0, 2 - day.length) + day;
  96. return ym + '-' + day;
  97. },
  98. getSummary(params) {
  99. if (!params) {
  100. params = {};
  101. }
  102. params['summary'] = true;
  103. this.daysUrl.query(params).then((response) => {
  104. return response.json();
  105. }, (response) => {
  106. alert('getSummary: failed to get resource');
  107. }).then((json) => {
  108. this.daysSummary = json;
  109. });
  110. },
  111. getDay(day) {
  112. if (day instanceof Date) {
  113. this.date = day;
  114. day = this.dateToString(day);
  115. } else if (this.date && this.date instanceof Date) {
  116. day = this.dateToString(this.date);
  117. } else {
  118. var today = new Date();
  119. day = this.dateToString(today);
  120. this.date = today;
  121. }
  122. this.$router.push('/day/' + day);
  123. this.daysUrl.get({day: day}).then((response) => {
  124. return response.json();
  125. }, (response) => {
  126. alert('getDay: failed to get resource');
  127. }).then((dayData) => {
  128. if (!dayData.day) {
  129. dayData.day = day;
  130. }
  131. this.day = dayData;
  132. });
  133. },
  134. login() {
  135. this.loginUrl.save({username: this.username, password: this.password}).then((response) => {
  136. return response.json();
  137. }, (response) => {
  138. alert('login: failed to get resource');
  139. }).then((data) => {
  140. this.showLoginForm = false;
  141. this.getUserInfo();
  142. });
  143. },
  144. logout() {
  145. this.logoutUrl.get().then((response) => {
  146. return response.json();
  147. }, (response) => {
  148. alert('logout: failed to get resource');
  149. }).then((json) => {
  150. this.loggedInUser = {};
  151. });
  152. },
  153. getUserInfo(callback) {
  154. this.currentUserUrl.get().then((response) => {
  155. return response.json();
  156. }, (response) => {
  157. alert('getUserInfo: failed to get resource');
  158. }).then((data) => {
  159. this.loggedInUser = data || {};
  160. if (callback) {
  161. callback(this.loggedInUser);
  162. }
  163. });
  164. }
  165. },
  166. components: {
  167. Datepicker, Group
  168. }
  169. }
  170. </script>
  171. <style>
  172. #app {
  173. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  174. -webkit-font-smoothing: antialiased;
  175. -moz-osx-font-smoothing: grayscale;
  176. color: #2c3e50;
  177. margin-top: 0px;
  178. }
  179. #datepicker {
  180. padding: 10px;
  181. }
  182. #panel .md-layout {
  183. flex: initial;
  184. }
  185. #toolbar-title {
  186. flex: 1;
  187. }
  188. #login-form {
  189. width: 200px;
  190. }
  191. </style>