Skip to content

Instantly share code, notes, and snippets.

@timhunt
Created July 5, 2017 14:52
Show Gist options
  • Save timhunt/d0026e28c7766a1748ace130e4190fbf to your computer and use it in GitHub Desktop.
Save timhunt/d0026e28c7766a1748ace130e4190fbf to your computer and use it in GitHub Desktop.
Simple drag drop
define(['jquery', 'core/modal_factory', 'core/modal_events', 'theme_osep/drag'],
function($, ModalFactory, ModalEvents, drag) {
/**
* @alias block_dashboardboxes/customise
*/
var t = {
listToOrder: 'ul.sortme',
sectionDraggingStartTime: null,
itemDragging: null,
proxy: null,
init: function() {
// AJAX for section drag and click-to-move.
$(t.listToOrder).on('mousedown', 'li', function(event) {
event.preventDefault();
t.startDrag(event);
});
$(t.listToOrder).on('click', 'li', function(event) {
event.preventDefault();
t.accessibleMove();
});
$(t.listToOrder).on('click', '.reorder-list-dialogue ul a', function(event) {
event.preventDefault();
t.accessibleMoveTo();
});
},
startDrag : function(event) {
t.sectionDraggingStartTime = new Date().getTime();
t.itemDragging = $(event.currentTarget).closest('li');
t.itemDragging.addClass('itemmoving');
t.proxy = $('<div class="itemproxy">' + t.itemDragging.html() + '</div>');
$(document.body).append(t.proxy);
t.proxy.css(t.itemDragging.offset());
// Start drag.
drag.start(event, t.proxy, t.dragMove, t.dragEnd);
},
dragMove : function(x, y) {
var previous = t.itemDragging.prev('li'),
next = t.itemDragging.next('li');
if (previous[0] && previous.offset().top > t.proxy.offset().top) {
t.itemDragging.insertBefore(previous);
} else if (next[0] && next.offset().top < t.proxy.offset().top) {
t.itemDragging.insertAfter(next);
}
},
dragEnd : function(event) {
t.proxy.remove();
t.proxy = null;
t.itemDragging.removeClass('itemmoving');
var newOrder = $(t.listToOrder + ' > li').map(
function() { return this.id; }).get();
alert(newOrder);
}
}
return t;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment