Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. @extends('chat.app')
  2.  
  3. @section('content')
  4. <style type="text/css">
  5.  
  6. .online_item {
  7. border-bottom: 1px solid #d6d6d6 !important;
  8. padding : 0px 10px 5px 10px !important;
  9. margin-bottom: 10px;
  10. }
  11. .online_item>img {
  12. width: 40px;
  13. height: 40px;
  14. border: 2px solid transparent;
  15. border-radius: 50%;
  16. }
  17.  
  18. .online_item>.online {
  19. border: 2px solid #00a65a;
  20. }
  21.  
  22. </style>
  23. <div class="content-wrapper">
  24.  
  25. <section class="content-header">
  26. <h1> General Group </h1>
  27. </section>
  28.  
  29. <section class="content">
  30. <div class="row">
  31.  
  32. <!-- ============ Chat Interface ================-->
  33. <div class="col-md-6 col-xs-12 col-sm-9">
  34. <div class="box box-primary direct-chat direct-chat-primary">
  35. <div class="box-header with-border">
  36. <i class="fa fa-comments-o"> </i>
  37. <h3 class="box-title">Chat</h3>
  38. <div class="box-tools pull-right">
  39. <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> </button>
  40. </div>
  41. </div>
  42.  
  43. <div class="box-body">
  44. {{-- <div class="online_item container">
  45. @foreach($users as $user)
  46. <img class="ava" id="user-{{$user->username}}" src="{{ $user->avatar }}" alt="{{ $user->name }}" >
  47. @endforeach
  48. </div> --}}
  49. <div class="direct-chat-messages">
  50. <!-- Message Here -->
  51. </div>
  52. </div>
  53.  
  54. <div class="box-footer">
  55. <div class="input-group">
  56. <input class="input-message form-control" type="text" name="message" placeholder="Type Message ...">
  57. <span class="input-group-btn">
  58. <button class="btn btn-primary btn-flat send-message">Send</button>
  59. </span>
  60. </div>
  61. </div>
  62. </div>
  63.  
  64. <!-- Other Users chat -->
  65. <script id="chat_message_template" type="text/template" >
  66. <div class="direct-chat-msg text-display col-md-9">
  67. <div class="direct-chat-info clearfix">
  68. <span class="direct-chat-name pull-left"> </span>
  69. <span class="direct-chat-timestamp pull-right"> </span>
  70. </div>
  71.  
  72. <img class="direct-chat-img" src="" alt="Message User Image">
  73.  
  74. <div class="direct-chat-text"> </div>
  75. </div>
  76. </script>
  77.  
  78. <!-- My chat -->
  79. <script id="chat_message_template_right" type="text/template" >
  80. <div class="direct-chat-msg right text-display col-md-9 col-md-offset-3">
  81. <div class="direct-chat-info clearfix">
  82. <span class="direct-chat-name pull-right"> </span>
  83. <span class="direct-chat-timestamp pull-left"> </span>
  84. </div>
  85.  
  86. <img class="direct-chat-img" src="" alt="Message User Image">
  87.  
  88. <div class="direct-chat-text"> </div>
  89. </div>
  90. </script>
  91. </div>
  92.  
  93.  
  94. <!-- ============ Group member and online user ================-->
  95. <div class="col-md-6 col-xs-12 col-sm-9">
  96. <div class="box box-primary direct-chat direct-chat-primary">
  97. <div class="box-header with-border">
  98. <i class="fa fa-comments-o"> </i>
  99. <h3 class="box-title">Member</h3>
  100. <div class="box-tools pull-right">
  101. <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i> </button>
  102. </div>
  103. </div>
  104.  
  105. <div class="box-body">
  106. <div class="online_item container">
  107. @foreach($users as $user)
  108. <img class="ava" id="user-{{$user->username}}" src="{{ $user->avatar }}" alt="{{ $user->name }}" >
  109. @endforeach
  110. </div>
  111.  
  112. </div>
  113.  
  114. <div class="box-footer">
  115.  
  116. </div>
  117. </div>
  118. </div>
  119.  
  120. <script id="user_online" type="text/template" >
  121. <div class="text-display col-md-9 col-md-offset-3">
  122.  
  123. </div>
  124. </script>
  125. </div>
  126. </section>
  127. </div>
  128.  
  129.  
  130.  
  131. <script>
  132.  
  133. var lastId;
  134.  
  135. // get chat history
  136. function getHistory() {
  137. lastId = 0;
  138. $.get('/chat/message', {after_id: lastId} ).success(function(response) {
  139. var length = response.length;
  140.  
  141. $.each( response, function( key, value ) {
  142. addMessageToUI(response[key]);
  143. if(key == length-1) {
  144. lastId = value['id_history'];
  145. }
  146. });
  147.  
  148. });
  149.  
  150. };
  151.  
  152.  
  153. // init
  154. function init() {
  155. $('.send-message').click(sendMessage);
  156. $('.input-message').keypress(checkSend);
  157. }
  158.  
  159. // Send on enter/return key
  160. function checkSend(e) {
  161. if (e.keyCode === 13) {
  162. return sendMessage();
  163. }
  164. }
  165.  
  166. // Handle the send button being clicked
  167. function sendMessage() {
  168. var messageText = $('.input-message').val();
  169. if(messageText.length < 3) {
  170. return false;
  171. }
  172.  
  173. var data = {chat_text: messageText};
  174. $.post('/chat/message', data).success(sendMessageSuccess);
  175.  
  176. return false;
  177. }
  178.  
  179. // Handle the success callback
  180. function sendMessageSuccess() {
  181. $('.input-message').val('');
  182. }
  183.  
  184. // Get message from last id
  185. function addMessage(data) {
  186. $.get('/chat/message', {after_id: lastId}).success(function(response) {
  187.  
  188. var length = response.length;
  189.  
  190. $.each( response, function( key, value ) {
  191. addMessageToUI(response[key]);
  192.  
  193. if(key == length-1) {
  194. lastId = value['id_history'];
  195.  
  196. }
  197. });
  198. });
  199.  
  200. }
  201.  
  202. // Build the UI for a new message and add to the DOM
  203. function addMessageToUI(data) {
  204. var user = {!! json_encode(auth()->user()->username) !!};
  205. var username = ''+data.name+' (@'+data.username+')'
  206.  
  207. if(user === data.username) {
  208. var el = createMessageRightEl();
  209. } else {
  210. var el = createMessageEl();
  211. }
  212. el.find('.direct-chat-text').html(data.text);
  213. el.find('.direct-chat-name').html(username);
  214. el.find('.direct-chat-img').attr('src', data.avatar);
  215. el.find('.direct-chat-timestamp').text(strftime('%H:%M:%S %P', new Date(data.timestamp)));
  216.  
  217. var messages = $('.direct-chat-messages');
  218. messages.append(el);
  219. messages.scrollTop(messages[0].scrollHeight);
  220. }
  221.  
  222. function createMessageEl() {
  223. var text = $('#chat_message_template').text();
  224. var el = $(text);
  225. return el;
  226. }
  227.  
  228. function createMessageRightEl() {
  229. var text = $('#chat_message_template_right').text();
  230. var el = $(text);
  231. return el;
  232. }
  233.  
  234. function addUserOnline(data) {
  235. $('#user-'+data.name+'').addClass('online')
  236. }
  237.  
  238. function removeUserOnline(data) {
  239. $('#user-'+data.name+'').removeClass('online')
  240. }
  241.  
  242.  
  243. $(init);
  244.  
  245.  
  246. /******************* PUSHER *********************/
  247.  
  248. var pusher = new Pusher('{{env("PUSHER_KEY")}}', {
  249. cluster: 'ap1',
  250. authEndpoint: '/chat/auth',
  251. auth: {
  252. headers: {
  253. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  254. }
  255. }
  256. });
  257.  
  258. var presenceChannel = pusher.subscribe('{{$chatChannel}}');
  259.  
  260. presenceChannel.bind('new-message', addMessage);
  261.  
  262. presenceChannel.bind('pusher:subscription_succeeded', function(members) {
  263. getHistory()
  264. members.each(function(member) {
  265. addUserOnline(member.info);
  266. });
  267. });
  268.  
  269. presenceChannel.bind('pusher:member_added', function(member) {
  270. addUserOnline(member.info);
  271. });
  272.  
  273. presenceChannel.bind('pusher:member_removed', function(member) {
  274. removeUserOnline(member.info);
  275. });
  276.  
  277.  
  278. </script>
  279.  
  280.  
  281. @endsection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement