Skip to content

Commit 5a66b0f

Browse files
authored
Improve wrapPanels (#214)
Add support for panels inside rows
1 parent 119d653 commit 5a66b0f

File tree

1 file changed

+100
-61
lines changed

1 file changed

+100
-61
lines changed

custom/util/grid.libsonnet

Lines changed: 100 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -90,77 +90,116 @@ local panelUtil = import './panel.libsonnet';
9090
],
9191
),
9292
wrapPanels(panels, panelWidth=8, panelHeight=8, startY=0):
93+
94+
local calculateGridPosForPanel(acc, panel) =
95+
local gridPos = std.get(panel, 'gridPos', {});
96+
local width = std.get(gridPos, 'w', panelWidth);
97+
local height = std.get(gridPos, 'h', panelHeight);
98+
if acc.cursor.x + width > gridWidth
99+
then
100+
// start new row as width exceeds gridWidth
101+
{
102+
panels+: [
103+
panel {
104+
gridPos+:
105+
{
106+
x: 0,
107+
y: acc.cursor.y + height,
108+
w: width,
109+
h: height,
110+
},
111+
},
112+
],
113+
cursor+:: {
114+
x: 0 + width,
115+
y: acc.cursor.y + height,
116+
maxH: if height > acc.cursor.maxH then height else acc.cursor.maxH,
117+
},
118+
}
119+
else
120+
// enough width, place panel on current row
121+
{
122+
panels+: [
123+
panel {
124+
gridPos+:
125+
{
126+
x: acc.cursor.x,
127+
y: acc.cursor.y,
128+
w: width,
129+
h: height,
130+
},
131+
},
132+
],
133+
cursor+:: {
134+
x: acc.cursor.x + width,
135+
y: acc.cursor.y,
136+
maxH: if height > acc.cursor.maxH then height else acc.cursor.maxH,
137+
},
138+
};
139+
93140
std.foldl(
94141
function(acc, panel)
95142
if panel.type == 'row'
96143
then
97-
// when type=row, start new row immediatly and shift Y of new row by max height recorded
98-
acc + {
99-
panels+: [
100-
panel + {
101-
gridPos+:
144+
(
145+
if std.objectHas(panel, 'panels') && std.length(panel.panels) > 0
146+
then
147+
local rowPanels =
148+
std.foldl(
149+
function(acc, panel)
150+
acc + calculateGridPosForPanel(acc, panel),
151+
panel.panels,
102152
{
103-
x: acc.cursor.x,
104-
y: acc.cursor.y + acc.cursor.maxH,
105-
w: 0,
106-
h: 1,
107-
},
108-
},
109-
],
110-
cursor:: {
111-
x: 0,
112-
y: acc.cursor.y + acc.cursor.maxH + 1,
113-
maxH: 0,
114-
},
115-
}
116-
else
117-
// handle regular panel
118-
local gridPos = std.get(panel, 'gridPos', {});
119-
local width = std.get(gridPos, 'w', panelWidth);
120-
local height = std.get(gridPos, 'h', panelHeight);
121-
if acc.cursor.x + width > gridWidth
122-
then
123-
// start new row as width exceeds gridWidth
124-
acc + {
125-
panels+: [
126-
panel + {
127-
gridPos+:
128-
{
153+
panels+: [],
154+
// initial
155+
cursor:: {
129156
x: 0,
130-
y: acc.cursor.y + height,
131-
w: width,
132-
h: height,
157+
y: acc.cursor.y + acc.cursor.maxH + 1,
158+
maxH: 0,
133159
},
134-
},
135-
],
136-
cursor+:: {
137-
x: 0 + width,
138-
y: acc.cursor.y + height,
139-
maxH: if height > super.maxH then height else super.maxH,
140-
},
141-
}
142-
else
143-
// enough width, place panel on current row
144-
acc + {
145-
panels+: [
146-
panel + {
147-
gridPos+:
148-
{
149-
x: acc.cursor.x,
150-
y: acc.cursor.y,
151-
w: width,
152-
h: height,
160+
},
161+
);
162+
acc {
163+
panels+: [
164+
panel {
165+
//rows panels
166+
panels: rowPanels.panels,
167+
gridPos+: {
168+
x: 0,
169+
y: acc.cursor.y + acc.cursor.maxH,
170+
w: 0,
153171
},
172+
173+
},
174+
],
175+
cursor:: rowPanels.cursor,
176+
}
177+
else
178+
acc {
179+
panels+: [
180+
panel {
181+
panels: [],
182+
gridPos+:
183+
{
184+
x: acc.cursor.x,
185+
y: acc.cursor.y + acc.cursor.maxH,
186+
w: 0,
187+
h: 1,
188+
},
189+
},
190+
],
191+
cursor:: {
192+
x: 0,
193+
y: acc.cursor.y + acc.cursor.maxH + 1,
194+
maxH: 0,
154195
},
155-
],
156-
cursor+:: {
157-
x: acc.cursor.x + width,
158-
y: acc.cursor.y,
159-
maxH: if height > super.maxH then height else super.maxH,
160-
},
161-
},
196+
}
197+
)
198+
else
199+
// handle regular panel
200+
acc + calculateGridPosForPanel(acc, panel),
162201
panels,
163-
// Initial value for acc
202+
// Initial value for acc:
164203
{
165204
panels: [],
166205
cursor:: {

0 commit comments

Comments
 (0)