|
| 1 | +"use strict"; |
| 2 | +/** |
| 3 | + The functions in this file are still experimental in nature, and as we |
| 4 | + expand the number of functions in this file it is expected that the API |
| 5 | + will change. |
| 6 | +
|
| 7 | + Possible API additions in the future: |
| 8 | + - attach button to boolean |
| 9 | + - attach button to number |
| 10 | + - attach button to boolean (toggle) -- auto update class when clicked |
| 11 | + - attach chooser to list of buttons |
| 12 | + -> needs a list of button ids |
| 13 | +*/ |
| 14 | + |
| 15 | + |
| 16 | +// requirements |
| 17 | +if (d3 === undefined) { |
| 18 | + alert("d3.js must be downloaded+included to use utils.js!"); |
| 19 | +} |
| 20 | + |
| 21 | +if ($ === undefined) { |
| 22 | + alert("jQuery must be downloaded+included to use utils.js!"); |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + Given the id of an HTML select element and the key name of a SendableChooser |
| 29 | + object setup in networktables, this will sync the select combo box with the |
| 30 | + contents of the SendableChooser, and you will be able to select an object |
| 31 | + using the select element. |
| 32 | +
|
| 33 | + :param html_id: An ID of an HTML select element |
| 34 | + :param nt_key: The name of the NetworkTables key that the SendableChooser |
| 35 | + is associated with |
| 36 | +
|
| 37 | + See the WPILib documentation for information on how to use SendableChooser |
| 38 | + in your robot's program. |
| 39 | +*/ |
| 40 | +function attachSelectToSendableChooser(html_id, nt_key) { |
| 41 | + |
| 42 | + if (!nt_key.startsWith('/')) { |
| 43 | + nt_key = '/SmartDashboard' + nt_key; |
| 44 | + } |
| 45 | + |
| 46 | + function update(key, value, isNew) { |
| 47 | + updateSelectWithChooser(html_id, nt_key); |
| 48 | + } |
| 49 | + |
| 50 | + NetworkTables.addKeyListener(nt_key + '/options', update, true); |
| 51 | + NetworkTables.addKeyListener(nt_key + '/default', update, true); |
| 52 | + NetworkTables.addKeyListener(nt_key + '/selected', update, true); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +/** |
| 57 | + This function is designed to be used from the onValueChanged callback |
| 58 | + whenever values from a SendableChooser change, but you probably should |
| 59 | + prefer to use attachSendableChooserToCombo instead. |
| 60 | +
|
| 61 | + See attachSelectToSendableChooser documentation. |
| 62 | +*/ |
| 63 | +function updateSelectWithChooser(html_id, nt_key) { |
| 64 | + |
| 65 | + var options = NetworkTables.getValue(nt_key + '/options'); |
| 66 | + if (options === undefined) |
| 67 | + return; |
| 68 | + |
| 69 | + var optDefault = NetworkTables.getValue(nt_key + '/default'); |
| 70 | + var selected = NetworkTables.getValue(nt_key + '/selected'); |
| 71 | + |
| 72 | + var opt = d3.select(html_id) |
| 73 | + .selectAll("option") |
| 74 | + .data(options); |
| 75 | + |
| 76 | + opt.enter() |
| 77 | + .append("option"); |
| 78 | + |
| 79 | + opt.text(function(d,i){ |
| 80 | + return options[i]; |
| 81 | + }); |
| 82 | + |
| 83 | + opt.exit().remove(); |
| 84 | + |
| 85 | + if (selected !== undefined) { |
| 86 | + $(html_id).val(selected); |
| 87 | + } else if (optDefault !== undefined) { |
| 88 | + $(html_id).val(optDefault); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | + |
0 commit comments