-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-chat.js
328 lines (282 loc) · 8.41 KB
/
angular-chat.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***
* File: angular-chat.js
* Author: Jade Krafsig
* Source: design1online.com, LLC
* License: GNU Public License
***/
/***
* Purpose: load bootstrap ui angular modules
* Precondition: none
* Postcondition: modules loaded
***/
angular.module('angular_chat', ['ui.bootstrap', 'directive.colorPicker']);
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
function chatCtrl($scope, $http) {
/***
* Configurable global variables
***/
$scope.messageLimit = 50;
$scope.defaultUsername = "Guest";
$scope.selectedColor = "#000000";
$scope.chatChannels = [
{
text: "Basic Channel",
value: "angular_chat",
default: true
},
{
text: "Advanced Channel",
value: "angular_chat_advanced"
},
];
$scope.emoticon_url = "http://www.freesmileys.org/smileys/smiley-basic/";
$scope.emoticons = {
':-)' : 'biggrin.gif',
':)' : 'biggrin.gif',
':D' : 'laugh.gif',
':-D' : 'laugh.gif',
':-|' : 'mellow.gif',
':|' : 'mellow.gif',
':-p' : 'tongue.gif',
':p' : 'tongue.gif',
':-(' : 'sad.gif',
':(' : 'sad.gif'
};
/***
* Static global variables, do not change
***/
$scope.loggedIn = false;
$scope.errorMsg;
$scope.realtimeStatus = 0;
$scope.toggleEmoticons = false;
/***
* Purpose: clear the message object
* Precondition: none
* Postcondition: message object has been reset
***/
$scope.clearMsg = function() {
$scope.message = {
username: $scope.defaultUsername,
email: 'n/a',
text: ''
};
}
/***
* Purpose: listen for a font color change broadcast
* Precondition: color value
* Postcondition: selected color has been set
***/
$scope.$on('colorChange', function(obj, color){
$scope.selectedColor = color;
});
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
$scope.chatLogs = function() {
PUBNUB.history( {
channel : $scope.selectedChannel,
limit : $scope.messageLimit
}, function(messages) {
// Shows All Messages
$scope.$apply(function(){
$scope.chatMessages = messages.reverse();
});
});
}
/***
* Purpose: load the existing chat logs
* Precondition: none
* Postcondition: chat logs have been loaded
***/
$scope.attemptLogin = function() {
$scope.errorMsg = "";
if (!$scope.message.username) {
$scope.errorMsg = "You must enter a username.";
return;
}
if (!$scope.realtimeStatus) {
$scope.errorMsg = "You're not connect to PubNub.";
return;
}
$scope.loggedIn = true;
}
/***
* Purpose: remove error message formatting when the message input changes
* Precondition: none
* Postcondition: error message class removed from message input
***/
$scope.$watch('message.text', function(newValue, oldValue) {
if (newValue) {
$("#inputMessage").removeClass("error");
$scope.errorMsg = "";
}
}, true);
/***
* Purpose: trying to post a message to the chat
* Precondition: loggedIn
* Postcondition: message added to chatMessages and sent to chatLog
***/
$scope.postMessage = function() {
//make sure they are logged in
if (!$scope.loggedIn) {
$scope.errorMsg = "You must login first.";
return;
}
//make sure they enter a chat message
if (!$scope.message.text) {
$scope.errorMsg = "You must enter a message.";
$("#inputMessage").addClass("error");
return;
}
//set the message date
d = new Date();
$scope.message.date = d.getDay() + "/" + d.getMonth() + "/" + d.getFullYear();
$scope.message.time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
//change font color
$scope.message.color = $scope.selectedColor;
//replace hyperlinks with clickable urls
$scope.message.text = $scope.replaceURLWithLink($scope.message.text);
//replace smileys
$scope.message.text = $scope.replaceEmoticons($scope.message.text);
//send the message to the selected channel
PUBNUB.publish({
channel : $scope.selectedChannel,
message : $scope.message
});
//reset the message input box
$scope.message.text = "";
};
/***
* Purpose: connect and access pubnub channel
* Preconditions: pubnub js file init
* Postconditions: pubnub is waiting and ready
***/
$scope.initChat = function(newChannel) {
if (newChannel)
$scope.selectedChannel = newChannel;
if (!$scope.chatChannels.length) {
$scope.errorMsg = "Missing chat channels, check config.";
return;
}
if (!$scope.selectedChannel) {
$scope.errorMsg = "You must select a channel.";
return;
}
//clear out any chat messages from a previous channel
$scope.chatMessages = Array();
//unsubscribe to the active channel
angular.forEach($scope.chatChannels, function(channel, key){
PUBNUB.unsubscribe({channel: channel.value});
});
//subscribe to the selected channel
PUBNUB.subscribe({
channel : $scope.selectedChannel,
restore : false,
callback : function(message) {
//update messages with the new message
$scope.$apply(function(){
$scope.chatMessages.unshift(message);
});
},
error : function(data) {
$scope.errorMsg = data;
},
disconnect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 0;
});
},
reconnect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 1;
});
},
connect : function() {
$scope.$apply(function(){
$scope.realtimeStatus = 2;
//load the chat logs
$scope.chatLogs();
});
}
});
}
/***
* Purpose: trying to post a message to the chat
* Precondition: loggedIn
* Postcondition: message added to chatMessages and sent to chatLog
***/
$scope.attemptLogout = function() {
$("#inputMessage").removeClass("error");
$scope.clearMsg();
$scope.loggedIn = false;
}
/***
* Purpose: set the chat channel to the default channel value
* Precondition: at least one channel defined
* Postcondition: selectedChannel set to the default
***/
$scope.defaultChannel = function() {
var chatChannel;
angular.forEach($scope.chatChannels, function(channel, key){
if (!chatChannel)
chatChannel = channel;
if (channel.default) {
chatChannel = channel;
return;
}
});
$scope.selectedChannel = chatChannel.value;
}
/***
* Purpose: add an emoticon to the message input
* Precondition: emoticon has been selected
* Postcondition: emoticon value appended to message input
***/
$scope.insertEmoticon = function(selected) {
$scope.message.text = $scope.message.text + " " + selected + " ";
}
/***
* Purpose: regex to replace emoticons with image html
* Precondition: text to replace emoticons in
* Postcondition: any emoticons found have been replaced with html images
***/
$scope.replaceEmoticons = function(text) {
patterns = [];
metachars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;
// build a regex pattern for each defined property
for (var i in $scope.emoticons) {
if ($scope.emoticons.hasOwnProperty(i)){ // escape metacharacters
patterns.push('('+i.replace(metachars, "\\$&")+')');
}
}
// build the regular expression and replace
return text.replace(new RegExp(patterns.join('|'),'g'), function (match) {
return typeof $scope.emoticons[match] != 'undefined' ?
'<img src="'+$scope.emoticon_url+$scope.emoticons[match]+'"/>' :
match;
});
}
/***
* Purpose: replace url text with a clickable link (opens in a new window)
* Precondition: text to replace urls in
* Postcondition: urls have been replaced with a clickable hyperlink
***/
$scope.replaceURLWithLink = function(text) {
var exp = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(exp,"<a href='$1' target='_blank'>$1</a>");
}
/***
* Purpose: Initialize the chatroom
* Preconditions: none
* Postconditions: none
***/
$scope.clearMsg();
$scope.defaultChannel();
$scope.initChat();
}