-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.tpl.php
166 lines (141 loc) · 5.98 KB
/
chat.tpl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script src="http://<?php echo $_SERVER['SERVER_NAME']; ?>:8283/socket.io/socket.io.js"></script>
<script>
(function ($) {
var myNick = 'me';
var newlyJoined = true;
var socket = io.connect('http://<?php print $_SERVER['SERVER_NAME'] ?>:8283');
socket.on('connect', function () {
$('#chat').addClass('connected');
});
socket.on('users', function (users) {
$('#nicknames').empty().append($('<span>Online: </span>'));
for (var i in users) {
$('#nicknames').append($('<b>').text(users[i]));
}
});
socket.on('user message', message);
socket.on('reconnect', function () {
$('#lines').remove();
message('System', 'Reconnected to the server');
});
socket.on('reconnecting', function () {
message('System', 'Attempting to re-connect to the server');
});
socket.on('error', function (e) {
message('System', e ? e : 'A unknown error occurred');
});
function message(msg) {
console.log(msg);
$('#lines').append($('<p>').append($('<small>').text($('#receiver').val())).append($('<b>').text(' - '), linkify(msg['message'])));
}
function linkify(inputText) {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with www. (without // before it, or it'd re-link the ones done above)
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
//Change email addresses to mailto:: links
var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
return replacedText
}
function tstamp(stamp) {
var currentTime = new Date();
if (typeof stamp != 'undefined') {
currentTime.setTime(stamp);
}
var days = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat');
var day = currentTime.getDay();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
if (minutes < 10) {
minutes = "0" + minutes;
}
if (hours > 11) {
var ap = 'p';
}
else {
var ap = 'a';
}
if (hours > 12) {
hours = hours - 12;
}
return "[" + days[day] + " " + hours + ":" + minutes + ap + "m] ";
}
$(document).ready(function () {
$('input#message').focus(function () {
if ($(this).val() == 'Type your chat messages here...') {
$(this).val('');
}
});
$('input#show-timestamps').click(function () {
if ($(this).is(':checked')) {
$('#messages p small').show();
}
else {
$('#messages p small').hide();
}
})
socket.emit('user', <?php print json_encode(array('name' => isset($user->name) ? $user->name : NULL, 'uid' => $user->uid)) ?>, function (nick) {
if (nick != 'me') {
myNick = nick;
socket.emit('get log');
return $('#chat').addClass('nickname-set');
}
});
$('#send-message').submit(function () {
if ($('#receiver').val() == '' || $('#to').val() == '') {
alert('Please select a user to chat with him first');
return;
}
if ($('#message').val() == '') {
return;
}
msg = {'message': $('#message').val(), 'sender':<?php echo $user->uid ?>, 'receiver': $('#to').val(), 'timestamp': tstamp()};
socket.emit('user message', msg, function(res){
console.log(res);
});
clear();
$('#lines').get(0).scrollTop = 10000000;
return false;
});
function clear() {
$('#message').val('').focus();
}
socket.on('online users', function (users) {
console.log(users);
});
socket.on('user status', function (user) {
console.log(user);
});
$('.chat-with-user').click(function () {
$('#to').val($(this).attr('data-uid'));
$('#receiver').val($(this).html());
$('#chat-with').html('Chating with : ' + $(this).html());
})
});
})(jQuery);
</script>
<div id="chat" sender="">
<div id="messages">
<div id="users">
<p>online users</p>
<ul>
<?php foreach ($users as $user): ?>
<li><a class="chat-with-user" href="#" data-uid="<?php echo $user->uid ?>"><?php echo $user->name ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<div id="lines">
</div>
</div>
<form id="send-message" autocomplete="off">
<p id="chat-with"></p>
<input id="to" type="hidden" value="" autocomplete="off" />
<input id="receiver" type="hidden" value="" autocomplete="off" />
<input id="message" type="text" value="Type your chat messages here..." autocomplete="off" />
<button>Send</button>
</form>
</div>
<small><input id="show-timestamps" type="checkbox" checked="checked" /> Show timestamps</small>