mirror of
https://github.com/jgthms/bulma.git
synced 2024-11-28 12:24:23 +00:00
139 lines
3.4 KiB
JavaScript
139 lines
3.4 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
// Dropdowns
|
|
|
|
const $dropdowns = getAll('.dropdown:not(.is-hoverable)');
|
|
|
|
if ($dropdowns.length > 0) {
|
|
$dropdowns.forEach($el => {
|
|
$el.addEventListener('click', event => {
|
|
event.stopPropagation();
|
|
$el.classList.toggle('is-active');
|
|
});
|
|
});
|
|
|
|
document.addEventListener('click', event => {
|
|
closeDropdowns();
|
|
});
|
|
}
|
|
|
|
function closeDropdowns() {
|
|
$dropdowns.forEach($el => {
|
|
$el.classList.remove('is-active');
|
|
});
|
|
}
|
|
|
|
// Toggles
|
|
|
|
const $burgers = getAll('.burger');
|
|
|
|
if ($burgers.length > 0) {
|
|
$burgers.forEach($el => {
|
|
$el.addEventListener('click', () => {
|
|
const target = $el.dataset.target;
|
|
const $target = document.getElementById(target);
|
|
$el.classList.toggle('is-active');
|
|
$target.classList.toggle('is-active');
|
|
});
|
|
});
|
|
}
|
|
|
|
// Modals
|
|
|
|
const $html = document.documentElement;
|
|
const $modals = getAll('.modal');
|
|
const $modalButtons = getAll('.modal-button');
|
|
const $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
|
|
|
|
if ($modalButtons.length > 0) {
|
|
$modalButtons.forEach($el => {
|
|
$el.addEventListener('click', () => {
|
|
const target = $el.dataset.target;
|
|
const $target = document.getElementById(target);
|
|
$html.classList.add('is-clipped');
|
|
$target.classList.add('is-active');
|
|
});
|
|
});
|
|
}
|
|
|
|
if ($modalCloses.length > 0) {
|
|
$modalCloses.forEach($el => {
|
|
$el.addEventListener('click', () => {
|
|
closeModals();
|
|
});
|
|
});
|
|
}
|
|
|
|
document.addEventListener('keydown', event => {
|
|
const e = event || window.event;
|
|
if (e.keyCode === 27) {
|
|
closeModals();
|
|
closeDropdowns();
|
|
}
|
|
});
|
|
|
|
function closeModals() {
|
|
$html.classList.remove('is-clipped');
|
|
$modals.forEach($el => {
|
|
$el.classList.remove('is-active');
|
|
});
|
|
}
|
|
|
|
// Clipboard
|
|
|
|
const $highlights = getAll('.highlight');
|
|
let itemsProcessed = 0;
|
|
|
|
if ($highlights.length > 0) {
|
|
$highlights.forEach($el => {
|
|
const copy = '<button class="copy">Copy</button>';
|
|
const expand = '<button class="expand">Expand</button>';
|
|
$el.insertAdjacentHTML('beforeend', copy);
|
|
|
|
if ($el.firstElementChild.scrollHeight > 480 && $el.firstElementChild.clientHeight <= 480) {
|
|
$el.insertAdjacentHTML('beforeend', expand);
|
|
}
|
|
|
|
itemsProcessed++;
|
|
if (itemsProcessed === $highlights.length) {
|
|
addHighlightControls();
|
|
}
|
|
});
|
|
}
|
|
|
|
function addHighlightControls() {
|
|
const $highlightButtons = getAll('.highlight .copy, .highlight .expand');
|
|
|
|
$highlightButtons.forEach($el => {
|
|
$el.addEventListener('mouseenter', () => {
|
|
$el.parentNode.style.boxShadow = '0 0 0 1px #ed6c63';
|
|
});
|
|
|
|
$el.addEventListener('mouseleave', () => {
|
|
$el.parentNode.style.boxShadow = 'none';
|
|
});
|
|
});
|
|
|
|
const $highlightExpands = getAll('.highlight .expand');
|
|
|
|
$highlightExpands.forEach($el => {
|
|
$el.addEventListener('click', () => {
|
|
$el.parentNode.firstElementChild.style.maxHeight = 'none';
|
|
});
|
|
});
|
|
}
|
|
|
|
new Clipboard('.copy', {
|
|
target: function(trigger) {
|
|
return trigger.previousSibling;
|
|
}
|
|
});
|
|
|
|
// Functions
|
|
|
|
function getAll(selector) {
|
|
return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
|
|
}
|
|
|
|
});
|