js/supportchat.js

Thu, 18 Mar 2010 18:35:50 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 18 Mar 2010 18:35:50 +0000
changeset 28
85fb6653b6e0
parent 27
8ca116c827cf
child 33
60f794938bf7
permissions
-rw-r--r--

Display incoming messages in the UI

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(err)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
22 alert(err); //FIXME: Handle gracefully
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
23 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
24
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
25 /* Initiate the connection to the XMPP server */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
26 function start_connection()
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
27 {
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
28 conn = new Strophe.Connection(support_config.bosh_url);
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
29 var ret = true;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
30 try
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
31 {
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
32 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
33 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
34 catch(e)
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
35 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
36 ret = false;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
37 }
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
38 return ret;
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
39 }
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
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
42 /*** UI handling */
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
43
23
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
44 var ui_state = "question";
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
45 function set_ui_state(new_state)
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
46 {
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
47 if(ui_state != new_state)
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
48 {
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
49 $("#support-"+ui_state).hide();
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
50 $("#support-"+(ui_state=new_state)).show();
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
51 }
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
52 }
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
53
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
54 /* 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
55 function on_question_submit()
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
56 {
9
6677316d8834 Example question submit handler until BOSH backend integrated
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
57 var question_type = $("#support-question-type").val();
13
7ae5a537d7f1 Fix variable name
Matthew Wild <mwild1@gmail.com>
parents: 12
diff changeset
58 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
59 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
60
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
61 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
62
23
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
63 set_ui_state("wait");
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
64
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
65 // 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
66 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
67 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
68 {
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 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
70 team_muc.join(support_config.team_rooms[question_type], our_nick);
23
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
71 else if(ui_state == "wait")
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
72 {
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
73 set_ui_state("converse");
ee84e7c22600 New logic for controlling the current UI state
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
74 }
28
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
75 },
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
76
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
77 message: function (stanza, muc, nick, message)
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
78 {
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
79 var html = "<span class='muc-message'><span class='muc-nick'>" + htmlescape(nick) + "</span>" + ": " + htmlescape(message) + "</span><br/>\n";
85fb6653b6e0 Display incoming messages in the UI
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
80 $("#support-log").append(html).scrollTop($("#support-log")[0].scrollHeight);
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
81 }
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 });
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 // 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
85 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
86 .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
87 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
88 {
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 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
90 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
91 },
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 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
93 {
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 //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
95 });
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
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 // 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
98 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
99 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
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 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
102 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
103
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 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
105 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
106 },
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
107
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
108 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
109 {
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
110 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
111 {
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
112 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
113 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
114 }
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
115 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
116 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
117 }
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
118 });
7
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
119 }
53201a5347f9 Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
120
3
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 function display_ui()
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 {
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 // Display pop-up, showing question form
12
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
124 if(start_connection())
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
125 {
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
126 $("#support-chat").show();
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
127 $("#support-question-submit").click(on_question_submit);
7b0df9aad29c Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents: 9
diff changeset
128 }
3
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 }
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
27
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
131 /*** Helper functions */
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
132 function htmlescape(s)
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
133 {
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
134 return s.replace(/&/g,'&amp;').
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
135 replace(/>/g,'&gt;').
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
136 replace(/</g,'&lt;').
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
137 replace(/"/g,'&quot;');
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
138 }
8ca116c827cf Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
139
3
de7fbf06cfa5 Add skeleton supportchat.js
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 $(display_ui); //FIXME (debugging)

mercurial