What I'm trying to achieve here is sort queue of .item-dragable
between different lists. Users can click on right and left arrows and items move in queue respectively, where the previous or next item gets placed in place of item that was moved. There is also a placeholder element that should not be counted as a valid sortable, so there are checks to ignore it. If an item is in the last list and is moved to the right, it moves to the first list. If it is in the first list and gets moved to the left it moves to last list (last item in last list, first item in first list).
The first thing that came to mind was switch statements, but I can't figure out a way to implement them in this case, as each check is unique. Could you please provide another alternative or let me know if there is any other better way than using so many if
/ else
statements in this case?
There are more bits to this code that find correct adjacent lists, re initiate some stuff etc., but adding these in would make question very lengthy.
if ( (item.is(':last-child') || item.next().hasClass('item-dragable-placeholder') ) && direction == "right") {
var itemListAdjTmp = itemListAdj.find('.item-dragable').first(),
itemPartner;
if(itemList.find('.item-dragable').length <= 1) {
itemPartner = item.next('li');
item.insertBefore(itemListAdjTmp);
itemListAdjTmp.insertBefore(itemPartner);
} else {
itemPartner = item.prev('.item-dragable');
item.insertBefore(itemListAdjTmp);
itemListAdjTmp.insertAfter(itemPartner);
}
}
else if (!item.is(':last-child') && !item.next().hasClass('item-dragable-placeholder') && direction == "right") {
item.insertAfter(item.next('.item-dragable'));
}
else if (item.is(':first-child') && direction == "left") {
var itemListAdjTmp = itemListAdj.find('.item-dragable').last(),
itemPartner;
if(itemList.find('.item-dragable').length <= 1) {
itemPartner = item.next('li');
item.insertAfter(itemListAdjTmp);
itemListAdjTmp.insertBefore(itemPartner);
} else {
itemPartner = item.next('.item-dragable');
item.insertAfter(itemListAdjTmp);
itemListAdjTmp.insertBefore(itemPartner);
}
}
else if (!item.is('first-child') && direction == "left") {
item.insertBefore(item.prev('.item-dragable'));
}
Example markup:
<li class="queue-month">
<h3>November</h3>
<ol class="clearfix sortable-queue-month">
<li class="item-dragable" data-position="4">
<a href="#" class="remove" title="Remove item">Remove item</a>
<img src="..." alt="Socks outline">
<span class="drag-indicator">Drag</span>
<div class="sort-indicator-mobile">
<a href="#" class="sort-left">Sort Left</a>
<a href="#" class="sort-right">Sort Right</a>
</div>
</li>
<li class="item-dragable" data-position="5">
<a href="#" class="remove" title="Remove item">Remove item</a>
<img src="..." alt="Socks outline">
<span class="drag-indicator">Drag</span>
<div class="sort-indicator-mobile">
<a href="#" class="sort-left">Sort Left</a>
<a href="#" class="sort-right">Sort Right</a>
</div>
</li>
<li class="item-dragable" data-position="5">
<a href="#" class="remove" title="Remove item">Remove item</a>
<img src="<?= CDN ?>img/png/sock-placeholder.png" alt="Socks outline">
<span class="drag-indicator">Drag</span>
<div class="sort-indicator-mobile">
<a href="#" class="sort-left">Sort Left</a>
<a href="#" class="sort-right">Sort Right</a>
</div>
</li>
</ol>
</li>
<li class="queue-month">
<h3>November</h3>
<ol class="clearfix">
<li class="item-dragable-placeholder">
<a href="#" class="add-surprise-item" data-image="<?= CDN ?>img/png/sock-placeholder.png">Surprise me</a>
</li>
</ol>
</li>