Merge branch 'development' of github.com:prawnsalad/KiwiIRC into development
[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 186\r
696a66f8 187 gw.on('disconnect', function (event) {\r
343cd967
PV
188 that.view.$el.removeClass('connected');\r
189\r
ab1e3748 190 // Reconnection phase will start to kick in\r
696a66f8
D
191 gw_stat = 1;\r
192 });\r
24d27c8c
D
193\r
194\r
696a66f8 195 gw.on('reconnecting', function (event) {\r
ab1e3748 196 var msg = translateText('client_models_application_reconnect_in_x_seconds', [event.delay/1000]) + '...';\r
24d27c8c
D
197\r
198 // Only need to mention the repeating re-connection messages on server panels\r
199 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 200 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 201 });\r
696a66f8 202 });\r
24d27c8c
D
203\r
204\r
78354438
D
205 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r
206 gw.on('kiwi:connected', function (event) {\r
ab1e3748
D
207 var msg;\r
208\r
343cd967 209 that.view.$el.addClass('connected');\r
696a66f8 210\r
ab1e3748
D
211 // If we were reconnecting, show some messages we have connected back OK\r
212 if (gw_stat === 1) {\r
213\r
214 // No longer in the reconnection state\r
215 gw_stat = 0;\r
216\r
217 msg = translateText('client_models_application_reconnect_successfully') + ' :)';\r
09c26937 218 that.message.text(msg, {timeout: 5000});\r
696a66f8 219\r
ab1e3748
D
220 // Mention the re-connection on every channel\r
221 _kiwi.app.connections.forEach(function(connection) {\r
222 connection.reconnect();\r
78354438 223\r
ab1e3748 224 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c 225\r
ab1e3748
D
226 connection.panels.forEach(function(panel) {\r
227 if (!panel.isChannel())\r
228 return;\r
24d27c8c 229\r
ab1e3748
D
230 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
231 });\r
24d27c8c 232 });\r
ab1e3748 233 }\r
696a66f8 234\r
696a66f8
D
235 });\r
236 })();\r
237\r
09c26937 238\r
645fe41b 239 gw.on('kiwi:reconfig', function () {\r
bceb5db6 240 $.getJSON(that.get('settings_path'), function (data) {\r
645fe41b
JA
241 that.server_settings = data.server_settings || {};\r
242 that.translations = data.translations || {};\r
243 });\r
244 });\r
09c26937
D
245\r
246\r
247 gw.on('kiwi:jumpserver', function (data) {\r
d6eec6ed
D
248 var serv;\r
249 // No server set? Then nowhere to jump to.\r
250 if (typeof data.kiwi_server === 'undefined')\r
251 return;\r
252\r
253 serv = data.kiwi_server;\r
254\r
255 // Strip any trailing slash from the end\r
256 if (serv[serv.length-1] === '/')\r
257 serv = serv.substring(0, serv.length-1);\r
258\r
09c26937
D
259 // Force the jumpserver now?\r
260 if (data.force) {\r
bf169745
D
261 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r
262 var jump_server_interval = Math.random() * (360 - 300) + 300;\r
ab1e3748 263 jump_server_interval = 1;\r
09c26937 264\r
bf169745 265 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r
a749d105 266 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
09c26937
D
267 that.message.text(msg, {timeout: 10000});\r
268\r
269 setTimeout(function forcedReconnect() {\r
a749d105 270 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r
09c26937
D
271 that.message.text(msg, {timeout: 8000});\r
272\r
273 setTimeout(function forcedReconnectPartTwo() {\r
e5baa247 274 _kiwi.app.kiwi_server = serv;\r
338817f1 275\r
d6eec6ed
D
276 _kiwi.gateway.reconnect(function() {\r
277 // Reconnect all the IRC connections\r
278 that.connections.forEach(function(con){ con.reconnect(); });\r
279 });\r
09c26937
D
280 }, 5000);\r
281\r
282 }, jump_server_interval * 1000);\r
283 }\r
284 });\r
3dc62fbb 285 }\r
696a66f8 286\r
41cba2fd 287 });\r
696a66f8 288\r
3dc62fbb 289})();\r