Sha256: 0e8974eb5f6949af7b1d635713f91f11e1ac1507f7e401e6b129afa0d8f643c5

Contents?: true

Size: 1.26 KB

Versions: 109

Compression:

Stored size: 1.26 KB

Contents

import * as bootstrap from 'bootstrap'

document.addEventListener('turbo:load', bindAndObserveToasts)
document.addEventListener('turbo:render', bindAndObserveToasts)

// TODO: testear con capybara
function bindToastElements () {
  const toastQuery = '.pg-toast:not(.hide):not(.show)'

  const toastElList = document.querySelectorAll(toastQuery)
  Array.from(toastElList).map(toastEl => {
    toastEl.addEventListener('hidden.bs.toast', () => {
      toastEl.remove()
    })
    return new bootstrap.Toast(toastEl).show()
  })
}

function bindAndObserveToasts () {
  bindToastElements()

  // Select the node that will be observed for mutations
  const targetNode = document.getElementById('flash')

  // Options for the observer (which mutations to observe)
  const config = { attributes: true, childList: true, subtree: true }

  // Callback function to execute when mutations are observed
  const callback = (mutationList, observer) => {
    for (const mutation of mutationList) {
      if (mutation.type === 'childList') {
        bindToastElements()
      }
    }
  }

  // Create an observer instance linked to the callback function
  const observer = new MutationObserver(callback)

  // Start observing the target node for configured mutations
  observer.observe(targetNode, config)
}

Version data entries

109 entries across 109 versions & 1 rubygems

Version Path
pg_rails-7.6.23 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.22 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.22.pre.3 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.22.pre.2 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.22.pre.1 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.11 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.10 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.9 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.8 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.7 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.6 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.5 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.4 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.3 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.2 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.21.pre.1 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.20 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.19 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.6.18 pg_layout/app/javascript/config/bootstrap.js