Sha256: 69e829649f75981c70159e2c220808ad08053163403dceb6da5dca824b9e5079

Contents?: true

Size: 2 KB

Versions: 46

Compression:

Stored size: 2 KB

Contents

import ApplicationController from "satis/controllers/application_controller"

/*
  Usage:

  <div data-controller="show-hide">
    <input type="checkbox" data-show-hide-target="input" />
    <div data-show-hide-showable="true" data-show-hide-value="true">
      This is shown when the show-hide.input value equates to true, it is hidden otherwise
    </div>
    <div data-show-hide-hidable="true" data-show-hide-value="true">
      This is hidden when the show-hide.input value equates to true, it is shown otherwise
    </div>
  </div>
 */
export default class extends ApplicationController {
  static targets = ["showable", "hidable", "input"]

  connect() {
    this.boundUpdate = this.update.bind(this)

    this.watchOn = "input"
    if (this.inputTarget.type == "hidden") {
      this.watchOn = "change"
    }

    this.inputTarget.addEventListener(this.watchOn, this.boundUpdate)

    this.update()
  }

  disconnect() {
    this.inputTarget.removeEventListener(this.watchOn, this.boundUpdate)
  }

  update() {
    this.toggle(this.currentValue)
  }

  toggle(value) {
    if (this.hasShowableTarget) {
      this.showableTargets.forEach((element) => {
        if (element.getAttribute("data-show-hide-value") == value) {
          element.classList.remove("hidden")
        } else {
          element.classList.add("hidden")
        }
      })
    }
    if (this.hasHidableTarget) {
      this.hidableTargets.forEach((element) => {
        if (element.getAttribute("data-show-hide-value") == value) {
          element.classList.add("hidden")
        } else {
          element.classList.remove("hidden")
        }
      })
    }
  }

  get currentValue() {
    if (this.inputTarget.type == "checkbox") {
      return this.inputTarget.checked ? "true" : "false"
    } else if (this.inputTarget.tagName == "SELECT" && this.data.get("attr")) {
      let option = this.inputTarget.options[this.inputTarget.selectedIndex]
      return option.getAttribute(this.data.get("attr"))
    } else {
      return this.inputTarget.value
    }
  }
}

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
satis-2.1.53 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.52 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.51 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.50 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.49 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.48 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.47 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.46 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.45 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.44 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.43 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.42 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.41 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.40 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.39 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.38 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.37 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.36 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.35 app/javascript/satis/utility_controllers/show_hide_controller.js
satis-2.1.33 app/javascript/satis/utility_controllers/show_hide_controller.js