Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 17f38b0

Browse files
committed
fix(menu): fix options with x-ng-click and data-ng-click not working
closes #3258, references #3252
1 parent 691dd2f commit 17f38b0

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/components/menu/menu-interim-element.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function MenuProvider($$interimElementProvider) {
148148
// Traverse up the event until we get to the menuContentEl to see if
149149
// there is an ng-click and that the ng-click is not disabled
150150
do {
151-
if (target && target.hasAttribute('ng-click')) {
151+
if (hasAnyAttribute(target, ['ng-click', 'data-ng-click', 'x-ng-click'])) {
152152
if (!target.hasAttribute('disabled')) {
153153
close();
154154
}
@@ -161,6 +161,16 @@ function MenuProvider($$interimElementProvider) {
161161
opts.mdMenuCtrl.close();
162162
});
163163
}
164+
165+
function hasAnyAttribute(target, attrs) {
166+
if (!target) return false;
167+
for (var i = 0, attr; attr = attrs[i]; ++i) {
168+
if (target.hasAttribute(attr)) {
169+
return true;
170+
}
171+
}
172+
return false;
173+
}
164174
});
165175

166176
// kick off initial focus in the menu on the first element

src/components/menu/menu.spec.js

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,72 @@ describe('md-menu directive', function () {
7272
expect(getOpenMenuContainer().length).toBe(0);
7373
}));
7474

75-
it('closes on option click', function () {
76-
expect(getOpenMenuContainer().length).toBe(0);
75+
describe('closes with ng-click', function() {
76+
it('closes on normal option click', function () {
77+
expect(getOpenMenuContainer().length).toBe(0);
7778

78-
openMenu(setup());
79+
openMenu(setup());
7980

80-
expect(something).toBeFalsy();
81-
expect(getOpenMenuContainer().length).toBe(1);
81+
expect(something).toBeFalsy();
82+
expect(getOpenMenuContainer().length).toBe(1);
8283

83-
var btn = angular.element(getOpenMenuContainer()[0].querySelector('md-button'));
84-
btn.triggerHandler({ type: 'click' });
84+
var btn = getOpenMenuContainer()[0].querySelector('md-button');
85+
btn.click();
8586

86-
waitForMenuClose();
87+
waitForMenuClose();
88+
89+
expect(something).toBeTruthy();
90+
91+
expect(getOpenMenuContainer().length).toBe(0);
92+
});
93+
94+
it('closes with data-ng-click', inject(function($rootScope, $compile) {
95+
expect(getOpenMenuContainer().length).toBe(0);
96+
97+
var template = '' +
98+
'<md-menu>' +
99+
' <button ng-click="$mdOpenMenu($event)">Hello World</button>' +
100+
' <md-menu-content>' +
101+
' <md-menu-item>' +
102+
' <md-button data-ng-click="doSomething($event)"></md-button>' +
103+
' </md-menu-item>' +
104+
' </md-menu-content>' +
105+
'</md-menu>';
87106

88-
expect(something).toBeTruthy();
107+
openMenu($compile(template)($rootScope));
108+
expect(getOpenMenuContainer().length).toBe(1);
109+
110+
var btn = getOpenMenuContainer()[0].querySelector('md-button');
111+
btn.click();
112+
113+
waitForMenuClose();
114+
115+
expect(getOpenMenuContainer().length).toBe(0);
116+
}));
117+
118+
it('closes with x-ng-click', inject(function($rootScope, $compile) {
119+
expect(getOpenMenuContainer().length).toBe(0);
120+
121+
var template = '' +
122+
'<md-menu>' +
123+
' <button ng-click="$mdOpenMenu($event)">Hello World</button>' +
124+
' <md-menu-content>' +
125+
' <md-menu-item>' +
126+
' <md-button x-ng-click="doSomething($event)"></md-button>' +
127+
' </md-menu-item>' +
128+
' </md-menu-content>' +
129+
'</md-menu>';
130+
131+
openMenu($compile(template)($rootScope));
132+
expect(getOpenMenuContainer().length).toBe(1);
133+
134+
var btn = getOpenMenuContainer()[0].querySelector('md-button');
135+
btn.click();
136+
137+
waitForMenuClose();
89138

90-
// FIXME - why is the menu not auto-closing ?
91-
//expect(getOpenMenuContainer().length).toBe(0);
139+
expect(getOpenMenuContainer().length).toBe(0);
140+
}));
92141
});
93142

94143
// ********************************************

0 commit comments

Comments
 (0)