chat.html.twig 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <div class="page-chat">
  2. <div class="row">
  3. <div class="col-sm-4 col-md-5 col-lg-4">
  4. <ul class="row list-unstyled" id="chat-users"></ul>
  5. </div>
  6. <div class="col-sm-8 col-md-7 col-lg-8">
  7. <div id="chat-tabs">
  8. <ul class="nav nav-tabs" role="tablist">
  9. {% if not restrict_to_coach %}
  10. <li role="presentation" class="active">
  11. <a href="#all" aria-controls="all" role="tab" data-toggle="tab">{{ 'All'|get_lang }}</a>
  12. </li>
  13. {% endif %}
  14. </ul>
  15. <div class="tab-content">
  16. <div role="tabpanel" class="tab-pane active" id="{{ restrict_to_coach ? '' : 'all' }}">
  17. <div class="course-chat chat-history" id="{{ restrict_to_coach ? '' : 'chat-history' }}"></div>
  18. </div>
  19. </div>
  20. </div>
  21. <div class="profile row">
  22. <div class="col-xs-12">
  23. <div class="message-form-chat">
  24. <div class="tabbable">
  25. <ul class="nav nav-tabs">
  26. <li class="active">
  27. <a href="#tab1" data-toggle="tab">{{ 'Write'|get_lang }}</a>
  28. </li>
  29. <li>
  30. <a href="#tab2" id="preview" data-toggle="tab">{{ 'Preview'|get_lang }}</a>
  31. </li>
  32. <li>
  33. <button id="emojis" class="btn btn-link" type="button">
  34. <span class="sr-only">{{ 'Emoji'|get_lang }}</span>{{ emoji_smile }}
  35. </button>
  36. </li>
  37. </ul>
  38. <div class="tab-content">
  39. <div class="tab-pane active" id="tab1">
  40. <div class="row">
  41. <div class="col-sm-9">
  42. <span class="sr-only">{{ 'Message'|get_lang }}</span>
  43. <textarea id="chat-writer" name="message"></textarea>
  44. </div>
  45. <div class="col-sm-3">
  46. <button id="chat-send-message" type="button" {{ restrict_to_coach ? 'disabled' : '' }}
  47. class="btn btn-primary">{{ 'Send'|get_lang }}</button>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="tab-pane" id="tab2">
  52. <div id="html-preview" class="emoji-wysiwyg-editor-preview"></div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. <audio id="chat-alert" class="skip">
  63. <source src="{{ _p.web_main }}/chat/sound/notification.wav" type="audio/wav"></source>
  64. <source src="{{ _p.web_main }}chat/sound/notification.ogg" type="audio/ogg"></source>
  65. <source src="{{ _p.web_main }}chat/sound/notification.mp3" type="audio/mpeg"></source>
  66. </audio>
  67. <script>
  68. $(function () {
  69. var ChChat = {
  70. _ajaxUrl: '{{ _p.web_ajax }}course_chat.ajax.php?{{ _p.web_cid_query }}',
  71. _historySize: -1,
  72. usersOnline: 0,
  73. currentFriend: 0,
  74. call: false,
  75. track: function () {
  76. return $
  77. .get(ChChat._ajaxUrl, {
  78. action: 'track',
  79. size: ChChat._historySize,
  80. users_online: ChChat.usersOnline,
  81. friend: ChChat.currentFriend
  82. })
  83. .done(function (response) {
  84. if (response.data.history) {
  85. ChChat._historySize = response.data.oldFileSize;
  86. ChChat.setHistory(response.data.history);
  87. }
  88. if (response.data.userList) {
  89. ChChat.usersOnline = response.data.usersOnline;
  90. ChChat.setConnectedUsers(response.data.userList);
  91. }
  92. });
  93. },
  94. setHistory: function (messageList) {
  95. var chatHistoryContainer = ChChat.currentFriend ? ('#chat-history-' + ChChat.currentFriend) : '#chat-history';
  96. $(chatHistoryContainer)
  97. .html(messageList)
  98. .prop('scrollTop', function () {
  99. return this.scrollHeight;
  100. });
  101. $('#chat-alert').get(0).play();
  102. },
  103. setConnectedUsers: function (userList) {
  104. var html = '';
  105. userList.forEach(function (user) {
  106. var buttonStatus = user.isConnected ? 'success' : 'muted',
  107. buttonTitle = user.isConnected ? '{{ 'StartAChat'|get_lang }}' : '{{ 'LeaveAMessage'|get_lang }}';
  108. html += '<li class="col-xs-12">' +
  109. ' <div class="chat-user" data-name="' + user.complete_name + '" data-user="' + user.id + '">' +
  110. ' <img src="' + user.image_url + '" alt="' + user.complete_name + '" class="img-circle user-image-chat"/>' +
  111. ' <ul class="list-unstyled">' +
  112. ' <li>' + user.complete_name;
  113. if (user.id != {{ _u.user_id }}) {
  114. html += ' <button type="button" class="btn btn-link btn-xs" title="' + buttonTitle + '" data-name="' + user.complete_name + '" data-user="' + user.id + '">' +
  115. ' <i class="fa fa-comments text-' + buttonStatus + '"></i><span class="sr-only">' + buttonTitle + '</span>' +
  116. ' </button>';
  117. }
  118. html += ' </li>' +
  119. {% if chamilo_settings_get('profile.hide_username_in_course_chat') == 'false' %}
  120. ' <li><small>' + user.username + '</small></li>' +
  121. {% else %}
  122. ' <li>&nbsp;</li>' +
  123. {% endif %}
  124. ' </ul>' +
  125. ' </div>' +
  126. '</li>';
  127. });
  128. $('#chat-users').html(html);
  129. },
  130. onPreviewListener: function () {
  131. $
  132. .post(ChChat._ajaxUrl, {
  133. action: 'preview',
  134. 'message': $('textarea#chat-writer').val()
  135. })
  136. .done(function (response) {
  137. if (!response.status) {
  138. return;
  139. }
  140. $('#html-preview').html(response.data.message);
  141. });
  142. },
  143. onSendMessageListener: function (e) {
  144. e.preventDefault();
  145. if (!$('textarea#chat-writer').val().trim().length) {
  146. return;
  147. }
  148. var self = this;
  149. self.disabled = true;
  150. $
  151. .post(ChChat._ajaxUrl, {
  152. action: 'write',
  153. message: $('textarea#chat-writer').val(),
  154. friend: ChChat.currentFriend
  155. })
  156. .done(function (response) {
  157. self.disabled = false;
  158. if (!response.status) {
  159. return;
  160. }
  161. $('textarea#chat-writer').val('');
  162. $(".emoji-wysiwyg-editor").html('');
  163. });
  164. },
  165. onResetListener: function (e) {
  166. if (!confirm("{{ 'ConfirmReset'|get_lang }}")) {
  167. e.preventDefault();
  168. return;
  169. }
  170. $
  171. .get(ChChat._ajaxUrl, {
  172. action: 'reset',
  173. friend: ChChat.currentFriend
  174. })
  175. .done(function (response) {
  176. if (!response.status) {
  177. return;
  178. }
  179. ChChat.setHistory(response.data);
  180. });
  181. },
  182. init: function () {
  183. ChChat.track().done(function () {
  184. ChChat.init();
  185. });
  186. }
  187. };
  188. hljs.initHighlightingOnLoad();
  189. emojione.ascii = true;
  190. emojione.imagePathPNG = '{{ _p.web_lib }}javascript/emojione/png/';
  191. emojione.imagePathSVG = '{{ _p.web_lib }}javascript/emojione/svg/';
  192. emojione.imagePathSVGSprites = '{{ _p.web_lib }}javascript/emojione/sprites/';
  193. var emojiStrategy = {{ emoji_strategy|json_encode }};
  194. $.emojiarea.path = '{{ _p.web_lib }}javascript/emojione/png/';
  195. $.emojiarea.icons = {{ icons|json_encode }};
  196. $('body').on('click', '#chat-reset', ChChat.onResetListener);
  197. $('#preview').on('click', ChChat.onPreviewListener);
  198. $('#emojis').on('click', function () {
  199. $('[data-toggle="tab"][href="#tab1"]').show().tab('show');
  200. });
  201. $('textarea#chat-writer').emojiarea({
  202. button: '#emojis'
  203. });
  204. $('body').delay(1500).find('.emoji-wysiwyg-editor').textcomplete([{
  205. match: /\B:([\-+\w]*)$/,
  206. search: function (term, callback) {
  207. var results = [];
  208. var results2 = [];
  209. var results3 = [];
  210. $.each(emojiStrategy, function (shortname, data) {
  211. if (shortname.indexOf(term) > -1) {
  212. results.push(shortname);
  213. } else {
  214. if ((data.aliases !== null) && (data.aliases.indexOf(term) > -1)) {
  215. results2.push(shortname);
  216. } else if ((data.keywords !== null) && (data.keywords.indexOf(term) > -1)) {
  217. results3.push(shortname);
  218. }
  219. }
  220. });
  221. if (term.length >= 3) {
  222. results.sort(function (a, b) {
  223. return (a.length > b.length);
  224. });
  225. results2.sort(function (a, b) {
  226. return (a.length > b.length);
  227. });
  228. results3.sort();
  229. }
  230. var newResults = results.concat(results2).concat(results3);
  231. callback(newResults);
  232. },
  233. template: function (shortname) {
  234. return '<img class="emojione" src="{{ _p.web_lib }}javascript/emojione/png/'
  235. + emojiStrategy[shortname].unicode
  236. + '.png"> :' + shortname + ':';
  237. },
  238. replace: function (shortname) {
  239. return ':' + shortname + ': ';
  240. },
  241. index: 1,
  242. maxCount: 10
  243. }], {});
  244. $('button#chat-send-message').on('click', ChChat.onSendMessageListener);
  245. $('#chat-users').on('click', 'div.chat-user', function (e) {
  246. e.preventDefault();
  247. var jSelf = $(this),
  248. userId = parseInt(jSelf.data('user')) || 0;
  249. if (!userId) {
  250. return;
  251. }
  252. var exists = false;
  253. $('#chat-tabs ul.nav li').each(function (i, el) {
  254. if ($(el).data('user') == userId) {
  255. exists = true;
  256. }
  257. });
  258. $('button#chat-send-message').prop('disabled', false);
  259. if (exists) {
  260. $('#chat-tab-' + userId).tab('show');
  261. return;
  262. }
  263. $('#chat-tabs ul.nav-tabs').append('\
  264. <li role="presentation" data-user="' + userId + '">\
  265. <a id="chat-tab-' + userId + '" href="#chat-' + userId + '" aria-controls="chat-' + userId + '" role="tab" data-toggle="tab">' + jSelf.data('name') + '</a>\
  266. </li>\
  267. ');
  268. $('#chat-tabs .tab-content').append('\
  269. <div role="tabpanel" class="tab-pane" id="chat-' + userId + '">\
  270. <div class="course-chat chat-history" id="chat-history-' + userId + '"></div>\
  271. </div>\
  272. ');
  273. $('#chat-tab-' + userId).tab('show');
  274. });
  275. $('#chat-tabs ul.nav-tabs').on('shown.bs.tab', 'li a', function (e) {
  276. var jSelf = $(this);
  277. var userId = parseInt(jSelf.parent().data('user')) || 0;
  278. if (!userId) {
  279. ChChat.currentFriend = 0;
  280. return;
  281. }
  282. ChChat.currentFriend = userId;
  283. $(this).tab('show');
  284. });
  285. $('.emoji-wysiwyg-editor').on('keyup', function (e) {
  286. if (e.ctrlKey && e.keyCode === 13) {
  287. $('button#chat-send-message').trigger('click');
  288. }
  289. });
  290. ChChat.init();
  291. });
  292. </script>