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

Commit 92ab48e

Browse files
committed
fix(dropdown): ensure class is present in dropdown-menu
- Ensure `dropdown-menu` class is present when using as an attribute directive - Refactor `DropdownController` to be more agnostic of directive Closes #4523 Fixes #4442
1 parent c0dbf79 commit 92ab48e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/dropdown/dropdown.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
6565
};
6666
}])
6767

68-
.controller('UibDropdownController', ['$scope', '$attrs', '$parse', 'uibDropdownConfig', 'uibDropdownService', '$animate', '$uibPosition', '$document', '$compile', '$templateRequest', function($scope, $attrs, $parse, dropdownConfig, uibDropdownService, $animate, $position, $document, $compile, $templateRequest) {
68+
.controller('UibDropdownController', ['$scope', '$element', '$attrs', '$parse', 'uibDropdownConfig', 'uibDropdownService', '$animate', '$uibPosition', '$document', '$compile', '$templateRequest', function($scope, $element, $attrs, $parse, dropdownConfig, uibDropdownService, $animate, $position, $document, $compile, $templateRequest) {
6969
var self = this,
7070
scope = $scope.$new(), // create a child scope so we are not polluting original one
7171
templateScope,
@@ -77,9 +77,10 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
7777
keynavEnabled =false,
7878
selectedOption = null;
7979

80-
this.init = function(element) {
81-
self.$element = element;
8280

81+
$element.addClass('dropdown');
82+
83+
this.init = function() {
8384
if ($attrs.isOpen) {
8485
getIsOpen = $parse($attrs.isOpen);
8586
setIsOpen = getIsOpen.assign;
@@ -94,7 +95,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
9495

9596
if (appendToBody && self.dropdownMenu) {
9697
$document.find('body').append(self.dropdownMenu);
97-
element.on('$destroy', function handleDestroyEvent() {
98+
$element.on('$destroy', function handleDestroyEvent() {
9899
self.dropdownMenu.remove();
99100
});
100101
}
@@ -118,7 +119,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
118119
};
119120

120121
scope.getElement = function() {
121-
return self.$element;
122+
return $element;
122123
};
123124

124125
scope.isKeynavEnabled = function() {
@@ -128,7 +129,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
128129
scope.focusDropdownEntry = function(keyCode) {
129130
var elems = self.dropdownMenu ? //If append to body is used.
130131
(angular.element(self.dropdownMenu).find('a')) :
131-
(angular.element(self.$element).find('ul').eq(0).find('a'));
132+
(angular.element($element).find('ul').eq(0).find('a'));
132133

133134
switch (keyCode) {
134135
case (40): {
@@ -166,7 +167,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
166167

167168
scope.$watch('isOpen', function(isOpen, wasOpen) {
168169
if (appendToBody && self.dropdownMenu) {
169-
var pos = $position.positionElements(self.$element, self.dropdownMenu, 'bottom-left', true);
170+
var pos = $position.positionElements($element, self.dropdownMenu, 'bottom-left', true);
170171
var css = {
171172
top: pos.top + 'px',
172173
display: isOpen ? 'block' : 'none'
@@ -178,13 +179,13 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
178179
css.right = 'auto';
179180
} else {
180181
css.left = 'auto';
181-
css.right = (window.innerWidth - (pos.left + self.$element.prop('offsetWidth'))) + 'px';
182+
css.right = (window.innerWidth - (pos.left + $element.prop('offsetWidth'))) + 'px';
182183
}
183184

184185
self.dropdownMenu.css(css);
185186
}
186187

187-
$animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass).then(function() {
188+
$animate[isOpen ? 'addClass' : 'removeClass']($element, openClass).then(function() {
188189
if (angular.isDefined(isOpen) && isOpen !== wasOpen) {
189190
toggleInvoker($scope, { open: !!isOpen });
190191
}
@@ -239,8 +240,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
239240
return {
240241
controller: 'UibDropdownController',
241242
link: function(scope, element, attrs, dropdownCtrl) {
242-
dropdownCtrl.init(element);
243-
element.addClass('dropdown');
243+
dropdownCtrl.init();
244244
}
245245
};
246246
})
@@ -254,6 +254,8 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
254254
return;
255255
}
256256

257+
element.addClass('dropdown-menu');
258+
257259
var tplUrl = attrs.templateUrl;
258260
if (tplUrl) {
259261
dropdownCtrl.dropdownMenuTemplateUrl = tplUrl;
@@ -356,13 +358,14 @@ angular.module('ui.bootstrap.dropdown')
356358
angular.extend(this, uibDropdownService);
357359
}])
358360

359-
.controller('DropdownController', ['$scope', '$attrs', '$log', '$dropdownSuppressWarning', '$controller', function($scope, $attrs, $log, $dropdownSuppressWarning, $controller) {
361+
.controller('DropdownController', ['$scope', '$element', '$attrs', '$log', '$dropdownSuppressWarning', '$controller', function($scope, $element, $attrs, $log, $dropdownSuppressWarning, $controller) {
360362
if (!$dropdownSuppressWarning) {
361363
$log.warn('DropdownController is now deprecated. Use UibDropdownController instead.');
362364
}
363365

364366
angular.extend(this, $controller('UibDropdownController', {
365367
$scope: $scope,
368+
$element: $element,
366369
$attrs: $attrs
367370
}));
368371
}])
@@ -375,8 +378,7 @@ angular.module('ui.bootstrap.dropdown')
375378
$log.warn('dropdown is now deprecated. Use uib-dropdown instead.');
376379
}
377380

378-
dropdownCtrl.init(element);
379-
element.addClass('dropdown');
381+
dropdownCtrl.init();
380382
}
381383
};
382384
}])
@@ -394,6 +396,8 @@ angular.module('ui.bootstrap.dropdown')
394396
return;
395397
}
396398

399+
element.addClass('dropdown-menu');
400+
397401
var tplUrl = attrs.templateUrl;
398402
if (tplUrl) {
399403
dropdownCtrl.dropdownMenuTemplateUrl = tplUrl;

0 commit comments

Comments
 (0)