forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.js
295 lines (239 loc) · 9.77 KB
/
resize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
var resize = (function () {
var exports = {};
var narrow_window = false;
function confine_to_range(lo, val, hi) {
if (val < lo) {
return lo;
}
if (val > hi) {
return hi;
}
return val;
}
function size_blocks(blocks, usable_height) {
var sum_height = 0;
_.each(blocks, function (block) {
sum_height += block.real_height;
});
_.each(blocks, function (block) {
var ratio = block.real_height / sum_height;
ratio = confine_to_range(0.05, ratio, 0.85);
block.max_height = confine_to_range(80, usable_height * ratio, 1.2 * block.real_height);
});
}
function set_user_list_heights(res, usable_height, user_presences, group_pms) {
// Calculate these heights:
// res.user_presences_max_height
// res.group_pms_max_height
var blocks = [
{
real_height: user_presences.prop('scrollHeight'),
},
{
real_height: group_pms.prop('scrollHeight'),
},
];
size_blocks(blocks, usable_height);
res.user_presences_max_height = blocks[0].max_height;
res.group_pms_max_height = blocks[1].max_height;
}
function get_new_heights() {
var res = {};
var viewport_height = message_viewport.height();
var top_navbar_height = $("#top_navbar").safeOuterHeight(true);
var invite_user_link_height = $("#invite-user-link").safeOuterHeight(true) || 0;
res.bottom_whitespace_height = viewport_height * 0.4;
res.main_div_min_height = viewport_height - top_navbar_height;
res.bottom_sidebar_height = viewport_height - top_navbar_height;
res.right_sidebar_height = viewport_height - parseInt($("#right-sidebar").css("marginTop"), 10);
res.stream_filters_max_height =
res.bottom_sidebar_height
- $("#global_filters").safeOuterHeight(true)
- $("#streams_header").safeOuterHeight(true)
- 10; // stream_filters margin-bottom
// Don't let us crush the stream sidebar completely out of view
res.stream_filters_max_height = Math.max(80, res.stream_filters_max_height);
// RIGHT SIDEBAR
var user_presences = $('#user_presences').expectOne();
var group_pms = $('#group-pms').expectOne();
var usable_height =
res.right_sidebar_height
- $("#feedback_section").safeOuterHeight(true)
- parseInt(user_presences.css("marginTop"),10)
- parseInt(user_presences.css("marginBottom"), 10)
- $("#userlist-header").safeOuterHeight(true)
- $(".user-list-filter").safeOuterHeight(true)
- invite_user_link_height
- parseInt(group_pms.css("marginTop"),10)
- parseInt(group_pms.css("marginBottom"), 10)
- $("#group-pm-header").safeOuterHeight(true);
// set these
// res.user_presences_max_height
// res.group_pms_max_height
set_user_list_heights(
res,
usable_height,
user_presences,
group_pms
);
return res;
}
function left_userlist_get_new_heights() {
var res = {};
var viewport_height = message_viewport.height();
var viewport_width = message_viewport.width();
var top_navbar_height = $(".header").safeOuterHeight(true);
var stream_filters = $('#stream_filters').expectOne();
var user_presences = $('#user_presences').expectOne();
var group_pms = $('#group-pms').expectOne();
var stream_filters_real_height = stream_filters.prop("scrollHeight");
var user_list_real_height = user_presences.prop("scrollHeight");
var group_pms_real_height = group_pms.prop("scrollHeight");
res.bottom_whitespace_height = viewport_height * 0.4;
res.main_div_min_height = viewport_height - top_navbar_height;
res.bottom_sidebar_height = viewport_height
- parseInt($("#left-sidebar").css("marginTop"),10)
- parseInt($(".bottom_sidebar").css("marginTop"),10);
res.total_leftlist_height = res.bottom_sidebar_height
- $("#global_filters").safeOuterHeight(true)
- $("#streams_header").safeOuterHeight(true)
- $("#userlist-header").safeOuterHeight(true)
- $(".user-list-filter").safeOuterHeight(true)
- $("#group-pm-header").safeOuterHeight(true)
- parseInt(stream_filters.css("marginBottom"),10)
- parseInt(user_presences.css("marginTop"), 10)
- parseInt(user_presences.css("marginBottom"), 10)
- parseInt(group_pms.css("marginTop"), 10)
- parseInt(group_pms.css("marginBottom"), 10)
- 15;
var blocks = [
{
real_height: stream_filters_real_height,
},
{
real_height: user_list_real_height,
},
{
real_height: group_pms_real_height,
},
];
size_blocks(blocks, res.total_leftlist_height);
res.stream_filters_max_height = blocks[0].max_height;
res.user_presences_max_height = blocks[1].max_height;
res.group_pms_max_height = blocks[2].max_height;
res.viewport_height = viewport_height;
res.viewport_width = viewport_width;
return res;
}
exports.watch_manual_resize = function (element) {
return (function on_box_resize(cb) {
var box = document.querySelector(element);
if (!box) {
blueslip.error('Bad selector in watch_manual_resize: ' + element);
return;
}
var meta = {
box: box,
height: null,
mousedown: false,
};
var box_handler = function () {
meta.mousedown = true;
meta.height = meta.box.clientHeight;
};
meta.box.addEventListener("mousedown", box_handler);
// If the user resizes the textarea manually, we use the
// callback to stop autosize from adjusting the height.
var body_handler = function () {
if (meta.mousedown === true) {
meta.mousedown = false;
if (meta.height !== meta.box.clientHeight) {
meta.height = meta.box.clientHeight;
cb.call(meta.box, meta.height);
}
}
};
document.body.addEventListener("mouseup", body_handler);
return [box_handler, body_handler];
}(function (height) {
// This callback disables autosize on the textarea. It
// will be re-enabled when this component is next opened.
$(element).trigger("autosize.destroy")
.height(height + "px");
}));
};
exports.resize_bottom_whitespace = function (h) {
if (page_params.autoscroll_forever) {
$("#bottom_whitespace").height($("#compose-container")[0].offsetHeight);
} else if (h !== undefined) {
$("#bottom_whitespace").height(h.bottom_whitespace_height);
}
};
exports.resize_stream_filters_container = function (h) {
h = narrow_window ? left_userlist_get_new_heights() : get_new_heights();
exports.resize_bottom_whitespace(h);
$("#stream-filters-container").css('max-height', h.stream_filters_max_height);
$('#stream-filters-container').perfectScrollbar('update');
};
exports.resize_page_components = function () {
var h;
var sidebar;
if (page_params.left_side_userlist) {
var css_narrow_mode = message_viewport.is_narrow();
$("#top_navbar").removeClass("rightside-userlist");
if (css_narrow_mode && !narrow_window) {
// move stuff to the left sidebar (skinny mode)
narrow_window = true;
popovers.set_userlist_placement("left");
sidebar = $(".bottom_sidebar").expectOne();
sidebar.append($("#user-list").expectOne());
sidebar.append($("#group-pm-list").expectOne());
$("#user_presences").css("margin", "0px");
$("#group-pms").css("margin", "0px");
$("#userlist-toggle").css("display", "none");
$("#invite-user-link").hide();
} else if (!css_narrow_mode && narrow_window) {
// move stuff to the right sidebar (wide mode)
narrow_window = false;
popovers.set_userlist_placement("right");
sidebar = $("#right-sidebar").expectOne();
sidebar.append($("#user-list").expectOne());
sidebar.append($("#group-pm-list").expectOne());
$("#user_presences").css("margin", '');
$("#group-pms").css("margin", '');
$("#userlist-toggle").css("display", '');
$("#invite-user-link").show();
}
}
h = narrow_window ? left_userlist_get_new_heights() : get_new_heights();
exports.resize_bottom_whitespace(h);
$("#user_presences").css('max-height', h.user_presences_max_height);
$("#group-pms").css('max-height', h.group_pms_max_height);
$("#stream-filters-container")
.css('max-height', h.stream_filters_max_height)
// the `.css` method returns `$this`, so we can chain `perfectScrollbar`.
.perfectScrollbar('update');
activity.update_scrollbar.users();
activity.update_scrollbar.group_pms();
};
var _old_width = $(window).width();
exports.handler = function () {
var new_width = $(window).width();
if (new_width !== _old_width) {
_old_width = new_width;
condense.clear_message_content_height_cache();
}
popovers.hide_all();
exports.resize_page_components();
// This function might run onReady (if we're in a narrow window),
// but before we've loaded in the messages; in that case, don't
// try to scroll to one.
if (current_msg_list.selected_id() !== -1) {
navigate.scroll_to_selected();
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = resize;
}