fixed some re-ordering edge cases and agg_group now uses group instead of vis.aggs

This commit is contained in:
Jim Unger 2016-09-22 17:01:18 -05:00
parent 6797ca5ddd
commit f4717fa5eb
3 changed files with 32 additions and 8 deletions

View file

@ -3,7 +3,7 @@
{{ groupName }}
</div>
<div ng-class="groupName" draggable-container="vis.aggs" class="vis-editor-agg-group">
<div ng-class="groupName" draggable-container="group" class="vis-editor-agg-group">
<!-- wrapper needed for nesting-indicator -->
<div ng-repeat="agg in group" draggable-item="agg" class="vis-editor-agg-wrapper">
<!-- agg.html - controls for aggregation -->

View file

@ -43,7 +43,16 @@ uiModules
});
$scope.$on('agg-drag-start', e => $scope.dragging = true);
$scope.$on('agg-drag-end', e => $scope.dragging = false);
$scope.$on('agg-drag-end', e => {
$scope.dragging = false;
//the aggs have been reordered in [group] and we need
//to apply that ordering to [vis.aggs]
const baseIndex = $scope.vis.aggs.indexOf($scope.group[0]);
_.forEach($scope.group, (agg, index) => {
_.move($scope.vis.aggs, agg, baseIndex + index);
});
});
}
};

View file

@ -69,19 +69,34 @@ uiModules
const list = $scope.draggableContainerCtrl.getList();
const itemScope = $(el).scope();
const item = itemScope.draggableItemCtrl.getItem();
const toIndex = getSiblingItemIndex(list, sibling);
const fromIndex = list.indexOf(item);
const siblingIndex = getItemIndexFromElement(list, sibling);
const toIndex = getTargetIndex(list, fromIndex, siblingIndex);
_.move(list, item, toIndex);
}
function getSiblingItemIndex(list, sibling) {
if (!sibling) { // means the item was dropped at the end of the list
function getTargetIndex(list, fromIndex, siblingIndex) {
if (siblingIndex === -1) {
// means the item was dropped at the end of the list
return list.length - 1;
} else if (fromIndex < siblingIndex) {
// An item moving from a lower index to a higher index will offset the
// index of the earlier items by one.
return siblingIndex - 1;
}
const siblingScope = $(sibling).scope();
const siblingItem = siblingScope.draggableItemCtrl.getItem();
const siblingIndex = list.indexOf(siblingItem);
return siblingIndex;
}
function getItemIndexFromElement(list, element) {
if (!element) return -1;
const scope = $(element).scope();
const item = scope.draggableItemCtrl.getItem();
const index = list.indexOf(item);
return index;
}
}
};