js/supportchat.js

Thu, 18 Mar 2010 18:02:41 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Mar 2010 18:02:41 +0000
changeset 21
b691536ed125
parent 16
ebc14a22a0c1
child 22
54574593fa89
permissions
-rw-r--r--

Support for joining the question and team MUCs, and notifying team members of the question

12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
1 /* Config */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
2
16
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
3 var support_config = {
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
4 login_domain: "anon.localhost",
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
5 bosh_url: "/http-bind",
21
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
6 muc_server: "support.localhost",
16
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
7 team_rooms: {
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
8 "Sales": "sales@support.localhost",
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
9 "Technical": "technical@support.localhost"
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
10 }
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
11 };
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
13 /*** XMPP handling */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
14 var conn = null;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
15
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
16 /* Called by Strophe when status of connection changes
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
17 (from disconnected to connected, vice-versa, etc.)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
18 */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
19 function handle_connection_status(status, err)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
20 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
21 if(status != Strophe.Status.CONNECTING && status != Strophe.Status.AUTHENTICATING)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
22 set_busy_status(false);
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
23 else if(status != Strophe.Status.CONNECTED)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
24 set_busy_status(true);
15
071c513502d1 Whitespace fix
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
25
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
26 if(err)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
27 alert(err); //FIXME: Handle gracefully
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
28 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
29
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
30 /* Initiate the connection to the XMPP server */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
31 function start_connection()
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
32 {
16
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
33 conn = new Strophe.Connection(support_config.bosh_url);
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
34 var ret = true;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
35 try
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
36 {
16
ebc14a22a0c1 Switch to using a table of config options (will allow for easier configuration later)
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
37 conn.connect(support_config.login_domain, null, handle_connection_status, 50);
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
38 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
39 catch(e)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
40 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
41 ret = false;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
42 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
43 return ret;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
44 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
45
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
46
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
47 /*** UI handling */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
48
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
49 /* Handle the user submitting the question form */
7
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
50 function on_question_submit()
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
51 {
9
6677316d8834 Example question submit handler until BOSH backend integrated
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
52 var question_type = $("#support-question-type").val();
13
7ae5a537d7f1 Fix variable name
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
53 var question_name = $("#support-question-name").val();
9
6677316d8834 Example question submit handler until BOSH backend integrated
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
54 var question_text = $("#support-question-text").val();
6677316d8834 Example question submit handler until BOSH backend integrated
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
55
21
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
56 var our_nick = question_name;
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
57
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
58 // Create our question room
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
59 var question_muc = new MUC(conn, {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
60 joined: function (stanza, muc, nick)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
61 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
62 if(nick == our_nick)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
63 team_muc.join(support_config.team_rooms[question_type], our_nick);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
64 }
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
65 });
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
66
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
67 // Get a unique room name from the server and then join the question MUC
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
68 conn.sendIQ($iq({to: support_config.muc_server, type: "get"})
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
69 .c("query", { xmlns: "http://jabber.org/protocol/muc#unique" }),
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
70 function (result) // Success
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
71 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
72 var unique = Strophe.getText(result.getElementsByTagName("unique")[0]);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
73 question_muc.join(unique + "@" + support_config.muc_server, our_nick);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
74 },
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
75 function (result) // Failure
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
76 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
77 //FIXME
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
78 });
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
79
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
80 // Create the team MUC object (it will be joined after we join the question MUC)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
81 var team_muc = new MUC(conn, {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
82 joined: function (stanza, muc, nick)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
83 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
84 if(nick != our_nick)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
85 return;
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
86
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
87 for(var nick in team_muc.occupants)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
88 team_muc.send_private_message(nick, question_text + "\n" + question_muc.jid);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
89 },
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
90
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
91 error: function (stanza, muc, error)
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
92 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
93 if(error == "conflict")
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
94 {
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
95 our_nick += "_";
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
96 muc.join(support_config.team_rooms[question_type], our_nick);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
97 }
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
98 else
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
99 alert("unhandled error: " + error);
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
100 }
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
101 });
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
102
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
103 // Switch to waiting screen
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
104 $("#support-question").hide();
b691536ed125 Support for joining the question and team MUCs, and notifying team members of the question
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
105 $("#support-wait").show();
7
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
106 }
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
107
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
108 /* Update the UI according to whether we are
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
109 waiting on a network operation
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
110 */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
111 function set_busy_status(status)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
112 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
113 //FIXME
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
114 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
115
3
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 function display_ui()
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 {
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 // Display pop-up, showing question form
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
119 if(start_connection())
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
120 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
121 $("#support-chat").show();
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
122 $("#support-question-submit").click(on_question_submit);
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
123 }
3
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 }
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 $(display_ui); //FIXME (debugging)

mercurial