Skip to content

Commit

Permalink
[draggable] continue to work when debug info is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Nov 21, 2016
1 parent 269ee06 commit 7bed6e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ uiModules
.get('app/visualize')
.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
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);
}
};
});

0 comments on commit 7bed6e8

Please sign in to comment.