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.1.16 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.15 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.14 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.13 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.12 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.11 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.10 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.9 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.8 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.7 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.6 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.5 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.4 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.3 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.2 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.1.pre.6 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.1.pre.5 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.1.pre.4 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.1.pre.3 pg_layout/app/javascript/config/bootstrap.js
pg_rails-7.1.1.pre.2 pg_layout/app/javascript/config/bootstrap.js