Messagebar - 图1

Messagebar Vue Component

Messagebar Vue component represents Framework7’s Messagebar component.

Messagebar Components

There are following components included:

  • **f7-messagebar** - main messagebar element
  • **f7-messagebar-sheet** - messagebar sheet element
  • **f7-messagebar-sheet-image** - messagebar sheet image item
  • **f7-messagebar-sheet-item** - messagebar sheet item
  • **f7-messagebar-attachments** - messagebar attachments element
  • **f7-messagebar-attachment** - single messagebar attachment element

Messagebar - 图2

Messagebar Properties

PropTypeDefaultDescription
<f7-messagebar> properties
initbooleantrueInitializes Messagebar
namestringTextarea “name” attribute
textarea-idstring
number
Textarea “id” attribute
placeholderstringMessageTextarea placeholder text
valuestring
number
Textarea value
readonlybooleanfalseSets “readonly” textarea attribute
disabledbooleanfalseSets “disbled” textarea attribute
send-linkstringEnables Send link and specifies its text. This property will be ignored in case you use send-link slot
max-heightnumberDefines resizeable textarea max height
resizableboleantrueEnables resizeable textarea
sheet-visiblebooleanfalseMakes messagebar sheet visible/active
attachments-visiblebooleanfalseMakes messagebar attachments visible/active
resize-pagebooleantrueWhen enabled, it will resize messages page when messagebar textarea size changed
<f7-messagebar-sheet-image> properties
imagestringSheet image URL
checkedbooleanfalseIndicates whether this sheet image-item is checked or not
<f7-messagebar-attachment> properties
imagestringAttachment image URL
deletablebooleantrueDefines whether the attachment is deletable or not. In case of deletable the additional delete button will be rendered

Messagebar Methods

<f7-messagebar> methods
.clear()Clear textarea and update/reset its size
.setValue(newValue)Set messagebar textarea value/text
.getValue()Return messagebar textarea value
.resize()Force Messagebar to resize messages page depending on messagebar height/size
.focus()Focus messagebar textarea
.blur()Remove focus from messagebar textarea

Messagebar Events

EventArgumentsDescription
<f7-messagebar> events
change(event)Event will be triggered when “change” event occurs on messagebar textarea element
input(event)Event will be triggered when “input” event occurs on messagebar textarea element
focus(event)Event will be triggered when “focus” event occurs on messagebar textarea element
blur(event)Event will be triggered when “blur” event occurs on messagebar textarea element
submit
send
(value, clear)Event will be triggered when user clicks on messagebar “send link”
<f7-messagebar-sheet-image> events
change(event)Event will be triggered on sheet item checkbox change
<f7-messagebar-attachment> events
attachment:click(event)Event will be triggered on attachment click
attachment:delete(event)Event will be triggered on attachment delete button click

Messagebar Slots

Messagebar Vue component has additional slots for custom elements:

  • **default** - element will be inserted in the end of <div class="toolbar-inner">. element
  • **before-area** - element will be inserted right before textarea. Messagebar attachments go here
  • **after-area** - element will be inserted right after textarea
  • **send-link** - element will be inserted inside of send link
  • **before-inner** - element will be inserted right before <div class="toolbar-inner">. element
  • **after-inner** - element will be inserted right after <div class="toolbar-inner">. element. Messagebar sheet goes here
  • **inner-start** - element will be inserted in the beginning of <div class="toolbar-inner">. element
  • **inner-end** - element will be inserted in the end of <div class="toolbar-inner">. element. Same as default slot
  1. <f7-messagebar placeholder="Message" @submit="onSubmit">
  2. <div slot="before-inner">Before inner</div>
  3. <div slot="after-inner">After inner</div>
  4. <div slot="before-area">Before textarea</div>
  5. <div slot="after-area">After textarea</div>
  6. <f7-icon ios="f7:arrow_up_fill" md="material:send" slot="send-link"></f7-icon>
  7. <div>Default slot</div>
  8. </f7-messagebar>

Renders to:

  1. <div class="toolbar messagebar">
  2. <div>Before inner</div>
  3. <div class="toolbar-inner">
  4. <div class="messagebar-area">
  5. <div>Before textarea</div>
  6. <textarea placeholder="Message"></textarea>
  7. <div>After textarea</div>
  8. </div>
  9. <a href="#" class="link"><i class="icon f7-icons">arrow_up_fill</i></a>
  10. <div>Default slot</div>
  11. </div>
  12. <div>After inner</div>
  13. </div>

Access To Messagebar Instance

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

Examples

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

  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>