stylus/manage/draggable.js

46 lines
1.0 KiB
JavaScript
Raw Normal View History

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