Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draggable] track draggable item scopes #9153

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/ui/public/draggable/draggable_container.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ uiModules
.get('kibana')
.directive('draggableContainer', function () {

const $scopes = new WeakMap();

return {
restrict: 'A',
scope: true,
controllerAs: 'draggableContainerCtrl',
controller($scope, $attrs, $parse) {
controller($scope, $attrs, $parse, $element) {
$scopes.set($element.get(0), $scope);
this.linkDraggableItem = (el, $scope) => {
$scopes.set(el, $scope);
};

this.getList = () => $parse($attrs.draggableContainer)($scope);
},
link($scope, $el, attr) {
const drake = dragula({
containers: $el.toArray(),
moves(el, source, handle) {
const itemScope = $(el).scope();
if (!('draggableItemCtrl' in itemScope)) {
const itemScope = $scopes.get(el);
if (!itemScope || !('draggableItemCtrl' in itemScope)) {
return; // only [draggable-item] is draggable
}
return itemScope.draggableItemCtrl.moves(handle);
Expand Down Expand Up @@ -53,21 +60,24 @@ uiModules

function markDragging(isDragging) {
return el => {
const scope = $(el).scope();
const scope = $scopes.get(el);
if (!scope) return;
scope.isDragging = isDragging;
scope.$apply();
};
}

function forwardEvent(type, el, ...args) {
const name = `drag-${prettifiedDrakeEvents[type] || type}`;
const scope = $(el).scope();
const scope = $scopes.get(el);
if (!scope) return;
scope.$broadcast(name, el, ...args);
}

function drop(el, target, source, sibling) {
const list = $scope.draggableContainerCtrl.getList();
const itemScope = $(el).scope();
const itemScope = $scopes.get(el);
if (!itemScope) return;
const item = itemScope.draggableItemCtrl.getItem();
const fromIndex = list.indexOf(item);
const siblingIndex = getItemIndexFromElement(list, sibling);
Expand All @@ -91,7 +101,8 @@ uiModules
function getItemIndexFromElement(list, element) {
if (!element) return -1;

const scope = $(element).scope();
const scope = $scopes.get(element);
if (!scope) return;
const item = scope.draggableItemCtrl.getItem();
const index = list.indexOf(item);

Expand Down
3 changes: 2 additions & 1 deletion src/ui/public/draggable/draggable_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ uiModules
return movable;
};
},
link($scope, $el, attr) {
link($scope, $el, attr, draggableController) {
draggableController.linkDraggableItem($el.get(0), $scope);
}
};
});