4
0

main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // FUNCTIONS FOR CHAT
  2. $('body').arrive('#activeInfo', {onceOnly: true}, function() {
  3. chatLaunch();
  4. });
  5. function chatLaunch(){
  6. if(activeInfo.plugins["CHAT-enabled"] == true && activeInfo.plugins.includes["CHAT-authKey-include"] !== '' && activeInfo.plugins.includes["CHAT-appID-include"] !== '' && activeInfo.plugins.includes["CHAT-cluster-include"] !== ''){
  7. if (activeInfo.user.groupID <= activeInfo.plugins.includes["CHAT-Auth-include"]) {
  8. var menuList = `<li><a class="" href="javascript:void(0)" onclick="switchToPlugin('chat');chatEntry();"><i class="fa fa-comments-o fa-fw"></i> <span lang="en">Chat</span><small class="chat-counter label label-rouded label-info pull-right hidden">0</small></a></li>`;
  9. var htmlDOM = `
  10. <div id="container-plugin-chat" class="plugin-container hidden">
  11. <div class="chat-main-box bg-org">
  12. <!-- .chat-left-panel -->
  13. <div class="chat-left-aside">
  14. <div class="open-panel"><i class="ti-angle-right"></i></div>
  15. <div class="chat-left-inner bg-org"><ul class="chatonline style-none "></ul></div>
  16. </div>
  17. <!-- .chat-left-panel -->
  18. <!-- .chat-right-panel -->
  19. <div class="chat-right-aside">
  20. <div class="chat-box">
  21. <ul class="chat-list p-t-30"></ul>
  22. <div class="row send-chat-box">
  23. <div class="col-sm-12">
  24. <textarea class="form-control chat-input-send" placeholder="Type your message" lang="en"></textarea>
  25. <div class="custom-send">
  26. <button type="button" class="btn btn-info btn-lg custom-send-button"><i class="fa fa-paper-plane fa-2x"></i> </button>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. <!-- .chat-right-panel -->
  33. </div>
  34. </div>
  35. `;
  36. $('.append-menu').after(menuList);
  37. $('.plugin-listing').append(htmlDOM);
  38. pageLoad();
  39. // Enable pusher logging - don't include this in production
  40. //Pusher.logToConsole = true;
  41. // Add API Key & cluster here to make the connection
  42. var pusher = new Pusher(activeInfo.plugins.includes["CHAT-authKey-include"], {
  43. cluster: activeInfo.plugins.includes["CHAT-cluster-include"],
  44. encrypted: true
  45. });
  46. // Enter a unique channel you wish your users to be subscribed in.
  47. var channel = pusher.subscribe('org_channel');
  48. // bind the server event to get the response data and append it to the message div
  49. channel.bind('my-event',
  50. function(data) {
  51. formatMessage(data);
  52. $('.chat-list').append(formatMessage(data));
  53. $('.custom-send').html('<button type="button" class="btn btn-info btn-lg custom-send-button"><i class="fa fa-paper-plane fa-2x"></i> </button>');
  54. $(".chat-list").scrollTop($(".chat-list")[0].scrollHeight);
  55. if($('#container-plugin-chat').hasClass('hidden')){
  56. var chatSound = new Audio(activeInfo.plugins.includes["CHAT-newMessageSound-include"]);
  57. chatSound.play();
  58. message(data.username,data.message,activeInfo.settings.notifications.position,"#FFF","success","20000");
  59. $('.profile-image').addClass('animated loop-animation rubberBand');
  60. $('.chat-counter').removeClass('hidden').html(parseInt($('.chat-counter').text()) + 1);
  61. }
  62. });
  63. // check if the user is subscribed to the above channel
  64. channel.bind('pusher:subscription_succeeded', function(members) {
  65. organizrConsole('Plugin Function','Chat Websocket Connected!');
  66. organizrConsole('Plugin Function','Connecting to Organizr Chat DB');
  67. getMessagesAndUsers(activeInfo.settings.homepage.refresh["CHAT-userRefreshTimeout"], true);
  68. });
  69. /*jslint browser: true*/
  70. /*global $, jQuery, alert*/
  71. $(document).ready(function () {
  72. "use strict";
  73. $('.chat-left-inner > .chatonline').slimScroll({
  74. height: '100%',
  75. position: 'right',
  76. size: "0px",
  77. color: '#dcdcdc'
  78. });
  79. $('.chat-list').slimScroll({
  80. height: '100%',
  81. position: 'right',
  82. size: "0px",
  83. color: '#dcdcdc',
  84. start: 'bottom',
  85. });
  86. $(".open-panel").on("click", function () {
  87. $(".chat-left-aside").toggleClass("open-pnl");
  88. $(".open-panel i").toggleClass("ti-angle-left");
  89. });
  90. });
  91. }
  92. }
  93. }
  94. //Chat functions!
  95. $(document).on('keypress', '.chat-input-send', function(ev) {
  96. var keycode = (ev.keyCode ? ev.keyCode : ev.which);
  97. if (keycode == '13') {
  98. ev.preventDefault();
  99. $('.custom-send-button').click();
  100. }
  101. });
  102. // Send the Message enter by User
  103. $('body').on('click', '.custom-send-button', function(e) {
  104. e.preventDefault();
  105. var message = $('.chat-input-send').val();
  106. // Validate Name field
  107. if (message !== '') {
  108. organizrAPI2('POST','api/v2/plugins/chat/message',{ message : message }).success(function(data) {
  109. // Nada yet
  110. }).fail(function(xhr) {
  111. console.error("Organizr Function: API Connection Failed");
  112. });
  113. // Clear the message input field
  114. $('.chat-input-send').val('');
  115. // Show a loading image while sending
  116. $('.custom-send').html('<button type="button" class="btn btn-info btn-lg custom-send-button" disabled><i class="fa fa-spinner fa-pulse fa-2x"></i> </button>');
  117. }
  118. });
  119. function formatMessage(msg){
  120. var className = 'odd';
  121. if(msg.username == activeInfo.user.username){
  122. if(activeInfo.user.username == 'Guest' && activeInfo.user.uid !== msg.uid){
  123. className = '';
  124. }
  125. }else{
  126. className = '';
  127. }
  128. return `
  129. <li class="`+className+`">
  130. <div class="chat-image"> <img alt="male" src="`+msg.gravatar+`"> </div>
  131. <div class="chat-body">
  132. <div class="chat-text">
  133. <h4>`+msg.username+`</h4>
  134. <p> `+msg.message+` </p> <b>`+moment.utc(msg.date, "YYYY-MM-DD hh:mm").local().format('LLL')+`</b> </div>
  135. </div>
  136. </li>
  137. `;
  138. }
  139. function formatUsers(array){
  140. var users = {};
  141. var userList = '';
  142. array.reverse();
  143. $.each(array, function (i, v){
  144. if(!users.hasOwnProperty(v.username)){
  145. users[v.username] = {
  146. 'last':v.date,
  147. 'gravatar':v.gravatar
  148. }
  149. }
  150. });
  151. $.each(users, function (i, v) {
  152. userList += `
  153. <li>
  154. <a href="javascript:void(0)"><img src="`+v.gravatar+`" alt="user-img" class="img-circle"> <span>`+i+`<small class="text-success">`+moment.utc(v.last, "YYYY-MM-DD hh:mm[Z]").local().fromNow()+`</small></span></a>
  155. </li>
  156. `;
  157. });
  158. userList += '<li class="p-20"></li>';
  159. return userList;
  160. }
  161. function chatEntry(){
  162. $(".chat-list").scrollTop($(".chat-list")[0].scrollHeight);
  163. $('.chat-input-send').focus();
  164. $('.chat-counter').addClass('hidden').html('0');
  165. }
  166. function getMessagesAndUsers(timeout, initial = false){
  167. var timeout = (typeof timeout !== 'undefined') ? timeout : activeInfo.settings.homepage.refresh["CHAT-userRefreshTimeout"];
  168. organizrAPI2('GET','api/v2/plugins/chat/message').success(function(data) {
  169. var response = data.response;
  170. if(initial == true){
  171. $.each(response.data, function (i, v){
  172. $('.chat-list').append(formatMessage(v));
  173. });
  174. }
  175. $('.chatonline').html(formatUsers(response.data));
  176. }).fail(function(xhr) {
  177. console.error("Organizr Function: API Connection Failed");
  178. });
  179. var timeoutTitle = 'ChatUserList';
  180. if(typeof timeouts[timeoutTitle] !== 'undefined'){ clearTimeout(timeouts[timeoutTitle]); }
  181. timeouts[timeoutTitle] = setTimeout(function(){ getMessagesAndUsers(timeout, false); }, timeout);
  182. }
  183. $(document).on('click', '.profile-pic', function(e) {
  184. $('.profile-image').removeClass('animated loop-animation rubberBand');
  185. });