support-chat/js/supportchat.js

1
/* Config */
2
 
3
var support_config = {
4
	login_domain: "anon.localhost",
5
	bosh_url: "/http-bind",
6
	muc_server: "support.localhost",
7
	team_rooms: {
8
		"Sales": "sales@support.localhost",
9
		"Technical": "technical@support.localhost"
10
	},
11
	send_invites: false,
12
	offline_support: "support@localhost",
13
	alternative_url: "http://www.google.co.uk/"
14
};
15
 
16
/*** XMPP handling */
17
var conn = null;
18
 
19
/*** Query information */
20
var question_type; // E.g. "Sales", "Technical"
21
var question_name; // Name of the submitter
22
var question_text; // The query itself
23
 
24
/*** XMPP rooms */
25
var team_muc; // MUC of the assistant team
26
var question_muc; // Temporary room for query discussion
27
 
28
/* Called by Strophe when status of connection changes
29
   (from disconnected to connected, vice-versa, etc.)
30
*/
31
function handle_connection_status(status, err)
32
{
33
	if(err)
34
		set_ui_state("error");
35
}
36
 
37
/* Initiate the connection to the XMPP server */
38
function start_connection()
39
{
40
	conn = new Strophe.Connection(support_config.bosh_url);
41
	var ret = true;
42
	try
43
	{
44
		conn.connect(support_config.login_domain, null, handle_connection_status, 50);
45
	}
46
	catch(e)
47
	{
48
		ret = false;
49
	}
50
	return ret;
51
}
52
 
53
/*** UI handling */
54
 
55
/* Initial UI state */
56
var ui_state = "question";
57
 
58
/* Called to change the UI state (question, wait, converse) */
59
function set_ui_state(new_state)
60
{
61
	if(ui_state != new_state)
62
	{
63
		$("#support-"+ui_state).hide();
64
		$("#support-"+(ui_state=new_state)).show();
65
	}
66
}
67
 
68
var current_assistant = null;
69
function set_assistant(new_assistant)
70
{
71
	if(new_assistant == null)
72
	{
73
		$("#support-assistant-name").text("No assistant present.");
74
		question_muc.send_message("{System} There is now no active assistant");
75
		team_muc.send_message("Query has no active assistant");
76
		show_message(null, current_assistant + " has left, please stand by.");
77
	}
78
	else
79
	{
80
	$("#support-assistant-name").html("<u>Assistant:</u> "+htmlescape(new_assistant));
81
		question_muc.send_message("{System} "+new_assistant+": You are now the selected assistant");
82
		team_muc.send_message("Query being handled by "+new_assistant);
83
		show_message(null, "You are now being assisted by " + new_assistant)
84
	}
85
	current_assistant = new_assistant;
86
}
87
 
88
function show_message(nick, message)
89
{
90
	var html = "<span class='muc-"+(nick?"message":"system")+"'>";
91
	if(nick)
92
		html += "<span class='muc-nick'>" + htmlescape(nick) + "</span>" + ": ";
93
	html += htmlescape(message) + "</span><br/>\n";
94
	$("#support-log").append(html).scrollTop($("#support-log")[0].scrollHeight);
95
}
96
 
97
/* Handle the user submitting the question form */
98
function on_question_submit()
99
{
100
	question_type = $("#support-question-type").val();
101
	question_name = $("#support-question-name").val();
102
	question_text = $("#support-question-text").val();
103
	
104
	set_ui_state("wait");
105
	
106
	// Create our question room
107
	question_muc = new MUC(conn, {
108
		// Handle room joins
109
		joined: function (stanza, muc, occupant)
110
		{
111
			if(occupant.nick == question_muc.nick)
112
			{
113
				// We joined the question room, now join the team room and tell them
114
				team_muc.join(support_config.team_rooms[question_type], question_muc.nick);
115
				// Also post our question to the question room for reference
116
				question_muc.send_message(question_text);
117
			}
118
			else if(current_assistant == null)
119
			{
120
				// We were waiting for an assistant, and one just joined
121
				set_assistant(occupant.nick);
122
				if(ui_state == "wait")
123
				{
124
					$("#support-send-button").click(function ()
125
					{
126
						question_muc.send_message($("#support-input").val());
127
						$("#support-input").val("");
128
					});
129
					set_ui_state("converse");
130
				}
131
			}
132
		},
133
		
134
		// Someone left the question MUC
135
		left: function (stanza, muc, occupant)
136
		{
137
			if(occupant.nick == current_assistant)
138
				set_assistant(null);
139
		},
140
		
141
		// Handle incoming messages
142
		message: function (stanza, muc, nick, message)
143
		{
144
			if(nick != question_muc.nick && message.charAt(0) == "!") // Command
145
			{
146
				if(message.indexOf("!assist") == 0)
147
				{
148
					set_assistant(nick);
149
				}
150
				else if(message.indexOf("!stop") == 0)
151
				{
152
					set_assistant(null);
153
				}
154
			}
155
			else if(nick != question_muc.nick || message.indexOf("{System} ") != 0) // Normal message
156
			{
157
				show_message(nick, message);
158
			}
159
		}
160
	});
161
 
162
	// Get a unique room name from the server and then join the question MUC
163
	conn.sendIQ($iq({to: support_config.muc_server, type: "get"})
164
		.c("query", { xmlns: "http://jabber.org/protocol/muc#unique" }),
165
		function (result) // Success
166
		{
167
			var unique = Strophe.getText(result.getElementsByTagName("unique")[0]);
168
			question_muc.join(unique + "@" + support_config.muc_server, question_name);
169
		},
170
		function (result) // Failure to get unique room name
171
		{
172
			var unique = "support-"+Math.floor(Math.random()*512);
173
			question_muc.join(unique + "@" + support_config.muc_server, question_name);
174
		});
175
	
176
	// Create the team MUC object (it will be joined after we join the question MUC)
177
	team_muc = new MUC(conn, {
178
		// Someone joined the team MUC
179
		joined: function (stanza, muc, occupant)
180
		{
181
			if(occupant.nick != team_muc.nick)
182
				return;
183
			
184
			var sent = false;
185
			for(var nick in team_muc.occupants)
186
			{
187
				if(team_muc.occupants[nick].affiliation == "none" || nick == team_muc.nick)
188
					continue;
189
				sent = true;
190
				if(support_config.send_invites)
191
					team_muc.send_invite(team_muc.jid+"/"+nick, question_text + "\n" + question_muc.jid);
192
				else
193
					team_muc.send_private_message(nick, question_text + "\n" + question_muc.jid);
194
			}
195
			if(!sent)
196
			{
197
				set_ui_state("offline");
198
			}
199
		},
200
		
201
		error: function (stanza, muc, error)
202
		{
203
			if(error == "conflict")
204
			{
205
				muc.join(support_config.team_rooms[question_type], team_muc.nick+"_");
206
			}
207
			else
208
				set_ui_state("error");
209
		}
210
	});
211
}
212
 
213
function build_ui()
214
{
215
	return $(" \
216
	<div id='support-chat'> \
217
		<div id='support-question'> \
218
			<h2>What is the nature of your question?</h2> \
219
			<select id='support-question-type'> \
220
				<option>Sales</option> \
221
				<option>Technical</option> \
222
			</select> \
223
			<h2>What is your name?</h2> \
224
			<input id='support-question-name' type='text' /> \
225
			<h2>Your question:</h2> \
226
			<textarea id='support-question-text'></textarea><br/> \
227
			<input id='support-question-submit' type='submit' /> \
228
		</div> \
229
		<div id='support-wait'> \
230
			Please wait while we find someone to \
231
			answer your query... \
232
			<br/><br/><br/><br/> \
233
			<center><img src='waiting.gif' alt='Waiting' /></center> \
234
		</div> \
235
		<div id='support-converse'> \
236
			<div id='support-assistant-name'></div> \
237
			<div id='support-log'></div> \
238
			<div id='support-input-container'><textarea id='support-input' type='text' value=''></textarea></div> \
239
			<input id='support-send-button' type='submit' value='Send' /> \
240
			<div style='clear:right;'></div> \
241
		</div> \
242
		<div id='support-offline'> \
243
			<p>Sorry, there are no assistants available \
244
			   to answer your question at the moment. \
245
			</p> \
246
			<div id='support-offline-form'> \
247
				<p>To receive a reply to your question via \
248
				   email, please enter your email address \
249
				   below: \
250
				</p> \
251
				<input id='support-offline-email' type='text' /> \
252
				<input id='support-offline-submit-button' type='submit' value='Submit' /> \
253
			</div> \
254
			<div id='support-offline-thanks'> \
255
				<p>Thank you. Your question has been submitted \
256
				   and will be replied to as soon as an assistant \
257
				   becomes available.</p> \
258
			</div> \
259
		</div> \
260
		<div id='support-error'> \
261
			<p>Sorry, there is a problem with the live support \
262
			   service at the moment. Please see our \
263
			   <a href='"+support_config.alternative_url+"'>alternative \
264
			   support channels</a> to receive assistance. \
265
			</p> \
266
		</div> \
267
	</div> \
268
	");
269
}
270
 
271
function display_ui()
272
{
273
	// Display pop-up, showing question form
274
	var ui = build_ui();
275
	
276
	ui.appendTo("body");
277
	
278
	$("#support-question-submit").click(on_question_submit);
279
	$("#support-input").keypress(function (event) {
280
		if(event.keyCode == 13)
281
		{
282
			event.preventDefault();
283
			$("#support-send-button").click();
284
		}
285
	});
286
	$("#support-offline-submit-button").click(function ()
287
	{
288
		$("#support-offline-form").hide();
289
		conn.send($msg({to: support_config.offline_support, type: "normal"})
290
			.c("subject").t("Support query from " + question_name).up()
291
			.c("body").t(question_text + "\n\nReply via email to: "+
292
				$("#support-offline-email").val()));
293
		$("#support-offline-thanks").show();
294
	});
295
 
296
	ui.dialog({
297
		title:"Live Support",
298
		height: 400,
299
		width: 285
300
	});
301
 
302
	if(!start_connection())
303
	{
304
		set_ui_state("error");
305
	}
306
}
307
 
308
/*** Helper functions */
309
function htmlescape(s)
310
{
311
	return s.replace(/&/g,'&amp;').
312
		replace(/>/g,'&gt;').
313
		replace(/</g,'&lt;').
314
		replace(/"/g,'&quot;');
315
}
316
 
317
function activate_links()
318
{
319
	$("[href='#support-chat']").click(display_ui);
320
}
321
 
322
$(activate_links);