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