Sha256: 437797ca98c70c26d58625555a5889780dbe55782c3e170b6d87d6a15f731a77

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

var initializer = function() {
  configureSelect2(document);

  $(document).on('has_many_add:after', function(event, container) {
    configureSelect2(container);
  });

  function configureSelect2(container) {
    if (window.ActiveadminAddons.config.defaultSelect == 'select2') {
      $('select:not(.default-select)', container).each(function(i, el) {
        setupSelect2(el);
      });
    }

    $('select.select2', container).each(function(i, el) {
      setupSelect2(el);
    });

    function setupSelect2(select) {
      var selectConfig = {
        placeholder: '',
        width: '80%',
        allowClear: true,
      };

      function isFilter(path) {
        return $(select).closest(path).length > 0;
      }

      if (isFilter('.select_and_search')) {
        selectConfig.width = 'resolve';
        selectConfig.allowClear = false;
      } else if (isFilter('.filter_select')) {
        selectConfig.width = 'resolve';
      }

      $(select).select2(selectConfig);

      // Related with https://github.com/select2/select2/issues/3320
      $(select).on('select2:unselecting', function() {
        $(this).data('unselecting', true);
      }).on('select2:open', function() {
        if ($(this).data('unselecting')) {
          $(this).select2('close').removeData('unselecting');
        }
      });
    }
  }
};

$(initializer);
$(document).on('turbolinks:load turbo:load', initializer);

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin_addons-1.10.2 app/javascript/activeadmin_addons/inputs/select2.js
activeadmin_addons-1.10.1 app/javascript/activeadmin_addons/inputs/select2.js
activeadmin_addons-1.10.0 app/javascript/activeadmin_addons/inputs/select2.js