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

feat(dropdownToggle): Support hover #720

Closed
wants to merge 1 commit 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
15 changes: 15 additions & 0 deletions src/dropdownToggle/docs/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
<a class="dropdown-toggle">
Click me for a dropdown, yo!
</a>

<ul class="dropdown-menu">
<li ng-repeat="choice in items">
<a>{{choice}}</a>
</li>
</ul>
</li>


<li class="dropdown" ng-controller="DropdownCtrl">
<a class="dropdown-toggle" dropdown-hover="true">
Hover over me for a dropdown, yo!
</a>

<ul class="dropdown-menu">
<li ng-repeat="choice in items">
<a>{{choice}}</a>
</li>
</ul>
</li>

2 changes: 1 addition & 1 deletion src/dropdownToggle/docs/readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

DropdownToggle is a simple directive which will toggle a dropdown link on click. Simply put it on the `<a>` tag of the toggler-element, and it will find the nearest dropdown menu and toggle it when the `<a dropdown-toggle>` is clicked.
DropdownToggle is a simple directive which will toggle a dropdown link on click. Simply put it on the `<a>` tag of the toggler-element, and it will find the nearest dropdown menu and toggle it when the `<a dropdown-toggle>` is clicked. Alternately, you can also make a dropdown menu toggle on hover using the attribute `dropdown-hover` i.e. `<a dropdown-toggle dropdown-hover="true">`
13 changes: 13 additions & 0 deletions src/dropdownToggle/dropdownToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ angular.module('ui.bootstrap.dropdownToggle', []).directive('dropdownToggle', ['
link: function(scope, element, attrs) {
scope.$watch('$location.path', function() { closeMenu(); });
element.parent().bind('click', function() { closeMenu(); });

if (attrs.dropdownHover) {
element.parent().bind('mouseover', function(event) {
event.stopPropagation();
element.parent().addClass('open');
});

element.parent().bind('mouseout', function(event) {
event.stopPropagation();
element.parent().removeClass('open');
});
}

element.bind('click', function (event) {

var elementWasOpen = (element === openElement);
Expand Down
17 changes: 16 additions & 1 deletion src/dropdownToggle/test/dropdownToggleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@ describe('dropdownToggle', function() {
function dropdown() {
return $compile('<li class="dropdown"><a dropdown-toggle></a><ul dropdown-toggle><li>Hello</li></ul></li>')($rootScope);
}


function dropdownHover() {
return $compile('<li class="dropdown"><a dropdown-toggle dropdown-hover="true"></a><ul dropdown-toggle><li>Hello</li></ul></li>')($rootScope);
}

it('should toggle if hover is true', function() {
var elm = dropdownHover();
expect(elm.hasClass('open')).toBe(false);
elm.find('a').mouseover();
expect(elm.hasClass('open')).toBe(true);

// only close the dropdown after exiting li tag.
elm.find('li').mouseout();
expect(elm.hasClass('close')).toBe(false);
});

it('should toggle on `a` click', function() {
var elm = dropdown();
expect(elm.hasClass('open')).toBe(false);
Expand Down