Advertisement
Guest User

example_chat.tpl.php

a guest
Jul 10th, 2012
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script src="http://<?php print $_SERVER['SERVER_ADDR'] ?>:8080/socket.io/socket.io.js"></script>
  2. <script>
  3.   (function($){
  4.     var myNick = 'me';
  5.     var newlyJoined = true;
  6.     var socket = io.connect('http://<?php print $_SERVER['SERVER_ADDR'] ?>:8080');
  7.  
  8.     socket.on('connect', function () {
  9.       $('#chat').addClass('connected');
  10.     });
  11.  
  12.     socket.on('announcement', function (msg) {
  13.       $('#lines').append($('<p>').append($('<em>').text(msg)));
  14.     });
  15.  
  16.     socket.on('nicknames', function (nicknames) {
  17.     $('#nicknames').empty().append($('<span>Online:</span>'));
  18.     for (var i in nicknames) {
  19.         var attrs = {
  20.             'class': 'chatNickname',
  21.             'id': nicknames[i],
  22.             'text': nicknames[i]
  23.         };
  24.         $('#nicknames').append($('<div>',attrs));
  25.     }
  26. });
  27.  
  28.     socket.on('user message', message);
  29.     socket.on('private message', privateMessage);
  30.  
  31.     socket.on('reconnect', function () {
  32.       $('#lines').remove();
  33.       message('System', 'Reconnected to the server');
  34.     });
  35.  
  36.     socket.on('reconnecting', function () {
  37.       message('System', 'Attempting to re-connect to the server');
  38.     });
  39.  
  40.     socket.on('error', function (e) {
  41.       message('System', e ? e : 'A unknown error occurred');
  42.     });
  43.  
  44.     socket.on('chat log', function(chatlog) {
  45.       if (newlyJoined) {
  46.     var i = 0;
  47.     for (stamp in chatlog) {
  48.       if (chatlog[stamp].type == 'user message') {
  49.         var ts   = tstamp(stamp);
  50.         var nick = chatlog[stamp].nick
  51.         var msg  = chatlog[stamp].msg
  52.         message(ts, nick, msg);
  53.         i++;
  54.       }
  55.     }
  56.     if (i > 0) {
  57.       $('#lines').append($('<hr>')).append($('<small style="text-align:center; display:block; color: #888;">').text('Chat messages posted within the past half hour appear above this line.')).append($('<hr>'));
  58.       $('#lines').get(0).scrollTop = 10000000;      
  59.     }
  60.     newlyJoined = false;
  61.       }
  62.     });
  63.                
  64.     function message (msg_time, from, msg) {
  65.       $('#lines').append($('<p>').append($('<small>').text(msg_time)).append($('<b>').text(from), linkify(msg)));
  66.     }
  67.  
  68.     function privateMessage (message) {
  69.       $('#chatLogDiv').append($('<p>').append($('<small>').text(message.time)).append($('<b>').text(message.from), linkify(message.msg)));
  70.     }
  71.    
  72.     function linkify(inputText) {
  73.       //URLs starting with http://, https://, or ftp://
  74.       var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
  75.       var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
  76.  
  77.       //URLs starting with www. (without // before it, or it'd re-link the ones done above)
  78.       var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
  79.       var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
  80.  
  81.       //Change email addresses to mailto:: links
  82.       var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
  83.       var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
  84.  
  85.       return replacedText
  86.     }
  87.  
  88.     function tstamp(stamp) {
  89.       var currentTime = new Date();
  90.       if (typeof stamp != 'undefined') {
  91.     currentTime.setTime(stamp);
  92.       }
  93.       var days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat');
  94.       var day = currentTime.getDay();
  95.       var hours = currentTime.getHours();
  96.       var minutes = currentTime.getMinutes();
  97.       if (minutes < 10) {
  98.     minutes = "0" + minutes;
  99.       }
  100.       if (hours > 11) {
  101.     var ap = 'p';
  102.       }
  103.       else {
  104.     var ap = 'a';
  105.       }
  106.       if (hours > 12) {
  107.     hours = hours - 12;
  108.       }
  109.       return "["+ days[day] + " " + hours + ":" + minutes + ap + "m] ";
  110.     }
  111.  
  112.     function startPrivateChat(targetUser, sendingUser){
  113.         if(targetUser !=sendingUser){
  114.         var privateMessage1="starting private chat with " + targetUser + ". My name is " + sendingUser;
  115.         var chatBoxId= targetUser + 'ChatBox';
  116.         $("#content").append('<div class="chatBox" id="' + chatBoxId + '">' + sendingUser + ', ' + targetUser + '</div>');
  117.         $('#' + chatBoxId).append('new private chat');
  118.         $('#' + chatBoxId).dialog();
  119.         var data= {'to':targetUser, 'message':privateMessage1, 'from':sendingUser};
  120.         privateMessage(tstamp(), myNick, privateMessage1);
  121.         socket.emit('private message', data);
  122.         clear();    
  123.         }
  124.     }
  125.      
  126.        
  127.     $(document).ready(function() {
  128.     $('#nicknames').on('click', '.chatNickname', function(){
  129.       startPrivateChat(this.id,myNick);
  130.     });
  131.    
  132.       $('input#message').focus(function() {
  133.     if ($(this).val() == 'Type your chat messages here...') {
  134.       $(this).val('');
  135.     }
  136.       });
  137.  
  138.       $('input#show-timestamps').click(function() {
  139.     if ($(this).is(':checked')) {
  140.       $('#messages p small').show();
  141.     }
  142.     else {
  143.       $('#messages p small').hide();
  144.     }
  145.       })
  146.  
  147.       socket.emit('nickname', '<?php print $username ?>', function (nick) {
  148.     if (nick != 'me') {
  149.       myNick = nick;
  150.       socket.emit('get log');          
  151.       return $('#chat').addClass('nickname-set');
  152.     }
  153.       });
  154.  
  155.       $('#send-message').submit(function () {
  156.     message(tstamp(), myNick, $('#message').val());
  157.     socket.emit('user message', $('#message').val());
  158.     clear();
  159.     $('#lines').get(0).scrollTop = 10000000;
  160.     return false;
  161.       });
  162.  
  163.       function clear () {
  164.     $('#message').val('').focus();
  165.       };
  166.  
  167.     });
  168.   })(jQuery);
  169. </script>
  170. <?php
  171. drupal_add_library('system', 'ui.dialog');
  172. drupal_add_js('jQuery(document).ready(function(){jQuery("#dialog").dialog({ modal: true, autoOpen: false });});', 'inline');
  173. ?>
  174. <div id="dialog" title="Basic dialog">
  175.     <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
  176. </div>
  177. <div id="chat">
  178.   <div id="messages">
  179.     <div id="nicknames">
  180.     </div>
  181.     <div id="lines">
  182.     </div>
  183.   </div>
  184.   <form id="send-message" autocomplete="off">
  185.     <input id="message" type="text" value="Type your chat messages here..." autocomplete="off" />
  186.     <button>Send</button>
  187.   </form>
  188. </div>
  189. <small><input id="show-timestamps" type="checkbox" checked="checked" /> Show timestamps</small>
  190.  
  191. <div id='chatLogDiv'>test <br /></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement