Sha256: 89b46fa46949bfa6e5f68e73b7ea2de31d92e067d8ada2ec8a4d0c861a825997

Contents?: true

Size: 1.32 KB

Versions: 22

Compression:

Stored size: 1.32 KB

Contents

class CheckboxToggler {
  constructor(options, container){
    this.options = options;
    this.container = container;
    this._init();
    this._bind();
  }

  option(_key, _value) {
  }

  _init() {
    if (!this.container) {
      throw new Error('Container element not found');
    } else {
      this.$container = $(this.container);
    }

    if (!this.$container.find('.toggle_all').length) {
      throw new Error('"toggle all" checkbox not found');
    } else {
      this.toggle_all_checkbox = this.$container.find('.toggle_all');
    }

    this.checkboxes = this.$container.find(':checkbox').not(this.toggle_all_checkbox);
  }

  _bind() {
    this.checkboxes.change(event => this._didChangeCheckbox(event.target));
    this.toggle_all_checkbox.change(() => this._didChangeToggleAllCheckbox());
  }

  _didChangeCheckbox(_checkbox){
    const numChecked = this.checkboxes.filter(':checked').length;

    const allChecked = numChecked === this.checkboxes.length;
    const someChecked = (numChecked > 0) && (numChecked < this.checkboxes.length);

    this.toggle_all_checkbox.prop({ checked: allChecked, indeterminate: someChecked });
  }

  _didChangeToggleAllCheckbox() {
    const setting = this.toggle_all_checkbox.prop('checked');
    this.checkboxes.prop({ checked: setting });
    return setting;
  }
}

export default CheckboxToggler;

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
activeadmin-3.2.5 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.2.4 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.2.3 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.2.2 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.2.1 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.2.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.1.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-3.0.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.14.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.13.1 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.13.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.12.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.11.2 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.11.1 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.11.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.10.1 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.10.0 app/javascript/active_admin/lib/checkbox-toggler.js
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/app/javascript/active_admin/lib/checkbox-toggler.js
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.9.0 app/javascript/active_admin/lib/checkbox-toggler.js