James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 1 | // 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. |
| 23 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
| 24 | import * as proxy from 'org_frc971/aos/network/www/proxy'; |
| 25 | import {plotImu} from 'org_frc971/frc971/wpilib/imu_plotter'; |
| 26 | import {plotDemo} from 'org_frc971/aos/network/www/demo_plot'; |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame^] | 27 | import {plotData} from 'org_frc971/frc971/analysis/plot_data_utils'; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 28 | |
| 29 | import Connection = proxy.Connection; |
| 30 | import Configuration = configuration.aos.Configuration; |
| 31 | |
| 32 | const rootDiv = document.createElement('div'); |
| 33 | document.body.appendChild(rootDiv); |
| 34 | |
| 35 | const helpDiv = document.createElement('div'); |
| 36 | rootDiv.appendChild(helpDiv); |
| 37 | helpDiv.innerHTML = |
James Kuszmaul | 461b068 | 2020-12-22 22:20:21 -0800 | [diff] [blame] | 38 | 'Help: click + drag to pan, double click to reset, scroll to zoom, ' + |
| 39 | 'right-click + drag to zoom to rectangle, Esc to cancel. ' + |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 40 | 'Hold the x/y keys to only pan/zoom along the x/y axes.'; |
| 41 | |
| 42 | class PlotState { |
| 43 | public readonly div: HTMLElement; |
| 44 | private initialized = false; |
| 45 | constructor( |
| 46 | parentDiv: HTMLElement, |
| 47 | private readonly initializer: |
| 48 | (conn: Connection, element: Element) => void) { |
| 49 | this.div = document.createElement('div'); |
| 50 | parentDiv.appendChild(this.div); |
| 51 | this.hide(); |
| 52 | } |
| 53 | initialize(conn: Connection): void { |
| 54 | if (!this.initialized) { |
| 55 | this.initializer(conn, this.div); |
| 56 | this.initialized = true; |
| 57 | } |
| 58 | } |
| 59 | hide(): void { |
| 60 | this.div.style.display = "none"; |
| 61 | } |
| 62 | show(): void { |
| 63 | this.div.style.display = "block"; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | const plotSelect = document.createElement('select'); |
| 68 | rootDiv.appendChild(plotSelect); |
| 69 | |
| 70 | const plotDiv = document.createElement('div'); |
| 71 | plotDiv.style.top = (plotSelect.getBoundingClientRect().bottom + 10).toString(); |
| 72 | plotDiv.style.left = '0'; |
| 73 | plotDiv.style.position = 'absolute'; |
| 74 | rootDiv.appendChild(plotDiv); |
| 75 | |
| 76 | // The master list of all the plots that we provide. For a given config, it |
| 77 | // is possible that not all of these plots will be usable depending on the |
| 78 | // presence of certain channels. |
| 79 | const plotIndex = new Map<string, PlotState>([ |
| 80 | ['Demo', new PlotState(plotDiv, plotDemo)], |
James Kuszmaul | 4867136 | 2020-12-24 13:54:16 -0800 | [diff] [blame^] | 81 | ['IMU', new PlotState(plotDiv, plotImu)], |
| 82 | ['C++ Plotter', new PlotState(plotDiv, plotData)], |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 83 | ]); |
| 84 | |
| 85 | const invalidSelectValue = 'null'; |
| 86 | function getDefaultPlot(): string { |
| 87 | const urlParams = (new URL(document.URL)).searchParams; |
| 88 | const urlParamKey = 'plot'; |
| 89 | if (!urlParams.has(urlParamKey)) { |
| 90 | return invalidSelectValue; |
| 91 | } |
| 92 | const desiredPlot = urlParams.get(urlParamKey); |
| 93 | if (!plotIndex.has(desiredPlot)) { |
| 94 | return invalidSelectValue; |
| 95 | } |
| 96 | return desiredPlot; |
| 97 | } |
| 98 | |
| 99 | const conn = new Connection(); |
| 100 | |
| 101 | conn.connect(); |
| 102 | |
| 103 | conn.addConfigHandler((config: Configuration) => { |
| 104 | plotSelect.add(new Option("Select Plot", invalidSelectValue)); |
| 105 | for (const name of plotIndex.keys()) { |
| 106 | plotSelect.add(new Option(name, name)); |
| 107 | } |
| 108 | plotSelect.addEventListener('input', () => { |
| 109 | for (const plot of plotIndex.values()) { |
| 110 | plot.hide(); |
| 111 | } |
| 112 | if (plotSelect.value == invalidSelectValue) { |
| 113 | return; |
| 114 | } |
| 115 | plotIndex.get(plotSelect.value).initialize(conn); |
| 116 | plotIndex.get(plotSelect.value).show(); |
| 117 | }); |
| 118 | plotSelect.value = getDefaultPlot(); |
| 119 | // Force the event to occur once at the start. |
| 120 | plotSelect.dispatchEvent(new Event('input')); |
| 121 | }); |