1 _kiwi
.view
.ServerSelect = function () {
2 // Are currently showing all the controlls or just a nick_change box?
5 var model
= Backbone
.View
.extend({
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'
14 initialize: function () {
17 this.$el
= $($('#tmpl_server_select').html());
19 // Remove the 'more' link if the server has disabled server changing
20 if (_kiwi
.app
.server_settings
&& _kiwi
.app
.server_settings
.connection
) {
21 if (!_kiwi
.app
.server_settings
.connection
.allow_change
) {
22 this.$el
.find('.show_more').remove();
23 this.$el
.addClass('single_server');
27 _kiwi
.gateway
.bind('onconnect', this.networkConnected
, this);
28 _kiwi
.gateway
.bind('connecting', this.networkConnecting
, this);
29 _kiwi
.gateway
.bind('onirc_error', this.onIrcError
, this);
33 _kiwi
.gateway
.off('onconnect', this.networkConnected
, this);
34 _kiwi
.gateway
.off('connecting', this.networkConnecting
, this);
35 _kiwi
.gateway
.off('onirc_error', this.onIrcError
, this);
40 submitForm: function (event
) {
41 event
.preventDefault();
43 // Make sure a nick is chosen
44 if (!$('input.nick', this.$el
).val().trim()) {
45 this.setStatus('Select a nickname first!');
46 $('input.nick', this.$el
).select();
50 if (state
=== 'nick_change') {
51 this.submitNickChange(event
);
53 this.submitLogin(event
);
56 $('button', this.$el
).attr('disabled', 1);
60 submitLogin: function (event
) {
61 // If submitting is disabled, don't do anything
62 if ($('button', this.$el
).attr('disabled')) return;
65 nick
: $('input.nick', this.$el
).val(),
66 server
: $('input.server', this.$el
).val(),
67 port
: $('input.port', this.$el
).val(),
68 ssl
: $('input.ssl', this.$el
).prop('checked'),
69 password
: $('input.password', this.$el
).val(),
70 channel
: $('input.channel', this.$el
).val(),
71 channel_key
: $('input.channel_key', this.$el
).val()
74 this.trigger('server_connect', values
);
77 submitNickChange: function (event
) {
78 _kiwi
.gateway
.changeNick(null, $('input.nick', this.$el
).val());
79 this.networkConnecting();
82 showPass: function (event
) {
83 if (this.$el
.find('tr.have_pass input').is(':checked')) {
84 this.$el
.find('tr.pass').show().find('input').focus();
86 this.$el
.find('tr.pass').hide().find('input').val('');
90 channelKeyIconClick: function (event
) {
91 this.$el
.find('tr.have_key input').click();
94 showKey: function (event
) {
95 if (this.$el
.find('tr.have_key input').is(':checked')) {
96 this.$el
.find('tr.key').show().find('input').focus();
98 this.$el
.find('tr.key').hide().find('input').val('');
102 showMore: function (event
) {
103 $('.more', this.$el
).slideDown('fast');
104 $('input.server', this.$el
).select();
107 populateFields: function (defaults
) {
108 var nick
, server
, port
, channel
, channel_key
, ssl
, password
;
110 defaults
= defaults
|| {};
112 nick
= defaults
.nick
|| '';
113 server
= defaults
.server
|| '';
114 port
= defaults
.port
|| 6667;
115 ssl
= defaults
.ssl
|| 0;
116 password
= defaults
.password
|| '';
117 channel
= defaults
.channel
|| '';
118 channel_key
= defaults
.channel_key
|| '';
120 $('input.nick', this.$el
).val(nick
);
121 $('input.server', this.$el
).val(server
);
122 $('input.port', this.$el
).val(port
);
123 $('input.ssl', this.$el
).prop('checked', ssl
);
124 $('input#server_select_show_pass', this.$el
).prop('checked', !(!password
));
125 $('input.password', this.$el
).val(password
);
127 $('tr.pass', this.$el
).show();
129 $('input.channel', this.$el
).val(channel
);
130 $('input#server_select_show_channel_key', this.$el
).prop('checked', !(!channel_key
));
131 $('input.channel_key', this.$el
).val(channel_key
);
132 if (!(!channel_key
)) {
133 $('tr.key', this.$el
).show();
141 show: function (new_state
) {
142 new_state
= new_state
|| 'all';
146 if (new_state
=== 'all') {
147 $('.show_more', this.$el
).show();
149 } else if (new_state
=== 'more') {
150 $('.more', this.$el
).slideDown('fast');
152 } else if (new_state
=== 'nick_change') {
153 $('.more', this.$el
).hide();
154 $('.show_more', this.$el
).hide();
155 $('input.nick', this.$el
).select();
161 infoBoxShow: function() {
162 var $side_panel
= this.$el
.find('.side_panel');
164 width
: parseInt($side_panel
.css('left'), 10) + $side_panel
.find('.content:first').outerWidth()
168 infoBoxHide: function() {
169 var $side_panel
= this.$el
.find('.side_panel');
171 width
: parseInt($side_panel
.css('left'), 10)
175 infoBoxSet: function($info_view
) {
176 this.$el
.find('.side_panel .content')
181 setStatus: function (text
, class_name
) {
182 $('.status', this.$el
)
184 .attr('class', 'status')
185 .addClass(class_name
||'')
188 clearStatus: function () {
189 $('.status', this.$el
).hide();
192 networkConnected: function (event
) {
193 this.setStatus('Connected :)', 'ok');
194 $('form', this.$el
).hide();
197 networkConnecting: function (event
) {
198 this.setStatus('Connecting..', 'ok');
201 onIrcError: function (data
) {
202 $('button', this.$el
).attr('disabled', null);
204 if (data
.error
== 'nickname_in_use') {
205 this.setStatus('Nickname already taken');
206 this.show('nick_change');
209 if (data
.error
== 'password_mismatch') {
210 this.setStatus('Incorrect Password');
211 this.show('nick_change');
212 that
.$el
.find('.password').select();
216 showError: function (error_reason
) {
217 var err_text
= 'Error Connecting';
220 switch (error_reason
) {
222 err_text
= 'Server not found';
226 err_text
+= ' (Connection refused)';
230 err_text
+= ' (' + error_reason
+ ')';
234 this.setStatus(err_text
, 'error');
235 $('button', this.$el
).attr('disabled', null);
241 return new model(arguments
);