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