Commit fd2f720a by Peter Fogg Committed by cahrens

Add auto-expand to overview lists.

parent 2d3cba06
...@@ -12,7 +12,7 @@ $(document).ready(function() { ...@@ -12,7 +12,7 @@ $(document).ready(function() {
'.id-holder', '.id-holder',
'a.subsection-drag-handle', 'a.subsection-drag-handle',
'.subsection-list > ol', '.subsection-list > ol',
'.subsection-list > ol' '.courseware-section'
); );
// Unit // Unit
makeDraggable( makeDraggable(
...@@ -23,8 +23,9 @@ $(document).ready(function() { ...@@ -23,8 +23,9 @@ $(document).ready(function() {
); );
/* /*
* Make `type` draggable using `handleClass`, and able to be * Make `type` draggable using `handleClass`, able to be dropped
* dropped into `droppableClass`. * into `droppableClass`, and with parent type
* `parentLocationSelector`.
*/ */
function makeDraggable(type, handleClass, droppableClass, parentLocationSelector) { function makeDraggable(type, handleClass, droppableClass, parentLocationSelector) {
_.each( _.each(
...@@ -61,32 +62,44 @@ $(document).ready(function() { ...@@ -61,32 +62,44 @@ $(document).ready(function() {
var siblings = container.children().filter(function () { var siblings = container.children().filter(function () {
return $(this).data('id') !== undefined && !$(this).is(ele); return $(this).data('id') !== undefined && !$(this).is(ele);
}); });
// If the list is empty, we should prepend to it // If the container is collapsed, check to see if the
if(siblings.length == 0) { // element is on top of its parent list -- don't check the
if(Math.abs(eleY - container.offset().top) < 50) { // position of the container
var parentList = container.parents(ele.data('parent-location-selector')).first();
if(parentList.hasClass('collapsed')) {
if(Math.abs(eleY - parentList.offset().top) < 50) {
return { return {
ele: container, ele: container,
attachMethod: 'prepend' attachMethod: 'prepend',
parentList: parentList
}; };
} }
} }
// Otherwise the list is populated, and we should attach before/after a sibling // Otherwise, do check the container
else { else {
for(var j = 0; j < siblings.length; j++) { // If the list is empty, we should prepend to it
var $sibling = $(siblings[j]); if(siblings.length == 0 &&
var siblingHeight = $sibling.height(); Math.abs(eleY - container.offset().top) < 50) {
var siblingY = $sibling.offset().top; return {
ele: container,
if(Math.abs(eleY - siblingY) < siblingHeight) { attachMethod: 'prepend'
return { };
ele: $sibling, }
attachMethod: siblingY > eleY ? 'before' : 'after' // Otherwise the list is populated, and we should attach before/after a sibling
}; else {
for(var j = 0; j < siblings.length; j++) {
var $sibling = $(siblings[j]);
var siblingY = $sibling.offset().top;
if(Math.abs(eleY - siblingY) < $sibling.height()) {
return {
ele: $sibling,
attachMethod: siblingY > eleY ? 'before' : 'after'
};
}
} }
} }
} }
} }
// Failed drag // Failed drag
return { return {
ele: null, ele: null,
...@@ -103,33 +116,56 @@ $(document).ready(function() { ...@@ -103,33 +116,56 @@ $(document).ready(function() {
// Where we started, in case of a failed drag // Where we started, in case of a failed drag
offset: ele.offset(), offset: ele.offset(),
// Which element will be dropped into/onto on success // Which element will be dropped into/onto on success
dropDestination: null dropDestination: null,
// Timer if we're hovering over a collapsed section
expandTimer: null,
// The list which will be expanded on hover
toExpand: null
}; };
} }
function onDragMove(draggie, event, pointer) { function onDragMove(draggie, event, pointer) {
var ele = $(draggie.element); var ele = $(draggie.element);
var currentReplacement = findDestination(ele).ele; var destinationInfo = findDestination(ele);
var destinationEle = destinationInfo.ele;
var parentList = destinationInfo.parentList;
// Clear the timer if we're not hovering over any element
if(!parentList) {
clearTimeout(dragState.expandTimer);
}
// If we're hovering over a new element, clear the timer and
// set a new one
else if(!dragState.toExpand || parentList[0] !== dragState.toExpand[0]) {
clearTimeout(dragState.expandTimer);
dragState.expandTimer = setTimeout(function() {
parentList.removeClass('collapsed');
}, 1000);
dragState.toExpand = parentList;
}
// Clear out the old destination // Clear out the old destination
if(dragState.dropDestination) { if(dragState.dropDestination) {
dragState.dropDestination.removeClass('drop-destination'); dragState.dropDestination.removeClass('drop-destination');
} }
// Mark the new destination // Mark the new destination
if(currentReplacement) { if(destinationEle) {
currentReplacement.addClass('drop-destination'); destinationEle.addClass('drop-destination');
dragState.dropDestination = currentReplacement; dragState.dropDestination = destinationEle;
} }
} }
function onDragEnd(draggie, event, pointer) { function onDragEnd(draggie, event, pointer) {
var ele = $(draggie.element); var ele = $(draggie.element);
var intersect = findDestination(ele); var destinationInfo = findDestination(ele);
var destination = intersect.ele; var destination = destinationInfo.ele;
var method = intersect.attachMethod;
// If the drag succeeded, rearrange the DOM and send the result. // If the drag succeeded, rearrange the DOM and send the result.
if(destination) { if(destination) {
// Make sure we don't drop into a collapsed element
if(destinationInfo.parentList) {
destinationInfo.parentList.removeClass('collapsed');
}
var method = destinationInfo.attachMethod;
destination[method](ele); destination[method](ele);
handleReorder(ele); handleReorder(ele);
} }
...@@ -144,6 +180,7 @@ $(document).ready(function() { ...@@ -144,6 +180,7 @@ $(document).ready(function() {
if(dragState.dropDestination) { if(dragState.dropDestination) {
dragState.dropDestination.removeClass('drop-destination'); dragState.dropDestination.removeClass('drop-destination');
} }
clearTimeout(dragState.expandTimer);
dragState = {}; dragState = {};
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment