be_comp_chat.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Document : be_comp_chat.js
  3. * Author : pixelcave
  4. * Description: Custom JS code used in Chat Pages
  5. */
  6. var BeCompChat = function() {
  7. // Helper variables - set in initChat()
  8. var lWindow, lHeader, lFooter, cContainer, cHeight, cHead, cTalk, cPeople, cform, cTimeout;
  9. // Message Classes
  10. var classesMsgBase = 'rounded font-w600 p-10 mb-10 animated fadeIn',
  11. classesMsgSelf = 'ml-50 bg-primary-lighter text-primary-darker',
  12. classesMsgOther = 'mr-50 bg-body-light',
  13. classesMsgHeader = 'font-size-sm font-italic text-muted text-center mt-20 mb-10';
  14. // Init chat
  15. var initChat = function() {
  16. // Set variables
  17. lWindow = jQuery(window);
  18. lHeader = jQuery('#page-header');
  19. lFooter = jQuery('#page-footer');
  20. cContainer = jQuery('.js-chat-container');
  21. cHeight = cContainer.data('chat-height');
  22. cHead = jQuery('.js-chat-head');
  23. cTalk = jQuery('.js-chat-talk');
  24. cPeople = jQuery('.js-chat-people');
  25. cform = jQuery('.js-chat-form');
  26. // Chat height mode ('auto' for full height, number for specific height in pixels)
  27. switch (cHeight) {
  28. case 'auto':
  29. // Init chat windows' height to full available (also on browser resize or orientation change)
  30. jQuery(window).on('resize.cb.chat orientationchange.cb.chat', function(){
  31. clearTimeout(cTimeout);
  32. cTimeout = setTimeout(function(){
  33. initChatWindows();
  34. }, 150);
  35. }).triggerHandler('resize.cb.chat');
  36. break;
  37. default:
  38. // Init chat windows' height with a specific height
  39. initChatWindows(cHeight);
  40. }
  41. // Enable scroll lock to chat talk and people window
  42. cTalk.scrollLock('enable');
  43. if (cPeople.length) {
  44. cPeople.scrollLock('enable');
  45. }
  46. // Init form submission
  47. jQuery('form', cform).on('submit', function(e){
  48. // Stop form submission
  49. e.preventDefault();
  50. // Get chat input
  51. var chatInput = jQuery('.js-chat-input', jQuery(this));
  52. // Add message
  53. chatAddMessage(chatInput.data('target-chat-id'), chatInput.val(), 'self', chatInput);
  54. });
  55. };
  56. // Init chat windows' height
  57. var initChatWindows = function(customHeight) {
  58. var cHeightFinal;
  59. // If height is specified
  60. if (customHeight) {
  61. cHeightFinal = parseInt(customHeight);
  62. } else {
  63. // Calculate height
  64. var cHeightFinal = lWindow.height() -
  65. (lHeader.length ? lHeader.outerHeight() : 0) -
  66. (lFooter.length ? lFooter.outerHeight() : 0) -
  67. (parseInt(cContainer.css('padding-top')) + parseInt(cContainer.css('padding-bottom'))) -
  68. cHead.outerHeight();
  69. }
  70. // Add a minimum height
  71. if (cHeightFinal < 200) {
  72. cHeightFinal = 200;
  73. }
  74. // Set height to chat windows (+ people window if exists)
  75. cTalk.css('height', cHeightFinal - cform.outerHeight());
  76. if (cPeople.length) {
  77. cPeople.css('height', cHeightFinal);
  78. }
  79. };
  80. // Add a header message to a chat window
  81. var chatAddHeader = function(chatId, chatMsg) {
  82. // Get chat window
  83. var chatWindow = jQuery('.js-chat-talk[data-chat-id="' + chatId + '"]');
  84. // If time header and chat window exists
  85. if (chatMsg && chatWindow.length) {
  86. chatWindow.append('<div class="' + classesMsgHeader + '">'
  87. + jQuery('<div />').text(chatMsg).html()
  88. + '</div>');
  89. // Scroll the message list to the bottom
  90. chatWindow.animate({ scrollTop: chatWindow[0].scrollHeight }, 150);
  91. }
  92. };
  93. // Add a message to a chat window
  94. var chatAddMessage = function(chatId, chatMsg, chatMsgLevel, chatInput) {
  95. // Get chat window
  96. var chatWindow = jQuery('.js-chat-talk[data-chat-id="' + chatId + '"]');
  97. // If message and chat window exists
  98. if (chatMsg && chatWindow.length) {
  99. // Post it to its related window (if message level is 'self', make it stand out)
  100. chatWindow.append('<div class="' + classesMsgBase + ' ' + ((chatMsgLevel === 'self') ? classesMsgSelf : classesMsgOther) + '">'
  101. + jQuery('<div />').text(chatMsg).html()
  102. + '</div>');
  103. // Scroll the message list to the bottom
  104. chatWindow.animate({ scrollTop: chatWindow[0].scrollHeight }, 150);
  105. // If input is set, reset it
  106. if (chatInput) {
  107. chatInput.val('');
  108. }
  109. }
  110. };
  111. return {
  112. init: function() {
  113. // Init chat
  114. initChat();
  115. },
  116. addHeader: function(chatId, chatMsg) {
  117. // Add time header
  118. chatAddHeader(chatId, chatMsg);
  119. },
  120. addMessage: function(chatId, chatMsg, chatMsgLevel) {
  121. // Add message
  122. chatAddMessage(chatId, chatMsg, chatMsgLevel, false);
  123. }
  124. };
  125. }();
  126. // Initialize when page loads
  127. jQuery(function(){ BeCompChat.init(); });