Messages - 图1

Messages Component

Messages Vue component represents Messages component.

Messages Components

There are following components included:

  • **f7-messages** - main Messages container
  • **f7-message** - single message element
  • **f7-messages-title** - single messages title element

Messages Properties

PropTypeDefaultDescription
<f7-messages> properties
initbooleantrueInitializes Messages component
new-messages-firstbooleanfalseEnable if you want to use new messages on top, instead of having them on bottom
scroll-messagesbooleantrueEnable/disable messages autoscrolling when adding new message
scroll-messages-on-edgebooleantrueIf enabled then messages autoscrolling will happen only when user is on top/bottom of the messages view
<f7-message> properties
typestringsentMessage type: sent (default) or received
textstringMessage text
avatarstringMessage user’s avatar URL
namestringMessage user’s name
imagestringMessage image URL
headerstringMessage header
footerstringMessage footer
text-headerstringMessage text header
text-footerstringMessage text footer
firstbooleanfalseDefines that the message is first in the conversation
lastbooleanfalseDefines that the message is last in the conversation
tailbooleanfalseDefines that the message has visual “tail”. Usually last message in conversation
same-namebooleanfalseDefines that this message sender name is the same as on previous message
same-headerbooleanfalseDefines that this message header text is the same as on previous message
same-footerbooleanfalseDefines that this message footer text is the same as on previous message
same-avatarbooleanfalseDefines that this message user’s avatar URL is the same as on previous message

Messages Methods

<f7-messages> methods
.scroll(duration, position);Scroll messages to top/bottom depending on newMessagesFirst parameter
  • duration - number scroll duration in ms
  • position - number scroll position in px
.showTyping(message);Show typing message indicator
.hideTyping()Hide typing message indicator

Messages Events

EventDescription
<f7-message> events
clickEvent will be triggered when user clicks on message bubble
click:nameEvent will be triggered when user clicks on message user’s name
click:textEvent will be triggered when user clicks on message text
click:avatarEvent will be triggered when user clicks on message user’s avatar
click:headerEvent will be triggered when user clicks on message header
click:footerEvent will be triggered when user clicks on message footer
click:bubbleEvent will be triggered when user clicks on message bubble

Messages Slots

Single message Vue component (<f7-message>) has additional slots for custom elements:

  • **default** - element will be inserted as a child of <div class="message-bubble"> element in the end
  • **start** - element will be inserted in the beginning and direct child of main message element <div class="message">
  • **end** - element will be inserted in the end and direct child of main message element <div class="message">
  • **content-start** - element will be inserted in the beginning and direct child of the <div class="message-content"> element
  • **content-end** - element will be inserted in the end and direct child of the <div class="message-content"> element
  • **bubble-start** - element will be inserted in the beginning and direct child of the <div class="message-bubble"> element
  • **bubble-end** - element will be inserted in the end and direct child of the <div class="message-bubble"> element. Same as **default** slot

The following slots can be used inside of single message instead of same props if you need to pass there more complext layout:

  • **header** - element will be inserted in message header
  • **footer** - element will be inserted in message footer
  • **text** - element will be inserted in message text
  • **name** - element will be inserted in message name
  • **image** - element will be inserted in message image (supposed tp be an <img> element)
  • **text-header** - element will be inserted in message text header
  • **text-footer** - element will be inserted in message text footer
  1. <f7-message
  2. type="sent"
  3. text="Hello World"
  4. name="John Doe"
  5. avatar="path/to/image.jpg"
  6. >
  7. <div slot="start">Start</div>
  8. <div slot="end">End</div>
  9. <div slot="content-start">Content Start</div>
  10. <div slot="content-end">Content End</div>
  11. <div slot="bubble-start">Bubble Start</div>
  12. <div slot="bubble-end">Bubble End</div>
  13. </f7-message>
  14. <!-- Renders to: -->
  15. <div class="message message-sent">
  16. <div>Start</div>
  17. <div class="message-avatar" style="background-image: url(path/to/image.jpg);"></div>
  18. <div class="message-content">
  19. <div>Content Start</div>
  20. <div class="message-name">John Doe</div>
  21. <div class="message-bubble">
  22. <div>Bubble Start</div>
  23. <div class="message-text">Hello World</div>
  24. <div>Bubble End</div>
  25. </div>
  26. <div>Content End</div>
  27. </div>
  28. <div>End</div>
  29. </div>

Access To Messages Instance

If you use automatic initalization to init Messages (with init:true prop) and need to use Messages API you can access its initialized instance by accessing **.f7Messages** component’s property.

Examples

Here is how the full example of Messages page where it can be used together with Messagebar:

  1. <template>
  2. <f7-page>
  3. <f7-navbar title="Messsages"></f7-navbar>
  4. <f7-messagebar
  5. :placeholder="placeholder"
  6. ref="messagebar"
  7. :attachments-visible="attachmentsVisible"
  8. :sheet-visible="sheetVisible"
  9. >
  10. <f7-link
  11. icon-ios="f7:camera_fill"
  12. icon-md="material:camera_alt"
  13. slot="inner-start"
  14. @click="sheetVisible = !sheetVisible"
  15. ></f7-link>
  16. <f7-link
  17. icon-ios="f7:arrow_up_fill"
  18. icon-md="material:send"
  19. slot="inner-end"
  20. @click="sendMessage"
  21. ></f7-link>
  22. <f7-messagebar-attachments>
  23. <f7-messagebar-attachment
  24. v-for="(image, index) in attachments"
  25. :key="index"
  26. :image="image"
  27. @attachment:delete="deleteAttachment(image)"
  28. ></f7-messagebar-attachment>
  29. </f7-messagebar-attachments>
  30. <f7-messagebar-sheet>
  31. <f7-messagebar-sheet-image
  32. v-for="(image, index) in images"
  33. :key="index"
  34. :image="image"
  35. :checked="attachments.indexOf(image) >= 0"
  36. @change="handleAttachment"
  37. ></f7-messagebar-sheet-image>
  38. </f7-messagebar-sheet>
  39. </f7-messagebar>
  40. <f7-messages ref="messages">
  41. <f7-messages-title><b>Sunday, Feb 9,</b> 12:58</f7-messages-title>
  42. <f7-message
  43. v-for="(message, index) in messagesData"
  44. :key="index"
  45. :type="message.type"
  46. :image="message.image"
  47. :name="message.name"
  48. :avatar="message.avatar"
  49. :first="isFirstMessage(message, index)"
  50. :last="isLastMessage(message, index)"
  51. :tail="isTailMessage(message, index)"
  52. >
  53. <span slot="text" v-if="message.text" v-html="message.text"></span>
  54. </f7-message>
  55. <f7-message v-if="typingMessage"
  56. type="received"
  57. :typing="true"
  58. :first="true"
  59. :last="true"
  60. :tail="true"
  61. :header="`${typingMessage.name} is typing`"
  62. :avatar="typingMessage.avatar"
  63. ></f7-message>
  64. </f7-messages>
  65. </f7-page>
  66. </template>
  67. <script>
  68. export default {
  69. data() {
  70. return {
  71. attachments: [],
  72. sheetVisible: false,
  73. typingMessage: null,
  74. messagesData: [
  75. {
  76. type: 'sent',
  77. text: 'Hi, Kate',
  78. },
  79. {
  80. type: 'sent',
  81. text: 'How are you?',
  82. },
  83. {
  84. name: 'Kate',
  85. type: 'received',
  86. text: 'Hi, I am good!',
  87. avatar: 'http://lorempixel.com/100/100/people/9',
  88. },
  89. {
  90. name: 'Blue Ninja',
  91. type: 'received',
  92. text: 'Hi there, I am also fine, thanks! And how are you?',
  93. avatar: 'http://lorempixel.com/100/100/people/7',
  94. },
  95. {
  96. type: 'sent',
  97. text: 'Hey, Blue Ninja! Glad to see you ;)',
  98. },
  99. {
  100. type: 'sent',
  101. text: 'Hey, look, cutest kitten ever!',
  102. },
  103. {
  104. type: 'sent',
  105. image: 'http://lorempixel.com/200/260/cats/4/',
  106. },
  107. {
  108. name: 'Kate',
  109. type: 'received',
  110. text: 'Nice!',
  111. avatar: 'http://lorempixel.com/100/100/people/9',
  112. },
  113. {
  114. name: 'Kate',
  115. type: 'received',
  116. text: 'Like it very much!',
  117. avatar: 'http://lorempixel.com/100/100/people/9',
  118. },
  119. {
  120. name: 'Blue Ninja',
  121. type: 'received',
  122. text: 'Awesome!',
  123. avatar: 'http://lorempixel.com/100/100/people/7',
  124. },
  125. ],
  126. images: [
  127. 'http://lorempixel.com/300/300/cats/1/',
  128. 'http://lorempixel.com/200/300/cats/2/',
  129. 'http://lorempixel.com/400/300/cats/3/',
  130. 'http://lorempixel.com/300/150/cats/4/',
  131. 'http://lorempixel.com/150/300/cats/5/',
  132. 'http://lorempixel.com/300/300/cats/6/',
  133. 'http://lorempixel.com/300/300/cats/7/',
  134. 'http://lorempixel.com/200/300/cats/8/',
  135. 'http://lorempixel.com/400/300/cats/9/',
  136. 'http://lorempixel.com/300/150/cats/10/',
  137. ],
  138. people: [
  139. {
  140. name: 'Kate Johnson',
  141. avatar: 'http://lorempixel.com/100/100/people/9',
  142. },
  143. {
  144. name: 'Blue Ninja',
  145. avatar: 'http://lorempixel.com/100/100/people/7',
  146. },
  147. ],
  148. answers: [
  149. 'Yes!',
  150. 'No',
  151. 'Hm...',
  152. 'I am not sure',
  153. 'And what about you?',
  154. 'May be ;)',
  155. 'Lorem ipsum dolor sit amet, consectetur',
  156. 'What?',
  157. 'Are you sure?',
  158. 'Of course',
  159. 'Need to think about it',
  160. 'Amazing!!!',
  161. ],
  162. responseInProgress: false,
  163. };
  164. },
  165. computed: {
  166. attachmentsVisible() {
  167. const self = this;
  168. return self.attachments.length > 0;
  169. },
  170. placeholder() {
  171. const self = this;
  172. return self.attachments.length > 0 ? 'Add comment or Send' : 'Message';
  173. },
  174. },
  175. mounted() {
  176. const self = this;
  177. self.$f7ready(() => {
  178. self.messagebar = self.$refs.messagebar.f7Messagebar;
  179. self.messages = self.$refs.messages.f7Messages;
  180. });
  181. },
  182. methods: {
  183. isFirstMessage(message, index) {
  184. const self = this;
  185. const previousMessage = self.messagesData[index - 1];
  186. if (message.isTitle) return false;
  187. if (!previousMessage || previousMessage.type !== message.type || previousMessage.name !== message.name) return true;
  188. return false;
  189. },
  190. isLastMessage(message, index) {
  191. const self = this;
  192. const nextMessage = self.messagesData[index + 1];
  193. if (message.isTitle) return false;
  194. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  195. return false;
  196. },
  197. isTailMessage(message, index) {
  198. const self = this;
  199. const nextMessage = self.messagesData[index + 1];
  200. if (message.isTitle) return false;
  201. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  202. return false;
  203. },
  204. deleteAttachment(image) {
  205. const self = this;
  206. const index = self.attachments.indexOf(image);
  207. self.attachments.splice(index, 1)[0]; // eslint-disable-line
  208. },
  209. handleAttachment(e) {
  210. const self = this;
  211. const index = self.$$(e.target).parents('label.checkbox').index();
  212. const image = self.images[index];
  213. if (e.target.checked) {
  214. // Add to attachments
  215. self.attachments.unshift(image);
  216. } else {
  217. // Remove from attachments
  218. self.attachments.splice(self.attachments.indexOf(image), 1);
  219. }
  220. },
  221. sendMessage() {
  222. const self = this;
  223. const text = self.messagebar.getValue().replace(/\n/g, '<br>').trim();
  224. const messagesToSend = [];
  225. self.attachments.forEach((attachment) => {
  226. messagesToSend.push({
  227. image: attachment,
  228. });
  229. });
  230. if (text.trim().length) {
  231. messagesToSend.push({
  232. text,
  233. });
  234. }
  235. if (messagesToSend.length === 0) {
  236. return;
  237. }
  238. // Reset attachments
  239. self.attachments = [];
  240. // Hide sheet
  241. self.sheetVisible = false;
  242. // Clear area
  243. self.messagebar.clear();
  244. // Focus area
  245. if (text.length) self.messagebar.focus();
  246. // Send message
  247. self.messagesData.push(...messagesToSend);
  248. // Mock response
  249. if (self.responseInProgress) return;
  250. self.responseInProgress = true;
  251. setTimeout(() => {
  252. const answer = self.answers[Math.floor(Math.random() * self.answers.length)];
  253. const person = self.people[Math.floor(Math.random() * self.people.length)];
  254. self.typingMessage = {
  255. name: person.name,
  256. avatar: person.avatar,
  257. };
  258. setTimeout(() => {
  259. self.messagesData.push({
  260. text: answer,
  261. type: 'received',
  262. name: person.name,
  263. avatar: person.avatar,
  264. });
  265. self.typingMessage = null;
  266. self.responseInProgress = false;
  267. }, 4000);
  268. }, 1000);
  269. },
  270. },
  271. };
  272. </script>