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