Commit | Line | Data |
---|---|---|
17f187e6 D |
1 | _kiwi.view.ServerSelect = Backbone.View.extend({ |
2 | events: { | |
3 | 'submit form': 'submitForm', | |
4 | 'click .show_more': 'showMore', | |
5 | 'change .have_pass input': 'showPass', | |
6 | 'change .have_key input': 'showKey', | |
7 | 'click .icon-key': 'channelKeyIconClick', | |
8 | 'click .show_server': 'showServer' | |
9 | }, | |
10 | ||
11 | initialize: function () { | |
12 | var that = this, | |
13 | text = { | |
14 | think_nick: _kiwi.global.i18n.translate('client_views_serverselect_form_title').fetch(), | |
15 | nickname: _kiwi.global.i18n.translate('client_views_serverselect_nickname').fetch(), | |
16 | have_password: _kiwi.global.i18n.translate('client_views_serverselect_enable_password').fetch(), | |
17 | password: _kiwi.global.i18n.translate('client_views_serverselect_password').fetch(), | |
18 | channel: _kiwi.global.i18n.translate('client_views_serverselect_channel').fetch(), | |
19 | channel_key: _kiwi.global.i18n.translate('client_views_serverselect_channelkey').fetch(), | |
20 | require_key: _kiwi.global.i18n.translate('client_views_serverselect_channelkey_required').fetch(), | |
21 | key: _kiwi.global.i18n.translate('client_views_serverselect_key').fetch(), | |
22 | start: _kiwi.global.i18n.translate('client_views_serverselect_connection_start').fetch(), | |
23 | server_network: _kiwi.global.i18n.translate('client_views_serverselect_server_and_network').fetch(), | |
24 | server: _kiwi.global.i18n.translate('client_views_serverselect_server').fetch(), | |
25 | port: _kiwi.global.i18n.translate('client_views_serverselect_port').fetch(), | |
26 | powered_by: _kiwi.global.i18n.translate('client_views_serverselect_poweredby').fetch() | |
27 | }; | |
50ac472f | 28 | |
17f187e6 D |
29 | this.$el = $(_.template($('#tmpl_server_select').html().trim(), text)); |
30 | ||
31 | // Remove the 'more' link if the server has disabled server changing | |
32 | if (_kiwi.app.server_settings && _kiwi.app.server_settings.connection) { | |
33 | if (!_kiwi.app.server_settings.connection.allow_change) { | |
34 | this.$el.find('.show_more').remove(); | |
35 | this.$el.addClass('single_server'); | |
36 | } | |
37 | } | |
16952e58 | 38 | |
17f187e6 D |
39 | // Are currently showing all the controlls or just a nick_change box? |
40 | this.state = 'all'; | |
50ac472f | 41 | |
17f187e6 | 42 | this.more_shown = false; |
50ac472f | 43 | |
17f187e6 D |
44 | this.model.bind('new_network', this.newNetwork, this); |
45 | _kiwi.gateway.bind('onconnect', this.networkConnected, this); | |
46 | _kiwi.gateway.bind('connecting', this.networkConnecting, this); | |
47 | _kiwi.gateway.bind('onirc_error', this.onIrcError, this); | |
48 | }, | |
50ac472f | 49 | |
17f187e6 D |
50 | dispose: function() { |
51 | this.model.off('new_network', this.newNetwork, this); | |
52 | _kiwi.gateway.off('onconnect', this.networkConnected, this); | |
53 | _kiwi.gateway.off('connecting', this.networkConnecting, this); | |
54 | _kiwi.gateway.off('onirc_error', this.onIrcError, this); | |
50ac472f | 55 | |
17f187e6 D |
56 | this.remove(); |
57 | }, | |
50ac472f | 58 | |
17f187e6 D |
59 | submitForm: function (event) { |
60 | event.preventDefault(); | |
50ac472f | 61 | |
17f187e6 D |
62 | // Make sure a nick is chosen |
63 | if (!$('input.nick', this.$el).val().trim()) { | |
64 | this.setStatus(_kiwi.global.i18n.translate('client_views_serverselect_nickname_error_empty').fetch()); | |
65 | $('input.nick', this.$el).select(); | |
50ac472f | 66 | return; |
17f187e6 | 67 | } |
50ac472f | 68 | |
17f187e6 D |
69 | if (this.state === 'nick_change') { |
70 | this.submitNickChange(event); | |
71 | } else { | |
72 | this.submitLogin(event); | |
73 | } | |
50ac472f | 74 | |
17f187e6 D |
75 | $('button', this.$el).attr('disabled', 1); |
76 | return; | |
77 | }, | |
78 | ||
79 | submitLogin: function (event) { | |
80 | // If submitting is disabled, don't do anything | |
81 | if ($('button', this.$el).attr('disabled')) return; | |
82 | ||
83 | var values = { | |
84 | nick: $('input.nick', this.$el).val(), | |
85 | server: $('input.server', this.$el).val(), | |
86 | port: $('input.port', this.$el).val(), | |
87 | ssl: $('input.ssl', this.$el).prop('checked'), | |
88 | password: $('input.password', this.$el).val(), | |
89 | channel: $('input.channel', this.$el).val(), | |
90 | channel_key: $('input.channel_key', this.$el).val(), | |
91 | options: this.server_options | |
92 | }; | |
93 | ||
94 | this.trigger('server_connect', values); | |
95 | }, | |
96 | ||
97 | submitNickChange: function (event) { | |
98 | _kiwi.gateway.changeNick(null, $('input.nick', this.$el).val()); | |
99 | this.networkConnecting(); | |
100 | }, | |
101 | ||
102 | showPass: function (event) { | |
103 | if (this.$el.find('tr.have_pass input').is(':checked')) { | |
104 | this.$el.find('tr.pass').show().find('input').focus(); | |
105 | } else { | |
106 | this.$el.find('tr.pass').hide().find('input').val(''); | |
107 | } | |
108 | }, | |
50ac472f | 109 | |
17f187e6 D |
110 | channelKeyIconClick: function (event) { |
111 | this.$el.find('tr.have_key input').click(); | |
112 | }, | |
50ac472f | 113 | |
17f187e6 D |
114 | showKey: function (event) { |
115 | if (this.$el.find('tr.have_key input').is(':checked')) { | |
116 | this.$el.find('tr.key').show().find('input').focus(); | |
117 | } else { | |
118 | this.$el.find('tr.key').hide().find('input').val(''); | |
119 | } | |
120 | }, | |
121 | ||
122 | showMore: function (event) { | |
123 | if (!this.more_shown) { | |
124 | $('.more', this.$el).slideDown('fast'); | |
125 | $('.show_more', this.$el) | |
126 | .children('.icon-caret-down') | |
127 | .removeClass('icon-caret-down') | |
128 | .addClass('icon-caret-up'); | |
129 | $('input.server', this.$el).select(); | |
130 | this.more_shown = true; | |
131 | } else { | |
132 | $('.more', this.$el).slideUp('fast'); | |
133 | $('.show_more', this.$el) | |
134 | .children('.icon-caret-up') | |
135 | .removeClass('icon-caret-up') | |
136 | .addClass('icon-caret-down'); | |
137 | $('input.nick', this.$el).select(); | |
138 | this.more_shown = false; | |
139 | } | |
140 | }, | |
141 | ||
142 | populateFields: function (defaults) { | |
143 | var nick, server, port, channel, channel_key, ssl, password; | |
144 | ||
145 | defaults = defaults || {}; | |
146 | ||
147 | nick = defaults.nick || ''; | |
148 | server = defaults.server || ''; | |
149 | port = defaults.port || 6667; | |
150 | ssl = defaults.ssl || 0; | |
151 | password = defaults.password || ''; | |
152 | channel = defaults.channel || ''; | |
153 | channel_key = defaults.channel_key || ''; | |
154 | ||
155 | $('input.nick', this.$el).val(nick); | |
156 | $('input.server', this.$el).val(server); | |
157 | $('input.port', this.$el).val(port); | |
158 | $('input.ssl', this.$el).prop('checked', ssl); | |
159 | $('input#server_select_show_pass', this.$el).prop('checked', !(!password)); | |
160 | $('input.password', this.$el).val(password); | |
161 | if (!(!password)) { | |
162 | $('tr.pass', this.$el).show(); | |
163 | } | |
164 | $('input.channel', this.$el).val(channel); | |
165 | $('input#server_select_show_channel_key', this.$el).prop('checked', !(!channel_key)); | |
166 | $('input.channel_key', this.$el).val(channel_key); | |
167 | if (!(!channel_key)) { | |
168 | $('tr.key', this.$el).show(); | |
169 | } | |
50ac472f | 170 | |
17f187e6 D |
171 | // Temporary values |
172 | this.server_options = {}; | |
cc54c0a1 | 173 | |
17f187e6 D |
174 | if (defaults.encoding) |
175 | this.server_options.encoding = defaults.encoding; | |
176 | }, | |
cc54c0a1 | 177 | |
17f187e6 D |
178 | hide: function () { |
179 | this.$el.slideUp(); | |
180 | }, | |
50ac472f | 181 | |
17f187e6 D |
182 | show: function (new_state) { |
183 | new_state = new_state || 'all'; | |
50ac472f | 184 | |
17f187e6 | 185 | this.$el.show(); |
50ac472f | 186 | |
17f187e6 D |
187 | if (new_state === 'all') { |
188 | $('.show_more', this.$el).show(); | |
50ac472f | 189 | |
17f187e6 D |
190 | } else if (new_state === 'more') { |
191 | $('.more', this.$el).slideDown('fast'); | |
50ac472f | 192 | |
17f187e6 D |
193 | } else if (new_state === 'nick_change') { |
194 | $('.more', this.$el).hide(); | |
195 | $('.show_more', this.$el).hide(); | |
196 | $('input.nick', this.$el).select(); | |
50ac472f | 197 | |
17f187e6 D |
198 | } else if (new_state === 'enter_password') { |
199 | $('.more', this.$el).hide(); | |
200 | $('.show_more', this.$el).hide(); | |
201 | $('input.password', this.$el).select(); | |
202 | } | |
b69461a7 | 203 | |
17f187e6 D |
204 | this.state = new_state; |
205 | }, | |
50ac472f | 206 | |
17f187e6 D |
207 | infoBoxShow: function() { |
208 | var $side_panel = this.$el.find('.side_panel'); | |
50ac472f | 209 | |
17f187e6 D |
210 | // Some theme may hide the info panel so check before we |
211 | // resize ourselves | |
212 | if (!$side_panel.is(':visible')) | |
213 | return; | |
50ac472f | 214 | |
17f187e6 D |
215 | this.$el.animate({ |
216 | width: parseInt($side_panel.css('left'), 10) + $side_panel.find('.content:first').outerWidth() | |
217 | }); | |
218 | }, | |
219 | ||
220 | infoBoxHide: function() { | |
221 | var $side_panel = this.$el.find('.side_panel'); | |
222 | this.$el.animate({ | |
223 | width: parseInt($side_panel.css('left'), 10) | |
224 | }); | |
225 | }, | |
226 | ||
227 | infoBoxSet: function($info_view) { | |
228 | this.$el.find('.side_panel .content') | |
229 | .empty() | |
230 | .append($info_view); | |
231 | }, | |
232 | ||
233 | setStatus: function (text, class_name) { | |
234 | $('.status', this.$el) | |
235 | .text(text) | |
236 | .attr('class', 'status') | |
237 | .addClass(class_name||'') | |
238 | .show(); | |
239 | }, | |
240 | clearStatus: function () { | |
241 | $('.status', this.$el).hide(); | |
242 | }, | |
243 | ||
244 | reset: function() { | |
245 | this.populateFields(); | |
246 | this.clearStatus(); | |
247 | ||
248 | this.$('button').attr('disabled', null); | |
249 | }, | |
250 | ||
251 | newNetwork: function(network) { | |
252 | // Keep a reference to this network so we can interact with it | |
253 | this.model.current_connecting_network = network; | |
254 | }, | |
255 | ||
256 | networkConnected: function (event) { | |
257 | this.model.trigger('connected', _kiwi.app.connections.getByConnectionId(event.server)); | |
258 | this.model.current_connecting_network = null; | |
259 | }, | |
260 | ||
261 | networkConnecting: function (event) { | |
262 | this.model.trigger('connecting'); | |
263 | this.setStatus(_kiwi.global.i18n.translate('client_views_serverselect_connection_trying').fetch(), 'ok'); | |
264 | ||
265 | this.$('.status').append('<a class="show_server"><i class="icon-info-sign"></i></a>'); | |
266 | }, | |
267 | ||
268 | showServer: function() { | |
269 | // If we don't have a current connection in the making then we have nothing to show | |
270 | if (!this.model.current_connecting_network) | |
271 | return; | |
50ac472f | 272 | |
17f187e6 D |
273 | _kiwi.app.view.barsShow(); |
274 | this.model.current_connecting_network.panels.server.view.show(); | |
275 | }, | |
276 | ||
277 | onIrcError: function (data) { | |
278 | $('button', this.$el).attr('disabled', null); | |
279 | ||
280 | switch(data.error) { | |
281 | case 'nickname_in_use': | |
282 | this.setStatus(_kiwi.global.i18n.translate('client_views_serverselect_nickname_error_alreadyinuse').fetch()); | |
283 | this.show('nick_change'); | |
284 | this.$el.find('.nick').select(); | |
285 | break; | |
286 | case 'erroneus_nickname': | |
287 | this.setStatus(_kiwi.global.i18n.translate('client_views_serverselect_nickname_invalid').fetch()); | |
288 | this.show('nick_change'); | |
289 | this.$el.find('.nick').select(); | |
290 | break; | |
291 | case 'password_mismatch': | |
292 | this.setStatus(_kiwi.global.i18n.translate('client_views_serverselect_password_incorrect').fetch()); | |
293 | this.show('enter_password'); | |
294 | this.$el.find('.password').select(); | |
295 | break; | |
296 | } | |
297 | }, | |
50ac472f | 298 | |
17f187e6 D |
299 | showError: function (error_reason) { |
300 | var err_text = _kiwi.global.i18n.translate('client_views_serverselect_connection_error').fetch(); | |
50ac472f | 301 | |
17f187e6 D |
302 | if (error_reason) { |
303 | switch (error_reason) { | |
304 | case 'ENOTFOUND': | |
305 | err_text = _kiwi.global.i18n.translate('client_views_serverselect_server_notfound').fetch(); | |
306 | break; | |
50ac472f | 307 | |
17f187e6 D |
308 | case 'ECONNREFUSED': |
309 | err_text += ' (' + _kiwi.global.i18n.translate('client_views_serverselect_connection_refused').fetch() + ')'; | |
310 | break; | |
311 | ||
312 | default: | |
313 | err_text += ' (' + error_reason + ')'; | |
314 | } | |
315 | } | |
50ac472f | 316 | |
17f187e6 D |
317 | this.setStatus(err_text, 'error'); |
318 | $('button', this.$el).attr('disabled', null); | |
319 | this.show(); | |
320 | } | |
321 | }); |