Sha256: ed56e7cec9603af7b3fd0dc068e18ccddaf4cb35ca8ba96555dfae837eca39eb

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

import ApplicationController from './application_controller'

export default class extends ApplicationController {
  static outlets = ['checkbox-select']

  connect () {
    if (!this.hasCheckboxSelectOutlet) {
      // Set default outlet if none specified
      this.element.setAttribute(`data-${this.identifier}-checkbox-select-outlet`, '.trestle-table')
    }

    this.baseHref = this.element.href

    this.update()
  }

  update () {
    this.toggleEnabled()
    this.updateHref()
  }

  toggleEnabled () {
    this.element.classList.toggle('disabled', this.checkboxes.length === 0)
  }

  updateHref () {
    this.element.href = this.hrefWithSelectedIds
  }

  get checkboxes () {
    if (this.hasCheckboxSelectOutlet) {
      return this.checkboxSelectOutlet.checked
    } else {
      return []
    }
  }

  get selectedIds () {
    return this.checkboxes.map(c => c.value)
  }

  get hrefWithSelectedIds () {
    const ids = this.selectedIds

    if (ids.length) {
      return `${this.baseHref}?ids=${ids.join(',')}`
    } else {
      return this.baseHref
    }
  }

  checkboxSelectOutletConnected (outlet, element) {
    element.addEventListener('checkbox-select:change', this.update.bind(this))
  }

  checkboxSelectOutletDisconnected (outlet, element) {
    element.removeEventListener('checkbox-select:change', this.update.bind(this))
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.10.1 frontend/js/controllers/batch_action_controller.js
trestle-0.10.0 frontend/js/controllers/batch_action_controller.js
trestle-0.10.0.pre2 frontend/js/controllers/batch_action_controller.js