perfect-scrollbar.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. app.directive('perfectScrollbar', ['$parse', '$window',
  2. function($parse, $window) {
  3. var psOptions = ['wheelSpeed', 'wheelPropagation', 'minScrollbarLength', 'useBothWheelAxes', 'useKeyboard', 'suppressScrollX', 'suppressScrollY', 'scrollXMarginOffset', 'scrollYMarginOffset', 'includePadding'//, 'onScroll', 'scrollDown'
  4. ];
  5. return {
  6. restrict: 'EA',
  7. transclude: true,
  8. template: '<div><div ng-transclude></div></div>',
  9. replace: true,
  10. link: function($scope, $elem, $attr) {
  11. var jqWindow = angular.element($window);
  12. var options = {};
  13. if(!$scope.app.isMobile) {
  14. for(var i = 0, l = psOptions.length; i < l; i++) {
  15. var opt = psOptions[i];
  16. if($attr[opt] !== undefined) {
  17. options[opt] = $parse($attr[opt])();
  18. }
  19. }
  20. $scope.$evalAsync(function() {
  21. $elem.perfectScrollbar(options);
  22. var onScrollHandler = $parse($attr.onScroll);
  23. $elem.scroll(function() {
  24. var scrollTop = $elem.scrollTop();
  25. var scrollHeight = $elem.prop('scrollHeight') - $elem.height();
  26. $scope.$apply(function() {
  27. onScrollHandler($scope, {
  28. scrollTop: scrollTop,
  29. scrollHeight: scrollHeight
  30. });
  31. });
  32. });
  33. });
  34. function update(event) {
  35. $scope.$evalAsync(function() {
  36. if($attr.scrollDown == 'true' && event != 'mouseenter') {
  37. setTimeout(function() {
  38. $($elem).scrollTop($($elem).prop("scrollHeight"));
  39. }, 100);
  40. }
  41. $elem.perfectScrollbar('update');
  42. });
  43. }
  44. // This is necessary when you don't watch anything with the scrollbar
  45. $elem.bind('mousemove', update);
  46. // Possible future improvement - check the type here and use the appropriate watch for non-arrays
  47. if($attr.refreshOnChange) {
  48. $scope.$watchCollection($attr.refreshOnChange, function() {
  49. update();
  50. });
  51. }
  52. // this is from a pull request - I am not totally sure what the original issue is but seems harmless
  53. if($attr.refreshOnResize) {
  54. jqWindow.on('resize', update);
  55. }
  56. $elem.bind('$destroy', function() {
  57. jqWindow.off('resize', update);
  58. $elem.perfectScrollbar('destroy');
  59. });
  60. }
  61. }
  62. };
  63. }]);