(function($, undefined) {
// document ready function
$(function() {
init();
$('ul.select-list').on({
'dragstart': dragstart,
'dragend': dragend
}, 'li');
$('ul.drop-list').on({
'dragenter dragover': dragover,
'dragleave': dragleave,
'drop': drop
}, 'li.dropzone');
});
// Initializes the lists
function init() {
$('ul.select-list').find('li').not('[class="header"]').each(function() {
var height = getSlotHeight() * $(this).data('slots');
$(this).height(height);
}).attr('draggable', true);
$('ul.drop-list').find('li').each(function() {
$(this).height(getSlotHeight());
}).addClass('dropzone');
}
// Get the height of the grid
function getSlotHeight() {
return 20;
}
/**
* Checks whether target is a kompatible dropzone
* A dropzone needs the dropzone class
* and needs to have enough consequent slots to drop the object into
*/
function isDropZone(target, draggedObj) {
var isDropZone = true;
var slots = draggedObj.data('slots');
for (var i = 1; i < slots; i++) {
target = target.next();
if (target.size() == 0 || !target.hasClass('dropzone')) {
isDropZone = false;
break;
}
}
return isDropZone;
}
/*
* The following events are executed in the order the handlers are declared
* dragstart being first and dragend being last
*/
// dragstart event handler
function dragstart(e) {
e.stopPropagation();
var dt = e.originalEvent.dataTransfer;
dt.effectAllowed = 'move';
dt.setData('text/html', '');
$('ul.select-list').data({
draggedObj: $(this)
});
}
// dragover event handler
function dragover(e) {
e.preventDefault();
e.stopPropagation();
var data = $('ul.select-list').data();
if (!data.draggedObj || !isDropZone($(this), data.draggedObj)) {
e.originalEvent.dataTransfer.dropEffect = 'none';
return;
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.