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

Fix to allow a form inside a dropdown toggle #324

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 14 additions & 2 deletions src/dropdownToggle/dropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @restrict class or attribute
* @example:
<li class="dropdown">
<a class="dropdown-toggle">My Dropdown Menu</a>
<a class="dropdown-toggle" close-on-click="true|false">My Dropdown Menu</a>
<ul class="dropdown-menu">
<li ng-repeat="choice in dropChoices">
<a ng-href="{{choice.href}}">{{choice.text}}</a>
Expand All @@ -20,8 +20,20 @@ angular.module('ui.bootstrap.dropdownToggle', []).directive('dropdownToggle', ['
link: function(scope, element, attrs) {
scope.$watch('$location.path', function() { closeMenu(); });
element.parent().bind('click', function() { closeMenu(); });
element.bind('click', function (event) {

var closeOnClick = angular.isDefined(attrs.closeOnClick) ? scope.$eval(attrs.closeOnClick) : true;
if (closeOnClick === false) {
angular.forEach(element.parent().children(), function(ele) {
var aEle = angular.element(ele);
if (aEle.hasClass('dropdown-menu')) {
aEle.bind('click', function() {
event.stopPropagation();
});
}
});
}

element.bind('click', function (event) {
var elementWasOpen = (element === openElement);

event.preventDefault();
Expand Down
23 changes: 21 additions & 2 deletions src/dropdownToggle/test/dropdownToggleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ describe('dropdownToggle', function() {

}));

function dropdown() {
return $compile('<li class="dropdown"><a dropdown-toggle></a><ul dropdown-toggle><li>Hello</li></ul></li>')($rootScope);
function dropdown(closeOnClick) {
if (closeOnClick === undefined) {
closeOnClick = true;
}
return $compile('<li class="dropdown"><a dropdown-toggle close-on-click="' + closeOnClick + '"></a><ul dropdown-toggle close-on-click="' + closeOnClick + '"><li>Hello</li></ul><div class="dropdown-menu"></div></li>')($rootScope);
}

it('should toggle on `a` click', function() {
Expand Down Expand Up @@ -63,5 +66,21 @@ describe('dropdownToggle', function() {
expect(elm1.hasClass('open')).toBe(false);
expect(elm2.hasClass('open')).toBe(true);
});

it('should not close on click in dropdown-menu when close-on-click="false"', function() {
var elm1 = dropdown(false);
elm1.find('a').click();
expect(elm1.hasClass('open')).toBe(true);
elm1.parent().find('div').click(); // dropdown-menu
expect(elm1.hasClass('open')).toBe(true);
});

it('should still close on click (outside popup) when close-on-click="false"', function() {
var elm1 = dropdown(false);
elm1.find('ul').click();
expect(elm1.hasClass('open')).toBe(true);
elm1.click();
expect(elm1.hasClass('open')).toBe(false);
});
});