9 Replies Latest reply on Sep 11, 2009 4:22 AM by r_olsen

    UI: looking for JS advice on how to avoid numerous if statements..

    r_olsen Level 1

      Cheers,

       

      With a simple UI (the user may tick several checkboxes, radiobuttons and/or fill in stuff with text editboxes), I end up with many many if statements trying to find out what the user actually clicked/typed.

      Eg. I've got 3 checkboxes - I would then go ahead and write several statements like if(1 && 2 && 3), if(! 1 && 2 && 3), etc. There's so many possible combinations so this will get rather overwhelming in no time. There has to be a smarter way to do this?

       

      Thanks,

       

      Rasmus

        • 1. Re: UI: looking for JS advice on how to avoid numerous if statements..
          Harbs. Level 6

          What I generally do, is build a js object for the whole set of UI 

          options.

           

          i.e.:

           

          var options =

           

          You then can check each property, when applicable in your script very 

          easily.

           

          Harbs

          • 2. Re: UI: looking for JS advice on how to avoid numerous if statements..
            [Jongware] Most Valuable Participant

            3 checkboxes will allow 8 different combinations; for 4, this becomes 16 -- and so on. Then again, you don't have to check every possible combination!

             

            I'd have to see your dialog, but suppose you have a 4 checkboxes to change text:

            [ ] Centered

            [ ] Underlined

            [ ] Large

            [ ] Small

             

            Large & Small are mutually exclusive, so change these to radio buttons:

            [ ] Centered

            [ ] Underlined

            ( ) Large

            (*) Normal

            ( ) Small

             

            ("Normal" is added so you have a default option.) Do you need to check for all possible combinations of 'centered' and 'underlined'? Well, no -- you can process one first, then the other, as they have nothing in common.

            1 person found this helpful
            • 3. Re: UI: looking for JS advice on how to avoid numerous if statements..
              r_olsen Level 1

              Hey Harbs,

               

              Quick reply, thanks. Even with an object I would still have to write all the if statements?

              Instead of if ( ! oCheckBox1 && oCheckBox2 && oCheckBox3 ) {} it would object wise be if ( ! oObject.property1 && oObject.property2 && oObject.property3 ){}. Sorry for being a little dense - care to write a quick example?

               

              Rasmus

              • 4. Re: UI: looking for JS advice on how to avoid numerous if statements..
                Harbs. Level 6

                I'm not sure exactly what you're after. You should try to break it 

                down into pieces as much as possible. It's hard to imagine a scenario 

                where you'd really need to do totally different things based on all 

                the permutations.

                 

                If you have to step through all the possibilities, using "switch/case" 

                might organize it a bit better...

                 

                Harbs

                • 5. Re: UI: looking for JS advice on how to avoid numerous if statements..
                  r_olsen Level 1

                  Jong,

                   

                  Good point with the radiobuttons. Actually this is something I will try with the first row (doBleed, doSlug). The clear unused swatches and paragraphstyles is something I've written into the script yesterday. Haven't started writing the if's in the showUI function yet. The possible combinations is just overwhelming.

                   

                  Thanks,

                  Rasmus

                   

                  with ( oDialog )
                  {
                      with (dialogColumns.add() )
                      {

                          // Left Coloumn
                          with (dialogRows.add() )
                              staticTexts.add({staticLabel:"Cleanup pages"} );
                          with(borderPanels.add() ){
                              with(dialogColumns.add() ){
                                  with (dialogRows.add() )
                                      var doBleed = checkboxControls.add( {staticLabel:"Bleed"} )
                                      var doSlug = checkboxControls.add( {staticLabel:"Slug"} )
                                      var doCustom = textEditboxes.add( {editContents:"custom offset in mm.",minWidth:120} )
                                  }
                              }
                      }
                              // Middle Column
                              with (dialogColumns.add() ){
                                  with (dialogRows.add() )
                                      staticTexts.add({staticLabel:"Cleanup stuff"} );
                                  with(borderPanels.add() ){
                                      with(dialogColumns.add() ){
                                      with (dialogRows.add() )
                                          var doSwatches = checkboxControls.add( {staticLabel:"Unused swatches"} )
                                          var doStyles = checkboxControls.add( {staticLabel:"Unused paragraphstyles"} )
                                          }
                                      }
                              }
                              // Right Column
                      with (dialogColumns.add() ){
                                  with (dialogRows.add() )
                                      staticTexts.add({staticLabel:"Written by"} );
                                  with(borderPanels.add() ){
                                      with(dialogColumns.add() ){
                                      with (dialogRows.add() )
                                          staticTexts.add({staticLabel:"Rasmus Olsen"} );
                                          with(dialogRows.add() ){
                                          staticTexts.add({staticLabel:"Version 1.4"} );
                                          }
                                      }
                                  }
                              }
                  }


                  function showUI ( oDoc, oDialog, oArray )
                  {   
                      if ( oDialog.show() == true )
                      {           
                              if ( myBleedCheck.checkedState && !mySlugCheck.checkedState && myCustomEntry.editContents.length == 0  )

                              {

                                  cleanup ( oDoc, oArray, "bleed" );

                                  oDialog.destroy();
                              }
                              else if ( !myBleedCheck.checkedState && mySlugCheck.checkedState && myCustomEntry.editContents.length == 0  )
                              {
                                  cleanup ( oDoc, oArray, "slug" );
                                  oDialog.destroy();
                              }
                              else if ( !myBleedCheck.checkedState && !mySlugCheck.checkedState && myCustomEntry.editContents.length == 0  )
                              {
                                  alert ( "Click something.." );
                                  oDialog.show() == true;
                              }
                              else if ( !myBleedCheck.checkedState && !mySlugCheck.checkedState && myCustomEntry.editContents.length > 0  )
                              {
                                  cleanup ( oDoc, oArray, parseInt ( myCustomEntry.editContents ) );
                                  oDialog.destroy();
                              }
                              else if     ( 
                                              ( myBleedCheck.checkedState && mySlugCheck.checkedState && myCustomEntry.editContents.length == 0 )
                                          ||
                                              ( myBleedCheck.checkedState && mySlugCheck.checkedState && myCustomEntry.editContents.length > 0 )
                                          ||
                                              ( !myBleedCheck.checkedState && mySlugCheck.checkedState && myCustomEntry.editContents.length > 0 )
                                          ||
                                              ( myBleedCheck.checkedState && !mySlugCheck.checkedState && myCustomEntry.editContents.length > 0 )
                                          )
                              {
                                  alert("You click too much soldier.. try again");
                                  oDialog.show() == true;
                              }
                      }
                      else
                      {
                          oDialog.destroy();       
                      }
                  }

                  • 6. Re: UI: looking for JS advice on how to avoid numerous if statements..
                    Harbs. Level 6

                    Okay. Now that I see what you're doing, here's some pointers:

                     

                    First off -- for validation, Script UI is really the way to go. But 

                    either way, you want to make the Bleed, Slug and Custom thingees 

                    radiobuttons rather than checkboxes, since they are mutually exclusive.

                     

                    Now once they are radio buttons, you just create a variable "area" 

                    which would ave a value of either "bleed" "slug", or "custom". You 

                    pass this variable to your function instead of the string. If it's 

                    custom, you'd pass the measurements as well. You just saved a bunch of 

                    "ifs". And so-on and so-forth...

                     

                    Harbs

                    1 person found this helpful
                    • 7. Re: UI: looking for JS advice on how to avoid numerous if statements..
                      r_olsen Level 1

                      I'm not sure I understand how you avoid parsing the string? Rewritten lines:

                       

                          with (dialogColumns.add() )
                          {
                              // Left Column
                              with (dialogRows.add() )
                                  staticTexts.add({staticLabel:"Cleanup pages"} );
                              with(borderPanels.add() ){
                                  with(dialogColumns.add() ){
                                      with (dialogRows.add() )
                                          var rLColumn = radiobuttonGroups.add();
                                          with ( rLColumn ){
                                                  radiobuttonControls.add( { staticLabel:"Bleed", checkedState:true } );
                                                  radiobuttonControls.add({staticLabel:"Slug"} );
                                                  radiobuttonControls.add({staticLabel:"Custom", minWidth:140} );
                                          }
                                      var tLColumn = textEditboxes.add( {editContents:"custom offset in mm.",minWidth:120} )
                                      }
                                  }
                          }

                       

                      function showUI ( oDoc, oDialog, oArray )
                      {   
                          if ( oDialog.show() )
                          {           
                                 
                                  if ( rLColumn.selectedButton == 0 )
                                      cleanup ( oDoc, oArray, "bleed" );
                                     
                                  if ( rLColumn.selectedButton == 1 )
                                      cleanup ( oDoc, oArray, "slug" );   
                                     
                                  if ( rLColumn.selectedButton == 2 )
                                      cleanup ( oDoc, oArray, parseInt ( tLColumn.editContents ) );

                          }
                          else
                          {
                              oDialog.destroy();       
                          }
                      }

                      • 8. Re: UI: looking for JS advice on how to avoid numerous if statements..
                        Harbs. Level 6

                        Something like this:

                         

                             if ( oDialog.show() )//Get all your variables

                             {

                                switch(rLColoumn.selectedButton)

                                {

                                    case 0: var area = "bleed";break;

                                    case 1: var area = "slug";break;

                                    case 2: var area = "custom";break;

                                }

                                var areaValue = tLColoumn.editValue;

                                ......

                                oDialog.destroy();

                         

                             } else {//bail out if the dialog is canceled

                                oDialog.destroy();

                                return;

                             }

                        //Now use your variables to run your functions...

                         

                        cleanup ( oDoc, oArray, area, areaValue );

                        ....

                         

                        Harbs

                        • 9. Re: UI: looking for JS advice on how to avoid numerous if statements..
                          r_olsen Level 1

                          Ah, the path of enlightment. Thanks.

                           

                          Rasmus