3 _kiwi
.model
.Application
= Backbone
.Model
.extend({
4 /** _kiwi.view.Application */
7 /** _kiwi.view.StatusMessage */
10 /* Address for the kiwi server */
13 initialize: function (options
) {
14 if (options
.container
) {
15 this.set('container', options
.container
);
18 // The base url to the kiwi server
19 this.set('base_path', options
.base_path
? options
.base_path
: '');
21 // Path for the settings.json file
22 this.set('settings_path', options
.settings_path
?
23 options
.settings_path
:
24 this.get('base_path') + '/assets/settings.json'
27 // Any options sent down from the server
28 this.server_settings
= options
.server_settings
|| {};
29 this.translations
= options
.translations
|| {};
30 this.themes
= options
.themes
|| [];
31 this.text_theme
= options
.text_theme
|| {};
33 // Best guess at where the kiwi server is if not already specified
34 this.kiwi_server
= options
.kiwi_server
|| this.detectKiwiServer();
36 // The applet to initially load
37 this.startup_applet_name
= options
.startup
|| 'kiwi_startup';
39 // Set any default settings before anything else is applied
40 if (this.server_settings
&& this.server_settings
.client
&& this.server_settings
.client
.settings
) {
41 this.applyDefaultClientSettings(this.server_settings
.client
.settings
);
46 initializeInterfaces: function () {
48 _kiwi
.gateway
= new _kiwi
.model
.Gateway();
49 this.bindGatewayCommands(_kiwi
.gateway
);
51 this.initializeClient();
52 this.initializeGlobals();
54 this.view
.barsHide(true);
58 detectKiwiServer: function () {
59 // If running from file, default to localhost:7777 by default
60 if (window
.location
.protocol
=== 'file:') {
61 return 'http://localhost:7778';
63 // Assume the kiwi server is on the same server
64 return window
.location
.protocol
+ '//' + window
.location
.host
;
69 showStartup: function() {
70 this.startup_applet
= _kiwi
.model
.Applet
.load(this.startup_applet_name
, {no_tab
: true});
71 this.startup_applet
.tab
= this.view
.$('.console');
72 this.startup_applet
.view
.show();
76 initializeClient: function () {
77 this.view
= new _kiwi
.view
.Application({model
: this, el
: this.get('container')});
79 // Takes instances of model_network
80 this.connections
= new _kiwi
.model
.NetworkPanelList();
83 this.applet_panels
= new _kiwi
.model
.PanelList();
84 this.applet_panels
.view
.$el
.addClass('panellist applets');
85 this.view
.$el
.find('.tabs').append(this.applet_panels
.view
.$el
);
88 * Set the UI components up
90 this.controlbox
= new _kiwi
.view
.ControlBox({el
: $('#kiwi .controlbox')[0]});
91 this.client_ui_commands
= new _kiwi
.misc
.ClientUiCommands(this, this.controlbox
);
93 this.rightbar
= new _kiwi
.view
.RightBar({el
: this.view
.$('.right_bar')[0]});
94 this.topicbar
= new _kiwi
.view
.TopicBar({el
: this.view
.$el
.find('.topic')[0]});
96 new _kiwi
.view
.AppToolbar({el
: _kiwi
.app
.view
.$el
.find('.toolbar .app_tools')[0]});
97 new _kiwi
.view
.ChannelTools({el
: _kiwi
.app
.view
.$el
.find('.channel_tools')[0]});
99 this.message
= new _kiwi
.view
.StatusMessage({el
: this.view
.$el
.find('.status_message')[0]});
101 this.resize_handle
= new _kiwi
.view
.ResizeHandler({el
: this.view
.$el
.find('.memberlists_resize_handle')[0]});
103 // Rejigg the UI sizes
104 this.view
.doLayout();
108 initializeGlobals: function () {
109 _kiwi
.global
.connections
= this.connections
;
111 _kiwi
.global
.panels
= this.panels
;
112 _kiwi
.global
.panels
.applets
= this.applet_panels
;
114 _kiwi
.global
.components
.Applet
= _kiwi
.model
.Applet
;
115 _kiwi
.global
.components
.Panel
=_kiwi
.model
.Panel
;
116 _kiwi
.global
.components
.MenuBox
= _kiwi
.view
.MenuBox
;
120 applyDefaultClientSettings: function (settings
) {
121 _
.each(settings
, function (value
, setting
) {
122 if (typeof _kiwi
.global
.settings
.get(setting
) === 'undefined') {
123 _kiwi
.global
.settings
.set(setting
, value
);
129 panels
: (function() {
132 var fn = function(panel_type
) {
135 // Default panel type
136 panel_type
= panel_type
|| 'connections';
138 switch (panel_type
) {
140 panels
= this.connections
.panels();
143 panels
= this.applet_panels
.models
;
147 // Active panels / server
148 panels
.active
= active_panel
;
149 panels
.server
= this.connections
.active_connection
?
150 this.connections
.active_connection
.panels
.server
:
156 _
.extend(fn
, Backbone
.Events
);
158 // Keep track of the active panel. Channel/query/server or applet
159 fn
.bind('active', function (new_active_panel
) {
160 active_panel
= new_active_panel
;
167 bindGatewayCommands: function (gw
) {
170 // As soon as an IRC connection is made, show the full client UI
171 gw
.on('connection:connect', function (event
) {
172 that
.view
.barsShow();
177 * Handle the reconnections to the kiwi server
180 // 0 = non-reconnecting state. 1 = reconnecting state.
183 // If the current or upcoming disconnect was planned
184 var unplanned_disconnect
= false;
186 gw
.on('disconnect', function (event
) {
187 unplanned_disconnect
= !gw
.disconnect_requested
;
189 if (unplanned_disconnect
) {
190 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnecting').fetch() + '...';
191 that
.message
.text(msg
, {timeout
: 10000});
194 that
.view
.$el
.removeClass('connected');
196 // Mention the disconnection on every channel
197 _kiwi
.app
.connections
.forEach(function(connection
) {
198 connection
.panels
.server
.addMsg('', styleText('quit', {text
: msg
}), 'action quit');
200 connection
.panels
.forEach(function(panel
) {
201 if (!panel
.isChannel())
204 panel
.addMsg('', styleText('quit', {text
: msg
}), 'action quit');
212 gw
.on('reconnecting', function (event
) {
213 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_in_x_seconds').fetch(event
.delay
/1000) + '...';
215 // Only need to mention the repeating re-connection messages on server panels
216 _kiwi
.app
.connections
.forEach(function(connection
) {
217 connection
.panels
.server
.addMsg('', styleText('quit', {text
: msg
}), 'action quit');
222 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event
223 gw
.on('kiwi:connected', function (event
) {
224 that
.view
.$el
.addClass('connected');
225 if (gw_stat
!== 1) return;
227 if (unplanned_disconnect
) {
228 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_reconnect_successfully').fetch() + ':)';
229 that
.message
.text(msg
, {timeout
: 5000});
232 // Mention the re-connection on every channel
233 _kiwi
.app
.connections
.forEach(function(connection
) {
234 connection
.reconnect();
236 connection
.panels
.server
.addMsg('', styleText('rejoin', {text
: msg
}), 'action join');
238 connection
.panels
.forEach(function(panel
) {
239 if (!panel
.isChannel())
242 panel
.addMsg('', styleText('rejoin', {text
: msg
}), 'action join');
251 gw
.on('kiwi:reconfig', function () {
252 $.getJSON(that
.get('settings_path'), function (data
) {
253 that
.server_settings
= data
.server_settings
|| {};
254 that
.translations
= data
.translations
|| {};
259 gw
.on('kiwi:jumpserver', function (data
) {
261 // No server set? Then nowhere to jump to.
262 if (typeof data
.kiwi_server
=== 'undefined')
265 serv
= data
.kiwi_server
;
267 // Strip any trailing slash from the end
268 if (serv
[serv
.length
-1] === '/')
269 serv
= serv
.substring(0, serv
.length
-1);
271 // Force the jumpserver now?
273 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once
274 var jump_server_interval
= Math
.random() * (360 - 300) + 300;
276 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect
277 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_prepare').fetch();
278 that
.message
.text(msg
, {timeout
: 10000});
280 setTimeout(function forcedReconnect() {
281 var msg
= _kiwi
.global
.i18n
.translate('client_models_application_jumpserver_reconnect').fetch();
282 that
.message
.text(msg
, {timeout
: 8000});
284 setTimeout(function forcedReconnectPartTwo() {
285 _kiwi
.app
.kiwi_server
= serv
;
287 _kiwi
.gateway
.reconnect(function() {
288 // Reconnect all the IRC connections
289 that
.connections
.forEach(function(con
){ con
.reconnect(); });
293 }, jump_server_interval
* 1000);