Messages - 图1

Messages

Messages component will help you with visualisation of comments and messaging system in your app.

Messages Layout

  1. <div class="page">
  2. <div class="page-content messages-content">
  3. <div class="messages">
  4. <!-- Date stamp -->
  5. <div class="messages-title"><b>Sunday, Feb 9</b> 12:58</div>
  6. <!-- Sent message (by default - green and on right side) -->
  7. <div class="message message-sent">
  8. <div class="message-content">
  9. <!-- Bubble with text -->
  10. <div class="message-bubble">
  11. <div class="message-text">Hi, Kate</div>
  12. </div>
  13. </div>
  14. </div>
  15. <!-- Another sent message -->
  16. <div class="message message-sent">
  17. <div class="message-content">
  18. <div class="message-bubble">
  19. <div class="message-text">How are you?</div>
  20. </div>
  21. </div>
  22. </div>
  23. <!-- Received message (by default - grey on left side) -->
  24. <div class="message message-with-avatar message-received">
  25. <!-- Sender name -->
  26. <div class="message-name">Kate</div>
  27. <!-- Bubble with text -->
  28. <div class="message-text">I am fine, thanks</div>
  29. <!-- Sender avatar -->
  30. <div style="background-image:url(http://lorempixel.com/output/people-q-c-100-100-9.jpg)" class="message-avatar"></div>
  31. </div>
  32. <div class="message message-received">
  33. <!-- Sender name -->
  34. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/7)"></div>
  35. <div class="message-content">
  36. <!-- Sender name -->
  37. <div class="message-name">Blue Ninja</div>
  38. <!-- Bubble with text -->
  39. <div class="message-bubble">
  40. <div class="message-text">Hi there, I am also fine, thanks! And how are you?</div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>

Messages page layout:

  • messages-content - required additional class for messages wrapper. Should be added to page-content

  • messages - required additional wrapper for messages bubbles. Required element.

  • messages-title - messages title

  • message - single message

Single Message Layout

Here is a full single message layout:

  1. <div class="message">
  2. <div class="message-avatar" style="background-image:url(path/to/avatar)"></div>
  3. <div class="message-content">
  4. <div class="message-name">John Doe</div>
  5. <div class="message-header">Message header</div>
  6. <div class="message-bubble">
  7. <div class="message-text-header">Text header</div>
  8. <div class="message-image">
  9. <img src="path/to/image">
  10. </div>
  11. <div class="message-text">Hello world!</div>
  12. <div class="message-text-footer">Text footer</div>
  13. </div>
  14. <div class="message-footer">Message footer</div>
  15. </div>
  16. </div>
  • message-avatar - sender avatar, optional

  • message-name - sender name, optional

  • message-header - single message header, optional

  • message-text-header - text header inside of bubble, optional

  • message-image - message image, optional

  • message-text - message text, optional

  • message-text-footer - text footer inside of bubble, optional

  • message-footer - footer text after bubble, optional

Additional classes for single message container

  • message-sent - additional class for single message which indicates that this message was sent by user. It stays on right side with green background color.

  • message-received - additional class for single message which indicates that this message was received by user. It stays on left side with grey background color.

  • message-tail - additional class for single message (received or sent) to add bubble “tail”

  • message-same-name - additional class for indicating that message has same sender name as previous message

  • message-same-avatar - additional class for indicating that message has same avatar as previous message

  • message-same-header - additional class for indicating that message has same message-header as previous message

  • message-same-footer - additional class for indicating that message has same message-footer as previous message

  • message-last - additional class for single message (received or sent) to indicate last received or last sent message in current conversation by one sender

  • message-first - additional class for single message (received or sent) to indicate first received or first sent message in current conversation by one sender

Messages App Methods

Now, when we have Messages’ HTML, we need to initialize it. We need to use related App’s method:

app.messages.create(parameters)Initialize Messages with parameters
  • parameters - object - object with Messages parameters
  • Method returns initialized Messages instance
app.messages.destroy(el)Destroy Messages instance
  • el - HTMLElement or string (with CSS Selector) or object. Messages element or Messages instance to destroy.
app.messages.get(el)Get Messages instance by HTML element
  • el - HTMLElement or string (with CSS Selector). Messages element.
  • Method returns Messages’s instance

Messages Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
autoLayoutbooleantrueEnable Auto Layout to add all required additional classes automatically based on passed conditions
newMessagesFirstbooleanfalseEnable if you want to use new messages on top, instead of having them on bottom
scrollMessagesbooleantrueEnable/disable messages autoscrolling when adding new message
scrollMessagesOnEdgebooleantrueIf enabled then messages autoscrolling will happen only when user is on top/bottom of the messages view
messagesarrayArray with initial messages. Each message in array should be presented as an object with single message parameters
onobjectObject with events handlers. For example:
  1. var messages = app.messages.create({
  2. el: ‘.messages’,
  3. on: {
  4. change: function () {
  5. console.log(‘Textarea value changed’)
  6. }
  7. }
  8. })
renderMessagefunction(message)Function to render single message. Must return full message HTML string
Autolayout Conditions
firstMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-first class will be added to message
lastMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-last class will be added to message
tailMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-tail class will be added to message
sameNameMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-name class will be added to message
sameHeaderMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-header class will be added to message
sameFooterMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-footer class will be added to message
sameAvatarMessageRulefunction(message, previousMessage, nextMessage)Function that must return boolean true or false based on required condition depending on previous and next messages. In case of match then message-same-avatar class will be added to message
customClassMessageRulefunction(message, previousMessage, nextMessage)Function that must return additional message classes as string, based on required condition depending on previous and next messages.

Single Message Parameters

Let’s look on single message parameters object that we should use when we pass messages array:

ParameterTypeDefaultDescription
textstringMessage text
headerstringSingle message header
footerstringSingle message footer
namestringSender name
avatarstringSender avatar URL string
typestringsentMessage type - sent or received
textHeaderstringMessage text header
textFooterstringMessage text footer
imagestringMessage image HTML string, e.g. <img src=”path/to/image”>. Can be used instead of imageSrc parameter
imageSrcstringMessage image URL string. Can be used instead of image parameter
isTitlebooleanDefines whether it should be rendered as a message or as a messages title

Messages Methods & Properties

So to create Messages we have to call:

  1. var messages = app.messages.create({ /* parameters */ })

After we initialize Messages we have its initialized instance in variable (like messages variable in example above) with helpful methods and properties:

Properties
messages.paramsObject with passed initialization parameters
messages.elMessages container HTML element (<div class=”messages”>)
messages.$elDom7 element with messages HTML element
messages.messagesArray with messages
Methods
messages.showTyping(message)Show typing message indicator
messages.hideTyping()Hide typing message indicator
messages.addMessage(message, method, animate);

Add new message to the end or to the beginning depending on method parameter

  • message - object - parameters of message to add. Required.
  • method - string - (append or prepend) dictates to add new message in the end or in the beginning of messages container. Optional, if not specified, then it will add message depending on newMessagesFirst parameter
  • animate - boolean - (by default true) You may pass here false and message will be added immediately without any transiton and scrolling animation. Optional.
  • Method returns Messages instance
messages.addMessages(messages, method, animate);Add multiple messages per once.
  • messages - array with messages to add. Each message in array should be presented as an object with message parameters Required.
  • Method returns Messages instance
messages.removeMessage(message);Remove message
  • message - HTMLElement or string (with CSS Selector) or number (with index number of message from messages array) of message to remove
  • Method returns Messages instance
messages.removeMessages(messages);Remove multiple messages
  • messages - array with messages to remove
  • Method returns Messages instance
messages.scroll(duration, position);Scroll messages to top/bottom depending on newMessagesFirst parameter
  • duration - number scroll duration in ms
  • position - number scroll position in px
messages.renderMessages()Render messages HTML depending on messages array
messages.layout();Force messages auto layout
messages.clear();Clear/remove all the messages
messages.destroy();Destroy messages instance

Messages Events

Messages will fire the following DOM events on messages element and events on app and messages instance:

DOM Events

EventTargetDescription
messages:beforedestroyMessages Element<div class=”messages”>Event will be triggered right before Messages instance will be destroyed

App and Messages Instance Events

Messages instance emits events on both self instance and app instance. App instance events has same names prefixed with messages.

EventTargetArgumentsDescription
beforeDestroymessages(messages)Event will be triggered right before Messages instance will be destroyed
messagesBeforeDestroyapp

Messages Auto Initialization

If you don’t need to use Messages API and your Messages is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional **messages-init** class to messages element, and all required parameters can be passed using **data-** attributes:

  1. <div class="messages messages-init" data-new-messages-first="true">
  2. ...
  3. </div>

Parameters that used in camelCase, for example newMessagesFirst, in data- attributes should be used in kebab-case as data-new-messages-first

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="title">Messages</div>
  5. </div>
  6. </div>
  7. <div class="toolbar messagebar">
  8. <div class="toolbar-inner">
  9. <div class="messagebar-area">
  10. <textarea class="resizable" placeholder="Message"></textarea>
  11. </div><a class="link send-link" href="#">Send</a>
  12. </div>
  13. </div>
  14. <div class="page-content messages-content">
  15. <div class="messages">
  16. <!-- Messages title -->
  17. <div class="messages-title"><b>Sunday, Feb 9,</b> 12:58</div>
  18. <!-- Full layout sent message -->
  19. <div class="message message-sent">
  20. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/7);"></div>
  21. <div class="message-content">
  22. <div class="message-name">John Doe</div>
  23. <div class="message-header">Message header</div>
  24. <div class="message-bubble">
  25. <div class="message-text-header">Text header</div>
  26. <div class="message-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  27. <div class="message-text-footer">Text footer</div>
  28. </div>
  29. <div class="message-footer">Message footer</div>
  30. </div>
  31. </div>
  32. <!-- Full layout received message -->
  33. <div class="message message-received">
  34. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/7);"></div>
  35. <div class="message-content">
  36. <div class="message-name">John Doe</div>
  37. <div class="message-header">Message header</div>
  38. <div class="message-bubble">
  39. <div class="message-text-header">Text header</div>
  40. <div class="message-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
  41. <div class="message-text-footer">Text footer</div>
  42. </div>
  43. <div class="message-footer">Message footer</div>
  44. </div>
  45. </div>
  46. <!-- Messages -->
  47. <div class="message message-sent">
  48. <div class="message-content">
  49. <div class="message-bubble">
  50. <div class="message-text">Hi, Kate</div>
  51. </div>
  52. </div>
  53. </div>
  54. <div class="message message-sent">
  55. <div class="message-content">
  56. <div class="message-bubble">
  57. <div class="message-text">How are you?</div>
  58. </div>
  59. </div>
  60. </div>
  61. <div class="message message-received">
  62. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/9);"></div>
  63. <div class="message-content">
  64. <div class="message-name">Kate</div>
  65. <div class="message-bubble">
  66. <div class="message-text">Hi, I am good!</div>
  67. </div>
  68. </div>
  69. </div>
  70. <div class="message message-received">
  71. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/7);"></div>
  72. <div class="message-content">
  73. <div class="message-name">Blue Ninja</div>
  74. <div class="message-bubble">
  75. <div class="message-text">Hi there, I am also fine, thanks! And how are you?</div>
  76. </div>
  77. </div>
  78. </div>
  79. <div class="message message-sent">
  80. <div class="message-content">
  81. <div class="message-bubble">
  82. <div class="message-text">Hey, Blue Ninja! Glad to see you ;)</div>
  83. </div>
  84. </div>
  85. </div>
  86. <div class="message message-sent">
  87. <div class="message-content">
  88. <div class="message-bubble">
  89. <div class="message-text">Hey, look, cutest kitten ever!</div>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="message message-sent">
  94. <div class="message-content">
  95. <div class="message-bubble">
  96. <div class="message-image"><img src="http://lorempixel.com/200/260/cats/4/" style="width:200px; height: 260px;"></div>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="message message-received">
  101. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/9);"></div>
  102. <div class="message-content">
  103. <div class="message-name">Kate</div>
  104. <div class="message-bubble">
  105. <div class="message-text">Nice!</div>
  106. </div>
  107. </div>
  108. </div>
  109. <div class="message message-received">
  110. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/9);"></div>
  111. <div class="message-content">
  112. <div class="message-name">Kate</div>
  113. <div class="message-bubble">
  114. <div class="message-text">Like it very much!</div>
  115. </div>
  116. </div>
  117. </div>
  118. <div class="message message-received">
  119. <div class="message-avatar" style="background-image:url(http://lorempixel.com/100/100/people/7);"></div>
  120. <div class="message-content">
  121. <div class="message-name">Blue Ninja</div>
  122. <div class="message-bubble">
  123. <div class="message-text">Awesome!</div>
  124. </div>
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Init Messages
  4. var messages = app.messages.create({
  5. el: '.messages',
  6. // First message rule
  7. firstMessageRule: function (message, previousMessage, nextMessage) {
  8. // Skip if title
  9. if (message.isTitle) return false;
  10. /* if:
  11. - there is no previous message
  12. - or previous message type (send/received) is different
  13. - or previous message sender name is different
  14. */
  15. if (!previousMessage || previousMessage.type !== message.type || previousMessage.name !== message.name) return true;
  16. return false;
  17. },
  18. // Last message rule
  19. lastMessageRule: function (message, previousMessage, nextMessage) {
  20. // Skip if title
  21. if (message.isTitle) return false;
  22. /* if:
  23. - there is no next message
  24. - or next message type (send/received) is different
  25. - or next message sender name is different
  26. */
  27. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  28. return false;
  29. },
  30. // Last message rule
  31. tailMessageRule: function (message, previousMessage, nextMessage) {
  32. // Skip if title
  33. if (message.isTitle) return false;
  34. /* if (bascially same as lastMessageRule):
  35. - there is no next message
  36. - or next message type (send/received) is different
  37. - or next message sender name is different
  38. */
  39. if (!nextMessage || nextMessage.type !== message.type || nextMessage.name !== message.name) return true;
  40. return false;
  41. }
  42. });
  43. // Init Messagebar
  44. var messagebar = app.messagebar.create({
  45. el: '.messagebar'
  46. });
  47. // Response flag
  48. var responseInProgress = false;
  49. // Send Message
  50. $$('.send-link').on('click', function () {
  51. var text = messagebar.getValue().replace(/\n/g, '<br>').trim();
  52. // return if empty message
  53. if (!text.length) return;
  54. // Clear area
  55. messagebar.clear();
  56. // Return focus to area
  57. messagebar.focus();
  58. // Add message to messages
  59. messages.addMessage({
  60. text: text,
  61. });
  62. if (responseInProgress) return;
  63. // Receive dummy message
  64. receiveMessage();
  65. });
  66. // Dummy response
  67. var answers = [
  68. 'Yes!',
  69. 'No',
  70. 'Hm...',
  71. 'I am not sure',
  72. 'And what about you?',
  73. 'May be ;)',
  74. 'Lorem ipsum dolor sit amet, consectetur',
  75. 'What?',
  76. 'Are you sure?',
  77. 'Of course',
  78. 'Need to think about it',
  79. 'Amazing!!!'
  80. ]
  81. var people = [
  82. {
  83. name: 'Kate Johnson',
  84. avatar: 'http://lorempixel.com/100/100/people/9'
  85. },
  86. {
  87. name: 'Blue Ninja',
  88. avatar: 'http://lorempixel.com/100/100/people/7'
  89. }
  90. ];
  91. function receiveMessage() {
  92. responseInProgress = true;
  93. setTimeout(function () {
  94. // Get random answer and random person
  95. var answer = answers[Math.floor(Math.random() * answers.length)];
  96. var person = people[Math.floor(Math.random() * people.length)];
  97. // Show typing indicator
  98. messages.showTyping({
  99. header: person.name + ' is typing',
  100. avatar: person.avatar
  101. });
  102. setTimeout(function () {
  103. // Add received dummy message
  104. messages.addMessage({
  105. text: answer,
  106. type: 'received',
  107. name: person.name,
  108. avatar: person.avatar
  109. });
  110. // Hide typing indicator
  111. messages.hideTyping();
  112. responseInProgress = false;
  113. }, 4000);
  114. }, 1000);
  115. }