Sha256: 76fb8c2387b5a066f9fbfbc2d251fa293dddd57c465000926c70efc513016bd4

Contents?: true

Size: 946 Bytes

Versions: 1

Compression:

Stored size: 946 Bytes

Contents

window.Resizing ||= {}
window.Resizing.Rails ||= {}

// Usage
// toast = new Resizing.Rails.Toast()
// toast.show('title', 'body-text')
//
class Toast {
  constructor() {
    this.container = document.getElementById('toast-container')
    this.template = document.getElementById('toast-template').content
  }

  show(title, text) {
    let $node = document.importNode(this.template, true)
    let $toast = $node.querySelector('.toast')
    let id = $toast.id = `toast-${this.generateUniqueId()}`
    $node.querySelector('.toast-title').textContent = title
    $node.querySelector('.toast-body').textContent = text
    this.container.appendChild($node)

    let $elem = document.getElementById(id)
    let t = new bootstrap.Toast($elem)
    t.show()
    $elem.addEventListener('hidden.bs.toast',() => {
      $elem.remove()
    })
  }

  generateUniqueId() {
    return (new Date).getTime().toString(16)
  }
}

window.Resizing.Rails.Toast = Toast

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resizing-rails-0.1.0.pre app/assets/javascripts/resizing/rails/toast.js