Sha256: 85f0c94c99d6c9cf51e655eda5cfcf3f6bdf4807c41e8842a99ae7d11472d22b

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

/* global FormData */

import ApplicationController from './application_controller'

import Modal from '../core/modal'

export default class extends ApplicationController {
  initialize () {
    this.boundLoad = this.load.bind(this)
  }

  connect () {
    this.element.addEventListener('click', this.boundLoad)
  }

  disconnect () {
    this.element.removeEventListener('click', this.boundLoad)
  }

  load (e) {
    e.preventDefault()

    this.dispatch('loading')

    Modal.load(this.url, this.fetchParams)
      .then((modal) => {
        this.modal = modal

        const modalController = this._getModalController(modal)
        modalController.modalTrigger = this

        this.dispatch('loaded', { detail: modal })
      })
  }

  get url () {
    if (this.isLink) {
      return this.element.href
    } else {
      const form = this.element.closest('form')

      if (form) {
        return form.action
      } else {
        throw new Error('Unable to determine modal trigger URL')
      }
    }
  }

  get fetchParams () {
    if (this.isLink) {
      return {}
    } else {
      const form = this.element.closest('form')

      if (form) {
        return {
          method: form.method,
          body: new FormData(form)
        }
      } else {
        return {}
      }
    }
  }

  get isLink () {
    return this.element.nodeName === 'A'
  }

  _getModalController (modal) {
    return this.application.getControllerForElementAndIdentifier(modal, 'modal')
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.10.0 frontend/js/controllers/modal_trigger_controller.js
trestle-0.10.0.pre2 frontend/js/controllers/modal_trigger_controller.js
trestle-0.10.0.pre frontend/js/controllers/modal_trigger_controller.js