-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
213 lines (170 loc) · 5.42 KB
/
index.js
File metadata and controls
213 lines (170 loc) · 5.42 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* global ym */
import './splash.css';
import initStatoscope, { Discovery } from '@statoscope/webpack-ui';
const splash = document.querySelector('#splash');
const progressContainer = document.querySelector('#splash #progress');
const error = document.querySelector('#splash #error');
const instructions = document.querySelector('#splash #instructions');
const fileInput = document.querySelector('#splash #file-input');
const uploadButton = document.querySelector('#splash #upload-button');
function reachGoal(name, params) {
if (typeof ym !== 'undefined') {
ym(68806498, 'reachGoal', name, params);
}
}
function init(files) {
for (const item of files) {
const { data } = item;
if (data.version) {
reachGoal(data.bundler || 'webpack', { version: data.version });
}
}
reachGoal('init', { files: files.length });
splash.parentNode.removeChild(splash);
initStatoscope(files);
}
const analyticsWarning = document.querySelector('#analytics-warning');
const closeAnalyticsWarning = document.querySelector('#close-analytics-warning');
if (!document.cookie.includes('analyticsWarningClosed')) {
analyticsWarning.classList.remove('hidden');
closeAnalyticsWarning.addEventListener('click', () => {
analyticsWarning.classList.add('hidden');
document.cookie = `analyticsWarningClosed=1; path=/; max-age=${365 * 24 * 60 * 60}`;
});
}
uploadButton.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', ({ target: { files } }) => {
if (files.length) {
handleFiles(Array.from(files)).finally(destroyProgressBars).then(init);
}
});
document.addEventListener('click', async (e) => {
if (!e.target.classList.contains('demo-button')) {
return;
}
reachGoal('demo');
instructions.classList.add('hidden');
const loaderResult = await loadDataWithProgress(() =>
Discovery.utils.loadDataFromUrl(e.target.dataset.file, {})
);
init([{ name: e.target.dataset.file, data: loaderResult.data }]);
});
document.addEventListener(
'click',
(event) => {
if (event.target.dataset.mayCopy) {
document.execCommand('copy');
}
},
true
);
document.addEventListener('copy', (event) => {
if (event.target.dataset.mayCopy) {
event.preventDefault();
if (event.clipboardData) {
event.clipboardData.setData('text/plain', event.target.textContent);
}
}
});
splash.addEventListener('dragover', function (e) {
if (e.dataTransfer.items && e.dataTransfer.items[0]) {
if (e.dataTransfer.items[0].kind === 'file') {
e.preventDefault();
}
}
});
splash.addEventListener('drop', function (e) {
const files = [];
if (e.dataTransfer.items) {
for (const item of e.dataTransfer.items) {
if (item.kind === 'file') {
e.preventDefault();
const file = item.getAsFile();
if (!/\.json/.test(file.name)) {
alert('Only JSON files may be loaded.');
return;
}
files.push(file);
}
}
}
if (files.length) {
handleFiles(files).finally(destroyProgressBars).then(init);
}
});
splash.addEventListener('dragend', function (e) {
if (e.dataTransfer.items) {
for (let i = 0; i < e.dataTransfer.items.length; i++) {
e.dataTransfer.items.remove(i);
}
} else {
e.dataTransfer.clearData();
}
});
function makeProgressBar() {
const progressbar = new Discovery.utils.progressbar({});
makeProgressBar.set.add(progressbar);
return progressbar;
}
function destroyProgressBars() {
for (const progressbar of makeProgressBar.set) {
progressbar.el.remove();
}
}
makeProgressBar.set = new Set();
async function loadDataWithProgress(loaderFn) {
const progressbar = makeProgressBar();
progressContainer.append(progressbar.el);
const loader = loaderFn();
await Discovery.utils.syncLoaderWithProgressbar(loader, progressbar);
return loader.result;
}
async function handleFiles(files) {
instructions.classList.add('hidden');
error.classList.remove('hidden');
error.innerHTML = '';
const rawData = [];
await Promise.all(
files.map(async (file) => {
try {
const loadResult = await loadDataWithProgress(() =>
Discovery.utils.loadDataFromFile(file, {})
);
rawData.push({
name: file.name,
data: loadResult.data,
});
reachGoal('json_upload');
} catch (e) {
error.innerHTML = `😭 Can't load ${file.name}<br/>${e.message}<br/>Please, try again`;
throw e;
}
})
);
return rawData;
}
(function jsonFromUrl() {
// json via jsonUrl query param
// Extract the 'jsonUrl' parameter from the URL
const urlParams = new URLSearchParams(window.location.search);
const jsonUrl = urlParams.get('jsonUrl');
console.log({ jsonUrl });
// Updated logic: run this on page load
if (jsonUrl) {
fetch(jsonUrl)
.then((res) => {
if (!res.ok) throw new Error('Failed to fetch');
return res.text(); // or .json() if you want parsed object
})
.then((text) => {
// Create a File object (only if your handleFiles requires it)
const blob = new Blob([text], { type: 'application/json' });
const file = new File([blob], 'remote.json', { type: 'application/json' });
// Call handleFiles directly, bypassing the input altogether
handleFiles([file]).finally(destroyProgressBars).then(init);
})
.catch((error) => {
console.error(`Could not load JSON from URL ${jsonUrl}`, error);
});
}
})();