Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Prevent whole page scroll when scrolling ui-choices #940

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ var uis = angular.module('ui.select', [])
generateId: function() {
return latestId++;
},
appendToBody: false
appendToBody: false,
preventPageScroll: true
})

// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
Expand Down
12 changes: 12 additions & 0 deletions src/uiSelectChoicesDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ uis.directive('uiSelectChoices',
if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat

if ($select.preventPageScroll) {// Prevent the whole page for scrolling when the choices ul reaches it's scroll limits.
element.bind('mousewheel', function(event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to element.on

var heightDif = this.offsetHeight - this.clientHeight,
maxScrollTop = this.scrollHeight - this.offsetHeight + heightDif;

if ((this.scrollTop === maxScrollTop && event.deltaY > 0) ||
(this.scrollTop === 0 && event.deltaY < 0)) {
event.preventDefault();
}
});
}

$compile(element, transcludeFn)(scope); //Passing current transcludeFn to be able to append elements correctly from uisTranscludeAppend

scope.$watch('$select.search', function(newValue) {
Expand Down
2 changes: 2 additions & 0 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ uis.controller('uiSelectCtrl',
ctrl.clickTriggeredSelect = false;
ctrl.$filter = $filter;

ctrl.preventPageScroll = undefined; //Initialized inside uiSelect directive link function

ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
if (ctrl.searchInput.length !== 1) {
throw uiSelectMinErr('searchInput', "Expected 1 input.ui-select-search but got '{0}'.", ctrl.searchInput.length);
Expand Down
8 changes: 8 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ uis.directive('uiSelect',
}
}();

$select.preventPageScroll = function() {
if (angular.isDefined(attrs.preventPageScroll)) {
return $parse(attrs.preventPageScroll)();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it desired to parse this without a $scope passed into this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same $parse function from the others.
it only needs to parse string to boolean.

} else {
return uiSelectConfig.preventPageScroll;
}
}();

$select.onSelectCallback = $parse(attrs.onSelect);
$select.onRemoveCallback = $parse(attrs.onRemove);

Expand Down