Notification view integrated into the core
[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
D
111\r
112 _kiwi.global.panels = this.panels;\r
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
3dc62fbb 119 },\r
696a66f8
D
120\r
121\r
3dc62fbb 122 applyDefaultClientSettings: function (settings) {\r
1cfc4800
JA
123 _.each(settings, function (value, setting) {\r
124 if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r
125 _kiwi.global.settings.set(setting, value);\r
126 }\r
127 });\r
3dc62fbb 128 },\r
1cfc4800
JA
129\r
130\r
3dc62fbb 131 panels: (function() {\r
aa24a133
D
132 var active_panel;\r
133\r
c966123a
D
134 var fn = function(panel_type) {\r
135 var panels;\r
136\r
137 // Default panel type\r
138 panel_type = panel_type || 'connections';\r
139\r
140 switch (panel_type) {\r
141 case 'connections':\r
142 panels = this.connections.panels();\r
143 break;\r
144 case 'applets':\r
145 panels = this.applet_panels.models;\r
146 break;\r
147 }\r
6d5faa6e
D
148\r
149 // Active panels / server\r
aa24a133 150 panels.active = active_panel;\r
a9bc007f
D
151 panels.server = this.connections.active_connection ?\r
152 this.connections.active_connection.panels.server :\r
153 null;\r
6d5faa6e
D
154\r
155 return panels;\r
156 };\r
157\r
158 _.extend(fn, Backbone.Events);\r
159\r
aa24a133
D
160 // Keep track of the active panel. Channel/query/server or applet\r
161 fn.bind('active', function (new_active_panel) {\r
27aafe0c 162 var previous_panel = active_panel;\r
aa24a133 163 active_panel = new_active_panel;\r
27aafe0c
D
164\r
165 _kiwi.global.events.emit('panel:active', {previous: previous_panel, active: active_panel});\r
aa24a133
D
166 });\r
167\r
6d5faa6e 168 return fn;\r
3dc62fbb 169 })(),\r
e4de4648
D
170\r
171\r
3dc62fbb 172 bindGatewayCommands: function (gw) {\r
645fe41b
JA
173 var that = this;\r
174\r
18ae1a72
D
175 // As soon as an IRC connection is made, show the full client UI\r
176 gw.on('connection:connect', function (event) {\r
696a66f8 177 that.view.barsShow();\r
696a66f8
D
178 });\r
179\r
180\r
24d27c8c
D
181 /**\r
182 * Handle the reconnections to the kiwi server\r
183 */\r
696a66f8 184 (function () {\r
d6eec6ed 185 // 0 = non-reconnecting state. 1 = reconnecting state.\r
696a66f8 186 var gw_stat = 0;\r
d6eec6ed 187\r
696a66f8 188 gw.on('disconnect', function (event) {\r
343cd967
PV
189 that.view.$el.removeClass('connected');\r
190\r
ab1e3748 191 // Reconnection phase will start to kick in\r
696a66f8
D
192 gw_stat = 1;\r
193 });\r
24d27c8c
D
194\r
195\r
696a66f8 196 gw.on('reconnecting', function (event) {\r
ab1e3748 197 var msg = translateText('client_models_application_reconnect_in_x_seconds', [event.delay/1000]) + '...';\r
24d27c8c
D
198\r
199 // Only need to mention the repeating re-connection messages on server panels\r
200 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 201 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 202 });\r
696a66f8 203 });\r
24d27c8c
D
204\r
205\r
78354438
D
206 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r
207 gw.on('kiwi:connected', function (event) {\r
ab1e3748
D
208 var msg;\r
209\r
343cd967 210 that.view.$el.addClass('connected');\r
696a66f8 211\r
127d8196
D
212 // Make the rpc globally available for plugins\r
213 _kiwi.global.rpc = _kiwi.gateway.rpc;\r
214\r
215 _kiwi.global.events.emit('connected');\r
216\r
ab1e3748
D
217 // If we were reconnecting, show some messages we have connected back OK\r
218 if (gw_stat === 1) {\r
219\r
220 // No longer in the reconnection state\r
221 gw_stat = 0;\r
222\r
223 msg = translateText('client_models_application_reconnect_successfully') + ' :)';\r
09c26937 224 that.message.text(msg, {timeout: 5000});\r
696a66f8 225\r
ab1e3748
D
226 // Mention the re-connection on every channel\r
227 _kiwi.app.connections.forEach(function(connection) {\r
228 connection.reconnect();\r
78354438 229\r
ab1e3748 230 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c 231\r
ab1e3748
D
232 connection.panels.forEach(function(panel) {\r
233 if (!panel.isChannel())\r
234 return;\r
24d27c8c 235\r
ab1e3748
D
236 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
237 });\r
24d27c8c 238 });\r
ab1e3748 239 }\r
696a66f8 240\r
696a66f8
D
241 });\r
242 })();\r
243\r
09c26937 244\r
645fe41b 245 gw.on('kiwi:reconfig', function () {\r
bceb5db6 246 $.getJSON(that.get('settings_path'), function (data) {\r
645fe41b
JA
247 that.server_settings = data.server_settings || {};\r
248 that.translations = data.translations || {};\r
249 });\r
250 });\r
09c26937
D
251\r
252\r
253 gw.on('kiwi:jumpserver', function (data) {\r
d6eec6ed
D
254 var serv;\r
255 // No server set? Then nowhere to jump to.\r
256 if (typeof data.kiwi_server === 'undefined')\r
257 return;\r
258\r
259 serv = data.kiwi_server;\r
260\r
261 // Strip any trailing slash from the end\r
262 if (serv[serv.length-1] === '/')\r
263 serv = serv.substring(0, serv.length-1);\r
264\r
09c26937
D
265 // Force the jumpserver now?\r
266 if (data.force) {\r
bf169745
D
267 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r
268 var jump_server_interval = Math.random() * (360 - 300) + 300;\r
ab1e3748 269 jump_server_interval = 1;\r
09c26937 270\r
bf169745 271 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r
a749d105 272 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
09c26937
D
273 that.message.text(msg, {timeout: 10000});\r
274\r
275 setTimeout(function forcedReconnect() {\r
a749d105 276 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r
09c26937
D
277 that.message.text(msg, {timeout: 8000});\r
278\r
279 setTimeout(function forcedReconnectPartTwo() {\r
89bbe76b 280 _kiwi.gateway.set('kiwi_server', serv);\r
338817f1 281\r
d6eec6ed
D
282 _kiwi.gateway.reconnect(function() {\r
283 // Reconnect all the IRC connections\r
284 that.connections.forEach(function(con){ con.reconnect(); });\r
285 });\r
09c26937
D
286 }, 5000);\r
287\r
288 }, jump_server_interval * 1000);\r
289 }\r
290 });\r
3dc62fbb 291 }\r
696a66f8 292\r
41cba2fd 293 });\r
696a66f8 294\r
3dc62fbb 295})();\r