Disconnect message to locales; No need to confirm disconnect when already disconnected
[KiwiIRC.git] / client / src / models / application.js
CommitLineData
3dc62fbb 1(function () {\r
696a66f8 2\r
3dc62fbb 3 _kiwi.model.Application = Backbone.Model.extend({\r
eaaf73b0 4 /** _kiwi.view.Application */\r
3dc62fbb 5 view: null,\r
696a66f8 6\r
eaaf73b0 7 /** _kiwi.view.StatusMessage */\r
3dc62fbb 8 message: null,\r
696a66f8 9\r
3dc62fbb 10 initialize: function (options) {\r
89bbe76b
D
11 this.app_options = options;\r
12\r
3dc62fbb
D
13 if (options.container) {\r
14 this.set('container', options.container);\r
696a66f8
D
15 }\r
16\r
17 // The base url to the kiwi server\r
903f9288 18 this.set('base_path', options.base_path ? options.base_path : '');\r
696a66f8 19\r
bceb5db6 20 // Path for the settings.json file\r
3dc62fbb
D
21 this.set('settings_path', options.settings_path ?\r
22 options.settings_path :\r
bceb5db6
D
23 this.get('base_path') + '/assets/settings.json'\r
24 );\r
25\r
93e84f75 26 // Any options sent down from the server\r
3dc62fbb
D
27 this.server_settings = options.server_settings || {};\r
28 this.translations = options.translations || {};\r
29 this.themes = options.themes || [];\r
1c098c72 30 this.text_theme = options.text_theme || {};\r
93e84f75 31\r
17f187e6
D
32 // The applet to initially load\r
33 this.startup_applet_name = options.startup || 'kiwi_startup';\r
34\r
4966ba53
D
35 // Set any default settings before anything else is applied\r
36 if (this.server_settings && this.server_settings.client && this.server_settings.client.settings) {\r
37 this.applyDefaultClientSettings(this.server_settings.client.settings);\r
38 }\r
3dc62fbb 39 },\r
2ffd1291 40\r
696a66f8 41\r
6b8fbed0 42 initializeInterfaces: function () {\r
89bbe76b
D
43 // Best guess at where the kiwi server is if not already specified\r
44 var kiwi_server = this.app_options.kiwi_server || this.detectKiwiServer();\r
45\r
696a66f8 46 // Set the gateway up\r
89bbe76b 47 _kiwi.gateway = new _kiwi.model.Gateway({kiwi_server: kiwi_server});\r
eaaf73b0 48 this.bindGatewayCommands(_kiwi.gateway);\r
696a66f8
D
49\r
50 this.initializeClient();\r
51 this.initializeGlobals();\r
52\r
53 this.view.barsHide(true);\r
3dc62fbb 54 },\r
696a66f8
D
55\r
56\r
3dc62fbb 57 detectKiwiServer: function () {\r
696a66f8
D
58 // If running from file, default to localhost:7777 by default\r
59 if (window.location.protocol === 'file:') {\r
9ea8542e 60 return 'http://localhost:7778';\r
696a66f8
D
61 } else {\r
62 // Assume the kiwi server is on the same server\r
9ea8542e 63 return window.location.protocol + '//' + window.location.host;\r
696a66f8 64 }\r
3dc62fbb 65 },\r
696a66f8
D
66\r
67\r
17f187e6
D
68 showStartup: function() {\r
69 this.startup_applet = _kiwi.model.Applet.load(this.startup_applet_name, {no_tab: true});\r
70 this.startup_applet.tab = this.view.$('.console');\r
71 this.startup_applet.view.show();\r
77c84a91
D
72\r
73 _kiwi.global.events.emit('loaded');\r
3dc62fbb 74 },\r
f2bb5380
D
75\r
76\r
3dc62fbb 77 initializeClient: function () {\r
eaaf73b0 78 this.view = new _kiwi.view.Application({model: this, el: this.get('container')});\r
c966123a 79\r
d199cacf
D
80 // Takes instances of model_network\r
81 this.connections = new _kiwi.model.NetworkPanelList();\r
82\r
f2bb5380 83 // Applets panel list\r
c966123a
D
84 this.applet_panels = new _kiwi.model.PanelList();\r
85 this.applet_panels.view.$el.addClass('panellist applets');\r
de214968 86 this.view.$el.find('.tabs').append(this.applet_panels.view.$el);\r
c966123a 87\r
ce13508b
D
88 /**\r
89 * Set the UI components up\r
90 */\r
f4d69a63 91 this.controlbox = (new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]})).render();\r
41cba2fd 92 this.client_ui_commands = new _kiwi.misc.ClientUiCommands(this, this.controlbox);\r
696a66f8 93\r
41cba2fd 94 this.rightbar = new _kiwi.view.RightBar({el: this.view.$('.right_bar')[0]});\r
de214968 95 this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});\r
696a66f8 96\r
4f99a29b 97 new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r
9936359c 98 new _kiwi.view.ChannelTools({el: _kiwi.app.view.$el.find('.channel_tools')[0]});\r
7de3dd03 99\r
707593d2 100 this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});\r
696a66f8 101\r
30f9f42a 102 this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});\r
696a66f8
D
103\r
104 // Rejigg the UI sizes\r
105 this.view.doLayout();\r
3dc62fbb 106 },\r
696a66f8
D
107\r
108\r
3dc62fbb 109 initializeGlobals: function () {\r
6d5faa6e 110 _kiwi.global.connections = this.connections;\r
a9bc007f 111\r
50a95cb6 112 _kiwi.global.panels = this.panels;\r
a9bc007f
D
113 _kiwi.global.panels.applets = this.applet_panels;\r
114\r
6228b635
D
115 _kiwi.global.components.Applet = _kiwi.model.Applet;\r
116 _kiwi.global.components.Panel =_kiwi.model.Panel;\r
ba389555 117 _kiwi.global.components.MenuBox = _kiwi.view.MenuBox;\r
d88d36f3 118 _kiwi.global.components.DataStore = _kiwi.model.DataStore;\r
c5a49cfa 119 _kiwi.global.components.Notification = _kiwi.view.Notification;\r
4314d7fb
D
120 _kiwi.global.components.Events = function() {\r
121 return kiwi.events.createProxy();\r
122 };\r
3dc62fbb 123 },\r
696a66f8
D
124\r
125\r
3dc62fbb 126 applyDefaultClientSettings: function (settings) {\r
1cfc4800
JA
127 _.each(settings, function (value, setting) {\r
128 if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r
129 _kiwi.global.settings.set(setting, value);\r
130 }\r
131 });\r
3dc62fbb 132 },\r
1cfc4800
JA
133\r
134\r
3dc62fbb 135 panels: (function() {\r
aa24a133
D
136 var active_panel;\r
137\r
c966123a 138 var fn = function(panel_type) {\r
50a95cb6
D
139 var app = _kiwi.app,\r
140 panels;\r
c966123a
D
141\r
142 // Default panel type\r
143 panel_type = panel_type || 'connections';\r
144\r
145 switch (panel_type) {\r
146 case 'connections':\r
50a95cb6 147 panels = app.connections.panels();\r
c966123a
D
148 break;\r
149 case 'applets':\r
50a95cb6 150 panels = app.applet_panels.models;\r
c966123a
D
151 break;\r
152 }\r
6d5faa6e
D
153\r
154 // Active panels / server\r
aa24a133 155 panels.active = active_panel;\r
50a95cb6
D
156 panels.server = app.connections.active_connection ?\r
157 app.connections.active_connection.panels.server :\r
a9bc007f 158 null;\r
6d5faa6e
D
159\r
160 return panels;\r
161 };\r
162\r
163 _.extend(fn, Backbone.Events);\r
164\r
aa24a133
D
165 // Keep track of the active panel. Channel/query/server or applet\r
166 fn.bind('active', function (new_active_panel) {\r
27aafe0c 167 var previous_panel = active_panel;\r
aa24a133 168 active_panel = new_active_panel;\r
27aafe0c
D
169\r
170 _kiwi.global.events.emit('panel:active', {previous: previous_panel, active: active_panel});\r
aa24a133
D
171 });\r
172\r
6d5faa6e 173 return fn;\r
3dc62fbb 174 })(),\r
e4de4648
D
175\r
176\r
3dc62fbb 177 bindGatewayCommands: function (gw) {\r
645fe41b
JA
178 var that = this;\r
179\r
18ae1a72
D
180 // As soon as an IRC connection is made, show the full client UI\r
181 gw.on('connection:connect', function (event) {\r
696a66f8 182 that.view.barsShow();\r
696a66f8
D
183 });\r
184\r
185\r
24d27c8c
D
186 /**\r
187 * Handle the reconnections to the kiwi server\r
188 */\r
696a66f8 189 (function () {\r
d6eec6ed 190 // 0 = non-reconnecting state. 1 = reconnecting state.\r
696a66f8 191 var gw_stat = 0;\r
d6eec6ed 192\r
696a66f8 193 gw.on('disconnect', function (event) {\r
343cd967
PV
194 that.view.$el.removeClass('connected');\r
195\r
ab1e3748 196 // Reconnection phase will start to kick in\r
696a66f8
D
197 gw_stat = 1;\r
198 });\r
24d27c8c
D
199\r
200\r
696a66f8 201 gw.on('reconnecting', function (event) {\r
ab1e3748 202 var msg = translateText('client_models_application_reconnect_in_x_seconds', [event.delay/1000]) + '...';\r
24d27c8c
D
203\r
204 // Only need to mention the repeating re-connection messages on server panels\r
205 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 206 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 207 });\r
696a66f8 208 });\r
24d27c8c
D
209\r
210\r
78354438
D
211 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r
212 gw.on('kiwi:connected', function (event) {\r
ab1e3748
D
213 var msg;\r
214\r
343cd967 215 that.view.$el.addClass('connected');\r
696a66f8 216\r
127d8196
D
217 // Make the rpc globally available for plugins\r
218 _kiwi.global.rpc = _kiwi.gateway.rpc;\r
219\r
220 _kiwi.global.events.emit('connected');\r
221\r
ab1e3748
D
222 // If we were reconnecting, show some messages we have connected back OK\r
223 if (gw_stat === 1) {\r
224\r
225 // No longer in the reconnection state\r
226 gw_stat = 0;\r
227\r
228 msg = translateText('client_models_application_reconnect_successfully') + ' :)';\r
09c26937 229 that.message.text(msg, {timeout: 5000});\r
696a66f8 230\r
ab1e3748
D
231 // Mention the re-connection on every channel\r
232 _kiwi.app.connections.forEach(function(connection) {\r
233 connection.reconnect();\r
78354438 234\r
ab1e3748 235 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c 236\r
ab1e3748
D
237 connection.panels.forEach(function(panel) {\r
238 if (!panel.isChannel())\r
239 return;\r
24d27c8c 240\r
ab1e3748
D
241 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
242 });\r
24d27c8c 243 });\r
ab1e3748 244 }\r
696a66f8 245\r
696a66f8
D
246 });\r
247 })();\r
248\r
09c26937 249\r
645fe41b 250 gw.on('kiwi:reconfig', function () {\r
bceb5db6 251 $.getJSON(that.get('settings_path'), function (data) {\r
645fe41b
JA
252 that.server_settings = data.server_settings || {};\r
253 that.translations = data.translations || {};\r
254 });\r
255 });\r
09c26937
D
256\r
257\r
258 gw.on('kiwi:jumpserver', function (data) {\r
d6eec6ed
D
259 var serv;\r
260 // No server set? Then nowhere to jump to.\r
261 if (typeof data.kiwi_server === 'undefined')\r
262 return;\r
263\r
264 serv = data.kiwi_server;\r
265\r
266 // Strip any trailing slash from the end\r
267 if (serv[serv.length-1] === '/')\r
268 serv = serv.substring(0, serv.length-1);\r
269\r
09c26937
D
270 // Force the jumpserver now?\r
271 if (data.force) {\r
bf169745
D
272 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r
273 var jump_server_interval = Math.random() * (360 - 300) + 300;\r
ab1e3748 274 jump_server_interval = 1;\r
09c26937 275\r
bf169745 276 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r
a749d105 277 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
09c26937
D
278 that.message.text(msg, {timeout: 10000});\r
279\r
280 setTimeout(function forcedReconnect() {\r
a749d105 281 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r
09c26937
D
282 that.message.text(msg, {timeout: 8000});\r
283\r
284 setTimeout(function forcedReconnectPartTwo() {\r
89bbe76b 285 _kiwi.gateway.set('kiwi_server', serv);\r
338817f1 286\r
d6eec6ed
D
287 _kiwi.gateway.reconnect(function() {\r
288 // Reconnect all the IRC connections\r
289 that.connections.forEach(function(con){ con.reconnect(); });\r
290 });\r
09c26937
D
291 }, 5000);\r
292\r
293 }, jump_server_interval * 1000);\r
294 }\r
295 });\r
3dc62fbb 296 }\r
696a66f8 297\r
41cba2fd 298 });\r
696a66f8 299\r
3dc62fbb 300})();\r