Extracting client user commands from application.js
[KiwiIRC.git] / client / src / models / application.js
1 (function () {
2
3 _kiwi.model.Application = Backbone.Model.extend({
4 /** _kiwi.view.Application */
5 view: null,
6
7 /** _kiwi.view.StatusMessage */
8 message: null,
9
10 /* Address for the kiwi server */
11 kiwi_server: null,
12
13 initialize: function (options) {
14 if (options.container) {
15 this.set('container', options.container);
16 }
17
18 // The base url to the kiwi server
19 this.set('base_path', options.base_path ? options.base_path : '');
20
21 // Path for the settings.json file
22 this.set('settings_path', options.settings_path ?
23 options.settings_path :
24 this.get('base_path') + '/assets/settings.json'
25 );
26
27 // Any options sent down from the server
28 this.server_settings = options.server_settings || {};
29 this.translations = options.translations || {};
30 this.themes = options.themes || [];
31 this.text_theme = options.text_theme || {};
32
33 // Best guess at where the kiwi server is if not already specified
34 this.kiwi_server = options.kiwi_server || this.detectKiwiServer();
35
36 // The applet to initially load
37 this.startup_applet_name = options.startup || 'kiwi_startup';
38
39 // Set any default settings before anything else is applied
40 if (this.server_settings && this.server_settings.client && this.server_settings.client.settings) {
41 this.applyDefaultClientSettings(this.server_settings.client.settings);
42 }
43 },
44
45
46 initializeInterfaces: function () {
47 // Set the gateway up
48 _kiwi.gateway = new _kiwi.model.Gateway();
49 this.bindGatewayCommands(_kiwi.gateway);
50
51 this.initializeClient();
52 this.initializeGlobals();
53
54 this.view.barsHide(true);
55 },
56
57
58 detectKiwiServer: function () {
59 // If running from file, default to localhost:7777 by default
60 if (window.location.protocol === 'file:') {
61 return 'http://localhost:7778';
62 } else {
63 // Assume the kiwi server is on the same server
64 return window.location.protocol + '//' + window.location.host;
65 }
66 },
67
68
69 showStartup: function() {
70 this.startup_applet = _kiwi.model.Applet.load(this.startup_applet_name, {no_tab: true});
71 this.startup_applet.tab = this.view.$('.console');
72 this.startup_applet.view.show();
73 },
74
75
76 initializeClient: function () {
77 this.view = new _kiwi.view.Application({model: this, el: this.get('container')});
78
79 // Takes instances of model_network
80 this.connections = new _kiwi.model.NetworkPanelList();
81
82 // Applets panel list
83 this.applet_panels = new _kiwi.model.PanelList();
84 this.applet_panels.view.$el.addClass('panellist applets');
85 this.view.$el.find('.tabs').append(this.applet_panels.view.$el);
86
87 /**
88 * Set the UI components up
89 */
90 this.controlbox = new _kiwi.view.ControlBox({el: $('#kiwi .controlbox')[0]});
91 this.client_ui_commands = new _kiwi.misc.ClientUiCommands(this, this.controlbox);
92
93 this.rightbar = new _kiwi.view.RightBar({el: this.view.$('.right_bar')[0]});
94 this.topicbar = new _kiwi.view.TopicBar({el: this.view.$el.find('.topic')[0]});
95
96 new _kiwi.view.AppToolbar({el: _kiwi.app.view.$el.find('.toolbar .app_tools')[0]});
97 new _kiwi.view.ChannelTools({el: _kiwi.app.view.$el.find('.channel_tools')[0]});
98
99 this.message = new _kiwi.view.StatusMessage({el: this.view.$el.find('.status_message')[0]});
100
101 this.resize_handle = new _kiwi.view.ResizeHandler({el: this.view.$el.find('.memberlists_resize_handle')[0]});
102
103 // Rejigg the UI sizes
104 this.view.doLayout();
105 },
106
107
108 initializeGlobals: function () {
109 _kiwi.global.connections = this.connections;
110
111 _kiwi.global.panels = this.panels;
112 _kiwi.global.panels.applets = this.applet_panels;
113
114 _kiwi.global.components.Applet = _kiwi.model.Applet;
115 _kiwi.global.components.Panel =_kiwi.model.Panel;
116 _kiwi.global.components.MenuBox = _kiwi.view.MenuBox;
117 },
118
119
120 applyDefaultClientSettings: function (settings) {
121 _.each(settings, function (value, setting) {
122 if (typeof _kiwi.global.settings.get(setting) === 'undefined') {
123 _kiwi.global.settings.set(setting, value);
124 }
125 });
126 },
127
128
129 panels: (function() {
130 var active_panel;
131
132 var fn = function(panel_type) {
133 var panels;
134
135 // Default panel type
136 panel_type = panel_type || 'connections';
137
138 switch (panel_type) {
139 case 'connections':
140 panels = this.connections.panels();
141 break;
142 case 'applets':
143 panels = this.applet_panels.models;
144 break;
145 }
146
147 // Active panels / server
148 panels.active = active_panel;
149 panels.server = this.connections.active_connection ?
150 this.connections.active_connection.panels.server :
151 null;
152
153 return panels;
154 };
155
156 _.extend(fn, Backbone.Events);
157
158 // Keep track of the active panel. Channel/query/server or applet
159 fn.bind('active', function (new_active_panel) {
160 active_panel = new_active_panel;
161 });
162
163 return fn;
164 })(),
165
166
167 bindGatewayCommands: function (gw) {
168 var that = this;
169
170 // As soon as an IRC connection is made, show the full client UI
171 gw.on('connection:connect', function (event) {
172 that.view.barsShow();
173 });
174
175
176 /**
177 * Handle the reconnections to the kiwi server
178 */
179 (function () {
180 // 0 = non-reconnecting state. 1 = reconnecting state.
181 var gw_stat = 0;
182
183 // If the current or upcoming disconnect was planned
184 var unplanned_disconnect = false;
185
186 gw.on('disconnect', function (event) {
187 unplanned_disconnect = !gw.disconnect_requested;
188
189 if (unplanned_disconnect) {
190 var msg = _kiwi.global.i18n.translate('client_models_application_reconnecting').fetch() + '...';
191 that.message.text(msg, {timeout: 10000});
192 }
193
194 that.view.$el.removeClass('connected');
195
196 // Mention the disconnection on every channel
197 _kiwi.app.connections.forEach(function(connection) {
198 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');
199
200 connection.panels.forEach(function(panel) {
201 if (!panel.isChannel())
202 return;
203
204 panel.addMsg('', styleText('quit', {text: msg}), 'action quit');
205 });
206 });
207
208 gw_stat = 1;
209 });
210
211
212 gw.on('reconnecting', function (event) {
213 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_in_x_seconds').fetch(event.delay/1000) + '...';
214
215 // Only need to mention the repeating re-connection messages on server panels
216 _kiwi.app.connections.forEach(function(connection) {
217 connection.panels.server.addMsg('', styleText('quit', {text: msg}), 'action quit');
218 });
219 });
220
221
222 // After the socket has connected, kiwi handshakes and then triggers a kiwi:connected event
223 gw.on('kiwi:connected', function (event) {
224 that.view.$el.addClass('connected');
225 if (gw_stat !== 1) return;
226
227 if (unplanned_disconnect) {
228 var msg = _kiwi.global.i18n.translate('client_models_application_reconnect_successfully').fetch() + ':)';
229 that.message.text(msg, {timeout: 5000});
230 }
231
232 // Mention the re-connection on every channel
233 _kiwi.app.connections.forEach(function(connection) {
234 connection.reconnect();
235
236 connection.panels.server.addMsg('', styleText('rejoin', {text: msg}), 'action join');
237
238 connection.panels.forEach(function(panel) {
239 if (!panel.isChannel())
240 return;
241
242 panel.addMsg('', styleText('rejoin', {text: msg}), 'action join');
243 });
244 });
245
246 gw_stat = 0;
247 });
248 })();
249
250
251 gw.on('kiwi:reconfig', function () {
252 $.getJSON(that.get('settings_path'), function (data) {
253 that.server_settings = data.server_settings || {};
254 that.translations = data.translations || {};
255 });
256 });
257
258
259 gw.on('kiwi:jumpserver', function (data) {
260 var serv;
261 // No server set? Then nowhere to jump to.
262 if (typeof data.kiwi_server === 'undefined')
263 return;
264
265 serv = data.kiwi_server;
266
267 // Strip any trailing slash from the end
268 if (serv[serv.length-1] === '/')
269 serv = serv.substring(0, serv.length-1);
270
271 // Force the jumpserver now?
272 if (data.force) {
273 // Get an interval between 5 and 6 minutes so everyone doesn't reconnect it all at once
274 var jump_server_interval = Math.random() * (360 - 300) + 300;
275
276 // Tell the user we are going to disconnect, wait 5 minutes then do the actual reconnect
277 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();
278 that.message.text(msg, {timeout: 10000});
279
280 setTimeout(function forcedReconnect() {
281 var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_reconnect').fetch();
282 that.message.text(msg, {timeout: 8000});
283
284 setTimeout(function forcedReconnectPartTwo() {
285 _kiwi.app.kiwi_server = serv;
286
287 _kiwi.gateway.reconnect(function() {
288 // Reconnect all the IRC connections
289 that.connections.forEach(function(con){ con.reconnect(); });
290 });
291 }, 5000);
292
293 }, jump_server_interval * 1000);
294 }
295 });
296 }
297
298 });
299
300 })();