Skip to content

Commit aa78fcb

Browse files
committed
cleanup the js a little
1 parent e4c41a7 commit aa78fcb

1 file changed

Lines changed: 50 additions & 72 deletions

File tree

resources/js/pages/layout/PanelLayout.vue

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,61 @@
22
import { provide, ref, onMounted, onUnmounted, useTemplateRef } from 'vue';
33
import useResizable from '@/composables/use-resizable.js';
44
5-
const leftPanelRef = useTemplateRef('leftPanel');
6-
const rightPanelRef = useTemplateRef('rightPanel');
7-
8-
const leftPanelActive = ref(false);
9-
const rightPanelActive = ref(false);
10-
11-
provide('leftPanelActive', leftPanelActive);
12-
provide('rightPanelActive', rightPanelActive);
13-
14-
const { makeResizable } = useResizable();
15-
16-
const sidebarMinWidth = 175;
17-
const sidebarMaxWidth = 450;
18-
const leftSidebarCompactDefaultWidth = 270;
19-
const rightSidebarNarrowDefaultWidth = 300;
20-
const rightSidebarCompactDefaultWidth = 300;
21-
const sidebarWideDefaultWidth = 320;
22-
const sidebarNarrowBreakpoint = 1250;
23-
const sidebarCompactBreakpoint = 1400;
24-
25-
const getLeftSidebarDefaultWidth = () => {
26-
if (typeof window === 'undefined') return sidebarWideDefaultWidth;
27-
const w = window.innerWidth;
28-
if (w < sidebarNarrowBreakpoint) return sidebarMinWidth;
29-
if (w < sidebarCompactBreakpoint) return leftSidebarCompactDefaultWidth;
30-
return sidebarWideDefaultWidth;
5+
const minWidth = 175;
6+
const maxWidth = 450;
7+
const breakpoints = { narrow: 1250, compact: 1400 };
8+
9+
const left = {
10+
ref: useTemplateRef('leftPanel'),
11+
active: ref(false),
12+
edge: 'right',
13+
defaults: [minWidth, 270, 320],
3114
};
3215
33-
const getRightSidebarDefaultWidth = () => {
34-
if (typeof window === 'undefined') return sidebarWideDefaultWidth;
35-
const w = window.innerWidth;
36-
if (w < sidebarNarrowBreakpoint) return rightSidebarNarrowDefaultWidth;
37-
if (w < sidebarCompactBreakpoint) return rightSidebarCompactDefaultWidth;
38-
return sidebarWideDefaultWidth;
16+
const right = {
17+
ref: useTemplateRef('rightPanel'),
18+
active: ref(false),
19+
edge: 'left',
20+
defaults: [300, 300, 320],
3921
};
4022
41-
makeResizable(leftPanelRef, leftPanelActive, { edge: 'right', minWidth: sidebarMinWidth, maxWidth: sidebarMaxWidth, defaultWidth: getLeftSidebarDefaultWidth });
42-
makeResizable(rightPanelRef, rightPanelActive, { edge: 'left', minWidth: sidebarMinWidth, maxWidth: sidebarMaxWidth, defaultWidth: getRightSidebarDefaultWidth });
43-
44-
const applyBreakpointDefaults = () => {
45-
if (typeof window === 'undefined') return;
23+
provide('leftPanelActive', left.active);
24+
provide('rightPanelActive', right.active);
4625
26+
function getDefaultWidth(panel) {
4727
const w = window.innerWidth;
48-
let leftTarget;
49-
let rightTarget;
50-
if (w < sidebarNarrowBreakpoint) {
51-
leftTarget = sidebarMinWidth;
52-
rightTarget = rightSidebarNarrowDefaultWidth;
53-
} else if (w < sidebarCompactBreakpoint) {
54-
leftTarget = leftSidebarCompactDefaultWidth;
55-
rightTarget = rightSidebarCompactDefaultWidth;
56-
} else {
57-
leftTarget = sidebarWideDefaultWidth;
58-
rightTarget = sidebarWideDefaultWidth;
59-
}
28+
if (w < breakpoints.narrow) return panel.defaults[0];
29+
if (w < breakpoints.compact) return panel.defaults[1];
30+
return panel.defaults[2];
31+
}
6032
61-
const minPx = `${sidebarMinWidth}px`;
62-
const leftCompactPx = `${leftSidebarCompactDefaultWidth}px`;
63-
const rightNarrowPx = `${rightSidebarNarrowDefaultWidth}px`;
64-
const rightCompactPx = `${rightSidebarCompactDefaultWidth}px`;
65-
const widePx = `${sidebarWideDefaultWidth}px`;
33+
const { makeResizable } = useResizable();
6634
67-
const leftPriorDefaults = new Set([minPx, leftCompactPx, widePx]);
68-
const rightPriorDefaults = new Set([minPx, leftCompactPx, widePx, rightNarrowPx, rightCompactPx]);
35+
[left, right].forEach((panel) => {
36+
makeResizable(panel.ref, panel.active, {
37+
edge: panel.edge,
38+
minWidth,
39+
maxWidth,
40+
defaultWidth: () => getDefaultWidth(panel),
41+
});
6942
70-
const applyIfUnmodified = (panelEl, target, priorDefaults) => {
71-
if (!panelEl) return;
72-
const current = panelEl.style.width;
43+
// Pre-compute the set of pixel strings we treat as "unmodified by the user"
44+
panel.priorDefaults = new Set(panel.defaults.map((d) => `${d}px`));
45+
});
7346
74-
if (!current || priorDefaults.has(current)) {
75-
panelEl.style.width = `${target}px`;
76-
}
77-
};
47+
function applyBreakpointDefaults() {
48+
[left, right].forEach((panel) => {
49+
if (!panel.active.value) return;
7850
79-
if (leftPanelActive.value) applyIfUnmodified(leftPanelRef.value, leftTarget, leftPriorDefaults);
80-
if (rightPanelActive.value) applyIfUnmodified(rightPanelRef.value, rightTarget, rightPriorDefaults);
81-
};
51+
const el = panel.ref.value;
52+
if (!el) return;
53+
54+
const current = el.style.width;
55+
if (!current || panel.priorDefaults.has(current)) {
56+
el.style.width = `${getDefaultWidth(panel)}px`;
57+
}
58+
});
59+
}
8260
8361
onMounted(() => {
8462
applyBreakpointDefaults();
@@ -93,8 +71,8 @@ onUnmounted(() => {
9371
<template>
9472
<Teleport defer to="#main-content">
9573
<div
96-
v-show="leftPanelActive"
97-
:data-left-panel="leftPanelActive ? '' : undefined"
74+
v-show="left.active"
75+
:data-left-panel="left.active ? '' : undefined"
9876
ref="leftPanel"
9977
id="left-panel"
10078
tabindex="-1"
@@ -104,14 +82,14 @@ onUnmounted(() => {
10482
</Teleport>
10583

10684
<Teleport defer to="#main-content">
107-
<div v-show="leftPanelActive || rightPanelActive" class="field-to-panel-connector-initial" style="order: 1"></div>
108-
<div v-show="leftPanelActive || rightPanelActive" class="field-to-panel-connector-scroll-past" style="order: 1"></div>
85+
<div v-show="left.active || right.active" class="field-to-panel-connector-initial" style="order: 1"></div>
86+
<div v-show="left.active || right.active" class="field-to-panel-connector-scroll-past" style="order: 1"></div>
10987
</Teleport>
11088

11189
<Teleport defer to="#main-content">
11290
<div
113-
v-show="rightPanelActive"
114-
:data-right-panel="rightPanelActive ? '' : undefined"
91+
v-show="right.active"
92+
:data-right-panel="right.active ? '' : undefined"
11593
ref="rightPanel"
11694
id="right-panel"
11795
tabindex="-1"

0 commit comments

Comments
 (0)