chat.tpl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.history) {
  82. ChChat._historySize = response.data.oldFileSize;
  83. ChChat.setHistory(response.data.history);
  84. }
  85. if (response.data.userList) {
  86. ChChat.usersOnline = response.data.usersOnline;
  87. ChChat.setConnectedUsers(response.data.userList);
  88. }
  89. });
  90. },
  91. setHistory: function (messageList) {
  92. var chatHistoryContainer = ChChat.currentFriend ? ('#chat-history-' + ChChat.currentFriend) : '#chat-history';
  93. $(chatHistoryContainer)
  94. .html(messageList)
  95. .prop('scrollTop', function () {
  96. return this.scrollHeight;
  97. });
  98. $('#chat-alert').get(0).play();
  99. },
  100. setConnectedUsers: function (userList) {
  101. var html = '';
  102. userList.forEach(function (user) {
  103. var buttonStatus = user.isConnected ? 'success' : 'muted',
  104. buttonTitle = user.isConnected ? '{{ 'StartAChat'|get_lang }}' : '{{ 'LeaveAMessage'|get_lang }}';
  105. html += '<li class="col-xs-12 chat-user">' +
  106. ' <div>' +
  107. ' <img src="'+ user.image_url + '" alt="' + user.complete_name + '" class="img-circle user-image-chat"/>' +
  108. ' <ul class="list-unstyled">' +
  109. ' <li>' + user.complete_name;
  110. if (user.id != {{ _u.user_id }}) {
  111. html += ' <button type="button" class="btn btn-link btn-xs" title="' + buttonTitle + '" data-name="' + user.complete_name + '" data-user="' + user.id + '">' +
  112. ' <i class="fa fa-comments text-' + buttonStatus + '"></i><span class="sr-only">' + buttonTitle + '</span>' +
  113. ' </button>';
  114. }
  115. html += ' </li>' +
  116. ' <li><small>' + user.username + '</small></li>' +
  117. ' </ul>' +
  118. ' </div>' +
  119. '</li>';
  120. });
  121. $('#chat-users').html(html);
  122. },
  123. onPreviewListener: function () {
  124. $
  125. .post(ChChat._ajaxUrl, {
  126. action: 'preview',
  127. 'message': $('textarea#chat-writer').val()
  128. })
  129. .done(function (response) {
  130. if (!response.status) {
  131. return;
  132. }
  133. $('#html-preview').html(response.data.message);
  134. });
  135. },
  136. onSendMessageListener: function (e) {
  137. e.preventDefault();
  138. if (!$('textarea#chat-writer').val().trim().length) {
  139. return;
  140. }
  141. var self = this;
  142. self.disabled = true;
  143. $
  144. .post(ChChat._ajaxUrl, {
  145. action: 'write',
  146. message: $('textarea#chat-writer').val(),
  147. friend: ChChat.currentFriend
  148. })
  149. .done(function (response) {
  150. self.disabled = false;
  151. if (!response.status) {
  152. return;
  153. }
  154. $('textarea#chat-writer').val('');
  155. $(".emoji-wysiwyg-editor").html('');
  156. });
  157. },
  158. onResetListener: function (e) {
  159. if (!confirm("{{ 'ConfirmReset'|get_lang }}")) {
  160. e.preventDefault();
  161. return;
  162. }
  163. $
  164. .get(ChChat._ajaxUrl, {
  165. action: 'reset',
  166. friend: ChChat.currentFriend
  167. })
  168. .done(function (response) {
  169. if (!response.status) {
  170. return;
  171. }
  172. ChChat.setHistory(response.data);
  173. });
  174. },
  175. init: function () {
  176. ChChat.track().done(function () {
  177. ChChat.init();
  178. });
  179. }
  180. };
  181. hljs.initHighlightingOnLoad();
  182. emojione.ascii = true;
  183. emojione.imagePathPNG = '{{ _p.web_lib }}javascript/emojione/png/';
  184. emojione.imagePathSVG = '{{ _p.web_lib }}javascript/emojione/svg/';
  185. emojione.imagePathSVGSprites = '{{ _p.web_lib }}javascript/emojione/sprites/';
  186. var emojiStrategy = {{ emoji_strategy|json_encode }};
  187. $.emojiarea.path = '{{ _p.web_lib }}javascript/emojione/png/';
  188. $.emojiarea.icons = {{ icons|json_encode }};
  189. $('body').on('click', '#chat-reset', ChChat.onResetListener);
  190. $('#preview').on('click', ChChat.onPreviewListener);
  191. $('#emojis').on('click', function () {
  192. $('[data-toggle="tab"][href="#tab1"]')
  193. .show()
  194. .tab('show');
  195. });
  196. $('textarea#chat-writer').emojiarea({
  197. button: '#emojis'
  198. });
  199. $('body').delay(1500).find('.emoji-wysiwyg-editor').textcomplete([
  200. {
  201. match: /\B:([\-+\w]*)$/,
  202. search: function (term, callback) {
  203. var results = [];
  204. var results2 = [];
  205. var results3 = [];
  206. $.each(emojiStrategy, function (shortname, data) {
  207. if (shortname.indexOf(term) > -1) {
  208. results.push(shortname);
  209. } else {
  210. if ((data.aliases !== null) && (data.aliases.indexOf(term) > -1)) {
  211. results2.push(shortname);
  212. } else if ((data.keywords !== null) && (data.keywords.indexOf(term) > -1)) {
  213. results3.push(shortname);
  214. }
  215. }
  216. });
  217. if (term.length >= 3) {
  218. results.sort(function (a, b) {
  219. return (a.length > b.length);
  220. });
  221. results2.sort(function (a, b) {
  222. return (a.length > b.length);
  223. });
  224. results3.sort();
  225. }
  226. var newResults = results.concat(results2).concat(results3);
  227. callback(newResults);
  228. },
  229. template: function (shortname) {
  230. return '<img class="emojione" src="{{ _p.web_lib }}javascript/emojione/png/'
  231. + emojiStrategy[shortname].unicode
  232. + '.png"> :' + shortname + ':';
  233. },
  234. replace: function (shortname) {
  235. return ':' + shortname + ': ';
  236. },
  237. index: 1,
  238. maxCount: 10
  239. }
  240. ], {});
  241. $('button#chat-send-message').on('click', ChChat.onSendMessageListener);
  242. $('#chat-users').on('click', 'button.btn', function (e) {
  243. e.preventDefault();
  244. var jSelf = $(this),
  245. userId = parseInt(jSelf.data('user')) || 0;
  246. if (!userId) {
  247. return;
  248. }
  249. var exists = false;
  250. $('#chat-tabs ul.nav li').each(function (i, el) {
  251. if ($(el).data('user') == userId) {
  252. exists = true;
  253. }
  254. });
  255. if (exists) {
  256. $('#chat-tab-' + userId).tab('show');
  257. return;
  258. }
  259. $('#chat-tabs ul.nav-tabs').append('\
  260. <li role="presentation" data-user="' + userId + '">\
  261. <a id="chat-tab-' + userId + '" href="#chat-' + userId + '" aria-controls="chat-' + userId + '" role="tab" data-toggle="tab">' + jSelf.data('name') + '</a>\
  262. </li>\
  263. ');
  264. $('#chat-tabs .tab-content').append('\
  265. <div role="tabpanel" class="tab-pane" id="chat-' + userId + '">\
  266. <div class="course-chat chat-history" id="chat-history-' + userId + '"></div>\
  267. </div>\
  268. ');
  269. $('#chat-tab-' + userId).tab('show');
  270. });
  271. $('#chat-tabs ul.nav-tabs').on('shown.bs.tab', 'li a', function (e) {
  272. var jSelf = $(this);
  273. var userId = parseInt(jSelf.parent().data('user')) || 0;
  274. if (!userId) {
  275. ChChat.currentFriend = 0;
  276. return;
  277. }
  278. ChChat.currentFriend = userId;
  279. $(this).tab('show');
  280. });
  281. $('.emoji-wysiwyg-editor').on('keyup', function (e) {
  282. if (e.ctrlKey && e.keyCode === 13) {
  283. $('button#chat-send-message').trigger('click');
  284. }
  285. });
  286. ChChat.init();
  287. });
  288. </script>