Sha256: a311ed6879e8e171bf174721383d9765d63b0e5e2d17adf80b7583ba3c0e4655

Contents?: true

Size: 964 Bytes

Versions: 44

Compression:

Stored size: 964 Bytes

Contents

export default class AutoSelectOptionsFromUrl {
  constructor(options = {}) {
    this.$source = options.source;
    this.$select = options.select;
    this.sourceToParams = options.sourceToParams;
    this.run();
  }

  run() {
    this.$source.on("change", this._onSourceChange.bind(this));
    this._onSourceChange();
  }

  _onSourceChange() {
    const select = this.$select;
    const params = this.sourceToParams(this.$source);
    const url = this.$source.data("url");

    $.getJSON(url, params, function (data) {
      select.find("option:not([value=''])").remove();
      const selectedValue = select.data("selected");

      data.forEach((option) => {
        let optionElement = $(`<option value="${option.id}">${option.body}</option>`).appendTo(select);
        if (option.id === selectedValue) {
          optionElement.attr("selected", true);
        }
      });

      if (selectedValue) {
        select.val(selectedValue);
      }
    });
  }
}

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
decidim-forms-0.29.1 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.4 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.27.9 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.29.0 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.3 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.27.8 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.29.0.rc4 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.29.0.rc3 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.29.0.rc2 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.29.0.rc1 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.2 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.27.7 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.1 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.27.6 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.26.10 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.26.9 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.0 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.27.5 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.0.rc5 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js
decidim-forms-0.28.0.rc4 app/packs/src/decidim/forms/admin/auto_select_options_from_url.component.js