js/supportchat.js

Thu, 18 Mar 2010 18:14:06 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Mar 2010 18:14:06 +0000
changeset 23
ee84e7c22600
parent 22
54574593fa89
child 26
662fcb6922ab
permissions
-rw-r--r--

New logic for controlling the current UI state

/* 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");
			}
		}
	});

	// 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);
		}
	});
	
	// Switch to waiting screen
	$("#support-question").hide();
	$("#support-wait").show();
}

function display_ui()
{
	// Display pop-up, showing question form
	if(start_connection())
	{
		$("#support-chat").show();
		$("#support-question-submit").click(on_question_submit);
	}
}

$(display_ui); //FIXME (debugging)

mercurial