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 all 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
1 change: 1 addition & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ var uis = angular.module('ui.select', [])
return latestId++;
},
appendToBody: false,
preventPageScroll: true,
spinnerEnabled: false,
spinnerClass: 'glyphicon-refresh ui-select-spin',
backspaceReset: true
Expand Down
13 changes: 13 additions & 0 deletions src/uiSelectChoicesDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ uis.directive('uiSelectChoices',
clickTarget.attr('ng-click', '$select.select(' + parserResult.itemName + ',$select.skipFocusser,$event)');

return function link(scope, element, attrs, $select) {

if ($select.preventPageScroll) {// Prevent the whole page for scrolling when the choices ul reaches it's scroll limits.
element.on('mousewheel', function(event) {
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

$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult
$select.disableChoiceExpression = attrs.uiDisableChoice;
Expand Down
2 changes: 2 additions & 0 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ uis.controller('uiSelectCtrl',
}
})();

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 @@ -52,6 +52,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;
}
}();

scope.$watch('skipFocusser', function() {
var skipFocusser = scope.$eval(attrs.skipFocusser);
$select.skipFocusser = skipFocusser !== undefined ? skipFocusser : uiSelectConfig.skipFocusser;
Expand Down