js/supportchat.js

Mon, 22 Mar 2010 11:41:58 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 22 Mar 2010 11:41:58 +0000
changeset 33
60f794938bf7
parent 28
85fb6653b6e0
child 34
510ca613996a
permissions
-rw-r--r--

Make support-chat a dialog

/* Config */

var support_config = {
	login_domain: "anon.localhost",
	bosh_url: "/http-bind",
	muc_server: "support.localhost",
	team_rooms: {
		"Sales": "sales@support.localhost",
		"Technical": "technical@support.localhost"
	}
};

/*** XMPP handling */
var conn = null;

/* Called by Strophe when status of connection changes
   (from disconnected to connected, vice-versa, etc.)
*/
function handle_connection_status(status, err)
{
	if(err)
		alert(err); //FIXME: Handle gracefully
}

/* Initiate the connection to the XMPP server */
function start_connection()
{
	conn = new Strophe.Connection(support_config.bosh_url);
	var ret = true;
	try
	{
		conn.connect(support_config.login_domain, null, handle_connection_status, 50);
	}
	catch(e)
	{
		ret = false;
	}
	return ret;
}


/*** UI handling */

var ui_state = "question";
function set_ui_state(new_state)
{
	if(ui_state != new_state)
	{
		$("#support-"+ui_state).hide();
		$("#support-"+(ui_state=new_state)).show();
	}
}

/* Handle the user submitting the question form */
function on_question_submit()
{
	var question_type = $("#support-question-type").val();
	var question_name = $("#support-question-name").val();
	var question_text = $("#support-question-text").val();
	
	var our_nick = question_name;
	
	set_ui_state("wait");
	
	// Create our question room
	var question_muc = new MUC(conn, {
		joined: function (stanza, muc, nick)
		{
			if(nick == our_nick)
				team_muc.join(support_config.team_rooms[question_type], our_nick);
			else if(ui_state == "wait")
			{
				set_ui_state("converse");
			}
		},
		
		message: function (stanza, muc, nick, message)
		{
			var html = "<span class='muc-message'><span class='muc-nick'>" + htmlescape(nick) + "</span>" + ": " + htmlescape(message) + "</span><br/>\n";
			$("#support-log").append(html).scrollTop($("#support-log")[0].scrollHeight);
		}
	});

	// Get a unique room name from the server and then join the question MUC
	conn.sendIQ($iq({to: support_config.muc_server, type: "get"})
		.c("query", { xmlns: "http://jabber.org/protocol/muc#unique" }),
		function (result) // Success
		{
			var unique = Strophe.getText(result.getElementsByTagName("unique")[0]);
			question_muc.join(unique + "@" + support_config.muc_server, our_nick);
		},
		function (result) // Failure
		{
			//FIXME
		});
	
	// Create the team MUC object (it will be joined after we join the question MUC)
	var team_muc = new MUC(conn, {
		joined: function (stanza, muc, nick)
		{
			if(nick != our_nick)
				return;
			
			for(var nick in team_muc.occupants)
				team_muc.send_private_message(nick, question_text + "\n" + question_muc.jid);
		},
		
		error: function (stanza, muc, error)
		{
			if(error == "conflict")
			{
				our_nick += "_";
				muc.join(support_config.team_rooms[question_type], our_nick);
			}
			else
				alert("unhandled error: " + error);
		}
	});
}

function display_ui()
{
	// Display pop-up, showing question form
	if(start_connection())
	{
		$("#support-question-submit").click(on_question_submit);
		$("#support-chat").dialog({
			title:"Live Support",
			height: 400,
			width: 285
		});
	}
}

/*** Helper functions */
function htmlescape(s)
{
	return s.replace(/&/g,'&amp;').
		replace(/>/g,'&gt;').
		replace(/</g,'&lt;').
		replace(/"/g,'&quot;');
}

$(display_ui); //FIXME (debugging)

mercurial