Newer
Older
import { t } from "ttag";
import Dimension from "metabase-lib/lib/Dimension";
import { stripId, singularize } from "metabase/lib/formatting";
export default class DimensionOptions {
count: number;
dimensions: Dimension[];
fks: Array<{
field: FieldMetadata,
dimensions: Dimension[],
}>;
constructor(o) {
Object.assign(this, o);
}
all() {
return [].concat(this.dimensions, ...this.fks.map(fk => fk.dimensions));
}
hasDimension(dimension: Dimension): boolean {
for (const d of this.all()) {
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
if (dimension.isSameBaseDimension(d)) {
return true;
}
}
return false;
}
sections({ extraItems = [] } = {}) {
const table = this.dimensions[0] && this.dimensions[0].field().table;
const mainSection = {
name: this.name || (table && singularize(table.display_name)),
icon: this.icon || "table2",
items: [
...extraItems,
...this.dimensions.map(dimension => ({ dimension })),
],
};
const fkSections = this.fks.map(fk => ({
name: fk.name || stripId(fk.field.display_name),
icon: fk.icon || "connections",
items: fk.dimensions.map(dimension => ({ dimension })),
}));
const sections = [];
if (mainSection.items.length > 0) {
sections.push(mainSection);
}
sections.push(...fkSections);
return sections;
}
sectionsByType() {
const itemsBySection = {};
const addItem = item => {
const field = item.dimension.field();
for (const [key, { is }] of Object.entries(SECTIONS_BY_TYPE)) {
if (is(field)) {
itemsBySection[key] = itemsBySection[key] || [];
itemsBySection[key].push(item);
return;
}
}
};
for (const dimension of this.dimensions) {
addItem({
name: dimension.displayName(),
icon: null,
dimension: dimension,
});
}
for (const fk of this.fks) {
const fkName = stripId(fk.field.display_name);
for (const dimension of fk.dimensions) {
addItem({
name: fkName + " — " + dimension.displayName(),
icon: null,
dimension: dimension,
});
}
}
return SECTIONS_BY_TYPE_DISPLAY_ORDER.filter(
key => itemsBySection[key],
).map(key => ({
name: SECTIONS_BY_TYPE[key].name,
icon: SECTIONS_BY_TYPE[key].icon,
items: itemsBySection[key],
}));
}
}
// this is the actual order we should display the sections in
const SECTIONS_BY_TYPE_DISPLAY_ORDER = [
"date",
"location",
"boolean",
"category",
"number",
"text",
"connection",
"other",
];
// these are ordered by priority of categorizing fields
// e.x. if a field is a fk it should be considered a "connection" even if it is also a "number" or "text" because it comes first
const SECTIONS_BY_TYPE = {
connection: {
name: t`Connection`,
icon: "connections",
is: f => f.isFK(),
},
date: {
name: t`Date`,
icon: "calendar",
is: f => f.isDate() || f.isTime(),
},
location: {
name: t`Location`,
icon: "location",
is: f => f.isLocation() || f.isCoordinate(),
},
boolean: {
name: t`Boolean`,
icon: "io",
is: f => f.isBoolean(),
},
category: {
name: t`Category`,
is: f => f.isCategory(),
},
number: {
name: t`Number`,
icon: "int",
is: f => f.isNumber(),
},
text: {
name: t`Text`,
icon: "string",
is: f => f => f.isString(),
},
other: {
name: t`Other`,
icon: "unknown",
is: f => true,
},
};