blob: 8fbdb7c03b2d5aad363270bc81b3c492de15eb65 [file] [log] [blame]
James Kuszmaul5f5e1232020-12-22 20:58:00 -08001// This file provides an index of all standard plot configs.
2// In the future, this may be split into more pieces (e.g., to have
3// year-specific indices). For now, the pattern for creating a new plot
4// is that you provide an exported function (a la the plot*() functions imported
5// below) which, when called, will generate the plot in the provided div.
6// This file handles providing a master list of all known plots so that
7// the user can just open a single web-page and select the plot that they want
8// from a drop-down. A given plot will not be loaded until it has been selected
9// once, at which point it will stay loaded. This means that if the user wants
10// to switch between several plot configs, they don't incur any performance
11// penalty associated with swapping.
12// By default, no plot is selected, but the plot= URL parameter may be used
13// to specify a specific plot, so that people can create links to a specific
14// plot.
15// The plot*() functions are called *after* we have already received a valid
16// config from the web server, so config handlers do not need to be used.
17//
18// The exact setup of this will be in flux as we add more configs and figure out
19// what setups work best--we will likely end up with separate index files for
20// each robot year, and may even end up allowing plots to be specified solely
21// using JSON rather than requiring people to write a script just to create
22// a plot.
23import * as configuration from 'org_frc971/aos/configuration_generated';
24import * as proxy from 'org_frc971/aos/network/www/proxy';
25import {plotImu} from 'org_frc971/frc971/wpilib/imu_plotter';
26import {plotDemo} from 'org_frc971/aos/network/www/demo_plot';
27
28import Connection = proxy.Connection;
29import Configuration = configuration.aos.Configuration;
30
31const rootDiv = document.createElement('div');
32document.body.appendChild(rootDiv);
33
34const helpDiv = document.createElement('div');
35rootDiv.appendChild(helpDiv);
36helpDiv.innerHTML =
James Kuszmaul461b0682020-12-22 22:20:21 -080037 'Help: click + drag to pan, double click to reset, scroll to zoom, ' +
38 'right-click + drag to zoom to rectangle, Esc to cancel. ' +
James Kuszmaul5f5e1232020-12-22 20:58:00 -080039 'Hold the x/y keys to only pan/zoom along the x/y axes.';
40
41class PlotState {
42 public readonly div: HTMLElement;
43 private initialized = false;
44 constructor(
45 parentDiv: HTMLElement,
46 private readonly initializer:
47 (conn: Connection, element: Element) => void) {
48 this.div = document.createElement('div');
49 parentDiv.appendChild(this.div);
50 this.hide();
51 }
52 initialize(conn: Connection): void {
53 if (!this.initialized) {
54 this.initializer(conn, this.div);
55 this.initialized = true;
56 }
57 }
58 hide(): void {
59 this.div.style.display = "none";
60 }
61 show(): void {
62 this.div.style.display = "block";
63 }
64}
65
66const plotSelect = document.createElement('select');
67rootDiv.appendChild(plotSelect);
68
69const plotDiv = document.createElement('div');
70plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString();
71plotDiv.style.left = '0';
72plotDiv.style.position = 'absolute';
73rootDiv.appendChild(plotDiv);
74
75// The master list of all the plots that we provide. For a given config, it
76// is possible that not all of these plots will be usable depending on the
77// presence of certain channels.
78const plotIndex = new Map<string, PlotState>([
79 ['Demo', new PlotState(plotDiv, plotDemo)],
80 ['IMU', new PlotState(plotDiv, plotImu)]
81]);
82
83const invalidSelectValue = 'null';
84function getDefaultPlot(): string {
85 const urlParams = (new URL(document.URL)).searchParams;
86 const urlParamKey = 'plot';
87 if (!urlParams.has(urlParamKey)) {
88 return invalidSelectValue;
89 }
90 const desiredPlot = urlParams.get(urlParamKey);
91 if (!plotIndex.has(desiredPlot)) {
92 return invalidSelectValue;
93 }
94 return desiredPlot;
95}
96
97const conn = new Connection();
98
99conn.connect();
100
101conn.addConfigHandler((config: Configuration) => {
102 plotSelect.add(new Option("Select Plot", invalidSelectValue));
103 for (const name of plotIndex.keys()) {
104 plotSelect.add(new Option(name, name));
105 }
106 plotSelect.addEventListener('input', () => {
107 for (const plot of plotIndex.values()) {
108 plot.hide();
109 }
110 if (plotSelect.value == invalidSelectValue) {
111 return;
112 }
113 plotIndex.get(plotSelect.value).initialize(conn);
114 plotIndex.get(plotSelect.value).show();
115 });
116 plotSelect.value = getDefaultPlot();
117 // Force the event to occur once at the start.
118 plotSelect.dispatchEvent(new Event('input'));
119});