making it librejs compliant
[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
0a25c979
D
83 // If all connections are removed at some point, hide the bars\r
84 this.connections.on('remove', _.bind(function() {\r
85 if (this.connections.length === 0) {\r
86 this.view.barsHide();\r
87 }\r
88 }, this));\r
89\r
f2bb5380 90 // Applets panel list\r
c966123a
D
91 this.applet_panels = new _kiwi.model.PanelList();\r
92 this.applet_panels.view.$el.addClass('panellist applets');\r
de214968 93 this.view.$el.find('.tabs').append(this.applet_panels.view.$el);\r
c966123a 94\r
ce13508b
D
95 /**\r
96 * Set the UI components up\r
97 */\r
f4d69a63 98 this.controlbox = (new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]})).render();\r
41cba2fd 99 this.client_ui_commands = new _kiwi.misc.ClientUiCommands(this, this.controlbox);\r
696a66f8 100\r
41cba2fd 101 this.rightbar = new _kiwi.view.RightBar({el: this.view.$('.right_bar')[0]});\r
de214968 102 this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});\r
696a66f8 103\r
4f99a29b 104 new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});\r
9936359c 105 new _kiwi.view.ChannelTools({el: _kiwi.app.view.$el.find('.channel_tools')[0]});\r
7de3dd03 106\r
707593d2 107 this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});\r
696a66f8 108\r
30f9f42a 109 this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});\r
696a66f8
D
110\r
111 // Rejigg the UI sizes\r
112 this.view.doLayout();\r
3dc62fbb 113 },\r
696a66f8
D
114\r
115\r
3dc62fbb 116 initializeGlobals: function () {\r
6d5faa6e 117 _kiwi.global.connections = this.connections;\r
a9bc007f 118\r
50a95cb6 119 _kiwi.global.panels = this.panels;\r
a9bc007f
D
120 _kiwi.global.panels.applets = this.applet_panels;\r
121\r
6228b635
D
122 _kiwi.global.components.Applet = _kiwi.model.Applet;\r
123 _kiwi.global.components.Panel =_kiwi.model.Panel;\r
ba389555 124 _kiwi.global.components.MenuBox = _kiwi.view.MenuBox;\r
d88d36f3 125 _kiwi.global.components.DataStore = _kiwi.model.DataStore;\r
c5a49cfa 126 _kiwi.global.components.Notification = _kiwi.view.Notification;\r
4314d7fb
D
127 _kiwi.global.components.Events = function() {\r
128 return kiwi.events.createProxy();\r
129 };\r
3dc62fbb 130 },\r
696a66f8
D
131\r
132\r
3dc62fbb 133 applyDefaultClientSettings: function (settings) {\r
1cfc4800
JA
134 _.each(settings, function (value, setting) {\r
135 if (typeof _kiwi.global.settings.get(setting) === 'undefined') {\r
136 _kiwi.global.settings.set(setting, value);\r
137 }\r
138 });\r
3dc62fbb 139 },\r
1cfc4800
JA
140\r
141\r
3dc62fbb 142 panels: (function() {\r
aa24a133
D
143 var active_panel;\r
144\r
c966123a 145 var fn = function(panel_type) {\r
50a95cb6
D
146 var app = _kiwi.app,\r
147 panels;\r
c966123a
D
148\r
149 // Default panel type\r
150 panel_type = panel_type || 'connections';\r
151\r
152 switch (panel_type) {\r
153 case 'connections':\r
50a95cb6 154 panels = app.connections.panels();\r
c966123a
D
155 break;\r
156 case 'applets':\r
50a95cb6 157 panels = app.applet_panels.models;\r
c966123a
D
158 break;\r
159 }\r
6d5faa6e
D
160\r
161 // Active panels / server\r
aa24a133 162 panels.active = active_panel;\r
50a95cb6
D
163 panels.server = app.connections.active_connection ?\r
164 app.connections.active_connection.panels.server :\r
a9bc007f 165 null;\r
6d5faa6e
D
166\r
167 return panels;\r
168 };\r
169\r
170 _.extend(fn, Backbone.Events);\r
171\r
aa24a133
D
172 // Keep track of the active panel. Channel/query/server or applet\r
173 fn.bind('active', function (new_active_panel) {\r
27aafe0c 174 var previous_panel = active_panel;\r
aa24a133 175 active_panel = new_active_panel;\r
27aafe0c
D
176\r
177 _kiwi.global.events.emit('panel:active', {previous: previous_panel, active: active_panel});\r
aa24a133
D
178 });\r
179\r
6d5faa6e 180 return fn;\r
3dc62fbb 181 })(),\r
e4de4648
D
182\r
183\r
3dc62fbb 184 bindGatewayCommands: function (gw) {\r
645fe41b
JA
185 var that = this;\r
186\r
18ae1a72
D
187 // As soon as an IRC connection is made, show the full client UI\r
188 gw.on('connection:connect', function (event) {\r
696a66f8 189 that.view.barsShow();\r
696a66f8
D
190 });\r
191\r
192\r
24d27c8c
D
193 /**\r
194 * Handle the reconnections to the kiwi server\r
195 */\r
696a66f8 196 (function () {\r
d6eec6ed 197 // 0 = non-reconnecting state. 1 = reconnecting state.\r
696a66f8 198 var gw_stat = 0;\r
d6eec6ed 199\r
696a66f8 200 gw.on('disconnect', function (event) {\r
343cd967
PV
201 that.view.$el.removeClass('connected');\r
202\r
ab1e3748 203 // Reconnection phase will start to kick in\r
696a66f8
D
204 gw_stat = 1;\r
205 });\r
24d27c8c
D
206\r
207\r
696a66f8 208 gw.on('reconnecting', function (event) {\r
ab1e3748 209 var msg = translateText('client_models_application_reconnect_in_x_seconds', [event.delay/1000]) + '...';\r
24d27c8c
D
210\r
211 // Only need to mention the repeating re-connection messages on server panels\r
212 _kiwi.app.connections.forEach(function(connection) {\r
9ec48c54 213 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');\r
24d27c8c 214 });\r
696a66f8 215 });\r
24d27c8c
D
216\r
217\r
78354438
D
218 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event\r
219 gw.on('kiwi:connected', function (event) {\r
ab1e3748
D
220 var msg;\r
221\r
343cd967 222 that.view.$el.addClass('connected');\r
696a66f8 223\r
127d8196
D
224 // Make the rpc globally available for plugins\r
225 _kiwi.global.rpc = _kiwi.gateway.rpc;\r
226\r
227 _kiwi.global.events.emit('connected');\r
228\r
ab1e3748
D
229 // If we were reconnecting, show some messages we have connected back OK\r
230 if (gw_stat === 1) {\r
231\r
232 // No longer in the reconnection state\r
233 gw_stat = 0;\r
234\r
235 msg = translateText('client_models_application_reconnect_successfully') + ' :)';\r
09c26937 236 that.message.text(msg, {timeout: 5000});\r
696a66f8 237\r
ab1e3748
D
238 // Mention the re-connection on every channel\r
239 _kiwi.app.connections.forEach(function(connection) {\r
240 connection.reconnect();\r
78354438 241\r
ab1e3748 242 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
24d27c8c 243\r
ab1e3748
D
244 connection.panels.forEach(function(panel) {\r
245 if (!panel.isChannel())\r
246 return;\r
24d27c8c 247\r
ab1e3748
D
248 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');\r
249 });\r
24d27c8c 250 });\r
ab1e3748 251 }\r
696a66f8 252\r
696a66f8
D
253 });\r
254 })();\r
255\r
09c26937 256\r
645fe41b 257 gw.on('kiwi:reconfig', function () {\r
bceb5db6 258 $.getJSON(that.get('settings_path'), function (data) {\r
645fe41b
JA
259 that.server_settings = data.server_settings || {};\r
260 that.translations = data.translations || {};\r
261 });\r
262 });\r
09c26937
D
263\r
264\r
265 gw.on('kiwi:jumpserver', function (data) {\r
d6eec6ed
D
266 var serv;\r
267 // No server set? Then nowhere to jump to.\r
268 if (typeof data.kiwi_server === 'undefined')\r
269 return;\r
270\r
271 serv = data.kiwi_server;\r
272\r
273 // Strip any trailing slash from the end\r
274 if (serv[serv.length-1] === '/')\r
275 serv = serv.substring(0, serv.length-1);\r
276\r
09c26937
D
277 // Force the jumpserver now?\r
278 if (data.force) {\r
bf169745
D
279 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once\r
280 var jump_server_interval = Math.random() * (360 - 300) + 300;\r
ab1e3748 281 jump_server_interval = 1;\r
09c26937 282\r
bf169745 283 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect\r
a749d105 284 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
09c26937
D
285 that.message.text(msg, {timeout: 10000});\r
286\r
287 setTimeout(function forcedReconnect() {\r
a749d105 288 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();\r
09c26937
D
289 that.message.text(msg, {timeout: 8000});\r
290\r
291 setTimeout(function forcedReconnectPartTwo() {\r
89bbe76b 292 _kiwi.gateway.set('kiwi_server', serv);\r
338817f1 293\r
d6eec6ed
D
294 _kiwi.gateway.reconnect(function() {\r
295 // Reconnect all the IRC connections\r
296 that.connections.forEach(function(con){ con.reconnect(); });\r
297 });\r
09c26937
D
298 }, 5000);\r
299\r
300 }, jump_server_interval * 1000);\r
301 }\r
302 });\r
3dc62fbb 303 }\r
696a66f8 304\r
41cba2fd 305 });\r
696a66f8 306\r
3dc62fbb 307})();\r