-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathangular-scroll-dropdown.js
83 lines (71 loc) · 3.53 KB
/
angular-scroll-dropdown.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
74
75
76
77
78
79
80
81
82
83
/*
* angular-scroll-dropdown
*
* display dropdown even in div who have overflow=[auto|hidden|scroll]
*
*/
(function() {
'use strict';
angular.module('angular-scroll-dropdown', ['ui.bootstrap'])
.directive('dropdownscroll', ['$window', function($window) {
return {
restrict: 'C',
link: function (scope, elm) {
var button = elm.find('.dropdown-toggle');
// change dropdown position if click on button
button.bind('click', function() {
var dropdown = elm.find('.dropdown-menu-scoll');
var dropDownTopInBottom = button.offset().top + button.outerHeight() - $window.pageYOffset;
var dropDownTopInTop = button.offset().top - $window.pageYOffset;
if ($(window).height() < (dropDownTopInBottom + dropdown.height())) {
dropdown.css('top', (dropDownTopInTop - dropdown.height()) + "px");
} else {
dropdown.css('top', (dropDownTopInBottom) + "px");
}
dropdown.css('left', button.offset().left + "px");
});
// parent is scrolling => updates the position of the active dropdown (if there is one)
scope.$on('contentScroll:scrolling', function (event, scroll) {
var dropdown = elm.find('.dropdown-menu-scoll:visible');
if (dropdown.length !== 0) {
var dropDownTopInBottom = button.offset().top + button.outerHeight() - $window.pageYOffset;
var dropDownTopInTop = button.offset().top - $window.pageYOffset;
if ($(window).height() < (dropDownTopInBottom + dropdown.height())) {
dropdown.css('top', (dropDownTopInTop - dropdown.outerHeight()) + "px");
} else {
dropdown.css('top', (dropDownTopInBottom) + "px");
}
dropdown.css('left', button.offset().left + "px");
if (dropDownTopInTop < scroll.top || dropDownTopInBottom > scroll.bottom) {
button.click();
}
}
});
},
};
}])
.directive('contentscroll', ['$document', '$window', function($document, $window) {
return {
restrict: 'C',
link: function(scope, elm) {
var doc = angular.element($document);
// send message to children if scrolling
elm.bind('scroll', function() {
scope.$broadcast('contentScroll:scrolling',
{
top: elm.offset().top,
bottom: (elm.offset().top + elm.height()),
});
});
// Window has scrolling also
doc.bind("scroll", function() {
scope.$broadcast('contentScroll:scrolling',
{
top: elm.offset().top,
bottom: (elm.offset().top + elm.height()),
});
});
},
};
}]);
})();