@@ -2000,83 +2000,84 @@ Blockly.Blocks['data_firebase_write'] = {
20002000 . appendField ( new Blockly . FieldVariable ( "myFirebase" ) , "var_" )
20012001 . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATA , "寫入資料" ) ;
20022002 this . setColour ( 160 ) ;
2003- this . itemCount_ = 2 ;
2004- this . updateShape_ ( ) ;
2005- this . setMutator ( new Blockly . Mutator ( [ 'data_firebase_write_item' ] ) ) ;
2003+ this . appendValueInput ( 'data_0' )
2004+ . setAlign ( Blockly . ALIGN_RIGHT )
2005+ . appendField ( '資料 1 名稱:' )
2006+ . appendField ( new Blockly . FieldTextInput ( "..." ) , "name_0" )
2007+ . appendField ( '值:' ) ;
2008+ this . appendValueInput ( 'data_1' )
2009+ . setAlign ( Blockly . ALIGN_RIGHT )
2010+ . appendField ( '資料 2 名稱:' )
2011+ . appendField ( new Blockly . FieldTextInput ( "..." ) , "name_1" )
2012+ . appendField ( '值:' ) ;
20062013 this . setTooltip ( '' ) ;
20072014 this . setPreviousStatement ( true ) ;
20082015 this . setNextStatement ( true ) ;
2016+ this . setMutator ( new Blockly . Mutator ( [ 'data_firebase_write_item' ] ) ) ;
20092017 this . setHelpUrl ( 'http://www.example.com/' ) ;
2018+ this . itemCount_ = 2 ;
20102019 } ,
2011- mutationToDom : function ( ) {
2020+ mutationToDom : function ( workspace ) {
20122021 var container = document . createElement ( 'mutation' ) ;
20132022 container . setAttribute ( 'items' , this . itemCount_ ) ;
20142023 return container ;
20152024 } ,
2016- domToMutation : function ( xmlElement ) {
2017- this . itemCount_ = parseInt ( xmlElement . getAttribute ( 'items' ) , 10 ) ;
2018- this . updateShape_ ( ) ;
2025+ domToMutation : function ( container ) {
2026+ for ( var x = 0 ; x < this . itemCount_ ; x ++ ) {
2027+ this . removeInput ( 'data_' + x ) ;
2028+ }
2029+ this . itemCount_ = parseInt ( container . getAttribute ( 'items' ) , 10 ) ;
2030+ for ( var x = 0 ; x < this . itemCount_ ; x ++ ) {
2031+ var input = this . appendValueInput ( 'data_' + x )
2032+ . setAlign ( Blockly . ALIGN_RIGHT )
2033+ . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATACOLUMN + ( x + 1 ) + Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATANAME )
2034+ . appendField ( new Blockly . FieldTextInput ( "..." ) , "name_" + x )
2035+ . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATAVAL ) ;
2036+ }
20192037 } ,
20202038 decompose : function ( workspace ) {
2021- var containerBlock = Blockly . Block . obtain ( workspace ,
2022- 'data_firebase_write_container' ) ;
2039+ var containerBlock = Blockly . Block . obtain ( workspace , 'data_firebase_write_container' ) ;
20232040 containerBlock . initSvg ( ) ;
20242041 var connection = containerBlock . getInput ( 'STACK' ) . connection ;
2025- for ( var i = 0 ; i < this . itemCount_ ; i ++ ) {
2026- var itemBlock = Blockly . Block . obtain ( workspace , 'data_firebase_write_item' ) ;
2027- itemBlock . initSvg ( ) ;
2028- connection . connect ( itemBlock . previousConnection ) ;
2029- connection = itemBlock . nextConnection ;
2042+ for ( var x = 0 ; x < this . itemCount_ ; x ++ ) {
2043+ var optionBlock = Blockly . Block . obtain ( workspace , 'data_firebase_write_item' ) ;
2044+ optionBlock . initSvg ( ) ;
2045+ connection . connect ( optionBlock . previousConnection ) ;
2046+ connection = optionBlock . nextConnection ;
20302047 }
20312048 return containerBlock ;
20322049 } ,
20332050 compose : function ( containerBlock ) {
2034- var itemBlock = containerBlock . getInputTargetBlock ( 'STACK' ) ;
2035- var connections = [ ] ;
2036- while ( itemBlock ) {
2037- connections . push ( itemBlock . valueConnection_ ) ;
2038- itemBlock = itemBlock . nextConnection &&
2039- itemBlock . nextConnection . targetBlock ( ) ;
2051+ for ( var x = this . itemCount_ - 1 ; x >= 0 ; x -- ) {
2052+ this . removeInput ( 'data_' + x ) ;
20402053 }
2041- this . itemCount_ = connections . length ;
2042- this . updateShape_ ( ) ;
2043- for ( var i = 0 ; i < this . itemCount_ ; i ++ ) {
2044- if ( connections [ i ] ) {
2045- this . getInput ( 'data_' + i ) . connection . connect ( connections [ i ] ) ;
2054+ this . itemCount_ = 0 ;
2055+ var optionBlock = containerBlock . getInputTargetBlock ( 'STACK' ) ;
2056+ while ( optionBlock ) {
2057+ var input = this . appendValueInput ( 'data_' + this . itemCount_ )
2058+ . setAlign ( Blockly . ALIGN_RIGHT )
2059+ . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATACOLUMN + ( this . itemCount_ + 1 ) + Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATANAME )
2060+ . appendField ( new Blockly . FieldTextInput ( optionBlock . nameData_ || "..." ) , "name_" + this . itemCount_ )
2061+ . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATAVAL ) ;
2062+ if ( optionBlock . dataData_ ) {
2063+ input . connection . connect ( optionBlock . dataData_ ) ;
20462064 }
2065+ this . itemCount_ ++ ;
2066+ optionBlock = optionBlock . nextConnection &&
2067+ optionBlock . nextConnection . targetBlock ( ) ;
20472068 }
20482069 } ,
20492070 saveConnections : function ( containerBlock ) {
2050- var itemBlock = containerBlock . getInputTargetBlock ( 'STACK' ) ;
2051- var i = 0 ;
2052- while ( itemBlock ) {
2053- var input = this . getInput ( 'data_' + i ) ;
2054- itemBlock . valueConnection_ = input && input . connection . targetConnection ;
2055- i ++ ;
2056- itemBlock = itemBlock . nextConnection &&
2057- itemBlock . nextConnection . targetBlock ( ) ;
2058- }
2059- } ,
2060- updateShape_ : function ( ) {
2061- var v = [ ] ;
2062- if ( this . getInput ( 'EMPTY' ) ) {
2063- this . removeInput ( 'EMPTY' ) ;
2064- } else {
2065- var i = 0 ;
2066- while ( this . getInput ( 'data_' + i ) ) {
2067- v [ i ] = this . getInput ( 'data_' + i ) . fieldRow [ 1 ] . text_ ;
2068- this . removeInput ( 'data_' + i ) ;
2069- i ++ ;
2070- }
2071- }
2072- if ( this . itemCount_ == 0 ) { } else {
2073- for ( var i = 0 ; i < this . itemCount_ ; i ++ ) {
2074- this . appendValueInput ( 'data_' + i )
2075- . setAlign ( Blockly . ALIGN_RIGHT )
2076- . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATACOLUMN + ( i + 1 ) + Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATANAME )
2077- . appendField ( new Blockly . FieldTextInput ( '...' ) , "name_" + i )
2078- . appendField ( Blockly . Msg . WEBDUINO_FIREBASE_WRITEDATAVAL ) ;
2079- }
2071+ var optionBlock = containerBlock . getInputTargetBlock ( 'STACK' ) ;
2072+ var x = 0 ;
2073+ while ( optionBlock ) {
2074+ var name = this . getFieldValue ( 'name_' + x ) ;
2075+ var data = this . getInput ( 'data_' + x ) ;
2076+ optionBlock . nameData_ = name ;
2077+ optionBlock . dataData_ = data && data . connection . targetConnection ;
2078+ x ++ ;
2079+ optionBlock = optionBlock . nextConnection &&
2080+ optionBlock . nextConnection . targetBlock ( ) ;
20802081 }
20812082 } ,
20822083 newQuote_ : Blockly . Blocks [ 'text' ] . newQuote_
0 commit comments