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