Clickable channels in chanlist applet
[KiwiIRC.git] / client / assets / src / views / application.js
CommitLineData
50ac472f
D
1_kiwi.view.Application = Backbone.View.extend({
2 initialize: function () {
3 var that = this;
4
5 $(window).resize(function() { that.doLayout.apply(that); });
6 this.$el.find('.toolbar').resize(function() { that.doLayout.apply(that); });
7 $('#kiwi .controlbox').resize(function() { that.doLayout.apply(that); });
8
9 // Change the theme when the config is changed
10 _kiwi.global.settings.on('change:theme', this.updateTheme, this);
11 this.updateTheme(getQueryVariable('theme'));
12
13 _kiwi.global.settings.on('change:channel_list_style', this.setTabLayout, this);
14 this.setTabLayout(_kiwi.global.settings.get('channel_list_style'));
15
16 _kiwi.global.settings.on('change:show_timestamps', this.displayTimestamps, this);
17 this.displayTimestamps(_kiwi.global.settings.get('show_timestamps'));
18
19 this.doLayout();
20
21 $(document).keydown(this.setKeyFocus);
22
23 // Confirmation require to leave the page
24 window.onbeforeunload = function () {
25 if (_kiwi.gateway.isConnected()) {
247dd7ac 26 return _kiwi.global.i18n.translate('client_views_application_close_notice').fetch();
50ac472f
D
27 }
28 };
29
ee2f0962
D
30 // Keep tabs on the browser having focus
31 this.has_focus = true;
32
33 $(window).on('focus', function () {
34 that.has_focus = true;
35 });
36 $(window).on('blur', function () {
37 that.has_focus = false;
38 });
39
40
0b7949de 41 this.favicon = new _kiwi.view.Favicon();
50ac472f
D
42 this.initSound();
43 },
44
45
46
47 updateTheme: function (theme_name) {
48 // If called by the settings callback, get the correct new_value
49 if (theme_name === _kiwi.global.settings) {
50 theme_name = arguments[1];
51 }
52
53 // If we have no theme specified, get it from the settings
54 if (!theme_name) theme_name = _kiwi.global.settings.get('theme');
55
56 // Clear any current theme
57 this.$el.removeClass(function (i, css) {
58 return (css.match(/\btheme_\S+/g) || []).join(' ');
59 });
60
61 // Apply the new theme
62 this.$el.addClass('theme_' + (theme_name || 'relaxed'));
63 },
64
65
66 setTabLayout: function (layout_style) {
67 // If called by the settings callback, get the correct new_value
68 if (layout_style === _kiwi.global.settings) {
69 layout_style = arguments[1];
70 }
ee2f0962 71
50ac472f
D
72 if (layout_style == 'list') {
73 this.$el.addClass('chanlist_treeview');
74 } else {
75 this.$el.removeClass('chanlist_treeview');
76 }
ee2f0962 77
50ac472f
D
78 this.doLayout();
79 },
80
81
82 displayTimestamps: function (show_timestamps) {
83 // If called by the settings callback, get the correct new_value
84 if (show_timestamps === _kiwi.global.settings) {
85 show_timestamps = arguments[1];
86 }
87
88 if (show_timestamps) {
89 this.$el.addClass('timestamps');
90 } else {
91 this.$el.removeClass('timestamps');
92 }
93 },
94
95
96 // Globally shift focus to the command input box on a keypress
97 setKeyFocus: function (ev) {
98 // If we're copying text, don't shift focus
99 if (ev.ctrlKey || ev.altKey || ev.metaKey) {
100 return;
101 }
102
103 // If we're typing into an input box somewhere, ignore
104 if ((ev.target.tagName.toLowerCase() === 'input') || (ev.target.tagName.toLowerCase() === 'textarea') || $(ev.target).attr('contenteditable')) {
105 return;
106 }
107
108 $('#kiwi .controlbox .inp').focus();
109 },
110
111
112 doLayout: function () {
113 var el_kiwi = this.$el;
114 var el_panels = $('#kiwi .panels');
115 var el_memberlists = $('#kiwi .memberlists');
116 var el_toolbar = this.$el.find('.toolbar');
117 var el_controlbox = $('#kiwi .controlbox');
118 var el_resize_handle = $('#kiwi .memberlists_resize_handle');
119
120 var css_heights = {
121 top: el_toolbar.outerHeight(true),
122 bottom: el_controlbox.outerHeight(true)
123 };
124
125
126 // If any elements are not visible, full size the panals instead
127 if (!el_toolbar.is(':visible')) {
128 css_heights.top = 0;
129 }
130
131 if (!el_controlbox.is(':visible')) {
132 css_heights.bottom = 0;
133 }
134
135 // Apply the CSS sizes
136 el_panels.css(css_heights);
137 el_memberlists.css(css_heights);
138 el_resize_handle.css(css_heights);
139
140 // If we have channel tabs on the side, adjust the height
141 if (el_kiwi.hasClass('chanlist_treeview')) {
142 this.$el.find('.tabs', el_kiwi).css(css_heights);
143 }
144
145 // Determine if we have a narrow window (mobile/tablet/or even small desktop window)
146 if (el_kiwi.outerWidth() < 400) {
147 el_kiwi.addClass('narrow');
148 } else {
149 el_kiwi.removeClass('narrow');
150 }
151
152 // Set the panels width depending on the memberlist visibility
153 if (el_memberlists.css('display') != 'none') {
154 // Panels to the side of the memberlist
155 el_panels.css('right', el_memberlists.outerWidth(true));
156 // The resize handle sits overlapping the panels and memberlist
157 el_resize_handle.css('left', el_memberlists.position().left - (el_resize_handle.outerWidth(true) / 2));
158 } else {
159 // Memberlist is hidden so panels to the right edge
160 el_panels.css('right', 0);
161 // And move the handle just out of sight to the right
162 el_resize_handle.css('left', el_panels.outerWidth(true));
163 }
164
165 var input_wrap_width = parseInt($('#kiwi .controlbox .input_tools').outerWidth());
166 el_controlbox.find('.input_wrap').css('right', input_wrap_width + 7);
167 },
168
169
170 alertWindow: function (title) {
171 if (!this.alertWindowTimer) {
172 this.alertWindowTimer = new (function () {
173 var that = this;
174 var tmr;
175 var has_focus = true;
176 var state = 0;
177 var default_title = 'Kiwi IRC';
178 var title = 'Kiwi IRC';
179
180 this.setTitle = function (new_title) {
181 new_title = new_title || default_title;
182 window.document.title = new_title;
183 return new_title;
184 };
185
186 this.start = function (new_title) {
187 // Don't alert if we already have focus
188 if (has_focus) return;
189
190 title = new_title;
191 if (tmr) return;
192 tmr = setInterval(this.update, 1000);
193 };
194
195 this.stop = function () {
196 // Stop the timer and clear the title
197 if (tmr) clearInterval(tmr);
198 tmr = null;
199 this.setTitle();
200
201 // Some browsers don't always update the last title correctly
202 // Wait a few seconds and then reset
203 setTimeout(this.reset, 2000);
204 };
205
206 this.reset = function () {
207 if (tmr) return;
208 that.setTitle();
209 };
210
211
212 this.update = function () {
213 if (state === 0) {
214 that.setTitle(title);
215 state = 1;
216 } else {
217 that.setTitle();
218 state = 0;
219 }
220 };
221
222 $(window).focus(function (event) {
223 has_focus = true;
224 that.stop();
225
226 // Some browsers don't always update the last title correctly
227 // Wait a few seconds and then reset
228 setTimeout(that.reset, 2000);
229 });
230
231 $(window).blur(function (event) {
232 has_focus = false;
233 });
234 })();
235 }
236
237 this.alertWindowTimer.start(title);
238 },
239
240
241 barsHide: function (instant) {
242 var that = this;
243
244 if (!instant) {
245 this.$el.find('.toolbar').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
246 $('#kiwi .controlbox').slideUp({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
247 } else {
248 this.$el.find('.toolbar').slideUp(0);
249 $('#kiwi .controlbox').slideUp(0);
250 this.doLayout();
251 }
252 },
253
254 barsShow: function (instant) {
255 var that = this;
256
257 if (!instant) {
258 this.$el.find('.toolbar').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
259 $('#kiwi .controlbox').slideDown({queue: false, duration: 400, step: $.proxy(this.doLayout, this)});
260 } else {
261 this.$el.find('.toolbar').slideDown(0);
262 $('#kiwi .controlbox').slideDown(0);
263 this.doLayout();
264 }
265 },
266
267
268 initSound: function () {
269 var that = this,
270 base_path = this.model.get('base_path');
271
272 $script(base_path + '/assets/libs/soundmanager2/soundmanager2-nodebug-jsmin.js', function() {
273 if (typeof soundManager === 'undefined')
274 return;
275
276 soundManager.setup({
277 url: base_path + '/assets/libs/soundmanager2/',
278 flashVersion: 9, // optional: shiny features (default = 8)// optional: ignore Flash where possible, use 100% HTML5 mode
279 preferFlash: true,
280
281 onready: function() {
282 that.sound_object = soundManager.createSound({
283 id: 'highlight',
284 url: base_path + '/assets/sound/highlight.mp3'
285 });
286 }
287 });
288 });
289 },
290
291
292 playSound: function (sound_id) {
293 if (!this.sound_object) return;
294
295 if (_kiwi.global.settings.get('mute_sounds'))
296 return;
ee2f0962 297
50ac472f 298 soundManager.play(sound_id);
d70c63d4
K
299 },
300
ee2f0962
D
301
302 showNotification: function(title, message) {
303 var icon = this.model.get('base_path') + '/assets/img/ico.png';
304
305 // Check if we have notification support
306 if (!window.webkitNotifications)
307 return;
308
309 if (this.has_focus)
310 return;
311
312 if (webkitNotifications.checkPermission() === 0){
313 window.webkitNotifications.createNotification(icon, title, message).show();
314 }
50ac472f 315 }
d70c63d4 316});