bulma/docs/lib/main.js

116 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-07-01 17:30:39 +00:00
'use strict';
document.addEventListener('DOMContentLoaded', function () {
2017-07-02 16:34:44 +00:00
// Toggles
2017-07-01 17:30:39 +00:00
2017-07-02 16:34:44 +00:00
var $burgers = getAll('.burger');
var $fries = getAll('.fries');
2017-07-01 17:30:39 +00:00
2017-07-02 16:34:44 +00:00
if ($burgers.length > 0) {
$burgers.forEach(function ($el) {
$el.addEventListener('click', function () {
var target = $el.dataset.target;
var $target = document.getElementById(target);
$el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
2017-07-01 17:30:39 +00:00
});
}
// Modals
var $html = document.documentElement;
2017-07-02 15:52:20 +00:00
var $modals = getAll('.modal');
var $modalButtons = getAll('.modal-button');
var $modalCloses = getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button');
2017-07-01 17:30:39 +00:00
if ($modalButtons.length > 0) {
$modalButtons.forEach(function ($el) {
$el.addEventListener('click', function () {
var target = $el.dataset.target;
var $target = document.getElementById(target);
$html.classList.add('is-clipped');
$target.classList.add('is-active');
});
});
}
if ($modalCloses.length > 0) {
$modalCloses.forEach(function ($el) {
$el.addEventListener('click', function () {
$html.classList.remove('is-clipped');
closeModals();
});
});
}
document.addEventListener('keydown', function (e) {
if (e.keyCode === 27) {
$html.classList.remove('is-clipped');
closeModals();
}
});
function closeModals() {
$modals.forEach(function ($el) {
$el.classList.remove('is-active');
});
}
2017-07-02 15:52:20 +00:00
// Clipboard
var $highlights = getAll('.highlight');
var itemsProcessed = 0;
if ($highlights.length > 0) {
$highlights.forEach(function ($el) {
var copy = '<button class="copy">Copy</button>';
var expand = '<button class="expand">Expand</button>';
$el.insertAdjacentHTML('beforeend', copy);
2017-07-02 21:07:30 +00:00
if ($el.firstElementChild.scrollHeight > 320) {
2017-07-02 15:52:20 +00:00
$el.insertAdjacentHTML('beforeend', expand);
}
itemsProcessed++;
if (itemsProcessed === $highlights.length) {
addHighlightControls();
}
});
}
function addHighlightControls() {
var $highlightButtons = getAll('.highlight .copy, .highlight .expand');
$highlightButtons.forEach(function ($el) {
$el.addEventListener('mouseenter', function () {
$el.parentNode.style.boxShadow = '0 0 0 1px #ed6c63';
});
$el.addEventListener('mouseleave', function () {
$el.parentNode.style.boxShadow = 'none';
});
});
var $highlightExpands = getAll('.highlight .expand');
$highlightExpands.forEach(function ($el) {
$el.addEventListener('click', function () {
$el.parentNode.firstElementChild.style.maxHeight = 'none';
});
});
}
new Clipboard('.copy', {
target: function target(trigger) {
return trigger.previousSibling;
}
});
// Functions
function getAll(selector) {
return Array.prototype.slice.call(document.querySelectorAll(selector), 0);
}
2017-07-01 17:30:39 +00:00
});