Messagebar - 图1

Messagebar

Framework7 comes with special resizealbe toolbar for usage with Messages

Messagebar Layout

Messagebar layour is pretty simple:

  1. <div class="toolbar messagebar">
  2. <div class="toolbar-inner">
  3. <div class="messagebar-area">
  4. <!-- messagebar attachments -->
  5. <div class="messagebar-attachments">...</div>
  6. <!-- messagebar resizable textarea -->
  7. <textarea class="resizable" placeholder="Message"></textarea>
  8. </div>
  9. <a href="#" class="link">Send</a>
  10. </div>
  11. <!-- messagebar sheet -->
  12. <div class="messagebar-sheet">...</div>
  13. </div>

Where

  • **messagebar-attachments** - block with messagebar attachments. Optional
  • **messagebar-sheet** - block with messagebar sheet. Optional

Messagebar place is very important, it should be inside of page and right before page-content:

  1. <div class="page">
  2. <!-- navbar -->
  3. <div class="navbar">...</div>
  4. <!-- messagebar -->
  5. <div class="toolbar messagebar">...</div>
  6. <!-- page-content/messages-content -->
  7. <div class="page-content messages-content">
  8. ... messages
  9. </div>
  10. </div>

Messagebar Sheet Layout

If need additional sheet with, for example, images that we can attach to message then we use extra block designed for this:

  1. <div class="messagebar-sheet">
  2. <!-- selectable sheet image -->
  3. <label class="checkbox messagebar-sheet-image" style="background-image:url(path/to/image1.png)">
  4. <input type="checkbox">
  5. <i class="icon icon-checkbox"></i>
  6. </label>
  7. <!-- another selectable sheet image -->
  8. <label class="checkbox messagebar-sheet-image" style="background-image:url(path/to/image2.png)">
  9. <input type="checkbox">
  10. <i class="icon icon-checkbox"></i>
  11. </label>
  12. <!-- some custom sheet item -->
  13. <div class="messagebar-sheet-item">
  14. <!-- any custom content here -->
  15. </div>
  16. </div>

Messagebar Attachments Layout

Messages attachments block is designed to display currently attached message items/images:

  1. <div class="messagebar-attachments">
  2. <!-- image attachment -->
  3. <div class="messagebar-attachment">
  4. <img src="path/to/image1.png">
  5. </div>
  6. <!-- deletable image attachment -->
  7. <div class="messagebar-attachment">
  8. <img src="path/to/image2.png">
  9. <!-- attachment delete button -->
  10. <span class="messagebar-attachment-delete"></span>
  11. </div>
  12. </div>

Messagebar App Methods

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

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

Messagebar Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
elstring
HTMLElement
CSS selector or HTML element of messagebar element (div class=”messagebar”)
textareaElstring
HTMLElement
CSS selector or HTML element of messagebar textarea element. By default (if not passed) will try to look for textarea inside of messagebar
maxHeightnumbernullMax height of textarea when it resized depending on amount of its text
attachmentsarray[]Array with attachments. For example [‘path/to/image1.png’, ‘path/to/image2.png’]
resizePagebooleantrueDisable if you don’t want to resize messages page when messagebar textarea size changed
onobjectObject with events handlers. For example:
  1. var messagebar = app.messagebar.create({
  2. el: ‘.messagebar’,
  3. on: {
  4. change: function () {
  5. console.log(‘Textarea value changed’)
  6. }
  7. }
  8. })
Render functions
renderAttachmentsfunction(attachments)Function to render attachments block. Must return full attachments HTML string
renderAttachmentfunction(attachment)Function to render single attachment. Must return full attachment HTML string

Messagebar Methods & Properties

So to create Messagebar we have to call:

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

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

Properties
messagebar.elMessagebar HTML element.
messagebar.$elDom7 element with messagebar HTML element.
messagebar.textareaElMessagebar textarea HTML element
messagebar.$textareaElDom7 element with messagebar textarea HTML element
messagebar.paramsObject with passed initialization parameters
messagebar.attachmentsArray with messagebar attachments
Methods
messagebar.getValue();Get messagebar textarea value
messagebar.setValue(value);Set messagebar textarea value/text
messagebar.clear();Clear textarea and update/reset its size
messagebar.focus();Focus messagebar textarea
messagebar.blur();Remove focus from messagebar textarea
messagebar.setPlaceholder(placeholder)Set/change messagebar placeholder text
messagebar.resizePage()Force Messagebar to resize messages page depending on messagebar height/size
messagebar.attachmentsCreate()Dynamically create attachments block HTML element
messagebar.attachmentsShow()Show attachments block
messagebar.attachmentsHide()Hide attachments block
messagebar.attachmentsToggle()Toggle attachments block
messagebar.renderAttachments()Render attachments block based on attachments data
messagebar.sheetCreate()Dynamically create messagebar sheet block HTML element
messagebar.sheetShow()Show messagebar sheet
messagebar.sheetHide()Hide messagebar sheet
messagebar.sheetToggle()Toggle messagebar sheet
messagebar.destroy();Destroy messagebar instance

Messagebar Events

Messagebar will fire the following DOM events on messagebar element and events on app and messagebar instance:

DOM Events

EventTargetDescription
messagebar:changeMessagebar Element<div class=”messagebar”>Event will be triggered after messagebar textarea value changed
messagebar:focusMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar textarea gets focus
messagebar:blurMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar textarea loses focus
messagebar:resizepageMessagebar Element<div class=”messagebar”>Event will be triggered when messagebar resizes messages page
messagebar:attachmentdeleteMessagebar attachment element<div class=”messagebar-attachment”>Event will be triggered after click on messagebar attachment delete button
messagebar:attachmentclickMessagebar attachment element<div class=”messagebar-attachment”>Event will be triggered on messagebar attachment click
messagebar:beforedestroyMessagebar Element<div class=”messagebar”>Event will be triggered right before Messagebar instance will be destroyed

App and Messagebar Instance Events

Messagebar instance emits events on both self instance and app instance. App instance events has same names prefixed with messagebar.

EventTargetArgumentsDescription
changemessagebar(messagebar)Event will be triggered after messagebar textarea value changed. As an argument event handler receives messagebar instance
messagebarChangeapp
focusmessagebar(messagebar)Event will be triggered when messagebar textarea gets focus. As an argument event handler receives messagebar instance
messagebarFocusapp
blurmessagebar(messagebar)Event will be triggered when messagebar textarea loses focus. As an argument event handler receives messagebar instance
messagebarBlurapp
resizePagemessagebar(messagebar)Event will be triggered when messagebar resizes messages page. As an argument event handler receives messagebar instance
messagebarResizePageapp
attachmentDeletemessagebar(messagebar, attachmentEl, attachmentIndex)Event will be triggered after click on messagebar attachment delete button. As an argument event handler receives messagebar instance, clicked attachment HTML element and attachment index number
messagebarAttachmentDeleteapp
attachmentClickmessagebar(messagebar, attachmentEl, attachmentIndex)Event will be triggered on messagebar attachment click. As an argument event handler receives messagebar instance, clicked attachment HTML element and attachment index number
messagebarAttachmentClickapp
beforeDestroymessagebar(messagebar)Event will be triggered right before Messagebar instance will be destroyed
messagebarBeforeDestroyapp

Messagebar Auto Initialization

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

  1. <div class="toolbar messagebar messagebar-init" data-max-height="200">
  2. <div class="toolbar-inner">
  3. <div class="messagebar-area">
  4. <textarea placeholder="Message"></textarea>
  5. </div>
  6. <a href="#" class="link">Send</a>
  7. </div>
  8. </div>

Parameters that used in camelCase, for example maxHeight, in data- attributes should be used in kebab-case as data-max-height

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. <a class="link toggle-sheet" href="#">
  10. <i class="icon f7-icons ios-only">camera_fill</i>
  11. <i class="icon material-icons md-only">camera_alt</i>
  12. </a>
  13. <div class="messagebar-area">
  14. <textarea placeholder="Message"></textarea>
  15. </div>
  16. <a class="link" href="#">Send</a>
  17. </div>
  18. <div class="messagebar-sheet"></div>
  19. </div>
  20. <div class="page-content messages-content">
  21. <div class="messages">
  22. ...
  23. </div>
  24. </div>
  25. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Init Messages
  4. var messages = app.messages.create({
  5. ...
  6. });
  7. // Init messagebar
  8. var messagebar = app.messagebar.create({
  9. el: '.messagebar',
  10. attachments: []
  11. });
  12. // Available Images
  13. var images = [
  14. 'http://lorempixel.com/300/300/cats/1/',
  15. 'http://lorempixel.com/200/300/cats/2/',
  16. 'http://lorempixel.com/400/300/cats/3/',
  17. 'http://lorempixel.com/300/150/cats/4/',
  18. 'http://lorempixel.com/150/300/cats/5/',
  19. 'http://lorempixel.com/300/300/cats/6/',
  20. 'http://lorempixel.com/300/300/cats/7/',
  21. 'http://lorempixel.com/200/300/cats/8/',
  22. 'http://lorempixel.com/400/300/cats/9/',
  23. 'http://lorempixel.com/300/150/cats/10/'
  24. ]
  25. // Create sheet items
  26. var sheetHtml = '';
  27. images.forEach(function (image) {
  28. sheetHtml +=
  29. '<label class="checkbox messagebar-sheet-image" style="background-image:url(' + image + ')">' +
  30. '<input type="checkbox">' +
  31. '<i class="icon icon-checkbox"></i>' +
  32. '</label>';
  33. });
  34. $$('.messagebar-sheet').html(sheetHtml);
  35. /*========================
  36. Handle Attachments
  37. ========================*/
  38. function checkAttachments() {
  39. if (messagebar.attachments.length > 0) {
  40. messagebar.attachmentsShow();
  41. messagebar.setPlaceholder('Add comment or Send');
  42. } else {
  43. messagebar.attachmentsHide();
  44. messagebar.setPlaceholder('Message');
  45. }
  46. }
  47. $$('.messagebar-sheet-image input').on('change', function (e) {
  48. var index = $$(e.target).parents('.messagebar-sheet-image').index();
  49. var image = images[index];
  50. if (e.target.checked) {
  51. // Add to attachments
  52. messagebar.attachments.unshift(image)
  53. } else {
  54. // Remove from attachments
  55. messagebar.attachments.splice(messagebar.attachments.indexOf(image), 1);
  56. }
  57. messagebar.renderAttachments();
  58. checkAttachments();
  59. });
  60. messagebar.on('attachmentDelete', function (messagebar, attachmentEl, attachmentIndex) {
  61. var image = messagebar.attachments.splice(attachmentIndex, 1)[0];
  62. messagebar.renderAttachments();
  63. checkAttachments();
  64. // Uncheck in sheet
  65. var imageIndex = images.indexOf(image);
  66. messagebar.$el.find('.messagebar-sheet .checkbox').eq(imageIndex).find('input').prop('checked', false);
  67. });
  68. /*========================
  69. Toggle Sheet
  70. ========================*/
  71. $$('a.toggle-sheet').on('click', function () {
  72. messagebar.sheetToggle();
  73. });