Thu, 18 Mar 2010 18:35:35 +0000
Add htmlescape() helper function
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 | 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 | } |
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
|
75 | } |
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 | |
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 | // 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
|
79 | 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
|
80 | .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
|
81 | 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
|
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 | 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
|
84 | 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
|
85 | }, |
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 | 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
|
87 | { |
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 | //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
|
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 | // 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
|
92 | 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
|
93 | 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
|
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 | 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
|
96 | 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
|
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 | 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
|
99 | 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
|
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 | 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
|
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 | 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
|
105 | { |
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 | 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
|
107 | 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
|
108 | } |
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 | 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
|
110 | 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
|
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 | }); |
7
53201a5347f9
Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
113 | } |
53201a5347f9
Add dummy event handler for question submit
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
114 | |
3 | 115 | function display_ui() |
116 | { | |
117 | // Display pop-up, showing question form | |
12
7b0df9aad29c
Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
118 | if(start_connection()) |
7b0df9aad29c
Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
119 | { |
7b0df9aad29c
Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
120 | $("#support-chat").show(); |
7b0df9aad29c
Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
121 | $("#support-question-submit").click(on_question_submit); |
7b0df9aad29c
Connecting to the XMPP server
Matthew Wild <mwild1@gmail.com>
parents:
9
diff
changeset
|
122 | } |
3 | 123 | } |
124 | ||
27
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
125 | /*** Helper functions */ |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
126 | function htmlescape(s) |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
127 | { |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
128 | return s.replace(/&/g,'&'). |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
129 | replace(/>/g,'>'). |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
130 | replace(/</g,'<'). |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
131 | replace(/"/g,'"'); |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
132 | } |
8ca116c827cf
Add htmlescape() helper function
Matthew Wild <mwild1@gmail.com>
parents:
26
diff
changeset
|
133 | |
3 | 134 | $(display_ui); //FIXME (debugging) |