Sha256: 76374ad7fdd7064398e750d125c58b44ad3911fb7f9a976989a4a78a6d0d1b0f

Contents?: true

Size: 1.32 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
activeadmin-2.7.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.6.1 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.6.0 app/javascript/active_admin/lib/checkbox-toggler.js
activeadmin-2.5.0 app/javascript/active_admin/lib/checkbox-toggler.js