<commit_before>(function(){
  ko.components.register("ko-dropdown", {
    viewModel: function(params){
      var self = this;

      self.caption = params.caption;
      self.value = params.value;
      self.options = params.options;

      self.dropdownOpen = ko.observable();

      self.selectedOption = ko.observable();
      self.selectedText = ko.computed(function() {
        return ko.unwrap(self.selectedOption) ? ko.unwrap(self.selectedOption).text : "";
      });

      self.selectOption = function(option){
        self.value(option.value);
        self.selectedOption(option);
      }

      self.dropdownOptions = ko.computed(function(){
        var retOptions = [];
        self.options().forEach(function(option){
          if (typeof option === 'object' || option instanceof Object){
            retOptions.push(option);
          } else {
            retOptions.push({
              value: option,
              text: option
            });
          }
        });

        if (retOptions.length > 0 && !ko.unwrap(self.caption)) {
          self.selectOption(retOptions[0]);
        }

        return retOptions;
      });

      if (ko.unwrap(self.value)){
        self.selectedOption(findOptionByValue(ko.unwrap(self.value)));
      } else if (ko.unwrap(self.caption)){
        self.selectedOption({
          text: self.caption
        });
      }



      self.toggleOpen = function(data, event){
        event.stopPropagation();
        self.dropdownOpen(!self.dropdownOpen());
      }



      self.value.subscribe(function(newValue){
        var selectedValue = ko.unwrap(self.selectedOption) ? ko.unwrap(self.selectedOption).value : null;

        if (newValue !== selectedValue){
          var option = findOptionByValue(newValue);
          if (option != null){
            self.selectOption(option);
          }
        }
      });

      window.addEventListener('click', function (e) {
          self.dropdownOpen(false);
      });

      function findOptionByValue(val){
        var foundOption = null;
        if (ko.unwrap(self.dropdownOptions)){
          self.dropdownOptions().forEach(function(option){
            if (option.value === val){
              foundOption = option;
            }
          });
        }
        return foundOption;
      }
    },
    template: "<div class='ko-dropdown' data-bind='click: toggleOpen'>\
        <span class='ko-dropdown__selected' data-bind='text: selectedText'></span>\
        <ul class='ko-dropdown__list' data-bind='visible: dropdownOpen, foreach: dropdownOptions'>\
          <li class='ko-dropdown__list-item' data-bind='text: text, click: $parent.selectOption, visible: $parent.selectedText() != text'></li>\
        </ul>\
      </div>"
  });

}());<commit_msg>Fixed some issues with async options<commit_after>(function () {
    ko.components.register("ko-dropdown", {
        viewModel: function (params) {
            var self = this;

            self.caption = params.caption;
            self.value = params.value;
            self.options = params.options;

            self.dropdownOpen = ko.observable(false);

            self.selectedOption = ko.observable();
            self.selectedText = ko.computed(function () {
                return ko.unwrap(self.selectedOption) ? ko.unwrap(self.selectedOption).text : "";
            });

            self.selectOption = function (option) {
                self.value(option.value);
                self.selectedOption(option);
            }

            self.dropdownOptions = ko.computed(function () {
                var retOptions = [];
                self.options().forEach(function (option) {
                    if (typeof option === 'object' || option instanceof Object) {
                        retOptions.push(option);
                    } else {
                        retOptions.push({
                            value: option,
                            text: option
                        });
                    }
                });

                if (retOptions.length > 0 && !ko.unwrap(self.caption)) {
                    self.selectOption(retOptions[0]);
                }

                return retOptions;
            });

            self.dropdownOptions.subscribe(function () {
                self.value.notifySubscribers(self.value()); // so if dropdown options change, update selected value
            })

            if (ko.unwrap(self.value)) {
                self.selectedOption(findOptionByValue(ko.unwrap(self.value)));
            } else if (ko.unwrap(self.caption)) {
                self.selectedOption({
                    text: self.caption
                });
            }



            self.toggleOpen = function (data, event) {
                event.stopPropagation();
                self.dropdownOpen(!self.dropdownOpen());
            }



            self.value.subscribe(function (newValue) {
                var selectedValue = ko.unwrap(self.selectedOption) ? ko.unwrap(self.selectedOption).value : null;

                if (newValue !== selectedValue) {
                    var option = findOptionByValue(newValue);
                    if (option != null) {
                        self.selectOption(option);
                    }
                }
            });

            window.addEventListener('click', function (e) {
                self.dropdownOpen(false);
            });

            function findOptionByValue(val) {
                var foundOption = null;
                if (ko.unwrap(self.dropdownOptions)) {
                    self.dropdownOptions().forEach(function (option) {
                        if (option.value === val) {
                            foundOption = option;
                        }
                    });
                }
                return foundOption;
            }
        },
        template: "<div class='dropdown' data-bind='click: toggleOpen'>\
        <span class='selected' data-bind='text: selectedText'></span>\
        <ul data-bind='visible: dropdownOpen, foreach: dropdownOptions'>\
          <li data-bind='text: text, click: $parent.selectOption'></li>\
        </ul>\
      </div>"
    });

}());
{'fraction_non_alphanumeric': 0.126742301458671, 'fraction_numerical': 0.0006482982171799027, 'mean_word_length': 1.613722998729352, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2837004', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1525, 'n_words': 270}