message:new & message:display plugin hooks
[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
1e85105e 90 this.controlbox = new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]});\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
3dc62fbb 117 },\r
696a66f8
D
118\r
119\r
3dc62fbb 120 applyDefaultClientSettings: function (settings) {\r
1cfc4800
JA
121 _.each(settings, function (value, setting) {\r
122 if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r
123 _kiwi.global.settings.set(setting, value);\r
124 }\r
125 });\r
3dc62fbb 126 },\r
1cfc4800
JA
127\r
128\r
3dc62fbb 129 panels: (function() {\r
aa24a133
D
130 var active_panel;\r
131\r
c966123a
D
132 var fn = function(panel_type) {\r
133 var panels;\r
134\r
135 // Default panel type\r
136 panel_type = panel_type || 'connections';\r
137\r
138 switch (panel_type) {\r
139 case 'connections':\r
140 panels = this.connections.panels();\r
141 break;\r
142 case 'applets':\r
143 panels = this.applet_panels.models;\r
144 break;\r
145 }\r
6d5faa6e
D
146\r
147 // Active panels / server\r
aa24a133 148 panels.active = active_panel;\r
a9bc007f
D
149 panels.server = this.connections.active_connection ?\r
150 this.connections.active_connection.panels.server :\r
151 null;\r
6d5faa6e
D
152\r
153 return panels;\r
154 };\r
155\r
156 _.extend(fn, Backbone.Events);\r
157\r
aa24a133
D
158 // Keep track of the active panel. Channel/query/server or applet\r
159 fn.bind('active', function (new_active_panel) {\r
160 active_panel = new_active_panel;\r
161 });\r
162\r
6d5faa6e 163 return fn;\r
3dc62fbb 164 })(),\r
e4de4648
D
165\r
166\r
3dc62fbb 167 bindGatewayCommands: function (gw) {\r
645fe41b
JA
168 var that = this;\r
169\r
18ae1a72
D
170 // As soon as an IRC connection is made, show the full client UI\r
171 gw.on('connection:connect', function (event) {\r
696a66f8 172 that.view.barsShow();\r
696a66f8
D
173 });\r
174\r
175\r
24d27c8c
D
176 /**\r
177 * Handle the reconnections to the kiwi server\r
178 */\r
696a66f8 179 (function () {\r
d6eec6ed 180 // 0 = non-reconnecting state. 1 = reconnecting state.\r
696a66f8 181 var gw_stat = 0;\r
d6eec6ed
D
182\r
183 // If the current or upcoming disconnect was planned\r
09c26937 184 var unplanned_disconnect = false;\r
696a66f8
D
185\r
186 gw.on('disconnect', function (event) {\r
09c26937
D
187 unplanned_disconnect = !gw.disconnect_requested;\r
188\r
189 if (unplanned_disconnect) {\r
190 var msg = _kiwi.global.i18n.translate('client_models_application_reconnecting').fetch() + '...';\r
191 that.message.text(msg, {timeout: 10000});\r
192 }\r
696a66f8 193\r
343cd967
PV
194 that.view.$el.removeClass('connected');\r
195\r
696a66f8 196 // Mention the disconnection on every channel\r
24d27c8c 197 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 198 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c
D
199\r
200 connection.panels.forEach(function(panel) {\r
201 if (!panel.isChannel())\r
202 return;\r
203\r
9ec48c54 204 panel.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 205 });\r
696a66f8 206 });\r
696a66f8
D
207\r
208 gw_stat = 1;\r
209 });\r
24d27c8c
D
210\r
211\r
696a66f8 212 gw.on('reconnecting', function (event) {\r
247dd7ac 213 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_in_x_seconds').fetch(event.delay/1000) + '...';\r
24d27c8c
D
214\r
215 // Only need to mention the repeating re-connection messages on server panels\r
216 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 217 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 218 });\r
696a66f8 219 });\r
24d27c8c
D
220\r
221\r
78354438
D
222 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r
223 gw.on('kiwi:connected', function (event) {\r
343cd967 224 that.view.$el.addClass('connected');\r
696a66f8
D
225 if (gw_stat !== 1) return;\r
226\r
09c26937
D
227 if (unplanned_disconnect) {\r
228 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_successfully').fetch() + ':)';\r
229 that.message.text(msg, {timeout: 5000});\r
230 }\r
696a66f8 231\r
d6eec6ed 232 // Mention the re-connection on every channel\r
24d27c8c 233 _kiwi.app.connections.forEach(function(connection) {\r
78354438
D
234 connection.reconnect();\r
235\r
9ec48c54 236 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c
D
237\r
238 connection.panels.forEach(function(panel) {\r
239 if (!panel.isChannel())\r
240 return;\r
241\r
9ec48c54 242 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c 243 });\r
696a66f8 244 });\r
696a66f8
D
245\r
246 gw_stat = 0;\r
247 });\r
248 })();\r
249\r
09c26937 250\r
645fe41b 251 gw.on('kiwi:reconfig', function () {\r
bceb5db6 252 $.getJSON(that.get('settings_path'), function (data) {\r
645fe41b
JA
253 that.server_settings = data.server_settings || {};\r
254 that.translations = data.translations || {};\r
255 });\r
256 });\r
09c26937
D
257\r
258\r
259 gw.on('kiwi:jumpserver', function (data) {\r
d6eec6ed
D
260 var serv;\r
261 // No server set? Then nowhere to jump to.\r
262 if (typeof data.kiwi_server === 'undefined')\r
263 return;\r
264\r
265 serv = data.kiwi_server;\r
266\r
267 // Strip any trailing slash from the end\r
268 if (serv[serv.length-1] === '/')\r
269 serv = serv.substring(0, serv.length-1);\r
270\r
09c26937
D
271 // Force the jumpserver now?\r
272 if (data.force) {\r
bf169745
D
273 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r
274 var jump_server_interval = Math.random() * (360 - 300) + 300;\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
e5baa247 285 _kiwi.app.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