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