trabajo-afectivo/app/assets/javascripts/knowledge_base_public/dropdown.js
2019-06-04 05:40:48 +02:00

28 lines
797 B
JavaScript

(function() {
document.addEventListener('DOMContentLoaded', function(event) {
document
.querySelector('[data-toggle="dropdown"]')
.addEventListener('click', toggleDropdown)
document
.querySelector('.dropdown-menu')
.addEventListener('click', function(event) { event.stopPropagation() })
})
function toggleDropdown(event){
event.stopPropagation()
event.preventDefault()
var elem = document.querySelector('.dropdown-menu')
var open = elem.classList.toggle('is-open')
if(elem.setAttribute) // not supported by IE11
elem.setAttribute('aria-expanded', open ? 'true' : 'false')
if(open) {
window.addEventListener('click', toggleDropdown)
} else {
window.removeEventListener('click', toggleDropdown)
}
}
}())