-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
distribute.ts
47 lines (38 loc) · 1.25 KB
/
distribute.ts
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
import { roundDecimals } from '@grafana/data';
export const SPACE_BETWEEN = 1;
export const SPACE_AROUND = 2;
export const SPACE_EVENLY = 3;
const coord = (i: number, offs: number, iwid: number, gap: number) => roundDecimals(offs + i * (iwid + gap), 6);
export type Each = (idx: number, offPct: number, dimPct: number) => void;
/**
* @internal
*/
export function distribute(numItems: number, sizeFactor: number, justify: number, onlyIdx: number | null, each: Each) {
let space = 1 - sizeFactor;
/* eslint-disable no-multi-spaces */
// prettier-ignore
let gap = (
justify === SPACE_BETWEEN ? space / (numItems - 1) :
justify === SPACE_AROUND ? space / (numItems ) :
justify === SPACE_EVENLY ? space / (numItems + 1) : 0
);
if (isNaN(gap) || gap === Infinity) {
gap = 0;
}
// prettier-ignore
let offs = (
justify === SPACE_BETWEEN ? 0 :
justify === SPACE_AROUND ? gap / 2 :
justify === SPACE_EVENLY ? gap : 0
);
/* eslint-enable */
let iwid = sizeFactor / numItems;
let _iwid = roundDecimals(iwid, 6);
if (onlyIdx == null) {
for (let i = 0; i < numItems; i++) {
each(i, coord(i, offs, iwid, gap), _iwid);
}
} else {
each(onlyIdx, coord(onlyIdx, offs, iwid, gap), _iwid);
}
}