Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 32 additions & 104 deletions custom/util/grid.libsonnet
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';

local panelUtil = import './panel.libsonnet';

{
local root = self,

local rowPanelHeight = 1,
local gridWidth = 24,

// Calculates the number of rows for a set of panels.
countRows(panels, panelWidth):
std.ceil(std.length(panels) / std.floor(gridWidth / panelWidth)),

// Calculates gridPos for a panel based on its index, width and height.
gridPosForIndex(index, panelWidth, panelHeight, startY): {
local panelsPerRow = std.floor(gridWidth / panelWidth),
local row = std.floor(index / panelsPerRow),
local col = std.mod(index, panelsPerRow),
gridPos: {
w: panelWidth,
h: panelHeight,
x: panelWidth * col,
y: startY + (panelHeight * row) + row,
},
},

// Configures gridPos for each panel in a grid with equal width and equal height.
makePanelGrid(panels, panelWidth, panelHeight, startY):
std.mapWithIndex(
function(i, panel)
panel + root.gridPosForIndex(i, panelWidth, panelHeight, startY),
panels
),

'#makeGrid':: d.func.new(
|||
`makeGrid` returns an array of `panels` organized in a grid with equal `panelWidth`
Expand All @@ -52,90 +28,42 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
],
),
makeGrid(panels, panelWidth=8, panelHeight=8, startY=0):
// Get indexes for all Row panels
local rowIndexes =
xtd.array.filterMapWithIndex(
function(i, p) p.type == 'row',
function(i, p) i,
panels,
);

// Group panels below each Row panel
local rowGroups =
std.mapWithIndex(
function(i, r) {
header:
{
// Set initial values to ensure a value is set
// may be overridden at per Row panel
collapsed: false,
panels: [],
}
+ panels[r],
panels:
self.header.panels // prepend panels that are part of the Row panel
+ (if i == std.length(rowIndexes) - 1 // last rowIndex
then panels[r + 1:]
else panels[r + 1:rowIndexes[i + 1]]),
rows: root.countRows(self.panels, panelWidth),
},
rowIndexes
local sanitizedPanels = std.map(
function(p)
panelUtil.sanitizePanel(p)
+ (
if p.type != 'row'
then { gridPos+: { h: panelHeight, w: panelWidth } }
else {}
),
panels
);
local grouped = panelUtil.groupPanelsInRows(sanitizedPanels);

local panelsBeforeRows = panelUtil.getPanelsBeforeNextRow(grouped);
local rowPanels =
std.filter(
function(p) p.type == 'row',
grouped
);

// Loop over rowGroups
std.foldl(
function(acc, rowGroup) acc + {
local y = acc.nexty,
nexty: y // previous y
+ (rowGroup.rows * panelHeight) // height of all rows
+ rowGroup.rows // plus 1 for each row
+ acc.lastRowPanelHeight,

lastRowPanelHeight: rowPanelHeight, // set height for next round
local CalculateXforPanel(index, panel) =
local panelsPerRow = std.floor(gridWidth / panelWidth);
local col = std.mod(index, panelsPerRow);
panel + { gridPos+: { x: panelWidth * col } };

// Create a grid per group
local panels = root.makePanelGrid(rowGroup.panels, panelWidth, panelHeight, y + 1),
local panelsBeforeRowsWithX = std.mapWithIndex(CalculateXforPanel, panelsBeforeRows);

panels+:
[
// Add row header aka the Row panel
rowGroup.header + {
gridPos: {
w: gridWidth, // always full length
h: rowPanelHeight, // always 1 height
x: 0, // always at beginning
y: y,
},
panels:
// If row is collapsed, then store panels inside Row panel
if rowGroup.header.collapsed
then panels
else [],
},
]
+ (
// If row is not collapsed, then expose panels directly
if !rowGroup.header.collapsed
then panels
else []
),
},
rowGroups,
{
// Get panels that come before the rowGroups
local panelsBeforeRowGroups =
if std.length(rowIndexes) != 0
then panels[0:rowIndexes[0]]
else panels, // matches all panels if no Row panels found
local rows = root.countRows(panelsBeforeRowGroups, panelWidth),
nexty: startY + (rows * panelHeight) + rows,
local rowPanelsWithX =
std.map(
function(row)
row + { panels: std.mapWithIndex(CalculateXforPanel, row.panels) },
rowPanels
);

lastRowPanelHeight: 0, // starts without a row panel
local uncollapsed = panelUtil.resolveCollapsedFlagOnRows(panelsBeforeRowsWithX + rowPanelsWithX);

// Create a grid for the panels that come before the rowGroups
panels: root.makePanelGrid(panelsBeforeRowGroups, panelWidth, panelHeight, startY),
}
).panels,
panelUtil.normalizeY(uncollapsed),

'#wrapPanels':: d.func.new(
|||
Expand Down
11 changes: 6 additions & 5 deletions custom/util/panel.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,15 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
]
),
calculateLowestYforPanel(panel, panels):
local x1 = panel.gridPos.x;
local x2 = panel.gridPos.x + panel.gridPos.w;
xtd.number.maxInArray( // the new position is highest value (max) on the Y-scale
std.filterMap(
function(p) // find panels that overlap on X-scale
p.gridPos.x == x1
|| xtd.number.inRange(p.gridPos.x, x1, x2)
|| xtd.number.inRange((p.gridPos.x + p.gridPos.w), x1, x2),
local v1 = panel.gridPos.x;
local v2 = panel.gridPos.x + panel.gridPos.w;
local x1 = p.gridPos.x;
local x2 = p.gridPos.x + p.gridPos.w;
(v1 >= x1 && v1 < x2)
|| (v2 >= x1 && v2 < x2),
function(p) // return new position on Y-scale
p.gridPos.y + p.gridPos.h,
panels,
Expand Down
136 changes: 32 additions & 104 deletions gen/grafonnet-v10.0.0/custom/util/grid.libsonnet
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';

local panelUtil = import './panel.libsonnet';

{
local root = self,

local rowPanelHeight = 1,
local gridWidth = 24,

// Calculates the number of rows for a set of panels.
countRows(panels, panelWidth):
std.ceil(std.length(panels) / std.floor(gridWidth / panelWidth)),

// Calculates gridPos for a panel based on its index, width and height.
gridPosForIndex(index, panelWidth, panelHeight, startY): {
local panelsPerRow = std.floor(gridWidth / panelWidth),
local row = std.floor(index / panelsPerRow),
local col = std.mod(index, panelsPerRow),
gridPos: {
w: panelWidth,
h: panelHeight,
x: panelWidth * col,
y: startY + (panelHeight * row) + row,
},
},

// Configures gridPos for each panel in a grid with equal width and equal height.
makePanelGrid(panels, panelWidth, panelHeight, startY):
std.mapWithIndex(
function(i, panel)
panel + root.gridPosForIndex(i, panelWidth, panelHeight, startY),
panels
),

'#makeGrid':: d.func.new(
|||
`makeGrid` returns an array of `panels` organized in a grid with equal `panelWidth`
Expand All @@ -52,90 +28,42 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
],
),
makeGrid(panels, panelWidth=8, panelHeight=8, startY=0):
// Get indexes for all Row panels
local rowIndexes =
xtd.array.filterMapWithIndex(
function(i, p) p.type == 'row',
function(i, p) i,
panels,
);

// Group panels below each Row panel
local rowGroups =
std.mapWithIndex(
function(i, r) {
header:
{
// Set initial values to ensure a value is set
// may be overridden at per Row panel
collapsed: false,
panels: [],
}
+ panels[r],
panels:
self.header.panels // prepend panels that are part of the Row panel
+ (if i == std.length(rowIndexes) - 1 // last rowIndex
then panels[r + 1:]
else panels[r + 1:rowIndexes[i + 1]]),
rows: root.countRows(self.panels, panelWidth),
},
rowIndexes
local sanitizedPanels = std.map(
function(p)
panelUtil.sanitizePanel(p)
+ (
if p.type != 'row'
then { gridPos+: { h: panelHeight, w: panelWidth } }
else {}
),
panels
);
local grouped = panelUtil.groupPanelsInRows(sanitizedPanels);

local panelsBeforeRows = panelUtil.getPanelsBeforeNextRow(grouped);
local rowPanels =
std.filter(
function(p) p.type == 'row',
grouped
);

// Loop over rowGroups
std.foldl(
function(acc, rowGroup) acc + {
local y = acc.nexty,
nexty: y // previous y
+ (rowGroup.rows * panelHeight) // height of all rows
+ rowGroup.rows // plus 1 for each row
+ acc.lastRowPanelHeight,

lastRowPanelHeight: rowPanelHeight, // set height for next round
local CalculateXforPanel(index, panel) =
local panelsPerRow = std.floor(gridWidth / panelWidth);
local col = std.mod(index, panelsPerRow);
panel + { gridPos+: { x: panelWidth * col } };

// Create a grid per group
local panels = root.makePanelGrid(rowGroup.panels, panelWidth, panelHeight, y + 1),
local panelsBeforeRowsWithX = std.mapWithIndex(CalculateXforPanel, panelsBeforeRows);

panels+:
[
// Add row header aka the Row panel
rowGroup.header + {
gridPos: {
w: gridWidth, // always full length
h: rowPanelHeight, // always 1 height
x: 0, // always at beginning
y: y,
},
panels:
// If row is collapsed, then store panels inside Row panel
if rowGroup.header.collapsed
then panels
else [],
},
]
+ (
// If row is not collapsed, then expose panels directly
if !rowGroup.header.collapsed
then panels
else []
),
},
rowGroups,
{
// Get panels that come before the rowGroups
local panelsBeforeRowGroups =
if std.length(rowIndexes) != 0
then panels[0:rowIndexes[0]]
else panels, // matches all panels if no Row panels found
local rows = root.countRows(panelsBeforeRowGroups, panelWidth),
nexty: startY + (rows * panelHeight) + rows,
local rowPanelsWithX =
std.map(
function(row)
row + { panels: std.mapWithIndex(CalculateXforPanel, row.panels) },
rowPanels
);

lastRowPanelHeight: 0, // starts without a row panel
local uncollapsed = panelUtil.resolveCollapsedFlagOnRows(panelsBeforeRowsWithX + rowPanelsWithX);

// Create a grid for the panels that come before the rowGroups
panels: root.makePanelGrid(panelsBeforeRowGroups, panelWidth, panelHeight, startY),
}
).panels,
panelUtil.normalizeY(uncollapsed),

'#wrapPanels':: d.func.new(
|||
Expand Down
11 changes: 6 additions & 5 deletions gen/grafonnet-v10.0.0/custom/util/panel.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,15 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
]
),
calculateLowestYforPanel(panel, panels):
local x1 = panel.gridPos.x;
local x2 = panel.gridPos.x + panel.gridPos.w;
xtd.number.maxInArray( // the new position is highest value (max) on the Y-scale
std.filterMap(
function(p) // find panels that overlap on X-scale
p.gridPos.x == x1
|| xtd.number.inRange(p.gridPos.x, x1, x2)
|| xtd.number.inRange((p.gridPos.x + p.gridPos.w), x1, x2),
local v1 = panel.gridPos.x;
local v2 = panel.gridPos.x + panel.gridPos.w;
local x1 = p.gridPos.x;
local x2 = p.gridPos.x + p.gridPos.w;
(v1 >= x1 && v1 < x2)
|| (v2 >= x1 && v2 < x2),
function(p) // return new position on Y-scale
p.gridPos.y + p.gridPos.h,
panels,
Expand Down
Loading