-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.ts
executable file
·91 lines (80 loc) · 3 KB
/
main.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
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
import { Har } from "har-format";
import { validateOptions } from "./helpers/parse";
import { makeLegend as makeLegendInternal } from "./legend/legend";
import Paging from "./paging/paging";
import * as HarTransformer from "./transformers/har";
import { ChartOptions, ChartRenderOption, HarTransformerOptions } from "./typing/options";
import { WaterfallDocs } from "./typing/waterfall";
import { createWaterfallSvg } from "./waterfall/svg-chart";
/** default options to use if not set in `options` parameter */
const defaultChartOptions: Readonly<ChartOptions> = {
leftColumnWidth: 25,
legendHolder: undefined,
onParsed: undefined,
pageSelector: undefined,
rowHeight: 23,
selectedPage: 0,
showAlignmentHelpers: true,
showIndicatorIcons: true,
showMimeTypeIcon: true,
};
/** default options to use if not set in `options` parameter */
const defaultHarTransformerOptions: Readonly<HarTransformerOptions> = {
showUserTiming: false,
showUserTimingEndMarker: false,
};
/**
* Creates the html for diagrams legend
* @returns {HTMLUListElement} - Legend `<ul>` element
*/
export function makeLegend(): HTMLUListElement {
return makeLegendInternal();
}
function PerfCascade(waterfallDocsData: WaterfallDocs, chartOptions: Partial<ChartRenderOption> = {}): SVGSVGElement {
if (chartOptions["leftColumnWith"] !== undefined) {
// tslint:disable-next-line: no-console
console.warn("Depreciation Warning: The option 'leftColumnWith' has been fixed to 'leftColumnWidth', " +
"please update your code as this will get deprecated in the future");
chartOptions.leftColumnWidth = chartOptions["leftColumnWith"];
}
const options: ChartRenderOption = validateOptions({ ...defaultChartOptions, ...chartOptions } as ChartRenderOption);
// setup paging helper
const paging = new Paging(waterfallDocsData, options.selectedPage);
let doc = createWaterfallSvg(paging.getSelectedPage(), options);
// page update behavior
paging.onPageUpdate((_pageIndex, pageDoc) => {
const el = doc.parentElement as HTMLElement;
const newDoc = createWaterfallSvg(pageDoc, options);
el.replaceChild(newDoc, doc);
doc = newDoc;
});
if (options.pageSelector) {
paging.initPagingSelectBox(options.pageSelector);
}
if (options.legendHolder) {
options.legendHolder.innerHTML = "";
options.legendHolder.appendChild(makeLegendInternal());
}
return doc;
}
/**
* Create new PerfCascade from HAR data
* @param {Har} harData - HAR object
* @param {ChartOptions} options - PerfCascade options object
* @returns {SVGSVGElement} - Chart SVG Element
*/
export function fromHar(harData: Har, options: ChartOptions = {}): SVGSVGElement {
const harTransformerOptions = {
...defaultHarTransformerOptions,
...options,
};
const data = HarTransformer.transformDoc(harData, harTransformerOptions);
if (typeof options.onParsed === "function") {
options.onParsed(data);
}
return PerfCascade(data, options);
}
// additional imported members that get exported via UMD
export {
ChartOptions,
};