From 6e301680b33fd779d01429489945305f4e5ef7e6 Mon Sep 17 00:00:00 2001 From: "Emmanuel L. Daher" Date: Mon, 18 May 2015 13:24:47 -0300 Subject: [PATCH 1/2] Prevent whole page scroll when scrolling ui-choices Prevent the anoying default browser behavior of scrolling the whole page when scrolling in ui-choices options, usefull when it has lots of options, and changeble in uiSelectConfig and preventPageScroll attr. --- src/uiSelectChoicesDirective.js | 12 ++++++++++++ src/uiSelectController.js | 2 ++ src/uiSelectDirective.js | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/src/uiSelectChoicesDirective.js b/src/uiSelectChoicesDirective.js index 41d24e31b..0e4f7f65a 100644 --- a/src/uiSelectChoicesDirective.js +++ b/src/uiSelectChoicesDirective.js @@ -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) { + 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) { diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 9fe273ac2..bf435cc7d 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -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); diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 76df08b67..88767fa77 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -41,6 +41,14 @@ uis.directive('uiSelect', } }(); + $select.preventPageScroll = function() { + if (angular.isDefined(attrs.preventPageScroll)) { + return $parse(attrs.preventPageScroll)(); + } else { + return uiSelectConfig.preventPageScroll; + } + }(); + $select.onSelectCallback = $parse(attrs.onSelect); $select.onRemoveCallback = $parse(attrs.onRemove); From 2f90a56b6657a038a19cc0d37dc3338d152f949b Mon Sep 17 00:00:00 2001 From: "Emmanuel L. Daher" Date: Mon, 18 May 2015 13:35:39 -0300 Subject: [PATCH 2/2] defaultConfig for for my last commit, preventPageScroll default value for preventPageScroll config option. --- src/common.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/common.js b/src/common.js index efc7a5b9f..c64e4772f 100644 --- a/src/common.js +++ b/src/common.js @@ -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