| 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 | }; |
| 28 | |
| 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 | } |
| 38 | |
| 39 | // Are currently showing all the controlls or just a nick_change box? |
| 40 | this.state = 'all'; |
| 41 | |
| 42 | this.more_shown = false; |
| 43 | |
| 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 | }, |
| 49 | |
| 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); |
| 55 | |
| 56 | this.remove(); |
| 57 | }, |
| 58 | |
| 59 | submitForm: function (event) { |
| 60 | event.preventDefault(); |
| 61 | |
| 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(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (this.state === 'nick_change') { |
| 70 | this.submitNickChange(event); |
| 71 | } else { |
| 72 | this.submitLogin(event); |
| 73 | } |
| 74 | |
| 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 | }, |
| 109 | |
| 110 | channelKeyIconClick: function (event) { |
| 111 | this.$el.find('tr.have_key input').click(); |
| 112 | }, |
| 113 | |
| 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 | } |
| 170 | |
| 171 | // Temporary values |
| 172 | this.server_options = {}; |
| 173 | |
| 174 | if (defaults.encoding) |
| 175 | this.server_options.encoding = defaults.encoding; |
| 176 | }, |
| 177 | |
| 178 | hide: function () { |
| 179 | this.$el.slideUp(); |
| 180 | }, |
| 181 | |
| 182 | show: function (new_state) { |
| 183 | new_state = new_state || 'all'; |
| 184 | |
| 185 | this.$el.show(); |
| 186 | |
| 187 | if (new_state === 'all') { |
| 188 | $('.show_more', this.$el).show(); |
| 189 | |
| 190 | } else if (new_state === 'more') { |
| 191 | $('.more', this.$el).slideDown('fast'); |
| 192 | |
| 193 | } else if (new_state === 'nick_change') { |
| 194 | $('.more', this.$el).hide(); |
| 195 | $('.show_more', this.$el).hide(); |
| 196 | $('input.nick', this.$el).select(); |
| 197 | |
| 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 | } |
| 203 | |
| 204 | this.state = new_state; |
| 205 | }, |
| 206 | |
| 207 | infoBoxShow: function() { |
| 208 | var $side_panel = this.$el.find('.side_panel'); |
| 209 | |
| 210 | // Some theme may hide the info panel so check before we |
| 211 | // resize ourselves |
| 212 | if (!$side_panel.is(':visible')) |
| 213 | return; |
| 214 | |
| 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; |
| 272 | |
| 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 | }, |
| 298 | |
| 299 | showError: function (error_reason) { |
| 300 | var err_text = _kiwi.global.i18n.translate('client_views_serverselect_connection_error').fetch(); |
| 301 | |
| 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; |
| 307 | |
| 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 | } |
| 316 | |
| 317 | this.setStatus(err_text, 'error'); |
| 318 | $('button', this.$el).attr('disabled', null); |
| 319 | this.show(); |
| 320 | } |
| 321 | }); |