1 _kiwi
.view
.ServerSelect
= Backbone
.View
.extend({
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'
11 initialize: function () {
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()
29 this.$el
= $(_
.template($('#tmpl_server_select').html().trim(), text
));
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');
39 // Are currently showing all the controlls or just a nick_change box?
42 this.more_shown
= false;
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);
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);
59 submitForm: function (event
) {
60 event
.preventDefault();
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();
69 if (this.state
=== 'nick_change') {
70 this.submitNickChange(event
);
72 this.submitLogin(event
);
75 $('button', this.$el
).attr('disabled', 1);
79 submitLogin: function (event
) {
80 // If submitting is disabled, don't do anything
81 if ($('button', this.$el
).attr('disabled')) return;
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
94 this.trigger('server_connect', values
);
97 submitNickChange: function (event
) {
98 _kiwi
.gateway
.changeNick(null, $('input.nick', this.$el
).val());
99 this.networkConnecting();
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();
106 this.$el
.find('tr.pass').hide().find('input').val('');
110 channelKeyIconClick: function (event
) {
111 this.$el
.find('tr.have_key input').click();
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();
118 this.$el
.find('tr.key').hide().find('input').val('');
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;
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;
142 populateFields: function (defaults
) {
143 var nick
, server
, port
, channel
, channel_key
, ssl
, password
;
145 defaults
= defaults
|| {};
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
|| '';
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
);
162 $('tr.pass', this.$el
).show();
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();
172 this.server_options
= {};
174 if (defaults
.encoding
)
175 this.server_options
.encoding
= defaults
.encoding
;
182 show: function (new_state
) {
183 new_state
= new_state
|| 'all';
187 if (new_state
=== 'all') {
188 $('.show_more', this.$el
).show();
190 } else if (new_state
=== 'more') {
191 $('.more', this.$el
).slideDown('fast');
193 } else if (new_state
=== 'nick_change') {
194 $('.more', this.$el
).hide();
195 $('.show_more', this.$el
).hide();
196 $('input.nick', this.$el
).select();
198 } else if (new_state
=== 'enter_password') {
199 $('.more', this.$el
).hide();
200 $('.show_more', this.$el
).hide();
201 $('input.password', this.$el
).select();
204 this.state
= new_state
;
207 infoBoxShow: function() {
208 var $side_panel
= this.$el
.find('.side_panel');
210 // Some theme may hide the info panel so check before we
212 if (!$side_panel
.is(':visible'))
216 width
: parseInt($side_panel
.css('left'), 10) + $side_panel
.find('.content:first').outerWidth()
220 infoBoxHide: function() {
221 var $side_panel
= this.$el
.find('.side_panel');
223 width
: parseInt($side_panel
.css('left'), 10)
227 infoBoxSet: function($info_view
) {
228 this.$el
.find('.side_panel .content')
233 setStatus: function (text
, class_name
) {
234 $('.status', this.$el
)
236 .attr('class', 'status')
237 .addClass(class_name
||'')
240 clearStatus: function () {
241 $('.status', this.$el
).hide();
245 this.populateFields();
248 this.$('button').attr('disabled', null);
251 newNetwork: function(network
) {
252 // Keep a reference to this network so we can interact with it
253 this.model
.current_connecting_network
= network
;
256 networkConnected: function (event
) {
257 this.model
.trigger('connected', _kiwi
.app
.connections
.getByConnectionId(event
.server
));
258 this.model
.current_connecting_network
= null;
261 networkConnecting: function (event
) {
262 this.model
.trigger('connecting');
263 this.setStatus(_kiwi
.global
.i18n
.translate('client_views_serverselect_connection_trying').fetch(), 'ok');
265 this.$('.status').append('<a class="show_server"><i class="icon-info-sign"></i></a>');
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
)
273 _kiwi
.app
.view
.barsShow();
274 this.model
.current_connecting_network
.panels
.server
.view
.show();
277 onIrcError: function (data
) {
278 $('button', this.$el
).attr('disabled', null);
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();
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();
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();
299 showError: function (error_reason
) {
300 var err_text
= _kiwi
.global
.i18n
.translate('client_views_serverselect_connection_error').fetch();
303 switch (error_reason
) {
305 err_text
= _kiwi
.global
.i18n
.translate('client_views_serverselect_server_notfound').fetch();
309 err_text
+= ' (' + _kiwi
.global
.i18n
.translate('client_views_serverselect_connection_refused').fetch() + ')';
313 err_text
+= ' (' + error_reason
+ ')';
317 this.setStatus(err_text
, 'error');
318 $('button', this.$el
).attr('disabled', null);