Commit | Line | Data |
---|---|---|
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 | |
05934d33 JA |
54 | if (!theme_name) theme_name = _kiwi.global.settings.get('theme') || 'relaxed'; |
55 | ||
56 | theme_name = theme_name.toLowerCase(); | |
50ac472f D |
57 | |
58 | // Clear any current theme | |
05934d33 JA |
59 | $('[data-theme][rel="stylesheet"]').each(function (idx, link) { |
60 | var $link = $(link); | |
61 | $link.attr('rel', 'alternate ' + $link.attr('rel'))[0].disabled = true; | |
50ac472f D |
62 | }); |
63 | ||
64 | // Apply the new theme | |
05934d33 JA |
65 | //this.$el.addClass('theme_' + (theme_name || 'relaxed')); |
66 | var link = $('[data-theme][title=' + theme_name + ']'); | |
67 | if (link.length > 0) { | |
68 | link.attr('rel', 'stylesheet')[0].disabled = false; | |
69 | } | |
50ac472f D |
70 | }, |
71 | ||
72 | ||
73 | setTabLayout: function (layout_style) { | |
74 | // If called by the settings callback, get the correct new_value | |
75 | if (layout_style === _kiwi.global.settings) { | |
76 | layout_style = arguments[1]; | |
77 | } | |
ee2f0962 | 78 | |
50ac472f D |
79 | if (layout_style == 'list') { |
80 | this.$el.addClass('chanlist_treeview'); | |
81 | } else { | |
82 | this.$el.removeClass('chanlist_treeview'); | |
83 | } | |
ee2f0962 | 84 | |
50ac472f D |
85 | this.doLayout(); |
86 | }, | |
87 | ||
88 | ||
89 | displayTimestamps: function (show_timestamps) { | |
90 | // If called by the settings callback, get the correct new_value | |
91 | if (show_timestamps === _kiwi.global.settings) { | |
92 | show_timestamps = arguments[1]; | |
93 | } | |
94 | ||
95 | if (show_timestamps) { | |
96 | this.$el.addClass('timestamps'); | |
97 | } else { | |
98 | this.$el.removeClass('timestamps'); | |
99 | } | |
100 | }, | |
101 | ||
102 | ||
103 | // Globally shift focus to the command input box on a keypress | |
104 | setKeyFocus: function (ev) { | |
105 | // If we're copying text, don't shift focus | |
106 | if (ev.ctrlKey || ev.altKey || ev.metaKey) { | |
107 | return; | |
108 | } | |
109 | ||
110 | // If we're typing into an input box somewhere, ignore | |
111 | if ((ev.target.tagName.toLowerCase() === 'input') || (ev.target.tagName.toLowerCase() === 'textarea') || $(ev.target).attr('contenteditable')) { | |
112 | return; | |
113 | } | |
114 | ||
115 | $('#kiwi .controlbox .inp').focus(); | |
116 | }, | |
117 | ||
118 | ||
119 | doLayout: function () { | |
120 | var el_kiwi = this.$el; | |
121 | var el_panels = $('#kiwi .panels'); | |
122 | var el_memberlists = $('#kiwi .memberlists'); | |
123 | var el_toolbar = this.$el.find('.toolbar'); | |
124 | var el_controlbox = $('#kiwi .controlbox'); | |
125 | var el_resize_handle = $('#kiwi .memberlists_resize_handle'); | |
126 | ||
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 | }); |