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