@@ -328,6 +328,19 @@ Code.checkDeviceOnline = function (device) {
328328 device . inputArea = document . getElementById ( 'input-device' ) ;
329329 device . btn = document . getElementById ( 'check-btn' ) ;
330330 device . icon = document . getElementById ( 'check-icon' ) ;
331+ device . boardInst = null ;
332+
333+ document . addEventListener ( 'visibilitychange' , function ( evt ) {
334+ if ( ! document . hidden ) {
335+ device . check ( device . inputArea . value ) ;
336+ } else {
337+ if ( device . boardInst ) {
338+ device . boardInst . disconnect ( function ( ) {
339+ delete device . boardInst ;
340+ } ) ;
341+ }
342+ }
343+ } ) ;
331344
332345 if ( ! localStorage . boardCheckOpen ) {
333346 localStorage . boardCheckOpen = 0 ;
@@ -336,33 +349,44 @@ Code.checkDeviceOnline = function (device) {
336349 device . btn . onclick = function ( ) {
337350 if ( localStorage . boardCheckOpen == 1 ) {
338351 localStorage . boardCheckOpen = 0 ;
339- device . inputArea . className = device . inputArea . className . replace ( " open" , "" ) ;
352+ device . inputArea . className = device . inputArea . className . replace ( ' open' , '' ) ;
340353 } else {
341354 localStorage . boardCheckOpen = 1 ;
342- device . inputArea . className = device . inputArea . className + " open" ;
355+ device . inputArea . className = device . inputArea . className + ' open' ;
343356 }
344357 } ;
345358
346359 device . check = function ( v ) {
347- boardReady ( {
348- device : v ,
349- multi : true
350- } , true , function ( board ) {
351- device . icon . setAttribute ( 'class' , 'check board-online icon-power' ) ;
352- board . once ( webduino . BoardEvent . DISCONNECT , function ( e ) {
360+ if ( v . length > 3 && v . length <= 8 && v . indexOf ( '.' ) === - 1 ) {
361+ if ( device . boardInst ) {
362+ device . boardInst . disconnect ( function ( ) {
363+ doCheck ( ) ;
364+ } ) ;
365+ } else {
366+ doCheck ( ) ;
367+ }
368+
369+ function doCheck ( ) {
370+ var board = new webduino . WebArduino ( { device : v , multi : true , clientId : 'lightning' } ) ;
371+ board . on ( 'ready' , function ( ) {
372+ device . icon . setAttribute ( 'class' , 'check board-online icon-power' ) ;
373+ board . once ( webduino . BoardEvent . DISCONNECT , function ( e ) {
374+ device . icon . setAttribute ( 'class' , 'check board-error icon-power' ) ;
375+ if ( ! document . hidden ) {
376+ device . check ( v ) ;
377+ }
378+ } ) ;
379+ } ) ;
353380 device . icon . setAttribute ( 'class' , 'check board-error icon-power' ) ;
354- } ) ;
355- } ) ;
356- device . icon . setAttribute ( 'class' , 'check board-error icon-power' ) ;
381+ device . boardInst = board ;
382+ }
383+ }
357384 } ;
358385
359386 device . inputArea . oninput = function ( ) {
360387 localStorage . boardState = this . value ;
361- if ( this . value . length > 3 && this . value . length <= 8 && this . value . indexOf ( '.' ) === - 1 ) {
362- device . check ( this . value ) ;
363- } else {
364- device . icon . setAttribute ( 'class' , 'check icon-power' ) ;
365- }
388+ device . icon . setAttribute ( 'class' , 'check icon-power' ) ;
389+ device . check ( this . value ) ;
366390 } ;
367391
368392 if ( localStorage . boardState ) {
@@ -371,12 +395,12 @@ Code.checkDeviceOnline = function (device) {
371395 }
372396
373397 if ( localStorage . boardCheckOpen == 0 ) {
374- device . inputArea . className = device . inputArea . className . replace ( " open" , "" ) ;
398+ device . inputArea . className = device . inputArea . className . replace ( ' open' , '' ) ;
375399 } else if ( localStorage . boardCheckOpen == 1 && device . inputArea . value . length < 4 ) {
376400 localStorage . boardCheckOpen = 0 ;
377- device . inputArea . className = device . inputArea . className . replace ( " open" , "" ) ;
401+ device . inputArea . className = device . inputArea . className . replace ( ' open' , '' ) ;
378402 } else {
379- device . inputArea . className = device . inputArea . className + " open" ;
403+ device . inputArea . className = device . inputArea . className + ' open' ;
380404 }
381405
382406} ;
0 commit comments