|
@@ -0,0 +1,228 @@
|
|
1
|
+/* global $, APP, config*/
|
|
2
|
+
|
|
3
|
+var XMPP = require('../../xmpp/xmpp');
|
|
4
|
+var Moderator = require('../../xmpp/moderator');
|
|
5
|
+
|
|
6
|
+//FIXME: use LoginDialog to add retries to XMPP.connect method used when
|
|
7
|
+// anonymous domain is not enabled
|
|
8
|
+
|
|
9
|
+/**
|
|
10
|
+ * Creates new <tt>Dialog</tt> instance.
|
|
11
|
+ * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt> called
|
|
12
|
+ * when we either fail to connect or succeed(check Strophe.Status).
|
|
13
|
+ * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to Jicofo
|
|
14
|
+ * in order to create session-id after the connection is established.
|
|
15
|
+ * @constructor
|
|
16
|
+ */
|
|
17
|
+function Dialog(callback, obtainSession) {
|
|
18
|
+
|
|
19
|
+ var self = this;
|
|
20
|
+
|
|
21
|
+ var stop = false;
|
|
22
|
+
|
|
23
|
+ var connection = APP.xmpp.createConnection();
|
|
24
|
+
|
|
25
|
+ var message = '<h2 data-i18n="dialog.passwordRequired">';
|
|
26
|
+ message += APP.translation.translateString("dialog.passwordRequired");
|
|
27
|
+ message += '</h2>' +
|
|
28
|
+ '<input name="username" type="text" ' +
|
|
29
|
+ 'placeholder="user@domain.net" autofocus>' +
|
|
30
|
+ '<input name="password" ' +
|
|
31
|
+ 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
|
|
32
|
+ ' placeholder="user password">';
|
|
33
|
+
|
|
34
|
+ var okButton = APP.translation.generateTranslatonHTML("dialog.Ok");
|
|
35
|
+
|
|
36
|
+ var cancelButton = APP.translation.generateTranslatonHTML("dialog.Cancel");
|
|
37
|
+
|
|
38
|
+ var states = {
|
|
39
|
+ login: {
|
|
40
|
+ html: message,
|
|
41
|
+ buttons: [
|
|
42
|
+ { title: okButton, value: true},
|
|
43
|
+ { title: cancelButton, value: false}
|
|
44
|
+ ],
|
|
45
|
+ focus: ':input:first',
|
|
46
|
+ submit: function (e, v, m, f) {
|
|
47
|
+ e.preventDefault();
|
|
48
|
+ if (v) {
|
|
49
|
+ var jid = f.username;
|
|
50
|
+ var password = f.password;
|
|
51
|
+ if (jid && password) {
|
|
52
|
+ stop = false;
|
|
53
|
+ connection.reset();
|
|
54
|
+ connDialog.goToState('connecting');
|
|
55
|
+ connection.connect(jid, password, stateHandler);
|
|
56
|
+ }
|
|
57
|
+ } else {
|
|
58
|
+ // User cancelled
|
|
59
|
+ stop = true;
|
|
60
|
+ callback();
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ },
|
|
64
|
+ connecting: {
|
|
65
|
+ title: APP.translation.translateString('dialog.connecting'),
|
|
66
|
+ html: '<div id="connectionStatus"></div>',
|
|
67
|
+ buttons: [],
|
|
68
|
+ defaultButton: 0
|
|
69
|
+ },
|
|
70
|
+ finished: {
|
|
71
|
+ title: APP.translation.translateString('dialog.error'),
|
|
72
|
+ html: '<div id="errorMessage"></div>',
|
|
73
|
+ buttons: [
|
|
74
|
+ {
|
|
75
|
+ title: APP.translation.translateString('dialog.retry'),
|
|
76
|
+ value: 'retry'
|
|
77
|
+ },
|
|
78
|
+ {
|
|
79
|
+ title: APP.translation.translateString('dialog.Cancel'),
|
|
80
|
+ value: 'cancel'
|
|
81
|
+ },
|
|
82
|
+ ],
|
|
83
|
+ defaultButton: 0,
|
|
84
|
+ submit: function (e, v, m, f) {
|
|
85
|
+ e.preventDefault();
|
|
86
|
+ if (v === 'retry')
|
|
87
|
+ connDialog.goToState('login');
|
|
88
|
+ else
|
|
89
|
+ callback();
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+ };
|
|
93
|
+
|
|
94
|
+ var connDialog
|
|
95
|
+ = APP.UI.messageHandler.openDialogWithStates(states,
|
|
96
|
+ { persistent: true, closeText: '' }, null);
|
|
97
|
+
|
|
98
|
+ var stateHandler = function (status, message) {
|
|
99
|
+ if (stop) {
|
|
100
|
+ return;
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ var translateKey = "connection." + XMPP.getStatusString(status);
|
|
104
|
+ var statusStr = APP.translation.translateString(translateKey);
|
|
105
|
+
|
|
106
|
+ // Display current state
|
|
107
|
+ var connectionStatus =
|
|
108
|
+ connDialog.getState('connecting').find('#connectionStatus');
|
|
109
|
+
|
|
110
|
+ connectionStatus.text(statusStr);
|
|
111
|
+
|
|
112
|
+ switch (status) {
|
|
113
|
+ case XMPP.Status.CONNECTED:
|
|
114
|
+
|
|
115
|
+ stop = true;
|
|
116
|
+ if (!obtainSession) {
|
|
117
|
+ callback(connection, status);
|
|
118
|
+ return;
|
|
119
|
+ }
|
|
120
|
+ // Obtaining session-id status
|
|
121
|
+ connectionStatus.text(
|
|
122
|
+ APP.translation.translateString(
|
|
123
|
+ 'connection.FETCH_SESSION_ID'));
|
|
124
|
+
|
|
125
|
+ // Authenticate with Jicofo and obtain session-id
|
|
126
|
+ var roomName = APP.UI.generateRoomName();
|
|
127
|
+
|
|
128
|
+ // Jicofo will return new session-id when connected
|
|
129
|
+ // from authenticated domain
|
|
130
|
+ connection.sendIQ(
|
|
131
|
+ Moderator.createConferenceIq(roomName),
|
|
132
|
+ function (result) {
|
|
133
|
+
|
|
134
|
+ connectionStatus.text(
|
|
135
|
+ APP.translation.translateString(
|
|
136
|
+ 'connection.GOT_SESSION_ID'));
|
|
137
|
+
|
|
138
|
+ stop = true;
|
|
139
|
+
|
|
140
|
+ // Parse session-id
|
|
141
|
+ Moderator.parseSessionId(result);
|
|
142
|
+
|
|
143
|
+ callback(connection, status);
|
|
144
|
+ },
|
|
145
|
+ function (error) {
|
|
146
|
+ console.error("Auth on the fly failed", error);
|
|
147
|
+
|
|
148
|
+ stop = true;
|
|
149
|
+
|
|
150
|
+ var errorMsg =
|
|
151
|
+ APP.translation.translateString(
|
|
152
|
+ 'connection.GET_SESSION_ID_ERROR') +
|
|
153
|
+ $(error).find('>error').attr('code');
|
|
154
|
+
|
|
155
|
+ self.displayError(errorMsg);
|
|
156
|
+
|
|
157
|
+ connection.disconnect();
|
|
158
|
+ });
|
|
159
|
+
|
|
160
|
+ break;
|
|
161
|
+ case XMPP.Status.AUTHFAIL:
|
|
162
|
+ case XMPP.Status.CONNFAIL:
|
|
163
|
+ case XMPP.Status.DISCONNECTED:
|
|
164
|
+
|
|
165
|
+ stop = true;
|
|
166
|
+
|
|
167
|
+ callback(connection, status);
|
|
168
|
+
|
|
169
|
+ var errorMessage = statusStr;
|
|
170
|
+
|
|
171
|
+ if (message)
|
|
172
|
+ {
|
|
173
|
+ errorMessage += ': ' + message;
|
|
174
|
+ }
|
|
175
|
+ self.displayError(errorMessage);
|
|
176
|
+
|
|
177
|
+ break;
|
|
178
|
+ default:
|
|
179
|
+ break;
|
|
180
|
+ }
|
|
181
|
+ };
|
|
182
|
+
|
|
183
|
+ /**
|
|
184
|
+ * Displays error message in 'finished' state which allows either to cancel
|
|
185
|
+ * or retry.
|
|
186
|
+ * @param message the final message to be displayed.
|
|
187
|
+ */
|
|
188
|
+ this.displayError = function (message) {
|
|
189
|
+
|
|
190
|
+ var finishedState = connDialog.getState('finished');
|
|
191
|
+
|
|
192
|
+ var errorMessageElem = finishedState.find('#errorMessage');
|
|
193
|
+ errorMessageElem.text(message);
|
|
194
|
+
|
|
195
|
+ connDialog.goToState('finished');
|
|
196
|
+ };
|
|
197
|
+
|
|
198
|
+ /**
|
|
199
|
+ * Closes LoginDialog.
|
|
200
|
+ */
|
|
201
|
+ this.close = function () {
|
|
202
|
+ stop = true;
|
|
203
|
+ connDialog.close();
|
|
204
|
+ };
|
|
205
|
+}
|
|
206
|
+
|
|
207
|
+var LoginDialog = {
|
|
208
|
+
|
|
209
|
+ /**
|
|
210
|
+ * Displays login prompt used to establish new XMPP connection. Given
|
|
211
|
+ * <tt>callback(Strophe.Connection, Strophe.Status)</tt> function will be
|
|
212
|
+ * called when we connect successfully(status === CONNECTED) or when we fail
|
|
213
|
+ * to do so. On connection failure program can call Dialog.close() method in
|
|
214
|
+ * order to cancel or do nothing to let the user retry.
|
|
215
|
+ * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt>
|
|
216
|
+ * called when we either fail to connect or succeed(check
|
|
217
|
+ * Strophe.Status).
|
|
218
|
+ * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to
|
|
219
|
+ * Jicofo in order to create session-id after the connection is
|
|
220
|
+ * established.
|
|
221
|
+ * @returns {Dialog}
|
|
222
|
+ */
|
|
223
|
+ show: function (callback, obtainSession) {
|
|
224
|
+ return new Dialog(callback, obtainSession);
|
|
225
|
+ }
|
|
226
|
+};
|
|
227
|
+
|
|
228
|
+module.exports = LoginDialog;
|