@@ -23,39 +23,53 @@ const updateTarget = value => {
2323// Populate the list of available devices
2424const updateAvailableDevices = ( ) =>
2525 refresh ( ) . then ( ports => {
26- const dropDown = $ ( '#port_list' ) ;
26+ const ul = $ ( '#port_ul' ) ;
27+ ul . innerHTML = '' ;
2728 updateStatus ( `found ${ ports . length } ports connected` ) ;
28- dropDown . innerHTML = '' ;
2929
3030 ports . forEach ( port => {
31- const newOption = document . createElement ( 'option ' ) ;
32- newOption . text = port . comName ;
33- newOption . value = port . comName ;
34- dropDown . appendChild ( newOption ) ;
31+ const newLi = document . createElement ( 'li ' ) ;
32+ newLi . setAttribute ( 'data-value' , port . comName ) ;
33+ newLi . innerHTML = port . comName ;
34+ ul . appendChild ( newLi ) ;
3535 } ) ;
3636 } ) ;
3737
3838const onData = response => {
3939 // Callback function when data is read
4040 updateTarget ( response ) ;
4141 sound . play ( ) ;
42- updateStatus ( 'reading...completed' ) ;
4342} ;
4443
4544// Handle the 'Connect' button
4645const connectToDevice = ( ) => {
47- const dropDown = $ ( '#port_list' ) ;
48- const devicePath = dropDown . options [ dropDown . selectedIndex ] . value ;
49- connect ( devicePath , onData ) . then ( ( ) => {
50- updateStatus ( 'connected' ) ;
51- } ) ;
46+ const active = $ ( '.active' ) ;
47+ if ( ! active ) {
48+ updateStatus ( 'No device selected' ) ;
49+ } else {
50+ const devicePath = active . getAttribute ( 'data-value' ) ;
51+ connect ( devicePath , onData )
52+ . then ( ( ) => {
53+ $ ( '#overlay' ) . classList . add ( 'hidden' ) ;
54+ } )
55+ . catch ( ( ) => {
56+ updateStatus ( 'Error connecting!' ) ;
57+ } ) ;
58+ }
59+ } ;
60+
61+ const setActive = e => {
62+ if ( e . target && e . target . matches ( 'li' ) ) {
63+ const currentActive = $ ( '.active' ) ;
64+ if ( currentActive ) {
65+ currentActive . classList . remove ( 'active' ) ;
66+ }
67+ e . target . classList . add ( 'active' ) ;
68+ }
5269} ;
5370
71+ $ ( '#port_ul' ) . addEventListener ( 'click' , setActive ) ;
5472$ ( '#refresh_button' ) . addEventListener ( 'click' , updateAvailableDevices ) ;
5573$ ( '#connect_button' ) . addEventListener ( 'click' , connectToDevice ) ;
56- $ ( '#fullscreen' ) . addEventListener ( 'click' , ( ) => { } ) ;
57- $ ( '#test' ) . addEventListener ( 'click' , ( ) => {
58- updateTarget ( 'test' ) ;
59- } ) ;
6074
6175updateAvailableDevices ( ) ;
0 commit comments