@@ -7,19 +7,26 @@ uiModules
7
7
. get ( 'app/visualize' )
8
8
. directive ( 'draggableContainer' , function ( ) {
9
9
10
+ const $scopes = new WeakMap ( ) ;
11
+
10
12
return {
11
13
restrict : 'A' ,
12
14
scope : true ,
13
15
controllerAs : 'draggableContainerCtrl' ,
14
- controller ( $scope , $attrs , $parse ) {
16
+ controller ( $scope , $attrs , $parse , $element ) {
17
+ $scopes . set ( $element . get ( 0 ) , $scope ) ;
18
+ this . linkDraggableItem = ( el , $scope ) => {
19
+ $scopes . set ( el , $scope ) ;
20
+ } ;
21
+
15
22
this . getList = ( ) => $parse ( $attrs . draggableContainer ) ( $scope ) ;
16
23
} ,
17
24
link ( $scope , $el , attr ) {
18
25
const drake = dragula ( {
19
26
containers : $el . toArray ( ) ,
20
27
moves ( el , source , handle ) {
21
- const itemScope = $ ( el ) . scope ( ) ;
22
- if ( ! ( 'draggableItemCtrl' in itemScope ) ) {
28
+ const itemScope = $scopes . get ( el ) ;
29
+ if ( ! itemScope || ! ( 'draggableItemCtrl' in itemScope ) ) {
23
30
return ; // only [draggable-item] is draggable
24
31
}
25
32
return itemScope . draggableItemCtrl . moves ( handle ) ;
@@ -53,21 +60,24 @@ uiModules
53
60
54
61
function markDragging ( isDragging ) {
55
62
return el => {
56
- const scope = $ ( el ) . scope ( ) ;
63
+ const scope = $scopes . get ( el ) ;
64
+ if ( ! scope ) return ;
57
65
scope . isDragging = isDragging ;
58
66
scope . $apply ( ) ;
59
67
} ;
60
68
}
61
69
62
70
function forwardEvent ( type , el , ...args ) {
63
71
const name = `drag-${ prettifiedDrakeEvents [ type ] || type } ` ;
64
- const scope = $ ( el ) . scope ( ) ;
72
+ const scope = $scopes . get ( el ) ;
73
+ if ( ! scope ) return ;
65
74
scope . $broadcast ( name , el , ...args ) ;
66
75
}
67
76
68
77
function drop ( el , target , source , sibling ) {
69
78
const list = $scope . draggableContainerCtrl . getList ( ) ;
70
- const itemScope = $ ( el ) . scope ( ) ;
79
+ const itemScope = $scopes . get ( el ) ;
80
+ if ( ! itemScope ) return ;
71
81
const item = itemScope . draggableItemCtrl . getItem ( ) ;
72
82
const fromIndex = list . indexOf ( item ) ;
73
83
const siblingIndex = getItemIndexFromElement ( list , sibling ) ;
@@ -91,7 +101,8 @@ uiModules
91
101
function getItemIndexFromElement ( list , element ) {
92
102
if ( ! element ) return - 1 ;
93
103
94
- const scope = $ ( element ) . scope ( ) ;
104
+ const scope = $scopes . get ( element ) ;
105
+ if ( ! scope ) return ;
95
106
const item = scope . draggableItemCtrl . getItem ( ) ;
96
107
const index = list . indexOf ( item ) ;
97
108
0 commit comments