-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlayout.libsonnet
More file actions
167 lines (146 loc) · 5.55 KB
/
Copy pathlayout.libsonnet
File metadata and controls
167 lines (146 loc) · 5.55 KB
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
local grid_width = 24;
{
width:: {
_1:: grid_width,
_2:: grid_width / 2,
_3:: grid_width / 3,
_4:: grid_width / 4,
_5:: grid_width / 5,
_6:: grid_width / 6,
_8:: grid_width / 8,
full:: $.width._1,
half:: $.width._2,
third:: $.width._3,
quarter:: $.width._4,
fifth:: $.width._5,
sixth:: $.width._6,
heighth:: $.width._8,
// TODO: Legacy items, remove in the future
one_third:: grid_width / 3,
two_thirds:: grid_width / 3 * 2,
one_fifth:: grid_width / 5,
one_sixth:: grid_width / 6,
three_quarters:: grid_width / 4 * 3,
},
/**
* @name layout.pos
*
* Generate standard positions based on the specified height.
*
* @param height The standard height for a panel.
*
* @return Standard layout position values.
*/
pos(height):: {
_1:: { w: $.width._1, h: height },
_2:: { w: $.width._2, h: height },
_3:: { w: $.width._3, h: height },
_4:: { w: $.width._4, h: height },
_5:: { w: $.width._5, h: height },
_6:: { w: $.width._6, h: height },
_8:: { w: $.width._8, h: height },
full:: { w: $.width._1, h: height },
half:: { w: $.width._2, h: height },
one_third:: { w: $.width._3, h: height },
two_thirds:: { w: $.width._3 * 2, h: height },
one_quarter:: { w: $.width._4, h: height },
two_quarters:: { w: $.width._4 * 2, h: height },
three_quarters:: { w: $.width._4 * 3, h: height },
one_fifth:: { w: $.width._5, h: height },
two_fifths:: { w: $.width._5 * 2, h: height },
three_fifths:: { w: $.width._5 * 3, h: height },
four_fifths:: { w: $.width._5 * 4, h: height },
one_sixth:: { w: $.width._6, h: height },
two_sixths:: { w: $.width._6 * 2, h: height },
three_sixths:: { w: $.width._6 * 2, h: height },
four_sixths:: { w: $.width._6 * 2, h: height },
five_sixths:: { w: $.width._6 * 2, h: height },
one_heighth:: { w: $.width._8, h: height },
two_heighth:: { w: $.width._8 * 2, h: height },
three_heighth:: { w: $.width._8 * 3, h: height },
four_heighth:: { w: $.width._8 * 4, h: height },
five_heighth:: { w: $.width._8 * 5, h: height },
six_heighth:: { w: $.width._8 * 6, h: height },
seven_heighth:: { w: $.width._8 * 7, h: height },
title:: { w: $.width.full, h: height / 2 },
// TODO: Legacy items, remove in the future
_3_4: { w: $.width._4 * 3, h: height },
},
/**
* @name layout.generate_grid
*
* Generate Grafana grid, based on panels size.
* Should be called only once with the list of all panels that will be added.
* It puts panels from left to right, until there is a free space, and then uses new line.
* Line step on a new line break is the height of the last panel in a previous line.
* You can use rows as line breaks.
* For complex structures you can insert panels with pre-set position,
* but you must be careful to not introduce conflicts.
* If there are conflicts in grid, Grafana will solve them on import.
* Grafana's negative gravity will also be applied on import, it is not computed here.
* You should be careful with using both this and dynamic repeating with variables:
* static generator do not know about dynamic repeating,
* so Grafana conflict solver will be applied on import.
*
* @param panels List of panels (their width and height must be set in gridPos)
*
* @return List of panels with computed grid positions
*/
generate_grid(panels):: [
el {
gridPos: {
x: el.gridPos.x,
y: el.gridPos.y,
h: el.gridPos.h,
w: el.gridPos.w,
},
}
for el in std.foldl(function(_panels, p) (
local i = std.length(_panels);
local prev = (if i == 0 then null else _panels[i - 1]);
if i == 0 then
_panels + [p { gridPos: {
x: 0,
y: 0,
h: p.gridPos.h,
w: p.gridPos.w,
x_cursor: p.gridPos.w,
y_cursor: 0,
} }]
else
local line_break = prev.gridPos.x_cursor + p.gridPos.w > grid_width;
_panels + [p { gridPos: {
x:
if std.objectHas(p.gridPos, 'x') then
p.gridPos.x
else if line_break then
0
else
prev.gridPos.x_cursor,
y:
if std.objectHas(p.gridPos, 'y') then
p.gridPos.y
else if line_break then
prev.gridPos.y_cursor + prev.gridPos.h
else
prev.gridPos.y_cursor,
h: p.gridPos.h,
w: p.gridPos.w,
x_cursor:
if std.objectHas(p.gridPos, 'x') then
p.gridPos.x + p.gridPos.w
else if line_break then
p.gridPos.w
else
prev.gridPos.x_cursor + p.gridPos.w,
y_cursor:
if std.objectHas(p.gridPos, 'y') then
p.gridPos.y
else if line_break then
prev.gridPos.y_cursor + prev.gridPos.h
else
prev.gridPos.y_cursor,
} }]
), panels, [])
],
}