Sha256: 1ea9753b5e9d3923330016f54bff95e87052fdbc559c6537b3d8bc86bcc7fd96

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["all", "selectable"];

  selectAll(event) {
    const checked = event.target.checked;
    this.allTarget.indeterminate = false;
    this._setAllCheckboxes(checked);
    this._dispatch("change", { count: this.selectedCount });
  }

  onSelected() {
    this.allTarget.indeterminate = !!this._indeterminate;
    this._dispatch("change", { count: this.selectedCount });
  }

  get selectedCount() {
    return this.selected.length;
  }

  get selected() {
    return this.selectables.filter((c) => c.checked);
  }

  get selectables() {
    return new Array(...this.selectableTargets);
  }

  _setAllCheckboxes(checked) {
    this.selectables.forEach((el) => {
      const checkbox = el;

      if (!checkbox.disabled) {
        checkbox.checked = checked;
      }
    });
  }

  get _indeterminate() {
    return (
      this.selected.length !== this.selectableTargets.length &&
      this.selected.length > 0
    );
  }

  _dispatch(name, detail) {
    window.dispatchEvent(
      new CustomEvent(`rmp:select:${name}`, { bubbles: true, detail })
    );
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_mini_profiler-0.7.3 app/javascript/js/select_controller.js
rails_mini_profiler-0.7.2 app/javascript/js/select_controller.js
rails_mini_profiler-0.7.1 app/javascript/js/select_controller.js
rails_mini_profiler-0.7.0 app/javascript/js/select_controller.js
rails_mini_profiler-0.6.0 app/javascript/js/select_controller.js