js/supportchat.js

changeset 36
562ff07a3968
parent 35
43152bfafcc4
child 37
010783d24970
equal deleted inserted replaced
35:43152bfafcc4 36:562ff07a3968
1 /* Config */
2
3 var support_config = {
4 login_domain: "anon.localhost",
5 bosh_url: "/http-bind",
6 muc_server: "support.localhost",
7 team_rooms: {
8 "Sales": "sales@support.localhost",
9 "Technical": "technical@support.localhost"
10 }
11 };
12
13 /*** XMPP handling */
14 var conn = null;
15
16 /* Called by Strophe when status of connection changes
17 (from disconnected to connected, vice-versa, etc.)
18 */
19 function handle_connection_status(status, err)
20 {
21 if(err)
22 alert(err); //FIXME: Handle gracefully
23 }
24
25 /* Initiate the connection to the XMPP server */
26 function start_connection()
27 {
28 conn = new Strophe.Connection(support_config.bosh_url);
29 var ret = true;
30 try
31 {
32 conn.connect(support_config.login_domain, null, handle_connection_status, 50);
33 }
34 catch(e)
35 {
36 ret = false;
37 }
38 return ret;
39 }
40
41
42 /*** UI handling */
43
44 var ui_state = "question";
45 function set_ui_state(new_state)
46 {
47 if(ui_state != new_state)
48 {
49 $("#support-"+ui_state).hide();
50 $("#support-"+(ui_state=new_state)).show();
51 }
52 }
53
54 /* Handle the user submitting the question form */
55 function on_question_submit()
56 {
57 var question_type = $("#support-question-type").val();
58 var question_name = $("#support-question-name").val();
59 var question_text = $("#support-question-text").val();
60
61 var our_nick = question_name;
62
63 set_ui_state("wait");
64
65 // Create our question room
66 var question_muc = new MUC(conn, {
67 joined: function (stanza, muc, nick)
68 {
69 if(nick == our_nick)
70 team_muc.join(support_config.team_rooms[question_type], our_nick);
71 else if(ui_state == "wait")
72 {
73 var html = "<span class='muc-message'><span class='muc-nick'>" + htmlescape(nick) + "</span>" + " is answering your query</span><br/>\n";
74 $("#support-log").append(html).scrollTop($("#support-log")[0].scrollHeight);
75 $("#support-send-button").click(function ()
76 {
77 question_muc.send_message($("#support-input").val());
78 $("#support-input").val("");
79 });
80 set_ui_state("converse");
81 }
82 },
83
84 message: function (stanza, muc, nick, message)
85 {
86 var html = "<span class='muc-message'><span class='muc-nick'>" + htmlescape(nick) + "</span>" + ": " + htmlescape(message) + "</span><br/>\n";
87 $("#support-log").append(html).scrollTop($("#support-log")[0].scrollHeight);
88 }
89 });
90
91 // Get a unique room name from the server and then join the question MUC
92 conn.sendIQ($iq({to: support_config.muc_server, type: "get"})
93 .c("query", { xmlns: "http://jabber.org/protocol/muc#unique" }),
94 function (result) // Success
95 {
96 var unique = Strophe.getText(result.getElementsByTagName("unique")[0]);
97 question_muc.join(unique + "@" + support_config.muc_server, our_nick);
98 },
99 function (result) // Failure
100 {
101 //FIXME
102 });
103
104 // Create the team MUC object (it will be joined after we join the question MUC)
105 var team_muc = new MUC(conn, {
106 joined: function (stanza, muc, nick)
107 {
108 if(nick != our_nick)
109 return;
110
111 for(var nick in team_muc.occupants)
112 team_muc.send_private_message(nick, question_text + "\n" + question_muc.jid);
113 },
114
115 error: function (stanza, muc, error)
116 {
117 if(error == "conflict")
118 {
119 our_nick += "_";
120 muc.join(support_config.team_rooms[question_type], our_nick);
121 }
122 else
123 alert("unhandled error: " + error);
124 }
125 });
126 }
127
128 function display_ui()
129 {
130 // Display pop-up, showing question form
131 if(start_connection())
132 {
133 $("#support-question-submit").click(on_question_submit);
134 $("#support-chat").dialog({
135 title:"Live Support",
136 height: 400,
137 width: 285
138 });
139 }
140 }
141
142 /*** Helper functions */
143 function htmlescape(s)
144 {
145 return s.replace(/&/g,'&amp;').
146 replace(/>/g,'&gt;').
147 replace(/</g,'&lt;').
148 replace(/"/g,'&quot;');
149 }
150
151 $(display_ui); //FIXME (debugging)

mercurial