﻿
angular
    .module("umbraco")
    .controller("nuPickers.Shared.DropDownPicker.DropDownPickerEditorController",
        ['$scope', 'nuPickers.Shared.Editor.EditorResource',
        function ($scope, editorResource) {

            editorResource.getEditorDataItems($scope.model).then(function (response) {
                $scope.dropDownPickerOptions = response.data;

                editorResource.getPickedKeys($scope.model).then(function (pickedKeys) {
                    if (pickedKeys[0]) {
                        var i = 0;
                        var found = false;
                        do {
                            if ($scope.dropDownPickerOptions[i].key == pickedKeys[0]) {
                                $scope.pickedOption = $scope.dropDownPickerOptions[i];
                                found = true;
                            }
                            i++;

                        } while (!found && i < $scope.dropDownPickerOptions.length)
                    }
                });


                $scope.dropDownChange = function() {
                    $scope.model.value = editorResource.createSaveValue($scope.model.config, [$scope.pickedOption]);
                };

            });

        }]);
@@ -571,16 +571,17 @@
  false;%0A
+%0A
         
@@ -600,13 +600,69 @@
     
-do
+while (!found && i %3C $scope.dropDownPickerOptions.length)
  %7B%0A
+%0A
     
@@ -900,32 +900,33 @@
               %7D%0A
+%0A
                 
@@ -942,17 +942,16 @@
    i++;%0A
-%0A
         
@@ -972,65 +972,8 @@
   %7D 
-while (!found && i %3C $scope.dropDownPickerOptions.length)
 %0A   
@@ -1012,17 +1012,16 @@
    %7D);%0A%0A
-%0A
         

fix for #157 (condition check before first loop)