Wednesday, January 11, 2012

embarassed by the ghost of JavaScript Past

I wrote some unforgivable JavaScript in an MVC3 view here. I've been asked to clean it up. My code for my first pass is below. I mostly got away from using arrays in favor of using hashes. In the one place where I do use an array, I use push to put things into the array instead of the terrible, ghetto approach I was undertaking in which I would:

  1. go through a loop adding commas to a string
  2. make an array by splitting the string, thus defining the size of the array
  3. go through back through the loop I had just been through in order to populate the slots in the array
Share photos on twitter with Twitpic
<body onload="boo()">

 
 

I did the steps above in the misguided assumption that an array in JavaScript was like an array in C# and I would hence have to concretely define the length of the array at its instantiation. This was not so. Arrays may grow over time in JavaScript. The code below functions a little differently than what the code here did. I'll explain later.

 
 

@using AMD.Avalanche.Web.UI.Util

@model ProgramPlanShuffleListFiltrationHelper

<script type="text/javascript">

   var dtoPrograms =

         jQuery.parseJSON('@Html.Raw(Model.SerializedProgramPlansForPrograms)');

   var dtoSampleTypes =

         jQuery.parseJSON('@Html.Raw(Model.SerializedProgramPlansForSampleTypes)');

   var endList = new Object();

   var oldList = new Object();

   

   $(function () {

      setupProgramPlansShuffleList();

      $('#SelectedSampleTypeId').keypress(function (e) {

         switch (e.keyCode) {

            case 38:

               adjustProgramPlansShuffleList();

               break;

            case 40:

               adjustProgramPlansShuffleList();

               break;

            case 13:

               adjustProgramPlansShuffleList();

               break;

         }

      }).focus(function () {

         adjustProgramPlansShuffleList();

      }).click(function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-available').bind('onSelectedChanged', function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-selection').bind('onSelectedChanged', function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-to-selected').focus(function () {

         adjustProgramPlansShuffleList();

      }).click(function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-all-to-selected').focus(function () {

         adjustProgramPlansShuffleList();

      }).click(function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-all-to-available').focus(function () {

         adjustProgramPlansShuffleList();

      }).click(function () {

         adjustProgramPlansShuffleList();

      });

      $('#programsGroup-to-available').focus(function () {

         adjustProgramPlansShuffleList();

      }).click(function () {

         adjustProgramPlansShuffleList();

      });

   });

   

   function adjustProgramPlansShuffleList() {

      cleanUpNameBasedFiltration();

      prepareProgramPlanList(dtoPrograms, dtoSampleTypes);

      revampOldList();

      revampCurrentlyAvailableProgramPlans();

      revampOldList();

      revampCurrentlySelectedProgramPlans();

   }

   

   function setupProgramPlansShuffleList() {

      cleanUpNameBasedFiltration();

      prepareProgramPlanList(dtoPrograms, dtoSampleTypes);

      setupOldList();

      setupCurrentlySelectedProgramPlans();

      setupOldList();

      setupCurrentlyAvailableProgramPlans();

   }

   

   function cleanUpNameBasedFiltration() {

      $('#programsPlansGroup-available-clear').click();

      $('#programPlansGroup-selection-clear').click();

      $('#programPlansGroup-available-filter').val('');

      $('#programPlansGroup-selection-filter').val('');

   }

   

   function revampOldList() {

      oldList = new Object();

      $("#programPlansGroup-available option").each(function () {

         oldList[$(this).val()] = $(this).text();

      });

   }

   

   function setupOldList() {

      oldList = new Object();

      $("#programPlansGroup-selection option").each(function () {

         oldList[$(this).val()] = $(this).text();

      });

   }

   

   function revampCurrentlyAvailableProgramPlans() {

      var subList = new Object();

      $.each(endList, function (outerkey, outervalue) {

         $.each(oldList, function (innerkey, innervalue) {

            if (outerkey == innerkey) {

               subList[outerkey] = outervalue;

            }

         });

      });

      $("#programPlansGroup-available option").remove();

      $.each(subList, function (key, value) {

         $("#programPlansGroup-available").append($('<option>

               </option>').val(key).html(value));

      });

   }

   

   function revampCurrentlySelectedProgramPlans() {

      var subList = new Object();

      $.each(endList, function (outerkey, outervalue) {

         var noMatch = true;

         $.each(oldList, function (innerkey, innervalue) {

            if (outerkey == innerkey) {

               noMatch = false;

            }

         });

         if (noMatch) {

            subList[outerkey] = outervalue;

         }

      });

      $("#programPlansGroup-selection option").remove();

      $.each(subList, function (key, value) {

         $("#programPlansGroup-selection").append($('<option>

               </option>').val(key).html(value));

      });

   }

   

   function setupCurrentlyAvailableProgramPlans() {

      var subList = new Object();

      $.each(endList, function (outerkey, outervalue) {

         var noMatch = true;

         $.each(oldList, function (innerkey, innervalue) {

            if (outerkey == innerkey) {

               noMatch = false;

            }

         });

         if (noMatch) {

            subList[outerkey] = outervalue;

         }

      });

      $("#programPlansGroup-available option").remove();

      $.each(subList, function (key, value) {

         $("#programPlansGroup-available").append($('<option>

               </option>').val(key).html(value));

      });

   }

   

   function setupCurrentlySelectedProgramPlans() {

      var subList = new Object();

      $.each(endList, function (outerkey, outervalue) {

         $.each(oldList, function (innerkey, innervalue) {

            if (outerkey == innerkey) {

               subList[outerkey] = outervalue;

            }

         });

      });

      $("#programPlansGroup-selection option").remove();

      $.each(subList, function (key, value) {

         $("#programPlansGroup-selection").append($('<option>

               </option>').val(key).html(value));

      });

   }

   

   function prepareProgramPlanList(programsDto, sampleTypesDto) {

      var currentSampleType = $("#SelectedSampleTypeId option:selected").val();

      var currentPrograms = new Object();

      $("#programsGroup-selection option").each(function () {

         currentPrograms[$(this).val()] = $(this).text();

      });

      logic(programsDto, sampleTypesDto, currentSampleType, currentPrograms);

   }

   

   function logic(programsDto, sampleTypesDto, currentSampleType, currentPrograms) {

      endList = new Object();

      var sampleTypeMatchCollection = new Array();

      var matchMade = false;

      $.each(sampleTypesDto, function (key, value) {

         if (key == currentSampleType) {

            sampleTypeMatchCollection = value;

            matchMade = true;

         }

      });

      if (matchMade) {

         $.each(programsDto, function (outerkey, outervalue) {

            $.each(currentPrograms, function (midkey, midvalue) {

               if (outerkey == midkey) {

                  $.each(outervalue, function (innerkey,innervalue) {

                     var counter = 0;

                     $(sampleTypeMatchCollection).each(function () {

                        if (sampleTypeMatchCollection[counter] == innerkey) {

                           endList[innerkey] = innervalue;

                        }

                        counter++;

                     });

                  });

               }

            });

         });

      } else {

         endList["nomatch"] = "nomatch";

      }

   }

</script>

No comments:

Post a Comment