forked from finos/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
146 lines (131 loc) · 4.85 KB
/
build.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
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
const puppeteer = require("puppeteer");
const fs = require("fs");
const cp = require("child_process");
const EXAMPLES = require("./src/components/ExampleGallery/features.js").default;
const {convert} = require("@finos/perspective-viewer/dist/cjs/migrate.js");
const DEFAULT_VIEWPORT = {
width: 400,
height: 300,
};
function shuffle(array) {
let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}
async function run_with_theme(page, is_dark = false) {
await page.goto("http://localhost:8080/");
await page.setContent(template(is_dark));
await page.setViewport(DEFAULT_VIEWPORT);
await page.evaluate(async function () {
const viewer = document.querySelector("perspective-viewer");
await viewer.flush();
await viewer.toggleConfig();
});
const files = [];
for (const idx in EXAMPLES) {
const {config, viewport} = EXAMPLES[idx];
await await page.setViewport(viewport || DEFAULT_VIEWPORT);
const new_config = convert(
Object.assign(
{
plugin: "Datagrid",
group_by: [],
expressions: [],
split_by: [],
sort: [],
aggregates: {},
},
config
)
);
console.log(JSON.stringify(new_config));
await page.evaluate(async (config) => {
const viewer = document.querySelector("perspective-viewer");
await viewer.reset();
await viewer.restore(config);
}, new_config);
await page.waitForSelector("perspective-viewer:not([updating])");
const screenshot = await page.screenshot({
captureBeyondViewport: false,
fullPage: true,
});
const name = `static/features/feature_${idx}${
is_dark ? "_dark" : ""
}.png`;
files.push(name);
fs.writeFileSync(name, screenshot);
cp.execSync(`convert ${name} -resize 200x150 ${name}`);
}
cp.execSync(
`montage -mode concatenate -tile 5x ${shuffle(files).join(
" "
)} static/features/montage${is_dark ? "_dark" : "_light"}.png`
);
// fs.writeFileSync("features/index.html", `<html><style>img{width:200px;height:150px;</style><body>${html.join("")}</body></html>`);
}
async function run() {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await run_with_theme(page);
await run_with_theme(page, true);
await browser.close();
}
function template(is_dark) {
return `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<script src="/node_modules/@finos/perspective/dist/umd/perspective.js"></script>
<script src="/node_modules/@finos/perspective-viewer/dist/umd/perspective-viewer.js"></script>
<script src="/node_modules/@finos/perspective-viewer-datagrid/dist/umd/perspective-viewer-datagrid.js"></script>
<script src="/node_modules/@finos/perspective-viewer-d3fc/dist/umd/perspective-viewer-d3fc.js"></script>
<link rel='stylesheet' href="/node_modules/@finos/perspective-viewer/dist/css/material${
is_dark ? "-dark" : ""
}.css">
<style>
perspective-viewer {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<perspective-viewer editable>
</perspective-viewer>
<script>
const WORKER = window.perspective.worker();
async function on_load() {
var el = document.getElementsByTagName('perspective-viewer')[0];
const plugin = await el.getPlugin("Heatmap");
plugin.render_warning = false;
const table = WORKER.table(this.response);
el.load(table);
el.toggleConfig();
}
window.addEventListener('DOMContentLoaded', function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/node_modules/superstore-arrow/superstore.arrow', true);
xhr.responseType = "arraybuffer"
xhr.onload = on_load.bind(xhr);
xhr.send(null);
});
</script>
</body>
</html>`.trim();
}
run();