Skip to content

Commit 17ffe87

Browse files
committed
Tidy splitter mouse callback code
1 parent d43c2b8 commit 17ffe87

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

browser/static/src_browser.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ const path_type_to_class = {
161161
// Fetches the file tree from the server and schedules a continuation
162162
// that displays the file selector dropdown(s).
163163
async function renderPage() {
164-
initDrag('initial');
164+
setSplitterDragActions('initial');
165165
// TODO: figure out new proportional height from resize
166-
window.onresize = () => initDrag('resize');
166+
window.onresize = () => setSplitterDragActions('resize');
167167
await fetchFromServer({src_file_tree: ''},
168168
file_tree_from_server => setFileTree(
169169
file_tree_from_server,
170170
SourceItem.newFromSearch()));
171171
}
172172

173-
function initDrag(debug_reason) {
173+
// Set the mouse action (down,move,up) callbacks. When the mouse-down
174+
// happens, record the location (in variable `md`, which becomes the
175+
// context for the mouse actions; when the mouse-move happens, set the
176+
// splitter location based on the initial mouse location (`md`) and
177+
// where the mouse is now.
178+
function setSplitterDragActions(debug_reason) {
174179
const src_elem = document.getElementById('src');
175180
const splitter_elem = document.getElementById('splitter');
176181
const xref_elem = document.getElementById('xref');
@@ -202,12 +207,7 @@ function initDrag(debug_reason) {
202207
});
203208
}
204209

205-
document.onmouseup = () => {
206-
document.onmousemove = null;
207-
document.onmouseup = null;
208-
}
209-
210-
document.onmousemove = (e) => {
210+
function adjustSplitterLocation(e) {
211211
var delta_y = e.clientY - md.e.clientY;
212212

213213
// Prevent negative-sized elements.
@@ -216,6 +216,8 @@ function initDrag(debug_reason) {
216216
// units such as pt or cm to px, but by experiment, 20
217217
// seems a reasonable amount to leave.
218218

219+
const negative_space_buffer = 20;
220+
219221
// TODO: The following isn't quite right at the bottom: if
220222
// you move the splitter as far as possible,
221223
// mouse-up, then move it again, you can make the
@@ -227,18 +229,28 @@ function initDrag(debug_reason) {
227229
// const scrollbarWidth = window.innerWidth - document.body.clientWidth
228230
// https://destroytoday.com/blog/100vw-and-the-horizontal-overflow-you-probably-didnt-know-about
229231

230-
if (md.srcHeight + delta_y < 20) {
231-
delta_y = md.srcTop - md.splitterTop + 20;
232+
if (md.srcHeight + delta_y < negative_space_buffer) {
233+
delta_y = md.srcTop - md.splitterTop + negative_space_buffer;
232234
}
233-
if (md.xrefHeight - delta_y < 20) {
234-
delta_y = md.splitterHeight + md.xrefHeight - 20;
235+
if (md.xrefHeight - delta_y < negative_space_buffer) {
236+
delta_y = md.splitterHeight + md.xrefHeight - negative_space_buffer;
235237
}
236238

237239
src_elem.style.height = (md.srcHeight + delta_y) + 'px';
238240
splitter_elem.style.top = (md.splitterTop + delta_y) + 'px';
239241
xref_elem.style.height = (md.xrefHeight - delta_y) + 'px';
240242
xref_elem.style.top = (md.xrefTop + delta_y) + 'px'; // Not needed for grid layout?
241243
}
244+
245+
document.onmouseup = (e) => {
246+
adjustSplitterLocation(e);
247+
document.onmousemove = null;
248+
document.onmouseup = null;
249+
}
250+
251+
document.onmousemove = (e) => {
252+
adjustSplitterLocation(e);
253+
}
242254
}
243255
}
244256

@@ -371,7 +383,7 @@ async function displayNewSrcFile(source_item) {
371383
if (! isNaN(source_item.src_height)) { // isNan(null) is true
372384
// const src = document.getElementById('src');
373385
// const xref = document.getElementById('xref');
374-
// TODO: see initDrag for how to set the various heights/tops
386+
// TODO: see setSplitterDragActions() for how to set the various heights/tops
375387
}
376388
updateBrowserUrl(source_item);
377389
let progress = document.createElement('span');

0 commit comments

Comments
 (0)