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