chat.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div class="flow-chat" :class="{active:active}">
  3. <button class="flow-chat__toggle" @click="active=!active">=</button>
  4. <div class="flow-chat__area">
  5. <div class="flow-chat__container">
  6. <input class="handle" type="text" v-model="handle" @blur="sendChatRename" @keyup.enter="sendChatRename">
  7. <div ref="messages" class="flow-chat__messages">
  8. <div v-for="m in events" class="message">
  9. <div class="handle">
  10. <div class="name">{{ m.handle }} <span v-if="m.type!='msg'">{{ m.type }}</span></div>
  11. <div class="time">{{ m.time | time }}</div>
  12. </div>
  13. <div class="text">{{ m.message }}</div>
  14. </div>
  15. </div>
  16. <input ref="input" class="message" @keyup.enter="send" type="text" v-model="input">
  17. </div> <!-- /container -->
  18. <div class="flow-chat__users">
  19. <div class="flow-chat__user" v-for="u in userList">
  20. <img src="../assets/user.svg"><span>{{ u }}</span>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. // init
  28. let storedHandle = localStorage.getItem('handle')
  29. if (!storedHandle || storedHandle === '') {
  30. storedHandle = 'someone'
  31. }
  32. // Load handle from storage
  33. export default {
  34. filters: {
  35. time (value) {
  36. const d = new Date(value)
  37. const hours = pad(d.getHours(), 2)
  38. const minutes = pad(d.getMinutes(), 2)
  39. let msg = `${hours}:${minutes}`
  40. return msg
  41. }
  42. },
  43. data () {
  44. return {
  45. active: false,
  46. handle: storedHandle,
  47. events: [],
  48. userList: [],
  49. input: ''
  50. }
  51. },
  52. watch: {
  53. active (val, oldVal) {
  54. if (val === true && oldVal === false) {
  55. this.$refs.input.focus()
  56. }
  57. },
  58. events () {
  59. const height = this.$refs.messages.clientHeight
  60. if (this.$refs.messages.scrollTop + height >= this.$refs.messages.scrollHeight) {
  61. this.$nextTick(() => {
  62. this.$refs.messages.scrollTop = this.$refs.messages.scrollHeight
  63. })
  64. }
  65. if (this.active === false) {
  66. this.active = true
  67. }
  68. }
  69. },
  70. mounted () {
  71. // this.$flowService.on('chatEvent', this.addChatEvent)
  72. this.$flowService.on('chatEvent', this.recvChatEvent)
  73. this.$flowService.on('chatUserList', this.recvChatUserList)
  74. this.$flowService.on('sessionJoin', this.sendChatJoin)
  75. // we might not be joined in a sess
  76. // off
  77. this.$flowService.connected(this.sendChatJoin)
  78. },
  79. beforeDestroy () {
  80. this.$flowService.off('chatEvent', this.recvChatEvent)
  81. this.$flowService.off('chatUserList', this.recvChatUserList)
  82. this.$flowService.off('sessionJoin', this.sendChatJoin)
  83. },
  84. methods: {
  85. send () {
  86. const msg = this.input
  87. if (msg.trim() === '') { return }
  88. this.input = ''
  89. const msgEvent = {type: 'msg', handle: this.handle, message: msg, time: new Date()}
  90. this.$flowService.chatEvent(msgEvent)
  91. },
  92. recvChatUserList (v) {
  93. this.userList = v.data
  94. },
  95. recvChatEvent (v) {
  96. this.events.push(v.data)
  97. },
  98. sendChatJoin () {
  99. // Clear messages here
  100. this.events = []
  101. this.$flowService.chatJoin(this.handle, this.$route.params.sessId)
  102. },
  103. sendChatRename () {
  104. const oldHandle = localStorage.getItem('handle')
  105. if (this.handle === oldHandle) return
  106. localStorage.setItem('handle', this.handle)
  107. this.$flowService.chatRename(this.handle)
  108. }
  109. }
  110. }
  111. function pad (n, width, z) {
  112. z = z || '0'
  113. n = n + ''
  114. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n
  115. }
  116. </script>
  117. <style>
  118. .flow-chat {
  119. height:100%;
  120. box-sizing:border-box;
  121. position:relative;
  122. width:0px;
  123. transition: all .3s;
  124. }
  125. .flow-chat.active {
  126. width:530px;
  127. }
  128. .flow-chat__toggle {
  129. user-select:none;
  130. cursor: pointer;
  131. position:absolute;
  132. display:flex;
  133. justify-content: center;
  134. align-items: center;
  135. width:30px;
  136. height:50px;
  137. left:-30px;
  138. top:calc(50% - 25px);
  139. }
  140. .flow-chat__area {
  141. height:100%;
  142. overflow:hidden;
  143. display:flex;
  144. flex-flow:row;
  145. padding:8px 0px;
  146. }
  147. .flow-chat__container {
  148. height:100%;
  149. display:flex;
  150. flex-flow:column;
  151. flex:1;
  152. padding:0px 8px;
  153. }
  154. .flow-chat__container >*:not(last-child) {
  155. margin-bottom:10px;
  156. }
  157. .flow-chat__users {
  158. flex-basis:100px;
  159. padding:20px 8px;
  160. }
  161. .flow-chat__user {
  162. display:flex;
  163. flex-flow:row;
  164. justify-content: space-between;
  165. align-items: center;
  166. }
  167. .flow-chat__user img {
  168. height:10px;
  169. width:auto;
  170. }
  171. .flow-chat__user span {
  172. text-align:center;
  173. flex:1;
  174. width: 100px;
  175. overflow:hidden;
  176. white-space: nowrap;
  177. text-overflow: ellipsis;
  178. }
  179. .flow-chat__messages {
  180. font-size:12px;
  181. overflow-y:scroll;
  182. min-width:300px;
  183. flex:1;
  184. }
  185. .flow-chat__messages .message{
  186. padding:2px 2px 12px 2px;
  187. }
  188. .flow-chat__messages .handle {
  189. display:flex;
  190. flex-flow:row;
  191. justify-content: space-between;
  192. align-items: center;
  193. padding-top:2px;
  194. }
  195. .flow-chat__messages .handle .name {
  196. padding-bottom:4px;
  197. }
  198. .flow-chat__messages .handle .time{
  199. font-weight:normal;
  200. font-size:8px;
  201. }
  202. .flow-chat__messages .text {
  203. padding-top:0px;
  204. padding-left:9px;
  205. }
  206. </style>