-
Notifications
You must be signed in to change notification settings - Fork 23.1k
/
graph_arch_parser.js
90 lines (87 loc) · 3.89 KB
/
graph_arch_parser.js
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
/** @odoo-module **/
import { visitXML } from "@web/core/utils/xml";
import { GROUPABLE_TYPES } from "@web/search/utils/misc";
import { archParseBoolean } from "@web/views/utils";
const MODES = ["bar", "line", "pie"];
const ORDERS = ["ASC", "DESC", "asc", "desc", null];
export class GraphArchParser {
parse(arch, fields = {}) {
const archInfo = { fields, fieldAttrs: {}, groupBy: [], measures: [] };
visitXML(arch, (node) => {
switch (node.tagName) {
case "graph": {
if (node.hasAttribute("disable_linking")) {
archInfo.disableLinking = archParseBoolean(
node.getAttribute("disable_linking")
);
}
if (node.hasAttribute("stacked")) {
archInfo.stacked = archParseBoolean(node.getAttribute("stacked"));
}
if (node.hasAttribute("cumulated")) {
archInfo.cumulated = archParseBoolean(node.getAttribute("cumulated"));
}
if (node.hasAttribute("cumulated_start")) {
archInfo.cumulatedStart = archParseBoolean(
node.getAttribute("cumulated_start")
);
}
const mode = node.getAttribute("type");
if (mode && MODES.includes(mode)) {
archInfo.mode = mode;
}
const order = node.getAttribute("order");
if (order && ORDERS.includes(order)) {
archInfo.order = order.toUpperCase();
}
const title = node.getAttribute("string");
if (title) {
archInfo.title = title;
}
break;
}
case "field": {
const fieldName = node.getAttribute("name"); // exists (rng validation)
if (fieldName === "id") {
break;
}
const string = node.getAttribute("string");
if (string) {
if (!archInfo.fieldAttrs[fieldName]) {
archInfo.fieldAttrs[fieldName] = {};
}
archInfo.fieldAttrs[fieldName].string = string;
}
if (
node.getAttribute("invisible") === "True" ||
node.getAttribute("invisible") === "1"
) {
if (!archInfo.fieldAttrs[fieldName]) {
archInfo.fieldAttrs[fieldName] = {};
}
archInfo.fieldAttrs[fieldName].isInvisible = true;
break;
}
const isMeasure = node.getAttribute("type") === "measure";
if (isMeasure) {
archInfo.measures.push(fieldName);
// the last field with type="measure" (if any) will be used as measure else __count
archInfo.measure = fieldName;
} else {
const { type } = archInfo.fields[fieldName]; // exists (rng validation)
if (GROUPABLE_TYPES.includes(type)) {
let groupBy = fieldName;
const interval = node.getAttribute("interval");
if (interval) {
groupBy += `:${interval}`;
}
archInfo.groupBy.push(groupBy);
}
}
break;
}
}
});
return archInfo;
}
}