-
Notifications
You must be signed in to change notification settings - Fork 31
/
angular-slimscroll.js
73 lines (58 loc) · 2.06 KB
/
angular-slimscroll.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
angular.module('ui.slimscroll', []).directive('slimscroll', ['$timeout', '$window', function ($timeout, $window) {
'use strict';
return {
restrict: 'A',
link: function ($scope, $elem, $attr) {
var off = [];
var option = {};
var refresh = function () {
var isEventBound = false;
$timeout(function () {
if (angular.isDefined($attr.slimscroll)) {
option = $scope.$eval($attr.slimscroll) || {};
} else if ($attr.slimscrollOption) {
option = $scope.$eval($attr.slimscrollOption) || {};
}
var el = angular.element($elem);
el.slimScroll({ destroy: true });
if (option.onScroll && !isEventBound) {
el.slimScroll(option).bind('slimscrolling', function(e, pos) {
option.onScroll();
isEventBound = true;
});
} else {
el.slimScroll(option);
}
});
};
angular.element($window).bind('resize', function() {
if ($attr.slimscroll) {
option = $scope.$eval($attr.slimscroll);
} else if ($attr.slimscrollOption) {
option = $scope.$eval($attr.slimscrollOption);
}
$($elem).slimScroll(option);
});
var registerWatch = function () {
if (angular.isDefined($attr.slimscroll) && !option.noWatch) {
off.push($scope.$watchCollection($attr.slimscroll, refresh));
}
if ($attr.slimscrollWatch) {
off.push($scope.$watchCollection($attr.slimscrollWatch, refresh));
}
if ($attr.slimscrolllistento) {
off.push($scope.$on($attr.slimscrolllistento, refresh));
}
};
var destructor = function () {
angular.element($elem).slimScroll({destroy: true});
off.forEach(function (unbind) {
unbind();
});
off = null;
};
off.push($scope.$on('$destroy', destructor));
registerWatch();
}
};
}]);