-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
376 lines (338 loc) · 14.1 KB
/
Copy pathscript.js
File metadata and controls
376 lines (338 loc) · 14.1 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const archiveVersion = new URLSearchParams(window.location.search).get('archive');
const dataPath = archiveVersion
? `./archive/${encodeURIComponent(archiveVersion)}/data.json`
: './data.json';
const response = await fetch(dataPath);
if (!response.ok) throw new Error(`Unable to load spectrum data (${response.status})`);
const fullData = await response.json();
const simplifiedData = fullData.map(band => band.simplified
? { ...band, blocks: band.simplified }
: band);
const container = document.getElementById('spectrums');
const bandFilter = document.getElementById('bandFilter');
const pieChart = document.getElementById('pieChart');
const pieLegend = document.getElementById('pieLegend');
const lookupForm = document.getElementById('channelLookupForm');
const lookupMode = document.getElementById('lookupMode');
const channelNumber = document.getElementById('channelNumber');
const channelBandwidth = document.getElementById('channelBandwidth');
const startFrequency = document.getElementById('startFrequency');
const endFrequency = document.getElementById('endFrequency');
const lookupResult = document.getElementById('channelLookupResult');
let simplified = window.matchMedia('(max-width: 600px)').matches;
let selectedBand = 'overall';
let channelLookup = null;
const carrierColors = {
bite: '#008000',
telia: '#84c',
tele2: '#000'
};
const bandGroups = {
lowband: new Set(['900', '800', '700']),
midband: new Set(['2100', '1800', '2600', '1500', '2500', '2300']),
highband: new Set(['3600'])
};
const lteChannelBands = {
2100: [2110, 0, 599], 1800: [1805, 1200, 1949], 2600: [2620, 2750, 3449], 900: [925, 3450, 3799],
700: [791, 6150, 6449], 800: [758, 9210, 9659], 1500: [1452, 9920, 10359],
2500: [2570, 37750, 38249], 2300: [2300, 38650, 39649]
};
function channelFrequency(band, channel) {
const config = lteChannelBands[band];
return config[0] + (channel - config[1]) * 0.1;
}
function detectBand(start, end) {
const data = simplified ? getSimplifiedWithFallback() : fullData;
return data.find(band => {
const bandStart = band.startFrequency;
const bandEnd = band.endFrequency;
return lteChannelBands[band.id] && start < bandEnd && end > bandStart;
})?.id;
}
function detectBandFromChannel(channel) {
return Object.entries(lteChannelBands)
.find(([, [, min, max]]) => channel >= min && channel <= max)?.[0];
}
function clearChannelHighlight() {
document.querySelectorAll('.channel-marker').forEach(element => element.remove());
}
function applyChannelHighlight() {
clearChannelHighlight();
if (!channelLookup) return;
const { band, start, end } = channelLookup;
const section = document.getElementById(`band${band}`);
if (!section) return;
const chart = section.querySelector('.chart');
const bandData = (simplified ? getSimplifiedWithFallback() : fullData).find(item => item.id === band);
if (!bandData) return;
const bandStart = bandData.startFrequency;
const blocks = [...chart.querySelectorAll('.block')];
const totalWidth = blocks.reduce((sum, block) => sum + Number(block.dataset.end) - Number(block.dataset.start), 0);
const frequencyToPixels = frequency => {
const chartLeft = chart.getBoundingClientRect().left;
for (const block of blocks) {
const blockStartFrequency = Number(block.dataset.start);
const blockEndFrequency = Number(block.dataset.end);
const blockWidth = blockEndFrequency - blockStartFrequency;
if (frequency <= blockEndFrequency) {
const rect = block.getBoundingClientRect();
const ratio = Math.max(0, Math.min(1, (frequency - blockStartFrequency) / blockWidth));
return rect.left - chartLeft + rect.width * ratio;
}
}
const lastRect = blocks[blocks.length - 1].getBoundingClientRect();
return lastRect.right - chartLeft;
};
const marker = document.createElement('div');
marker.className = 'channel-marker';
const markerLeft = frequencyToPixels(Math.max(start, bandStart));
const markerRight = frequencyToPixels(Math.min(end, bandStart + totalWidth));
marker.style.left = `${markerLeft}px`;
marker.style.width = `${Math.max(0, markerRight - markerLeft)}px`;
chart.appendChild(marker);
}
function clearContainer() {
container.innerHTML = '';
}
function formatBandwidth(value) {
return Number(value.toFixed(3)).toString();
}
function formatChannelDetails(block) {
const channels = block.usedFor && typeof block.usedFor === 'object'
? block.usedFor
: block;
return [
channels.arfcns?.length && `2G GSM: ARFCN ${channels.arfcns.join(', ')}`,
channels.earfcns?.length && `4G LTE: EARFCN ${channels.earfcns.join(', ')}`,
channels.nrarfcns?.length && `5G: ARFCN ${channels.nrarfcns.join(', ')}`,
channels.uarfcns?.length && `3G: UARFCN ${channels.uarfcns.join(', ')}`
].filter(Boolean).join('<br>');
}
function renderChart(data) {
clearContainer();
data.forEach(band => {
const section = document.createElement('section');
section.id = `band${band.id}`;
const heading = document.createElement('div');
heading.className = 'band-heading';
heading.innerHTML = `<h2>${band.title}</h2>`;
if (hasAlternateVersion(band.id)) {
const toggleButton = document.createElement('button');
toggleButton.className = 'toggle-view-btn';
toggleButton.setAttribute('aria-pressed', simplified ? 'true' : 'false');
toggleButton.textContent = simplified ? 'Switch to full view' : 'Switch to simplifed view';
toggleButton.addEventListener('click', () => {
simplified = !simplified;
if (simplified) renderSimplified();
else renderFull();
});
heading.appendChild(toggleButton);
}
section.appendChild(heading);
const chart = document.createElement('div');
chart.className = 'chart';
chart.dataset.band = band.id;
band.blocks.forEach(b => {
const blockWidth = b.endFrequency - b.startFrequency;
const blk = document.createElement('div');
blk.className = `block ${b.type}`;
blk.style.flex = String(blockWidth);
blk.dataset.start = String(b.startFrequency);
blk.dataset.end = String(b.endFrequency);
blk.innerHTML = `
<strong>${b.owner}</strong>
<span>${formatBandwidth(blockWidth)} MHz</span>
`;
const frequency = `${b.startFrequency} - ${b.endFrequency} MHz`
+ (b.uplinkStartFrequency === undefined ? '' : ` downlink and ${b.uplinkStartFrequency} - ${b.uplinkEndFrequency} MHz uplink`);
const details = [
b.ownerLong && `<strong>Operated by:</strong><br>${b.ownerLong}`,
`<strong>Bandwidth:</strong><br>${formatBandwidth(blockWidth)} MHz (${frequency})`,
b.validUntil && `<strong>Valid until:</strong><br>${b.validUntil}`,
formatChannelDetails(b) && `<strong>Used for:</strong><br>${formatChannelDetails(b)}`,
typeof b.usedFor === 'string' && `<strong>Used for:</strong><br>${b.usedFor}`,
b.details && `<strong>Details:</strong><br>${b.details.replaceAll('\n', '<br>')}`
].filter(Boolean);
blk.dataset.details = details.join('<br><br>');
chart.appendChild(blk);
});
const start = document.createElement('div');
start.className = 'frequency start';
start.textContent = formatBandFrequency(band, 'start');
chart.appendChild(start);
const end = document.createElement('div');
end.className = 'frequency end';
end.textContent = formatBandFrequency(band, 'end');
chart.appendChild(end);
const hint = document.createElement('p');
hint.className = 'info';
hint.textContent = 'Click on a spectrum block to view details below.';
const details = document.createElement('div');
details.className = 'details';
section.append(chart, hint, details);
container.appendChild(section);
});
applyChannelHighlight();
}
function formatBandFrequency(band, edge) {
const downlink = edge === 'start' ? band.startFrequency : band.endFrequency;
if (band.type !== 'FDD') return `${downlink} MHz`;
const uplink = edge === 'start' ? band.uplinkStartFrequency : band.uplinkEndFrequency;
return `${downlink} / ${uplink} MHz`;
}
function getChartData(data) {
const totals = new Map();
data
.filter(band => selectedBand === 'overall' || bandGroups[selectedBand].has(band.id))
.flatMap(band => band.blocks)
.forEach(block => {
if (carrierColors[block.type]) {
const label = block.owner === 'T2' ? 'Tele2' : block.owner;
totals.set(label, (totals.get(label) || 0) + (block.endFrequency - block.startFrequency));
}
});
return [...totals.entries()].map(([label, value]) => ({
label,
value,
color: carrierColors[label.toLowerCase()]
}));
}
function renderPie(data) {
const chartData = getChartData(data);
const total = chartData.reduce((sum, item) => sum + item.value, 0);
pieChart.innerHTML = '';
pieLegend.innerHTML = '';
if (!total) {
pieChart.textContent = 'No spectrum data';
return;
}
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('viewBox', '0 0 200 200');
svg.setAttribute('aria-hidden', 'true');
let startAngle = -Math.PI / 2;
chartData.forEach(item => {
const angle = item.value / total * Math.PI * 2;
const endAngle = startAngle + angle;
const largeArc = angle > Math.PI ? 1 : 0;
const point = a => [100 + 100 * Math.cos(a), 100 + 100 * Math.sin(a)];
const start = point(startAngle);
const end = point(endAngle);
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute('d', `M 100 100 L ${start.join(' ')} A 100 100 0 ${largeArc} 1 ${end.join(' ')} Z`);
path.setAttribute('fill', item.color);
path.setAttribute('stroke', '#171717');
path.setAttribute('stroke-width', '2');
svg.appendChild(path);
startAngle = endAngle;
const legendItem = document.createElement('div');
legendItem.className = 'legend-item';
legendItem.innerHTML = `<span class="legend-swatch" style="background:${item.color}"></span><span>${item.label}: ${item.value.toFixed(1)} MHz (${(item.value / total * 100).toFixed(1)}%)</span>`;
pieLegend.appendChild(legendItem);
});
pieChart.appendChild(svg);
pieChart.setAttribute('aria-label', `${selectedBand} carrier spectrum share, ${total.toFixed(1)} MHz total`);
}
function renderFull() {
renderChart(fullData);
renderPie(fullData);
}
function getSimplifiedWithFallback() {
const simplifiedMap = new Map(simplifiedData.map(b => [b.id, b]));
const merged = fullData.map(fd => simplifiedMap.get(fd.id) || fd);
const fullIds = new Set(fullData.map(b => b.id));
simplifiedData.forEach(sd => { if (!fullIds.has(sd.id)) merged.push(sd); });
return merged;
}
function hasAlternateVersion(bandId) {
const fullBand = fullData.find(band => band.id === bandId);
const simplifiedBand = simplifiedData.find(band => band.id === bandId);
if (!fullBand || !simplifiedBand) return false;
return fullBand.blocks.length !== simplifiedBand.blocks.length
|| fullBand.blocks.some((block, index) => {
const simplifiedBlock = simplifiedBand.blocks[index];
return !simplifiedBlock
|| block.type !== simplifiedBlock.type
|| block.owner !== simplifiedBlock.owner
|| block.startFrequency !== simplifiedBlock.startFrequency
|| block.endFrequency !== simplifiedBlock.endFrequency;
});
}
function renderSimplified() {
const data = getSimplifiedWithFallback();
renderChart(data);
renderPie(data);
}
container.addEventListener('click', e => {
const blk = e.target.closest('.block');
if (!blk) return;
blk.parentNode.querySelectorAll('.block').forEach(b => b.classList.remove('selected'));
blk.classList.add('selected');
const sec = blk.closest('section');
const det = sec.querySelector('.details');
det.innerHTML = blk.dataset.details;
det.classList.add('visible');
});
if (bandFilter) {
bandFilter.addEventListener('click', event => {
const tab = event.target.closest('.band-tab');
if (!tab) return;
selectedBand = tab.dataset.filter;
bandFilter.querySelectorAll('.band-tab').forEach(item => {
const active = item === tab;
item.classList.toggle('active', active);
item.setAttribute('aria-selected', active ? 'true' : 'false');
});
renderPie(simplified ? getSimplifiedWithFallback() : fullData);
});
}
lookupMode.addEventListener('change', () => {
document.querySelectorAll('.lookup-inputs label').forEach(label => {
const visible = lookupMode.value === 'frequency'
? label.classList.contains('mode-frequency')
: label.classList.contains('mode-bandwidth');
label.hidden = !visible;
label.style.display = visible ? 'flex' : 'none';
});
});
lookupForm.addEventListener('submit', event => {
event.preventDefault();
let start;
let end;
if (lookupMode.value === 'lte-bandwidth') {
const channel = Number(channelNumber.value);
const bandwidth = Number(channelBandwidth.value);
if (!Number.isFinite(channel) || !Number.isFinite(bandwidth) || channel < 0 || bandwidth <= 0) {
lookupResult.textContent = 'Enter a valid channel and bandwidth.';
return;
}
const band = detectBandFromChannel(channel);
if (!band) {
lookupResult.textContent = 'That channel is outside the displayed frequency bands.';
return;
}
const frequency = channelFrequency(band, channel);
start = frequency - bandwidth / 2;
end = frequency + bandwidth / 2;
} else {
start = Number(startFrequency.value);
end = Number(endFrequency.value);
if (!Number.isFinite(start) || !Number.isFinite(end) || start < 0 || end <= start) {
lookupResult.textContent = 'Enter a valid start and end frequency.';
return;
}
}
const band = detectBand(start, end);
if (!band) {
clearChannelHighlight();
channelLookup = null;
lookupResult.textContent = 'No matching displayed frequency band was found.';
return;
}
channelLookup = { band, start, end };
lookupResult.textContent = `${band}: ${start.toFixed(2)} to ${end.toFixed(2)} MHz downlink`;
applyChannelHighlight();
document.getElementById(`band${band}`)?.scrollIntoView({ behavior: 'smooth', block: 'center' });
});
lookupMode.dispatchEvent(new Event('change'));
if (simplified) renderSimplified();
else renderFull();