Sha256: 52d5f9e7e2f930251d2ff5645091c5729510574f78843e2883c3cd3c16310b59

Contents?: true

Size: 899 Bytes

Versions: 5

Compression:

Stored size: 899 Bytes

Contents

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

export default class extends Controller {
  static targets = ["count"];

  connect() {
    this.setCount();
  }

  checkAll() {
    this.setAllCheckboxes(true);
    this.setCount();
  }

  checkNone() {
    this.setAllCheckboxes(false);
    this.setCount();
  }

  onChecked() {
    this.setCount();
  }

  setAllCheckboxes(checked) {
    this.checkboxes.forEach((el) => {
      const checkbox = el;

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

  setCount() {
    if (this.hasCountTarget) {
      const count = this.selectedCheckboxes.length;
      this.countTarget.innerHTML = `${count} selected`;
    }
  }

  get selectedCheckboxes() {
    return this.checkboxes.filter((c) => c.checked);
  }

  get checkboxes() {
    return new Array(...this.element.querySelectorAll("input[type=checkbox]"));
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

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