chat.tpl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. <li role="presentation" class="active">
  10. <a href="#all" aria-controls="all" role="tab" data-toggle="tab">{{ 'All'|get_lang }}</a>
  11. </li>
  12. </ul>
  13. <div class="tab-content">
  14. <div role="tabpanel" class="tab-pane active" id="all">
  15. <div class="course-chat chat-history" id="chat-history"></div>
  16. </div>
  17. </div>
  18. </div>
  19. <div class="profile row">
  20. <div class="col-xs-12">
  21. <div class="message-form-chat">
  22. <div class="tabbable">
  23. <ul class="nav nav-tabs">
  24. <li class="active">
  25. <a href="#tab1" data-toggle="tab">{{ 'Write'|get_lang }}</a>
  26. </li>
  27. <li>
  28. <a href="#tab2" id="preview" data-toggle="tab">{{ 'Preview'|get_lang }}</a>
  29. </li>
  30. <li>
  31. <button id="emojis" class="btn btn-link" type="button">
  32. <span class="sr-only">{{ 'Emoji'|get_lang }}</span>{{ emoji_smile }}
  33. </button>
  34. </li>
  35. </ul>
  36. <div class="tab-content">
  37. <div class="tab-pane active" id="tab1">
  38. <div class="row">
  39. <div class="col-sm-9">
  40. <span class="sr-only">{{ 'Message'|get_lang }}</span>
  41. <textarea id="chat-writer" name="message"></textarea>
  42. </div>
  43. <div class="col-sm-3">
  44. <button id="chat-send-message" type="button" class="btn btn-primary">{{ 'Send'|get_lang }}</button>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="tab-pane" id="tab2">
  49. <div id="html-preview" class="emoji-wysiwyg-editor-preview"></div>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. <audio id="chat-alert">
  60. <source src="{{ _p.web_main }}/chat/sound/notification.wav" type="audio/wav"></source>
  61. <source src="{{ _p.web_main }}chat/sound/notification.ogg" type="audio/ogg"></source>
  62. <source src="{{ _p.web_main }}chat/sound/notification.mp3" type="audio/mpeg"></source>
  63. </audio>
  64. <script>
  65. $(document).on('ready', function () {
  66. var ChChat = {
  67. _ajaxUrl: '{{ _p.web_ajax }}course_chat.ajax.php?{{ _p.web_cid_query }}',
  68. _historySize: -1,
  69. usersOnline: 0,
  70. currentFriend: 0,
  71. call: false,
  72. track: function () {
  73. return $
  74. .get(ChChat._ajaxUrl, {
  75. action: 'track',
  76. size: ChChat._historySize,
  77. users_online: ChChat.usersOnline,
  78. friend: ChChat.currentFriend
  79. })
  80. .done(function (response) {
  81. if (response.data.chatIsDenied) {
  82. alert("{{ 'ChatDenied'|get_lang }}");
  83. return;
  84. }
  85. if (response.data.history) {
  86. ChChat._historySize = response.data.oldFileSize;
  87. ChChat.setHistory(response.data.history);
  88. }
  89. if (response.data.userList) {
  90. ChChat.usersOnline = response.data.usersOnline;
  91. ChChat.setConnectedUsers(response.data.userList);
  92. }
  93. });
  94. },
  95. setHistory: function (messageList) {
  96. var chatHistoryContainer = ChChat.currentFriend ? ('#chat-history-' + ChChat.currentFriend) : '#chat-history';
  97. $(chatHistoryContainer)
  98. .html(messageList)
  99. .prop('scrollTop', function () {
  100. return this.scrollHeight;
  101. });
  102. $('#chat-alert').get(0).play();
  103. },
  104. setConnectedUsers: function (userList) {
  105. var html = '';
  106. userList.forEach(function (user) {
  107. var buttonStatus = user.isConnected ? 'success' : 'muted',
  108. buttonTitle = user.isConnected ? '{{ 'StartAChat'|get_lang }}' : '{{ 'LeaveAMessage'|get_lang }}';
  109. html += '<li class="col-xs-12 chat-user">' +
  110. ' <div>' +
  111. ' <img src="'+ user.image_url + '" alt="' + user.complete_name + '" class="img-circle user-image-chat"/>' +
  112. ' <ul class="list-unstyled">' +
  113. ' <li>' + user.complete_name;
  114. if (user.id != {{ _u.user_id }}) {
  115. html += ' <button type="button" class="btn btn-link btn-xs" title="' + buttonTitle + '" data-name="' + user.complete_name + '" data-user="' + user.id + '">' +
  116. ' <i class="fa fa-comments text-' + buttonStatus + '"></i><span class="sr-only">' + buttonTitle + '</span>' +
  117. ' </button>';
  118. }
  119. html += ' </li>' +
  120. ' <li><small>' + user.username + '</small></li>' +
  121. ' </ul>' +
  122. ' </div>' +
  123. '</li>';
  124. });
  125. $('#chat-users').html(html);
  126. },
  127. onPreviewListener: function () {
  128. $
  129. .post(ChChat._ajaxUrl, {
  130. action: 'preview',
  131. 'message': $('textarea#chat-writer').val()
  132. })
  133. .done(function (response) {
  134. if (!response.status) {
  135. return;
  136. }
  137. $('#html-preview').html(response.data.message);
  138. });
  139. },
  140. onSendMessageListener: function (e) {
  141. e.preventDefault();
  142. if (!$('textarea#chat-writer').val().trim().length) {
  143. return;
  144. }
  145. var self = this;
  146. self.disabled = true;
  147. $
  148. .post(ChChat._ajaxUrl, {
  149. action: 'write',
  150. message: $('textarea#chat-writer').val(),
  151. friend: ChChat.currentFriend
  152. })
  153. .done(function (response) {
  154. self.disabled = false;
  155. if (!response.status) {
  156. return;
  157. }
  158. $('textarea#chat-writer').val('');
  159. $(".emoji-wysiwyg-editor").html('');
  160. });
  161. },
  162. onResetListener: function (e) {
  163. if (!confirm("{{ 'ConfirmReset'|get_lang }}")) {
  164. e.preventDefault();
  165. return;
  166. }
  167. $
  168. .get(ChChat._ajaxUrl, {
  169. action: 'reset',
  170. friend: ChChat.currentFriend
  171. })
  172. .done(function (response) {
  173. if (!response.status) {
  174. return;
  175. }
  176. ChChat.setHistory(response.data);
  177. });
  178. },
  179. init: function () {
  180. ChChat.track().done(function () {
  181. ChChat.init();
  182. });
  183. }
  184. };
  185. hljs.initHighlightingOnLoad();
  186. emojione.ascii = true;
  187. emojione.imagePathPNG = '{{ _p.web_lib }}javascript/emojione/png/';
  188. emojione.imagePathSVG = '{{ _p.web_lib }}javascript/emojione/svg/';
  189. emojione.imagePathSVGSprites = '{{ _p.web_lib }}javascript/emojione/sprites/';
  190. var emojiStrategy = {{ emoji_strategy|json_encode }};
  191. $.emojiarea.path = '{{ _p.web_lib }}javascript/emojione/png/';
  192. $.emojiarea.icons = {{ icons|json_encode }};
  193. $('body').on('click', '#chat-reset', ChChat.onResetListener);
  194. $('#preview').on('click', ChChat.onPreviewListener);
  195. $('#emojis').on('click', function () {
  196. $('[data-toggle="tab"][href="#tab1"]')
  197. .show()
  198. .tab('show');
  199. });
  200. $('textarea#chat-writer').emojiarea({
  201. button: '#emojis'
  202. });
  203. $('body').delay(1500).find('.emoji-wysiwyg-editor').textcomplete([
  204. {
  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. ], {});
  245. $('button#chat-send-message').on('click', ChChat.onSendMessageListener);
  246. $('#chat-users').on('click', 'button.btn', function (e) {
  247. e.preventDefault();
  248. var jSelf = $(this),
  249. userId = parseInt(jSelf.data('user')) || 0;
  250. if (!userId) {
  251. return;
  252. }
  253. var exists = false;
  254. $('#chat-tabs ul.nav li').each(function (i, el) {
  255. if ($(el).data('user') == userId) {
  256. exists = true;
  257. }
  258. });
  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>