function handleInternalClass()

in public/src/js/utils/drag-dispatcher.js [58:105]


function handleInternalClass ({sourceItem, sourceGroup}, targetItem, targetGroup) {

    if (targetGroup.parent && targetGroup.parent.opts && !targetGroup.parent.opts.id) {
        alert('You cannot drag a collection to an empty front. You must create a new collection instead');
        return;
    }

    var {position, target, isAfter} = normalizeTarget(sourceItem, targetItem, targetGroup);

    var insertAt = 0;

    var targetGroupItems = targetGroup.items();
    for (var i = 0; i < targetGroupItems.length; i++) {
        var item = targetGroupItems[i];

        if (_.result(item, 'id') === _.result(target, 'id')) {
            break;
        } else if (_.result(item, 'id') !== _.result(sourceItem, 'id')) {
            insertAt++;
        }
    }
    insertAt += isAfter || 0;

    removeById(targetGroup.items, urlAbsPath(sourceItem.id));

    var newItems = newItemsConstructor(sourceItem, targetGroup);

    if (!newItems[0]) {
        alert('Sorry, but you can\'t add that item');
    } else {
        var targetContext = targetGroup.front;

        targetGroup.items.splice(insertAt, 0, newItems[0]);

        return validate(sourceItem, newItems, targetContext)
        .then(() => {
            if (targetGroup.parent) {
                return persist(sourceItem, newItems, sourceItem.front, sourceGroup, targetContext, targetGroup, position, isAfter);
            }
        })
        .catch(err => {
            _.each(newItems, item => targetGroup.items.remove(item));
            if (err) {
                alert(err);
            }
        });
    }
}