forked from bevacqua/angularjs-dragula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directive.js
56 lines (48 loc) · 1.33 KB
/
directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var dragula = require('dragula');
/*jshint unused: false*/
function register (angular) {
return ['dragulaService', function angularDragula (dragulaService) {
return {
restrict: 'A',
scope: {
dragulaScope: '=',
dragulaModel: '='
},
link: link
};
function link (scope, elem, attrs) {
var dragulaScope = scope.dragulaScope || scope.$parent;
var container = elem[0];
var name = scope.$eval(attrs.dragula);
var drake;
var bag = dragulaService.find(dragulaScope, name);
if (bag) {
drake = bag.drake;
drake.containers.push(container);
} else {
drake = dragula({
containers: [container]
});
dragulaService.add(dragulaScope, name, drake);
}
scope.$watch('dragulaModel', function (newValue, oldValue) {
if (!newValue) {
return;
}
if (drake.models) {
var modelIndex = oldValue ? drake.models.indexOf(oldValue) : -1;
if (modelIndex >= 0) {
drake.models.splice(modelIndex, 1, newValue);
} else {
drake.models.push(newValue);
}
} else {
drake.models = [newValue];
}
dragulaService.handleModels(dragulaScope, drake);
});
}
}];
}
module.exports = register;