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

Commit

Permalink
fix(dropdown): ensure class is present in dropdown-menu
Browse files Browse the repository at this point in the history
- Ensure `dropdown-menu` class is present when using as an attribute
  directive
- Refactor `DropdownController` to be more agnostic of directive

Closes #4523
Fixes #4442
  • Loading branch information
wesleycho committed Oct 3, 2015
1 parent c0dbf79 commit 92ab48e
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/dropdown/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
};
}])

.controller('UibDropdownController', ['$scope', '$attrs', '$parse', 'uibDropdownConfig', 'uibDropdownService', '$animate', '$uibPosition', '$document', '$compile', '$templateRequest', function($scope, $attrs, $parse, dropdownConfig, uibDropdownService, $animate, $position, $document, $compile, $templateRequest) {
.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) {
var self = this,
scope = $scope.$new(), // create a child scope so we are not polluting original one
templateScope,
Expand All @@ -77,9 +77,10 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
keynavEnabled =false,
selectedOption = null;

this.init = function(element) {
self.$element = element;

$element.addClass('dropdown');

this.init = function() {
if ($attrs.isOpen) {
getIsOpen = $parse($attrs.isOpen);
setIsOpen = getIsOpen.assign;
Expand All @@ -94,7 +95,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])

if (appendToBody && self.dropdownMenu) {
$document.find('body').append(self.dropdownMenu);
element.on('$destroy', function handleDestroyEvent() {
$element.on('$destroy', function handleDestroyEvent() {
self.dropdownMenu.remove();
});
}
Expand All @@ -118,7 +119,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
};

scope.getElement = function() {
return self.$element;
return $element;
};

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

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

scope.$watch('isOpen', function(isOpen, wasOpen) {
if (appendToBody && self.dropdownMenu) {
var pos = $position.positionElements(self.$element, self.dropdownMenu, 'bottom-left', true);
var pos = $position.positionElements($element, self.dropdownMenu, 'bottom-left', true);
var css = {
top: pos.top + 'px',
display: isOpen ? 'block' : 'none'
Expand All @@ -178,13 +179,13 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
css.right = 'auto';
} else {
css.left = 'auto';
css.right = (window.innerWidth - (pos.left + self.$element.prop('offsetWidth'))) + 'px';
css.right = (window.innerWidth - (pos.left + $element.prop('offsetWidth'))) + 'px';
}

self.dropdownMenu.css(css);
}

$animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass).then(function() {
$animate[isOpen ? 'addClass' : 'removeClass']($element, openClass).then(function() {
if (angular.isDefined(isOpen) && isOpen !== wasOpen) {
toggleInvoker($scope, { open: !!isOpen });
}
Expand Down Expand Up @@ -239,8 +240,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
return {
controller: 'UibDropdownController',
link: function(scope, element, attrs, dropdownCtrl) {
dropdownCtrl.init(element);
element.addClass('dropdown');
dropdownCtrl.init();
}
};
})
Expand All @@ -254,6 +254,8 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
return;
}

element.addClass('dropdown-menu');

var tplUrl = attrs.templateUrl;
if (tplUrl) {
dropdownCtrl.dropdownMenuTemplateUrl = tplUrl;
Expand Down Expand Up @@ -356,13 +358,14 @@ angular.module('ui.bootstrap.dropdown')
angular.extend(this, uibDropdownService);
}])

.controller('DropdownController', ['$scope', '$attrs', '$log', '$dropdownSuppressWarning', '$controller', function($scope, $attrs, $log, $dropdownSuppressWarning, $controller) {
.controller('DropdownController', ['$scope', '$element', '$attrs', '$log', '$dropdownSuppressWarning', '$controller', function($scope, $element, $attrs, $log, $dropdownSuppressWarning, $controller) {
if (!$dropdownSuppressWarning) {
$log.warn('DropdownController is now deprecated. Use UibDropdownController instead.');
}

angular.extend(this, $controller('UibDropdownController', {
$scope: $scope,
$element: $element,
$attrs: $attrs
}));
}])
Expand All @@ -375,8 +378,7 @@ angular.module('ui.bootstrap.dropdown')
$log.warn('dropdown is now deprecated. Use uib-dropdown instead.');
}

dropdownCtrl.init(element);
element.addClass('dropdown');
dropdownCtrl.init();
}
};
}])
Expand All @@ -394,6 +396,8 @@ angular.module('ui.bootstrap.dropdown')
return;
}

element.addClass('dropdown-menu');

var tplUrl = attrs.templateUrl;
if (tplUrl) {
dropdownCtrl.dropdownMenuTemplateUrl = tplUrl;
Expand Down

0 comments on commit 92ab48e

Please sign in to comment.