stylus/manage/draggable.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

/* global $$ installed updateInjectionOrder */
2018-11-25 23:37:08 +00:00
'use strict';
// Polyfill for mobile? - https://caniuse.com/#feat=dragndrop
(() => {
let dragged = null;
function isEnabled() {
return installed.dataset.sort === 'order';
}
2018-11-25 23:37:08 +00:00
function cleanup() {
$$('.entry.dragging').forEach(el => {
el.classList.remove('dragging');
});
if (isEnabled() && $('body').classList.contains('dragging')) {
updateInjectionOrder();
}
2018-11-25 23:37:08 +00:00
$('body').classList.remove('dragging');
}
document.addEventListener('dragstart', event => {
if (isEnabled()) {
const el = event.target && event.target.closest('.entry');
if (el) {
dragged = el;
el.classList.add('dragging');
$('body').classList.add('dragging');
}
} else {
cleanup();
return false;
2018-11-25 23:37:08 +00:00
}
}, false);
document.addEventListener('dragend', () => {
cleanup();
});
document.addEventListener('dragenter', event => {
if (isEnabled()) {
const el = event.target && event.target.closest('.entry, .entry-header');
if (el && dragged) {
// Insert after the target; keeps header at top
el.after(dragged);
}
2018-11-25 23:37:08 +00:00
}
}, false);
document.addEventListener('drop', event => {
event.preventDefault();
cleanup();
if (dragged) {
dragged = null;
}
}, false);
})();