1 _kiwi
.view
.Application
= Backbone
.View
.extend({
2 initialize: function () {
5 this.$el
= $($('#tmpl_application').html().trim());
8 $(this.model
.get('container') || 'body').append(this.$el
);
11 panels
: this.$el
.find('.panels'),
12 right_bar
: this.$el
.find('.right_bar'),
13 toolbar
: this.$el
.find('.toolbar'),
14 controlbox
: this.$el
.find('.controlbox'),
15 resize_handle
: this.$el
.find('.memberlists_resize_handle')
18 $(window
).resize(function() { that
.doLayout
.apply(that
); });
19 this.elements
.toolbar
.resize(function() { that
.doLayout
.apply(that
); });
20 this.elements
.controlbox
.resize(function() { that
.doLayout
.apply(that
); });
22 // Change the theme when the config is changed
23 _kiwi
.global
.settings
.on('change:theme', this.updateTheme
, this);
24 this.updateTheme(getQueryVariable('theme'));
26 _kiwi
.global
.settings
.on('change:channel_list_style', this.setTabLayout
, this);
27 this.setTabLayout(_kiwi
.global
.settings
.get('channel_list_style'));
29 _kiwi
.global
.settings
.on('change:show_timestamps', this.displayTimestamps
, this);
30 this.displayTimestamps(_kiwi
.global
.settings
.get('show_timestamps'));
32 this.$el
.appendTo($('body'));
35 $(document
).keydown(this.setKeyFocus
);
37 // Confirmation require to leave the page
38 window
.onbeforeunload = function () {
39 if (_kiwi
.gateway
.isConnected()) {
40 return _kiwi
.global
.i18n
.translate('client_views_application_close_notice').fetch();
44 // Keep tabs on the browser having focus
45 this.has_focus
= true;
47 $(window
).on('focus', function () {
48 that
.has_focus
= true;
50 $(window
).on('blur', function () {
51 that
.has_focus
= false;
55 this.favicon
= new _kiwi
.view
.Favicon();
61 updateTheme: function (theme_name
) {
62 // If called by the settings callback, get the correct new_value
63 if (theme_name
=== _kiwi
.global
.settings
) {
64 theme_name
= arguments
[1];
67 // If we have no theme specified, get it from the settings
68 if (!theme_name
) theme_name
= _kiwi
.global
.settings
.get('theme') || 'relaxed';
70 theme_name
= theme_name
.toLowerCase();
72 // Clear any current theme
73 $('[data-theme]:not([disabled])').each(function (idx
, link
) {
75 $link
.attr('rel', 'alternate ' + $link
.attr('rel')).attr('disabled', true)[0].disabled
= true;
78 // Apply the new theme
79 var link
= $('[data-theme][title=' + theme_name
+ ']');
80 if (link
.length
> 0) {
81 link
.attr('rel', 'stylesheet').attr('disabled', false)[0].disabled
= false;
88 setTabLayout: function (layout_style
) {
89 // If called by the settings callback, get the correct new_value
90 if (layout_style
=== _kiwi
.global
.settings
) {
91 layout_style
= arguments
[1];
94 if (layout_style
== 'list') {
95 this.$el
.addClass('chanlist_treeview');
97 this.$el
.removeClass('chanlist_treeview');
104 displayTimestamps: function (show_timestamps
) {
105 // If called by the settings callback, get the correct new_value
106 if (show_timestamps
=== _kiwi
.global
.settings
) {
107 show_timestamps
= arguments
[1];
110 if (show_timestamps
) {
111 this.$el
.addClass('timestamps');
113 this.$el
.removeClass('timestamps');
118 // Globally shift focus to the command input box on a keypress
119 setKeyFocus: function (ev
) {
120 // If we're copying text, don't shift focus
121 if (ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
125 // If we're typing into an input box somewhere, ignore
126 if ((ev
.target
.tagName
.toLowerCase() === 'input') || (ev
.target
.tagName
.toLowerCase() === 'textarea') || $(ev
.target
).attr('contenteditable')) {
130 $('#kiwi .controlbox .inp').focus();
134 doLayout: function () {
135 var el_kiwi
= this.$el
;
136 var el_panels
= this.elements
.panels
;
137 var el_right_bar
= this.elements
.right_bar
;
138 var el_toolbar
= this.elements
.toolbar
;
139 var el_controlbox
= this.elements
.controlbox
;
140 var el_resize_handle
= this.elements
.resize_handle
;
142 if (!el_kiwi
.is(':visible')) {
147 top
: el_toolbar
.outerHeight(true),
148 bottom
: el_controlbox
.outerHeight(true)
152 // If any elements are not visible, full size the panals instead
153 if (!el_toolbar
.is(':visible')) {
157 if (!el_controlbox
.is(':visible')) {
158 css_heights
.bottom
= 0;
161 // Apply the CSS sizes
162 el_panels
.css(css_heights
);
163 el_right_bar
.css(css_heights
);
164 el_resize_handle
.css(css_heights
);
166 // If we have channel tabs on the side, adjust the height
167 if (el_kiwi
.hasClass('chanlist_treeview')) {
168 this.$el
.find('.tabs', el_kiwi
).css(css_heights
);
171 // Determine if we have a narrow window (mobile/tablet/or even small desktop window)
172 if (el_kiwi
.outerWidth() < 400) {
173 el_kiwi
.addClass('narrow');
175 el_kiwi
.removeClass('narrow');
178 // Set the panels width depending on the memberlist visibility
179 if (el_right_bar
.css('display') != 'none') {
180 // Panels to the side of the memberlist
181 el_panels
.css('right', el_right_bar
.outerWidth(true));
182 // The resize handle sits overlapping the panels and memberlist
183 el_resize_handle
.css('left', el_right_bar
.position().left
- (el_resize_handle
.outerWidth(true) / 2));
185 // Memberlist is hidden so panels to the right edge
186 el_panels
.css('right', 0);
187 // And move the handle just out of sight to the right
188 el_resize_handle
.css('left', el_panels
.outerWidth(true));
191 var input_wrap_width
= parseInt($('#kiwi .controlbox .input_tools').outerWidth());
192 el_controlbox
.find('.input_wrap').css('right', input_wrap_width
+ 7);
196 alertWindow: function (title
) {
197 if (!this.alertWindowTimer
) {
198 this.alertWindowTimer
= new (function () {
201 var has_focus
= true;
203 var default_title
= _kiwi
.app
.server_settings
.client
.window_title
|| 'Kiwi IRC';
204 var title
= 'Kiwi IRC';
206 this.setTitle = function (new_title
) {
207 new_title
= new_title
|| default_title
;
208 window
.document
.title
= new_title
;
212 this.start = function (new_title
) {
213 // Don't alert if we already have focus
214 if (has_focus
) return;
218 tmr
= setInterval(this.update
, 1000);
221 this.stop = function () {
222 // Stop the timer and clear the title
223 if (tmr
) clearInterval(tmr
);
227 // Some browsers don't always update the last title correctly
228 // Wait a few seconds and then reset
229 setTimeout(this.reset
, 2000);
232 this.reset = function () {
238 this.update = function () {
240 that
.setTitle(title
);
248 $(window
).focus(function (event
) {
252 // Some browsers don't always update the last title correctly
253 // Wait a few seconds and then reset
254 setTimeout(that
.reset
, 2000);
257 $(window
).blur(function (event
) {
263 this.alertWindowTimer
.start(title
);
267 barsHide: function (instant
) {
271 this.$el
.find('.toolbar').slideUp({queue
: false, duration
: 400, step
: $.proxy(this.doLayout
, this)});
272 $('#kiwi .controlbox').slideUp({queue
: false, duration
: 400, step
: $.proxy(this.doLayout
, this)});
274 this.$el
.find('.toolbar').slideUp(0);
275 $('#kiwi .controlbox').slideUp(0);
280 barsShow: function (instant
) {
284 this.$el
.find('.toolbar').slideDown({queue
: false, duration
: 400, step
: $.proxy(this.doLayout
, this)});
285 $('#kiwi .controlbox').slideDown({queue
: false, duration
: 400, step
: $.proxy(this.doLayout
, this)});
287 this.$el
.find('.toolbar').slideDown(0);
288 $('#kiwi .controlbox').slideDown(0);
294 initSound: function () {
296 base_path
= this.model
.get('base_path');
298 $script(base_path
+ '/assets/libs/soundmanager2/soundmanager2-nodebug-jsmin.js', function() {
299 if (typeof soundManager
=== 'undefined')
303 url
: base_path
+ '/assets/libs/soundmanager2/',
304 flashVersion
: 9, // optional: shiny features (default = 8)// optional: ignore Flash where possible, use 100% HTML5 mode
307 onready: function() {
308 that
.sound_object
= soundManager
.createSound({
310 url
: base_path
+ '/assets/sound/highlight.mp3'
318 playSound: function (sound_id
) {
319 if (!this.sound_object
) return;
321 if (_kiwi
.global
.settings
.get('mute_sounds'))
324 soundManager
.play(sound_id
);
328 showNotification: function(title
, message
) {
329 var icon
= this.model
.get('base_path') + '/assets/img/ico.png';
331 // Check if we have notification support
332 if (!window
.webkitNotifications
)
338 if (webkitNotifications
.checkPermission() === 0){
339 window
.webkitNotifications
.createNotification(icon
, title
, message
).show();